mirror of
https://github.com/boudji-ludwig-pett/cnam-geometry-dash.git
synced 2025-06-10 22:20:40 +02:00
feat: rocket mode (#44)
Co-authored-by: Djelal BOUDJI <djelal@gmail.com>
This commit is contained in:
8
Assets/Scripts/GameMode.meta
Normal file
8
Assets/Scripts/GameMode.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 86a7976eac8daf648837e934393ac7ba
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
8
Assets/Scripts/GameMode/IGameMode.cs
Normal file
8
Assets/Scripts/GameMode/IGameMode.cs
Normal file
@ -0,0 +1,8 @@
|
||||
using UnityEngine;
|
||||
|
||||
public interface IGameMode
|
||||
{
|
||||
void Update(Player player);
|
||||
void OnCollisionEnter(Player player, Collision2D collision);
|
||||
void OnCollisionExit(Player player, Collision2D collision);
|
||||
}
|
2
Assets/Scripts/GameMode/IGameMode.cs.meta
Normal file
2
Assets/Scripts/GameMode/IGameMode.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 68e961c0b929a2a4d9e92b2f2bf9e197
|
78
Assets/Scripts/GameMode/NormalGameMode.cs
Normal file
78
Assets/Scripts/GameMode/NormalGameMode.cs
Normal file
@ -0,0 +1,78 @@
|
||||
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;
|
||||
|
||||
public void Update(Player player)
|
||||
{
|
||||
player.RigidBody.linearVelocity = new Vector2(HorizontalSpeed, player.RigidBody.linearVelocity.y);
|
||||
|
||||
if (player.HasStarted && player.IsColliding && Input.GetKey(JumpKey) && player.CanJump)
|
||||
{
|
||||
Jump(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);
|
||||
}
|
||||
|
||||
private void Jump(Player player)
|
||||
{
|
||||
player.RigidBody.linearVelocity = new Vector2(player.RigidBody.linearVelocity.x, 0);
|
||||
player.RigidBody.AddForce(Vector2.up * JumpForce, ForceMode2D.Impulse);
|
||||
player.LevelsLoader.IncreaseTotalJumps();
|
||||
}
|
||||
|
||||
private bool IsJumping(Player player)
|
||||
{
|
||||
return !player.IsColliding;
|
||||
}
|
||||
|
||||
private void AlignRotation(Player player)
|
||||
{
|
||||
Vector3 rotation = player.Transform.rotation.eulerAngles;
|
||||
rotation.z = Mathf.Round(rotation.z / 90) * 90;
|
||||
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.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"))
|
||||
{
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
|
||||
}
|
||||
|
||||
if (collision.gameObject.CompareTag("Win"))
|
||||
{
|
||||
SceneManager.LoadScene("SelectLevelScene");
|
||||
}
|
||||
}
|
||||
|
||||
public void OnCollisionExit(Player player, Collision2D collision)
|
||||
{
|
||||
player.IsColliding = false;
|
||||
}
|
||||
}
|
2
Assets/Scripts/GameMode/NormalGameMode.cs.meta
Normal file
2
Assets/Scripts/GameMode/NormalGameMode.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 222f727842e308847a028d3fce55d364
|
43
Assets/Scripts/GameMode/ShipGameMode.cs
Normal file
43
Assets/Scripts/GameMode/ShipGameMode.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class ShipGameMode : IGameMode
|
||||
{
|
||||
private const float HorizontalSpeed = 8.6f;
|
||||
private const float JumpForce = 26.6581f;
|
||||
private const KeyCode JumpKey = KeyCode.Space;
|
||||
|
||||
public void Update(Player player)
|
||||
{
|
||||
player.RigidBody.linearVelocity = new Vector2(HorizontalSpeed, player.RigidBody.linearVelocity.y);
|
||||
|
||||
if (player.HasStarted && Input.GetKey(JumpKey))
|
||||
{
|
||||
Jump(player);
|
||||
}
|
||||
}
|
||||
|
||||
private void Jump(Player player)
|
||||
{
|
||||
player.RigidBody.linearVelocity = new Vector2(player.RigidBody.linearVelocity.x, 0);
|
||||
player.RigidBody.AddForce(Vector2.up * JumpForce, ForceMode2D.Impulse);
|
||||
player.LevelsLoader.IncreaseTotalJumps();
|
||||
}
|
||||
|
||||
public void OnCollisionEnter(Player player, Collision2D collision)
|
||||
{
|
||||
if (collision.gameObject.CompareTag("Kill"))
|
||||
{
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
|
||||
}
|
||||
if (collision.gameObject.CompareTag("Win"))
|
||||
{
|
||||
SceneManager.LoadScene("HomeScene");
|
||||
}
|
||||
}
|
||||
|
||||
public void OnCollisionExit(Player player, Collision2D collision)
|
||||
{
|
||||
// rien pour l'instant
|
||||
}
|
||||
}
|
2
Assets/Scripts/GameMode/ShipGameMode.cs.meta
Normal file
2
Assets/Scripts/GameMode/ShipGameMode.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 28b5f8f9214141740af5157d6b421677
|
@ -1,101 +1,75 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using System.IO;
|
||||
|
||||
public class Player : MonoBehaviour
|
||||
{
|
||||
public Rigidbody2D rigidBody;
|
||||
public GameObject playerObject;
|
||||
public ParticleSystem particle;
|
||||
public LevelsLoader levelsLoader;
|
||||
public Rigidbody2D RigidBody { get; private set; }
|
||||
public Transform Transform { get; private set; }
|
||||
public ParticleSystem Particle { get; private set; }
|
||||
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 bool isColliding = true;
|
||||
private bool hasStarted = false;
|
||||
public IGameMode CurrentGameMode { get; set; }
|
||||
|
||||
private bool canJump = true;
|
||||
public void Awake()
|
||||
{
|
||||
RigidBody = GetComponent<Rigidbody2D>();
|
||||
Transform = transform;
|
||||
Particle = GetComponentInChildren<ParticleSystem>();
|
||||
SpriteRenderer = GetComponentInChildren<SpriteRenderer>();
|
||||
LevelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
levelsLoader = GameObject.FindGameObjectWithTag("LevelsLoader").GetComponent<LevelsLoader>();
|
||||
|
||||
var mainModule = particle.main;
|
||||
var mainModule = Particle.main;
|
||||
mainModule.simulationSpace = ParticleSystemSimulationSpace.World;
|
||||
particle.transform.parent = null;
|
||||
Particle.transform.parent = null;
|
||||
|
||||
Invoke(nameof(EnableInput), 0.1f);
|
||||
|
||||
CurrentGameMode = new NormalGameMode();
|
||||
}
|
||||
|
||||
private void EnableInput()
|
||||
{
|
||||
hasStarted = true;
|
||||
HasStarted = true;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
rigidBody.linearVelocity = new Vector2(8.6f, rigidBody.linearVelocity.y);
|
||||
|
||||
if (hasStarted && isColliding && Input.GetKey(KeyCode.Space) && canJump)
|
||||
{
|
||||
Jump();
|
||||
}
|
||||
|
||||
if (!IsJumping())
|
||||
{
|
||||
AlignRotation();
|
||||
particle.gameObject.SetActive(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
particle.gameObject.SetActive(false);
|
||||
transform.Rotate(Vector3.back * 360 * Time.deltaTime);
|
||||
}
|
||||
|
||||
UpdateParticlePositionAndRotation();
|
||||
}
|
||||
|
||||
private void Jump()
|
||||
{
|
||||
rigidBody.linearVelocity = new Vector2(rigidBody.linearVelocity.x, 0);
|
||||
rigidBody.AddForce(Vector2.up * 26.6581f, ForceMode2D.Impulse);
|
||||
levelsLoader.IncreaseTotalJumps();
|
||||
}
|
||||
|
||||
private bool IsJumping()
|
||||
{
|
||||
return !isColliding;
|
||||
}
|
||||
|
||||
private void AlignRotation()
|
||||
{
|
||||
Vector3 rotation = transform.rotation.eulerAngles;
|
||||
rotation.z = Mathf.Round(rotation.z / 90) * 90;
|
||||
transform.rotation = Quaternion.Euler(rotation);
|
||||
}
|
||||
|
||||
private void UpdateParticlePositionAndRotation()
|
||||
{
|
||||
particle.transform.position = transform.position + new Vector3(-0.19f, -0.64f, -10);
|
||||
particle.transform.rotation = Quaternion.Euler(0, 0, 150.464f);
|
||||
CurrentGameMode.Update(this);
|
||||
}
|
||||
|
||||
public void OnCollisionEnter2D(Collision2D collision)
|
||||
{
|
||||
isColliding = true;
|
||||
canJump = true;
|
||||
|
||||
if (collision.gameObject.CompareTag("Kill"))
|
||||
{
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
|
||||
}
|
||||
|
||||
if (collision.gameObject.CompareTag("Win"))
|
||||
{
|
||||
SceneManager.LoadScene("HomeScene");
|
||||
}
|
||||
CurrentGameMode.OnCollisionEnter(this, collision);
|
||||
}
|
||||
|
||||
public void OnCollisionExit2D(Collision2D collision)
|
||||
{
|
||||
isColliding = false;
|
||||
CurrentGameMode.OnCollisionExit(this, collision);
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D collision)
|
||||
{
|
||||
if (collision.CompareTag("ShipPortal"))
|
||||
{
|
||||
SpriteRenderer.sprite = Resources.Load<Sprite>("Shapes/Ship");
|
||||
ChangeGameMode(new ShipGameMode());
|
||||
}
|
||||
else if (collision.CompareTag("CubePortal"))
|
||||
{
|
||||
SpriteRenderer.sprite = Resources.Load<Sprite>("Shapes/BaseSquare");
|
||||
ChangeGameMode(new NormalGameMode());
|
||||
}
|
||||
}
|
||||
|
||||
public void ChangeGameMode(IGameMode newMode)
|
||||
{
|
||||
CurrentGameMode = newMode;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user