Skip to content

Commit

Permalink
fix: add interface for mechanics
Browse files Browse the repository at this point in the history
  • Loading branch information
Aniol0012 committed Sep 11, 2024
1 parent f35e519 commit 214ab6b
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 38 deletions.
12 changes: 5 additions & 7 deletions Assets/Scripts/Gameplay/PlayerSpawn.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
using Platformer.Core;
using Platformer.Mechanics;
using Platformer.Model;
using Platformer.Mechanics.Enums;

namespace Platformer.Gameplay
{
namespace Platformer.Gameplay {
/// <summary>
/// Fired when the player is spawned after dying.
/// </summary>
public class PlayerSpawn : Simulation.Event<PlayerSpawn>
{
public class PlayerSpawn : Simulation.Event<PlayerSpawn> {
PlatformerModel model = Simulation.GetModel<PlatformerModel>();

public override void Execute()
{
public override void Execute() {
var player = model.player;
player.collider2d.enabled = true;
player.controlEnabled = false;
if (player.audioSource && player.respawnAudio)
player.audioSource.PlayOneShot(player.respawnAudio);
player.health.Increment();
player.Teleport(model.spawnPoint.transform.position);
player.jumpState = PlayerController.JumpState.Grounded;
player.jumpState = JumpState.Grounded;
player.animator.SetBool("dead", false);
model.virtualCamera.m_Follow = player.transform;
model.virtualCamera.m_LookAt = player.transform;
Expand Down
3 changes: 3 additions & 0 deletions Assets/Scripts/Mechanics/Enums.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/Scripts/Mechanics/Enums/JumpState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Platformer.Mechanics.Enums {
public enum JumpState {
Grounded,
PrepareToJump,
Jumping,
InFlight,
Landed
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Mechanics/Enums/JumpState.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/Scripts/Mechanics/Interfaces.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Assets/Scripts/Mechanics/Interfaces/IMechanics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Platformer.Mechanics {
public interface IMechanics {
void Jump();
void Idle();
void Walk();
void Run();
void Crouch();
void Climb();
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Mechanics/Interfaces/IMechanics.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 48 additions & 31 deletions Assets/Scripts/Mechanics/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
using static Platformer.Core.Simulation;
using Platformer.Model;
using Platformer.Core;
using Platformer.Mechanics.Enums;

namespace Platformer.Mechanics {
/// <summary>
/// This is the main class used to implement control of the player.
/// It is a superset of the AnimationController class, but is inlined to allow for any kind of customisation.
/// </summary>
public class PlayerController : KinematicObject {
// TODO: This will be removed when the mechanics are implemented
public class PlayerController : KinematicObject, IMechanics {
// TODO This will be removed when the mechanics are implemented
public AudioClip jumpAudio;
public AudioClip respawnAudio;
public AudioClip ouchAudio;
Expand All @@ -29,7 +30,7 @@ public class PlayerController : KinematicObject {

public JumpState jumpState = JumpState.Grounded;

private bool stopJump;
private bool _stopJump;

/*internal new*/
public Collider2D collider2d;
Expand All @@ -39,47 +40,47 @@ public class PlayerController : KinematicObject {
public Health health;
public bool controlEnabled = true;

bool jump;
Vector2 move;
SpriteRenderer spriteRenderer;
private bool _jump;
private Vector2 _move;
private SpriteRenderer _spriteRenderer;
internal Animator animator;
readonly PlatformerModel model = Simulation.GetModel<PlatformerModel>();
private readonly PlatformerModel _model = Simulation.GetModel<PlatformerModel>();

public Bounds Bounds => collider2d.bounds;

void Awake() {
health = GetComponent<Health>();
audioSource = GetComponent<AudioSource>();
collider2d = GetComponent<Collider2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
_spriteRenderer = GetComponent<SpriteRenderer>();
animator = GetComponent<Animator>();
}

protected override void Update() {
if (controlEnabled) {
move.x = Input.GetAxis("Horizontal");
_move.x = Input.GetAxis("Horizontal");
if (jumpState == JumpState.Grounded && Input.GetButtonDown("Jump"))
jumpState = JumpState.PrepareToJump;
else if (Input.GetButtonUp("Jump")) {
stopJump = true;
_stopJump = true;
Schedule<PlayerStopJump>().player = this;
}
}
else {
move.x = 0;
_move.x = 0;
}

UpdateJumpState();
base.Update();
}

void UpdateJumpState() {
jump = false;
_jump = false;
switch (jumpState) {
case JumpState.PrepareToJump:
jumpState = JumpState.Jumping;
jump = true;
stopJump = false;
_jump = true;
_stopJump = false;
break;
case JumpState.Jumping:
if (!IsGrounded) {
Expand All @@ -102,34 +103,50 @@ void UpdateJumpState() {
}

protected override void ComputeVelocity() {
if (jump && IsGrounded) {
velocity.y = jumpTakeOffSpeed * model.jumpModifier;
jump = false;
if (_jump && IsGrounded) {
velocity.y = jumpTakeOffSpeed * _model.jumpModifier;
_jump = false;
}
else if (stopJump) {
stopJump = false;
else if (_stopJump) {
_stopJump = false;
if (velocity.y > 0) {
velocity.y = velocity.y * model.jumpDeceleration;
velocity.y = velocity.y * _model.jumpDeceleration;
}
}

if (move.x > 0.01f)
spriteRenderer.flipX = false;
else if (move.x < -0.01f)
spriteRenderer.flipX = true;
if (_move.x > 0.01f)
_spriteRenderer.flipX = false;
else if (_move.x < -0.01f)
_spriteRenderer.flipX = true;

animator.SetBool("grounded", IsGrounded);
animator.SetFloat("velocityX", Mathf.Abs(velocity.x) / maxSpeed);

targetVelocity = move * maxSpeed;
targetVelocity = _move * maxSpeed;
}

public enum JumpState {
Grounded,
PrepareToJump,
Jumping,
InFlight,
Landed
public void Jump() {
throw new System.NotImplementedException();
}

public void Idle() {
throw new System.NotImplementedException();
}

public void Walk() {
throw new System.NotImplementedException();
}

public void Run() {
throw new System.NotImplementedException();
}

public void Crouch() {
throw new System.NotImplementedException();
}

public void Climb() {
throw new System.NotImplementedException();
}
}
}

0 comments on commit 214ab6b

Please sign in to comment.