fix: rotation, smooth camera, ship portal collider, obstacle block collider, player rigid body detection, dynamic ground (#54)

This commit is contained in:
2025-04-06 17:24:10 +02:00
committed by GitHub
parent be5ea1e60a
commit 53af80411f
16 changed files with 222 additions and 187 deletions

View File

@ -1,30 +1,38 @@
using UnityEngine;
using UnityEngine.SceneManagement;
public class NormalGameMode : IGameMode
{
private const float HorizontalSpeed = 8.6f;
private const float JumpForce = 26.6581f;
private const KeyCode JumpKey = KeyCode.Space;
private bool isRotating = false;
private float targetRotationAngle = 0f;
private readonly float rotationSpeed = 360f;
public void Update(Player player)
{
player.RigidBody.linearVelocity = new Vector2(HorizontalSpeed * player.SpeedMultiplier, player.RigidBody.linearVelocity.y);
if (player.HasStarted && player.IsColliding && Input.GetKey(JumpKey) && player.CanJump)
if (player.IsColliding && Input.GetKey(JumpKey) && !isRotating)
{
Jump(player);
}
if (isRotating)
{
PerformRotation(player);
}
if (!IsJumping(player))
{
AlignRotation(player);
player.Particle.gameObject.SetActive(true);
}
else
{
player.Particle.gameObject.SetActive(false);
player.Transform.Rotate(Vector3.back * 360 * Time.deltaTime);
}
UpdateParticlePositionAndRotation(player);
@ -35,6 +43,22 @@ public class NormalGameMode : IGameMode
player.RigidBody.linearVelocity = new Vector2(player.RigidBody.linearVelocity.x, 0);
player.RigidBody.AddForce(Vector2.up * JumpForce, ForceMode2D.Impulse);
player.LevelsLoader.IncreaseTotalJumps();
isRotating = true;
targetRotationAngle = player.transform.eulerAngles.z - 90f;
}
private void PerformRotation(Player player)
{
float rotationThisFrame = rotationSpeed * Time.deltaTime;
float newRotation = Mathf.MoveTowardsAngle(player.transform.eulerAngles.z, targetRotationAngle, rotationThisFrame);
player.transform.rotation = Quaternion.Euler(0, 0, newRotation);
if (Mathf.Abs(Mathf.DeltaAngle(newRotation, targetRotationAngle)) < 0.1f)
{
player.transform.rotation = Quaternion.Euler(0, 0, targetRotationAngle);
isRotating = false;
AlignRotation(player);
}
}
private bool IsJumping(Player player)
@ -44,21 +68,21 @@ public class NormalGameMode : IGameMode
private void AlignRotation(Player player)
{
Vector3 rotation = player.Transform.rotation.eulerAngles;
Vector3 rotation = player.transform.eulerAngles;
rotation.z = Mathf.Round(rotation.z / 90) * 90;
player.Transform.rotation = Quaternion.Euler(rotation);
player.transform.rotation = Quaternion.Euler(rotation);
}
private void UpdateParticlePositionAndRotation(Player player)
{
player.Particle.transform.position = player.Transform.position + new Vector3(-0.19f, -0.64f, -10);
player.Particle.transform.position =
player.transform.position + new Vector3(-0.19f, -0.64f, -10);
player.Particle.transform.rotation = Quaternion.Euler(0, 0, 150.464f);
}
public void OnCollisionEnter(Player player, Collision2D collision)
{
player.IsColliding = true;
player.CanJump = true;
if (collision.gameObject.CompareTag("Kill"))
{

View File

@ -16,7 +16,7 @@ public class ShipGameMode : IGameMode
bool jumpPressed = Input.GetKey(JumpKey);
if (player.HasStarted && jumpPressed)
if (jumpPressed)
{
Jump(player);
@ -49,7 +49,9 @@ public class ShipGameMode : IGameMode
{
float angle = player.Transform.rotation.eulerAngles.z;
if (angle > 180f)
{
angle -= 360f;
}
return angle;
}