Files
Infratest/Assets/Code/Example/Platformer/Entity/PlayerEntity.cs
T
ThomasCharester 60c28f54f2 Миксер
Почини интерфейс
2026-03-23 23:59:32 +03:00

166 lines
5.9 KiB
C#

using Code.Infrastructure.Audio;
using Code.Infrastructure.Configs;
using Code.Infrastructure.EventBus;
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, EventBus eventBus)
{
_eventBus = eventBus;
_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
}
protected override void OnDestroy()
{
base.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, AudioGroupType.SFX);
}
[SerializeField] private Vector2 attackOffset;
private void AttackPerformed(InputAction.CallbackContext callbackContext)
{
if (!onGround) return;
//
//
_audioService.PlayOneShot(AudioNames.Platformer, 1, AudioGroupType.SFX);
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 EventBus _eventBus;
private void Start()
{
health.OnEntityDeath = OnEntityDeath;
health.OnEntityDamage = OnEntityDamage;
}
private void OnEntityDamage(int obj)
{
UnityEngine.Debug.Log("Player damaged");
_audioService.PlayOneShot(AudioNames.Platformer, 8, AudioGroupType.SFX);
_audioService.ApplyParameter(
"PlatformerDamage",
1f,
0.5f,
() => _audioService.ApplyParameter(
"PlatformerDamage",
5000f,
1f));
_eventBus.RaiseEvent<IPlayerDamagedHandle>(x=>x.PlayerDamaged());
}
private void OnEntityDeath()
{
_moveDirection = Vector2.zero;
animator.SetBool(Died, true);
UnbindControls();
_eventBus.RaiseEvent<IPlayerDiedHandle>(x=>x.PlayerDied());
// Destroy(gameObject, 1f);
}
}
public interface IPlayerDiedHandle : IGlobalSubscriber
{
void PlayerDied();
}
public interface IPlayerDamagedHandle : IGlobalSubscriber
{
void PlayerDamaged();
}
}