Skip to content

Commit

Permalink
Remove EventSystem field and fix constant string
Browse files Browse the repository at this point in the history
  • Loading branch information
kochounoyume committed Jul 2, 2024
1 parent 0f3594d commit c4edb9b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 49 deletions.
48 changes: 24 additions & 24 deletions Packages/PlayerControl/Runtime/AnimHashConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@ public sealed class AnimHashConstants
/// <summary>
/// "Speed" animation hash.
/// </summary>
public readonly int SpeedAnim;
public readonly int Speed;

/// <summary>
/// "IsGround" animation hash.
/// </summary>
public readonly int GroundAnim;
public readonly int IsGround;

/// <summary>
/// "JumpStart" animation hash.
/// </summary>
public readonly int JumpStartAnim;
public readonly int JumpStart;

/// <summary>
/// "DoubleJump" animation hash.
/// </summary>
public readonly int DoubleJumpAnim;
public readonly int DoubleJump;

/// <summary>
/// "Forward" animation hash.
/// </summary>
public readonly int ForwardAnim;
public readonly int Forward;

/// <summary>
/// "SideStep" animation hash.
/// </summary>
public readonly int SideStepAnim;
public readonly int SideStep;

// Ensure that this class is not generated before Awake.
public AnimHashConstants(in Object obj)
Expand All @@ -45,41 +45,41 @@ public AnimHashConstants(in Object obj)
{
throw new System.ArgumentNullException(nameof(obj));
}
SpeedAnim = Animator.StringToHash("Speed");
GroundAnim = Animator.StringToHash("IsGround");
JumpStartAnim = Animator.StringToHash("JumpStart");
DoubleJumpAnim = Animator.StringToHash("DoubleJump");
ForwardAnim = Animator.StringToHash("Forward");
SideStepAnim = Animator.StringToHash("SideStep");
Speed = Animator.StringToHash(nameof(Speed));
IsGround = Animator.StringToHash(nameof(IsGround));
JumpStart = Animator.StringToHash(nameof(JumpStart));
DoubleJump = Animator.StringToHash(nameof(DoubleJump));
Forward = Animator.StringToHash(nameof(Forward));
SideStep = Animator.StringToHash(nameof(SideStep));
}

public override string ToString()
{
StringBuilder builder = new StringBuilder(nameof(AnimHashConstants));
builder.Append(" {");
builder.Append(nameof(SpeedAnim));
builder.Append(nameof(Speed));
builder.Append(" = ");
builder.Append(SpeedAnim);
builder.Append(Speed);
builder.Append(", ");
builder.Append(nameof(GroundAnim));
builder.Append(nameof(IsGround));
builder.Append(" = ");
builder.Append(GroundAnim);
builder.Append(IsGround);
builder.Append(", ");
builder.Append(nameof(JumpStartAnim));
builder.Append(nameof(JumpStart));
builder.Append(" = ");
builder.Append(JumpStartAnim);
builder.Append(JumpStart);
builder.Append(", ");
builder.Append(nameof(DoubleJumpAnim));
builder.Append(nameof(DoubleJump));
builder.Append(" = ");
builder.Append(DoubleJumpAnim);
builder.Append(DoubleJump);
builder.Append(", ");
builder.Append(nameof(ForwardAnim));
builder.Append(nameof(Forward));
builder.Append(" = ");
builder.Append(ForwardAnim);
builder.Append(Forward);
builder.Append(", ");
builder.Append(nameof(SideStepAnim));
builder.Append(nameof(SideStep));
builder.Append(" = ");
builder.Append(SideStepAnim);
builder.Append(SideStep);
builder.Append(" }");
return builder.ToString();
}
Expand Down
12 changes: 6 additions & 6 deletions Packages/PlayerControl/Runtime/MobilePlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@ protected override void Start()
uiView.Joystick.OnValueChanged += value =>
{
const InputActionPhase phase = InputActionPhase.Performed;
base.OnActionTriggered(new CallbackContext(MoveAction, phase, value));
base.OnActionTriggered(new CallbackContext(Move, phase, value));
};
uiView.SprintButton.OnStart += () =>
{
const InputActionPhase phase = InputActionPhase.Performed;
base.OnActionTriggered(new CallbackContext(SprintAction, phase));
base.OnActionTriggered(new CallbackContext(Sprint, phase));
};
uiView.SprintButton.OnRelease += () =>
{
const InputActionPhase phase = InputActionPhase.Canceled;
base.OnActionTriggered(new CallbackContext(SprintAction, phase));
base.OnActionTriggered(new CallbackContext(Sprint, phase));
};
uiView.JumpButton.onClick.AddListener(() =>
{
const InputActionPhase phase = InputActionPhase.Started;
base.OnActionTriggered(new CallbackContext(JumpAction, phase));
base.OnActionTriggered(new CallbackContext(Jump, phase));
});
}

