0% found this document useful (0 votes)
9 views1 page

Player Controller Implementation in Unity

Uploaded by

ninesisach
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views1 page

Player Controller Implementation in Unity

Uploaded by

ninesisach
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

using UnityEngine;

using [Link];

[RequireComponent(typeof(PlayerStateMachine))]
public class PlayerController : MonoBehaviour {
private PlayerStateMachine _fsm;

private void Awake() {


_fsm = GetComponent<PlayerStateMachine>();

var move = GetComponent<IMovable>();


var rot = GetComponent<IRotatable>();
var grav = GetComponent<IGravitational>();
var ledge = GetComponent<ILedgeGrabbable>();
var combat = GetComponent<ICombatHandler>();

var walk = new WalkingState(_fsm, move, rot, grav);


var hang = new HangingState(_fsm, ledge);
var block = new BlockingState(_fsm, combat);
var attack = new AttackingState(_fsm, combat);

_fsm.SetState(walk);

(ledge as LedgeGrabHandler).OnLedgeGrab += () => _fsm.SetState(hang);


(ledge as LedgeGrabHandler).OnLedgeExit += () => _fsm.SetState(walk);
(combat as CombatHandler).OnBlock += () => _fsm.SetState(block);
(combat as CombatHandler).OnAttack += () => _fsm.SetState(attack);
}

private void Update() => _fsm?.Update();

public void OnMove([Link] ctx) =>


_fsm?.HandleInput([Link]<Vector2>());
public void OnJump([Link] ctx) { if ([Link])
_fsm?.HandleJump(); }
public void OnAttack([Link] ctx) { if ([Link])
_fsm?.HandleAttack(); }
public void OnBlock([Link] ctx) =>
_fsm?.HandleBlock([Link]<float>() > 0);
}

You might also like