77 lines
2.4 KiB
C#
77 lines
2.4 KiB
C#
using DRPGActionGame.Player.States;
|
|
using Godot;
|
|
using static DRPGActionGame.Player.States.State;
|
|
|
|
namespace DRPGActionGame.Player;
|
|
|
|
public partial class Player2 : CharacterBody3D
|
|
{
|
|
public const float SPEED = 5.0f;
|
|
private const float JUMP_VELOCITY = 4.5f;
|
|
public Vector3 Direction;
|
|
|
|
// Get the gravity from the project settings to be synced with RigidBody nodes.
|
|
private float gravity = ProjectSettings.GetSetting("physics/3d/default_gravity").AsSingle();
|
|
|
|
// [Export] private AnimationPlayer animationPlayer;
|
|
[Export] private AnimationTree animationTree;
|
|
public AnimationNodeStateMachinePlayback Playback;
|
|
private StateFactory stateFactory;
|
|
private State state;
|
|
|
|
public override void _Ready() {
|
|
stateFactory = new();
|
|
Playback = (AnimationNodeStateMachinePlayback)animationTree.Get("parameters/playback");
|
|
ChangeState("idle");
|
|
ChangeState("move");
|
|
}
|
|
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
Vector3 velocity = Velocity;
|
|
|
|
// Add the gravity.
|
|
if (!IsOnFloor()) velocity.Y -= gravity * (float)delta;
|
|
|
|
// Handle Jump.
|
|
if (Input.IsActionJustPressed("Jump") && IsOnFloor()) {
|
|
velocity.Y = JUMP_VELOCITY;
|
|
Playback.Travel("Jump");
|
|
}
|
|
|
|
// Get the input direction and handle the movement/deceleration.
|
|
// As good practice, you should replace UI actions with custom gameplay actions.
|
|
// 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)
|
|
// {
|
|
// velocity.X = Direction.X * SPEED;
|
|
// velocity.Z = Direction.Z * SPEED;
|
|
// playback.Travel("Walk");
|
|
// }
|
|
// else {
|
|
// velocity.X = Mathf.MoveToward(Velocity.X, 0, SPEED);
|
|
// velocity.Z = Mathf.MoveToward(Velocity.Z, 0, SPEED);
|
|
// playback.Travel("Idle");
|
|
// }
|
|
//
|
|
// Velocity = velocity;
|
|
|
|
MoveAndSlide();
|
|
}
|
|
|
|
public void ChangeState(string newStateName) {
|
|
if (state != null) {
|
|
state.Exit();
|
|
state.QueueFree();
|
|
}
|
|
//Add New State
|
|
state = new StateFactory().GetState(newStateName);
|
|
state.Setup("ChangeState", Playback, this);
|
|
state.Name = newStateName;
|
|
AddChild(state);
|
|
}
|
|
} |