Expand Down Expand Up @@ -99,12 +99,12 @@ protected override void Update()
{
delta = processor.Process(delta, null);
}
base.OnActionTriggered(new CallbackContext(LookAction, InputActionPhase.Performed, delta));
base.OnActionTriggered(new CallbackContext(Look, InputActionPhase.Performed, delta));
}

protected override void OnActionTriggered(in CallbackContext context)
{
if (context.CompareActionName(LookAction))
if (context.CompareActionName(Look))
{
#if UNITY_EDITOR
if (!isDebug)
Expand Down
34 changes: 15 additions & 19 deletions Packages/PlayerControl/Runtime/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ public class PlayerController : MonoBehaviour

private new ITransform transform;

private EventSystem eventSystem;

private AnimHashConstants constants;

private PointerEventData pointerEventData;
Expand All @@ -61,8 +59,6 @@ public class PlayerController : MonoBehaviour

protected ref readonly ITransform Transform => ref transform;

protected ref readonly EventSystem EventSystem => ref eventSystem;

protected ref readonly AnimHashConstants Constants => ref constants;

protected bool IsDoubleJump
Expand Down Expand Up @@ -108,22 +104,22 @@ protected Quaternion WorldRotation
/// <summary>
/// "Move" action name.
/// </summary>
public const string MoveAction = "Move";
public const string Move = nameof(Move);

/// <summary>
/// "Sprint" action name.
/// </summary>
public const string SprintAction = "Sprint";
public const string Sprint = nameof(Sprint);

/// <summary>
/// "Jump" action name.
/// </summary>
public const string JumpAction = "Jump";
public const string Jump = nameof(Jump);

/// <summary>
/// "Look" action name.
/// </summary>
public const string LookAction = "Look";
public const string Look = nameof(Look);

/// <summary>
/// The damper time for the move animation.
Expand All @@ -133,48 +129,47 @@ protected Quaternion WorldRotation
protected virtual void Start()
{
transform = GetComponent<ITransform>();
eventSystem = EventSystem.current;
constants = new AnimHashConstants(this);
PlayerInput.onActionTriggered += context => OnActionTriggered(context);
JumpControl.OnJump.AddListener(OnJump);
}

protected virtual void Update()
{
Animator.SetFloat(constants.SpeedAnim, CurrentSpeed);
Animator.SetBool(constants.GroundAnim, IsOnGround);
Animator.SetFloat(constants.Speed, CurrentSpeed);
Animator.SetBool(constants.IsGround, IsOnGround);

Vector3 currentDirection = LocalDirection;
float deltaTime = Time.deltaTime;
Animator.SetFloat(constants.ForwardAnim, currentDirection.z, MoveDampTime, deltaTime);
Animator.SetFloat(constants.SideStepAnim, currentDirection.x, MoveDampTime, deltaTime);
Animator.SetFloat(constants.Forward, currentDirection.z, MoveDampTime, deltaTime);
Animator.SetFloat(constants.SideStep, currentDirection.x, MoveDampTime, deltaTime);
}

protected virtual void OnActionTriggered(in CallbackContext context)
{
switch (context.ActionName)
{
case MoveAction when context.Phase is InputActionPhase.Performed or InputActionPhase.Canceled:
case Move when context.Phase is InputActionPhase.Performed or InputActionPhase.Canceled:
MoveControl.Move(context.Value);
break;
case SprintAction when context.Phase is InputActionPhase.Performed:
case Sprint when context.Phase is InputActionPhase.Performed:
const float sprintHoldSpeed = 4.0f;
MoveControl.MoveSpeed = sprintHoldSpeed;
break;
case SprintAction when context.Phase is InputActionPhase.Canceled:
case Sprint when context.Phase is InputActionPhase.Canceled:
const float sprintReleasedSpeed = 1.2f;
MoveControl.MoveSpeed = sprintReleasedSpeed;
break;
case JumpAction when context.Phase is InputActionPhase.Started:
case Jump when context.Phase is InputActionPhase.Started:
JumpControl.Jump();
break;
case LookAction when context.Phase is InputActionPhase.Performed:
case Look when context.Phase is InputActionPhase.Performed:
CameraControl.RotateCamera(context.Value);
break;
}
}

protected virtual void OnJump() => Animator.Play(IsDoubleJump ? constants.DoubleJumpAnim : constants.JumpStartAnim);
protected virtual void OnJump() => Animator.Play(IsDoubleJump ? constants.DoubleJump : constants.JumpStart);

/// <summary>
/// Check if the pointer is hitting UI.
Expand All @@ -184,6 +179,7 @@ protected bool IsPointerHittingUI()
{
Pointer device = playerInput.GetDevice<Pointer>();
if (device == null) return false;
EventSystem eventSystem = EventSystem.current;
pointerEventData ??= new PointerEventData(eventSystem);
pointerEventData.position = device.position.ReadValue();
List<RaycastResult> results = new List<RaycastResult>();
Expand Down

0 comments on commit c4edb9b

Please sign in to comment.