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;
}

View File

@ -7,6 +7,7 @@ public class LevelLoader : MonoBehaviour
public LevelsLoader levelsLoader;
public AudioSource audioSource;
public Text progressionText;
private readonly float groundY = -6.034f;
private GameObject GetPrefab(string type)
{
@ -43,6 +44,12 @@ public class LevelLoader : MonoBehaviour
instance.transform.localScale = new Vector3(newScaleX, newScaleY, originalScale.z);
}
GameObject groundPrefab = GetPrefab("Ground");
GameObject groundInstance = Instantiate(groundPrefab, new Vector3(current.LastX / 2, groundY, 0), Quaternion.identity);
float groundWidth = current.LastX;
groundInstance.transform.localScale = new Vector3(groundWidth / 5f * 2, 1, 1);
Instantiate(GetPrefab("WinnerWall"), new Vector3(current.LastX, 0, 0), Quaternion.Euler(0, 0, 90));
}

View File

@ -9,8 +9,6 @@ public class Player : MonoBehaviour
public LevelsLoader LevelsLoader { get; private set; }
public SpriteRenderer SpriteRenderer { get; private set; }
public bool IsColliding { get; set; } = true;
public bool HasStarted { get; private set; } = false;
public bool CanJump { get; set; } = true;
public IGameMode CurrentGameMode { get; set; }
public float SpeedMultiplier = 1f;
@ -30,16 +28,9 @@ public class Player : MonoBehaviour
mainModule.simulationSpace = ParticleSystemSimulationSpace.World;
Particle.transform.parent = null;
Invoke(nameof(EnableInput), 0.1f);
CurrentGameMode = new NormalGameMode();
}
private void EnableInput()
{
HasStarted = true;
}
public void Update()
{
CurrentGameMode.Update(this);

View File

@ -5,6 +5,7 @@ public class PlayerCamera : MonoBehaviour
public GameObject playerObject;
public float normalMinYFollow = 2.0f;
public float shipMinYFollow = 6.0f;
public float smoothSpeed = 5.0f;
private float initialY;
private void Start()
@ -28,6 +29,8 @@ public class PlayerCamera : MonoBehaviour
targetY = playerObject.transform.position.y;
}
transform.position = new Vector3(playerObject.transform.position.x, targetY, transform.position.z);
float newY = Mathf.Lerp(transform.position.y, targetY, smoothSpeed * Time.deltaTime);
transform.position = new Vector3(playerObject.transform.position.x, newY, transform.position.z);
}
}