state machine working

linux
Sascha Woitschetzki 2024-02-08 18:49:39 +07:00
parent bc504f5773
commit 97509170e7
7 changed files with 14 additions and 21 deletions

@ -11,7 +11,7 @@ public partial class CameraController : Node3D {
private const float maxZoom = 2f;
private const float minZoom = 10f;
public override void _Ready() => Input.MouseMode = Input.MouseModeEnum.Captured;
// public override void _Ready() => Input.MouseMode = Input.MouseModeEnum.Captured;
// public override void _Process(double delta) => GlobalPosition = GetParent<Node3D>().GlobalPosition;

@ -22,8 +22,8 @@ public partial class Player2 : CharacterBody3D
public override void _Ready() {
stateFactory = new();
Playback = (AnimationNodeStateMachinePlayback)animationTree.Get("parameters/playback");
ChangeState("Idle");
ChangeState("Walk");
ChangeState("idle");
ChangeState("move");
}
public override void _PhysicsProcess(double delta)
@ -44,6 +44,7 @@ public partial class Player2 : CharacterBody3D
// Vector2 inputDir = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
// Vector3 direction = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
Direction = new(Input.GetActionStrength("WalkLeft") - Input.GetActionStrength("WalkRight"), 0, Input.GetActionStrength("WalkUp") - Input.GetActionStrength("WalkDown"));
if (Direction != Vector3.Zero) ChangeState("move");
// if (Direction != Vector3.Zero)
// {

@ -6,14 +6,14 @@ public partial class IdleState : State {
private Player2 player;
public override void _Ready() {
player = GetParent<Player2>();
player.Playback.Travel("Idle");
player.Playback.Travel("idle");
}
public override void _PhysicsProcess(double delta) {
Vector3 velocity = player.Velocity;
if (Input.IsActionPressed("WalkRight") || Input.IsActionPressed("WalkLeft") || Input.IsActionPressed("WalkUp") || Input.IsActionPressed("WalkDown"))
player.ChangeState("Walk");
player.ChangeState("move");
velocity.X = Mathf.MoveToward(player.Velocity.X, 0, Player2.SPEED);
velocity.Z = Mathf.MoveToward(player.Velocity.Z, 0, Player2.SPEED);

@ -6,7 +6,7 @@ public partial class MoveState : State {
private Player2 player;
public override void _Ready() {
player = GetParent<Player2>();
player.Playback.Travel("Walk");
player.Playback.Travel("move");
}
public override void _PhysicsProcess(double delta) {
@ -14,12 +14,12 @@ public partial class MoveState : State {
if (player.Direction != Vector3.Zero)
{
player.ChangeState("Walking");
player.ChangeState("move");
velocity.X = player.Direction.X * Player2.SPEED;
velocity.Z = player.Direction.Z * Player2.SPEED;
}
else {
player.ChangeState("Idle");
player.ChangeState("idle");
}
player.Velocity = velocity;

@ -10,9 +10,9 @@ public abstract partial class State : Node2D {
public abstract void Exit();
public void Setup(string changeState, AnimationNodeStateMachinePlayback animation, Player2 persistantState) {
this.changeState = changeState;
this.animation = animation;
this.persistantState = persistantState;
public void Setup(string cS, AnimationNodeStateMachinePlayback a, Player2 pS) {
changeState = cS;
animation = a;
persistantState = pS;
}
}

@ -12,14 +12,5 @@ public partial class StateFactory : Node {
{"jump", new JumpState()}
};
// private void Init() {
// states = new() {
// {"idle", new IdleState()},
// {"attack", new AttackState()},
// {"move", new MoveState()},
// {"jump", new JumpState()}
// };
// }
public State GetState(string stateName) => CollectionExtensions.GetValueOrDefault(states, stateName);
}