Files
Infratest/Assets/Code/Example/Platformer/Entity/PlayerEntity.cs
T

139 lines
5.0 KiB
C#

using System;
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()
{
if(!health.IsDead) UnbindControls();
}
private void UnbindControls()
{
_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;
_audioService.PlayOneShot(AudioNames.Platformer, 6);
}
[SerializeField] private Vector2 attackOffset;
private void AttackPerformed(InputAction.CallbackContext callbackContext)
{
if (!onGround) return;
//
//
_audioService.PlayOneShot(AudioNames.Platformer, 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);
}
}
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);
[SerializeField] private EntityHealth health;
private void Start()
{
health.OnEntityDeath = OnEntityDeath;
health.OnEntityDamage = OnEntityDamage;
}
private void OnEntityDamage(int obj)
{
UnityEngine.Debug.Log("Player damaged");
}
private void OnEntityDeath()
{
_moveDirection = Vector2.zero;
animator.SetBool(Died, true);
UnbindControls();
// Destroy(gameObject, 1f);
}
}
}