109 lines
4.4 KiB
C#
109 lines
4.4 KiB
C#
using Code.Infrastructure.Audio;
|
|
using Code.Infrastructure.Camera;
|
|
using Code.Infrastructure.Configs;
|
|
using Code.Infrastructure.Input;
|
|
using Reflex.Attributes;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace Code.Example.Platformer.Entity
|
|
{
|
|
public class PlayerEntity : Entity
|
|
{
|
|
private static readonly int OnAttack = Animator.StringToHash("OnAttack");
|
|
private IInputService _inputService;
|
|
|
|
[Inject]
|
|
private void Construct(IInputService inputService, IAudioService audioService)
|
|
{
|
|
_audioService = audioService;
|
|
|
|
_inputService = inputService;
|
|
_inputService.BindActionPerformed((int)ActionMappings.Platformer, (int)PlatformerActions.Move,
|
|
MovementPerformed);
|
|
_inputService.BindActionCancelled((int)ActionMappings.Platformer, (int)PlatformerActions.Move,
|
|
MovementCancelled);
|
|
_inputService.BindActionPerformed((int)ActionMappings.Platformer, (int)PlatformerActions.Jump, JumpPerformed);
|
|
_inputService.BindActionPerformed((int)ActionMappings.Platformer, (int)PlatformerActions.Attack, AttackPerformed);
|
|
_inputService.BindActionPerformed((int)ActionMappings.Platformer, (int)PlatformerActions.Sprint, SprintPerformed);
|
|
_inputService.BindActionCancelled((int)ActionMappings.Platformer, (int)PlatformerActions.Sprint, SprintCancelled);
|
|
|
|
//cameraFactory.GetCinematicCamera(CameraMode.Platformer).Follow = transform; , CameraFactory cameraFactory
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
_inputService.UnbindActionPerformed((int)ActionMappings.Platformer, (int)PlatformerActions.Move,
|
|
MovementPerformed);
|
|
_inputService.UnbindActionCancelled((int)ActionMappings.Platformer, (int)PlatformerActions.Move,
|
|
MovementCancelled);
|
|
_inputService.UnbindActionPerformed((int)ActionMappings.Platformer, (int)PlatformerActions.Jump, JumpPerformed);
|
|
_inputService.UnbindActionPerformed((int)ActionMappings.Platformer, (int)PlatformerActions.Attack, AttackPerformed);
|
|
_inputService.UnbindActionPerformed((int)ActionMappings.Platformer, (int)PlatformerActions.Sprint, SprintPerformed);
|
|
_inputService.UnbindActionCancelled((int)ActionMappings.Platformer, (int)PlatformerActions.Sprint, SprintCancelled);
|
|
}
|
|
|
|
private float _acceleration = 1f;
|
|
|
|
protected override void Move()
|
|
{
|
|
rigidbody2D.linearVelocityX = _moveDirection.x * 2f * _acceleration;
|
|
}
|
|
|
|
private Vector2 _moveDirection;
|
|
private IAudioService _audioService;
|
|
|
|
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);
|
|
|
|
_acceleration = 1f;
|
|
}
|
|
|
|
[SerializeField] private Vector2 attackOffset;
|
|
|
|
private void AttackPerformed(InputAction.CallbackContext callbackContext)
|
|
{
|
|
if (!onGround) return;
|
|
//
|
|
|
|
//
|
|
_audioService.PlayOneShot(AudioNames.UISounds, 1);
|
|
animator.SetBool(OnAttack, true);
|
|
}
|
|
|
|
public void CastAttack()
|
|
{
|
|
var damagedOnes =
|
|
Physics2D.OverlapPointAll((Vector2)transform.position + attackOffset, LayerMasks.Masks[LayerMaskType.PlatformerEnemy]);
|
|
|
|
if (damagedOnes == null) return;
|
|
|
|
foreach (var damaged in damagedOnes)
|
|
{
|
|
damaged.gameObject.GetComponent<EntityHealth>().GiveDamage(1);
|
|
_audioService.PlayOneShot(AudioNames.UISounds, 2);
|
|
}
|
|
}
|
|
|
|
private void SprintPerformed(InputAction.CallbackContext callbackContext)
|
|
{
|
|
if (!onGround) return;
|
|
//
|
|
_acceleration = 3f;
|
|
}
|
|
private void SprintCancelled(InputAction.CallbackContext callbackContext)
|
|
{
|
|
_acceleration = 1f;
|
|
}
|
|
|
|
public void AttackEnd() => animator.SetBool(OnAttack, false);
|
|
}
|
|
} |