52 lines
1.8 KiB
C#
52 lines
1.8 KiB
C#
using System;
|
|
using Code.Infrastructure.Input;
|
|
using Reflex.Attributes;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace Code.Example.Platformer.Entity
|
|
{
|
|
public class PlayerEntity : Entity
|
|
{
|
|
private IInputService _inputService;
|
|
|
|
[Inject]
|
|
private void Construct(IInputService inputService)
|
|
{
|
|
_inputService = inputService;
|
|
_inputService.BindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Move,
|
|
MovementPerformed);
|
|
_inputService.BindActionCancelled((int)ActionMappings.Gameplay, (int)GameplayActions.Move,
|
|
MovementCancelled);
|
|
_inputService.BindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Brake, JumpPerformed);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
_inputService.UnbindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Move,
|
|
MovementPerformed);
|
|
_inputService.UnbindActionCancelled((int)ActionMappings.Gameplay, (int)GameplayActions.Move,
|
|
MovementCancelled);
|
|
_inputService.UnbindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Brake, JumpPerformed);
|
|
}
|
|
|
|
protected override void Move()
|
|
{
|
|
rigidbody2D.linearVelocityX = _moveDirection.x * 5f;
|
|
}
|
|
|
|
private Vector2 _moveDirection;
|
|
|
|
private void MovementPerformed(InputAction.CallbackContext callbackContext) =>
|
|
_moveDirection = callbackContext.ReadValue<Vector2>();
|
|
|
|
private void MovementCancelled(InputAction.CallbackContext callbackContext) =>
|
|
_moveDirection = Vector2.zero;
|
|
|
|
private void JumpPerformed(InputAction.CallbackContext callbackContext)
|
|
{
|
|
if (!onGround) return;
|
|
rigidbody2D.AddForceY(100f);
|
|
}
|
|
}
|
|
} |