i was wondering if you could help me out with some confusion i have over state machines and SOLID principles! sorry if the post gets too long, i'll try to keep things simple! So i have a huge player controller with dependencies of like 120 references and values (movement, crouch , mouse, jump , gravity etc etc) and i want to make it into a state machine. my confusion is in the whole state thing. i started out by trying to follow the Single responsibility principle so i broke down all functions to their own classes. so now i got movement.cs , jump.cs etc so all dependencies now are in their respective classes. But! my confusion again is, that if i were to make things into a state machine wouldn't the base class need to have those references? wouldnt that also make it become huge again? i.e. this is the new player controller
public class PlayerControllerManager : PortalTraveller, IDamageable
{
protected PlayerBaseState currentState;
protected PlayerStateFactory _statesFactory;
public PlayerBaseState CurrentState { get { return currentState; } set { currentState = value; } }
PlayerMovement playerMovement; // the first class i made from the previous player controller //
public Transform GetTransform()
{
return transform;
}
private void Awake()
{
playerMovement = GetComponent<PlayerMovement>();
}
}
The player movement class only holds values like speed, vectors and things like this. is this how its supposed to be? since the actual functions will go into their own state classes? am i overthinking it? am i doing stupid things??
ie this is the movement state class
public class PlayerMoveState : PlayerBaseState
{
public PlayerMoveState(PlayerControllerManager currentContext, PlayerStateFactory playerStateFactory) : base(currentContext, playerStateFactory) { }
public override void CheckSwitchStates() { }
public override void EnterState() { }
public override void ExitState() { }
public override void IntializeSubState() { }
public override void OnCollisionEnter() { }
public override void UpdateState() { }
}
can i store dependencies in here? is it good/bad practice? is there a better or well proper way to handle both a state machine and keep the SOLID principles? i did try something out by getting a public variable like this in my Playercontroller class
public Vector3 Move
{
get
{
return playerMovement.move;
}
set
{
Move = value;
}
}
and then did this in my moveState class
public override void UpdateState()
{
Vector2 movement = InputManager.GetPlayerMovement();
_baseContext.Move = new Vector3(movement.x, 0f, movement.y);
}
Does that mean that i need to declare all my variables again from the movement class to the context class so i can access them? or should i do it like this
public PlayerMovement playerMovement { get; set; }
and then just call
_baseContext.playerMovement.move = new Vector3(movement.x, 0f, movement.y);
Thank you for your time!
GetTransform()
method that just returns the same thing as the public.transform
getter that every component already inherits. \$\endgroup\$jump
andmove
are different states, you could not control the character's x-direction movement in the air, just like Castlevania does. Because you can only be in one state at a time. I recommend using composition to manage the code. \$\endgroup\$