initial add
This commit is contained in:
BIN
gobbos_delivery/scripts/states/.DS_Store
vendored
Normal file
BIN
gobbos_delivery/scripts/states/.DS_Store
vendored
Normal file
Binary file not shown.
11
gobbos_delivery/scripts/states/State.cs
Normal file
11
gobbos_delivery/scripts/states/State.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using Godot;
|
||||
|
||||
public partial class State : Node {
|
||||
[Signal]
|
||||
public delegate void TransitionedEventHandler(Node sender, string newState);
|
||||
|
||||
public virtual void Enter() { }
|
||||
public virtual void Exit() { }
|
||||
public virtual void Update(double delta) { }
|
||||
public virtual void PhysicsUpdate(double delta) { }
|
||||
}
|
||||
1
gobbos_delivery/scripts/states/State.cs.uid
Normal file
1
gobbos_delivery/scripts/states/State.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://ch2q7bjkugikg
|
||||
54
gobbos_delivery/scripts/states/StateMachine.cs
Normal file
54
gobbos_delivery/scripts/states/StateMachine.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using Godot;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public partial class StateMachine : State {
|
||||
[Export]
|
||||
public State InitialState { get; set; }
|
||||
|
||||
private State currentState;
|
||||
private readonly Dictionary<string, State> stateList = [];
|
||||
|
||||
public override void _Ready() {
|
||||
foreach (var child in GetChildren()) {
|
||||
if (child is State state)
|
||||
{
|
||||
stateList.Add(child.Name.ToString().ToLower(), state);
|
||||
state.Transitioned += OnChildTransition;
|
||||
}
|
||||
}
|
||||
|
||||
if (InitialState != null)
|
||||
{
|
||||
InitialState.Enter();
|
||||
currentState = InitialState;
|
||||
}
|
||||
}
|
||||
|
||||
public void Process(float delta)
|
||||
{
|
||||
currentState?.Update(delta);
|
||||
}
|
||||
|
||||
public override void PhysicsUpdate(double delta)
|
||||
{
|
||||
currentState?.PhysicsUpdate(delta);
|
||||
}
|
||||
|
||||
public void OnChildTransition(Node sender, string newStateName)
|
||||
{
|
||||
if (sender != currentState)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var newState = stateList[newStateName.ToLower()];
|
||||
if (newState == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
currentState?.Exit();
|
||||
newState.Enter();
|
||||
currentState = newState;
|
||||
}
|
||||
}
|
||||
1
gobbos_delivery/scripts/states/StateMachine.cs.uid
Normal file
1
gobbos_delivery/scripts/states/StateMachine.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://dwbhtjvpv0utl
|
||||
2
gobbos_delivery/scripts/states/player/.editorconfig
Normal file
2
gobbos_delivery/scripts/states/player/.editorconfig
Normal file
@@ -0,0 +1,2 @@
|
||||
[*.cs]
|
||||
csharp_new_line_before_open_brace = none
|
||||
77
gobbos_delivery/scripts/states/player/PlayerAirborn.cs
Normal file
77
gobbos_delivery/scripts/states/player/PlayerAirborn.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using Godot;
|
||||
|
||||
public partial class PlayerAirborn : State {
|
||||
private Player player;
|
||||
|
||||
public override void _Ready() {
|
||||
player = GetNode<Player>("../..");
|
||||
}
|
||||
|
||||
public override void Enter() {
|
||||
GD.Print("airborn");
|
||||
player.StateLabel?.Text = Name;
|
||||
player.InParachute = false;
|
||||
player.AnimStateMachine?.Travel("Airborne");
|
||||
}
|
||||
|
||||
public override void PhysicsUpdate(double delta) {
|
||||
var floatDelta = (float)delta;
|
||||
if (player.IsOnFloor()) {
|
||||
EmitSignal(SignalName.Transitioned, this, "PlayerGrounded");
|
||||
}
|
||||
|
||||
if (player.MoveDir.X != 0) {
|
||||
player.Velocity = new(
|
||||
Mathf.MoveToward(player.Velocity.X, player.MoveDir.X * player.Speed, player.Acceleration),
|
||||
player.Velocity.Y);
|
||||
}
|
||||
else {
|
||||
player.Velocity = new(
|
||||
Mathf.MoveToward(player.Velocity.X, 0.0f, player.AirFriction),
|
||||
player.Velocity.Y);
|
||||
}
|
||||
|
||||
if (player.Velocity.Y >= 0) {
|
||||
player.Velocity = new(
|
||||
player.Velocity.X,
|
||||
player.Velocity.Y + player.Gravity * floatDelta * player.FallMod);
|
||||
}
|
||||
else {
|
||||
player.Velocity = new(
|
||||
player.Velocity.X,
|
||||
player.Velocity.Y + player.Gravity * floatDelta);
|
||||
}
|
||||
|
||||
var wcl = player.WallCastLow != null && player.WallCastLow.IsColliding();
|
||||
var wcm = player.WallCastMid != null && player.WallCastLow.IsColliding();
|
||||
var wct = player.WallCastTop != null && player.WallCastTop.IsColliding();
|
||||
if (wcl && wcm && wct) {
|
||||
EmitSignal(SignalName.Transitioned, this, "PlayerWallGrab");
|
||||
}
|
||||
|
||||
if (Input.IsActionJustPressed("jump") && player.CanCoyoteJump) {
|
||||
player.Velocity = new(player.Velocity.X, player.Jump);
|
||||
}
|
||||
|
||||
LedgeLogic();
|
||||
|
||||
player.LastVelocityY = player.Velocity.Y;
|
||||
}
|
||||
|
||||
private void LedgeLogic() {
|
||||
if (player.IsOnFloor() || player.Velocity.Y <= -30 || player.MoveDir == Vector2.Zero) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!player.WallCastMid.IsColliding() || player.WallCastTop.IsColliding()) {
|
||||
return;
|
||||
}
|
||||
|
||||
var desiredPos = player.WallCastMid.GetCollisionPoint().Snapped(new Vector2(4, 4)) + new Vector2(-2 * player.MoveDir.X, -1);
|
||||
var posTween = CreateTween().SetTrans(Tween.TransitionType.Sine);
|
||||
posTween.TweenProperty(player, "global_position", desiredPos, 0.05);
|
||||
|
||||
player.LastVelocityY = 0.0f;
|
||||
player.Velocity = new (0.0f, player.LedgeJump);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://desj0bmav00ec
|
||||
83
gobbos_delivery/scripts/states/player/PlayerGrounded.cs
Normal file
83
gobbos_delivery/scripts/states/player/PlayerGrounded.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using Godot;
|
||||
|
||||
public partial class PlayerGrounded : State {
|
||||
private Player player;
|
||||
private Node3D modelRoot;
|
||||
|
||||
public override void _Ready() {
|
||||
player = GetNode<Player>("../..");
|
||||
modelRoot = GetNode<Node3D>("../../ModelContainer/SubViewportContainer/SubViewport/blockbench_export");
|
||||
}
|
||||
|
||||
public override void Enter() {
|
||||
player.StateLabel?.Text = Name;
|
||||
player.ResetCoyoteTimer();
|
||||
player.InParachute = false;
|
||||
player.AnimStateMachine?.Travel("Grounded");
|
||||
|
||||
var fall_speed = Mathf.Abs(player.LastVelocityY);
|
||||
fall_speed *= 0.0025f;
|
||||
if (modelRoot != null) {
|
||||
modelRoot.Scale = new (
|
||||
modelRoot.Scale.X + fall_speed,
|
||||
modelRoot.Scale.Y - fall_speed,
|
||||
modelRoot.Scale.Z + fall_speed);
|
||||
|
||||
var fallTween = GetTree().CreateTween();
|
||||
fallTween.TweenProperty(
|
||||
modelRoot,
|
||||
"scale",
|
||||
new Vector3(1.0f, 1.0f, 1.0f),
|
||||
0.2);
|
||||
}
|
||||
|
||||
|
||||
// if (player.SprintStateMachine) {
|
||||
// if (move_dir < 0) {
|
||||
// player.SprintStateMachine.travel("sprint_left");
|
||||
// }
|
||||
// else if (move_dir > 0) {
|
||||
// player.SprintStateMachine.travel("sprint_right");
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
public override void PhysicsUpdate(double delta) {
|
||||
if (!player.IsOnFloor()) {
|
||||
EmitSignal(SignalName.Transitioned, this, "PlayerAirborne");
|
||||
}
|
||||
|
||||
if (player.MoveDir.X != 0) {
|
||||
if (Input.IsActionPressed("slow_walk")) {
|
||||
player.Velocity = new (
|
||||
Mathf.MoveToward(player.Velocity.X, player.MoveDir.X * player.Speed * player.SlowMod, player.Acceleration),
|
||||
player.Velocity.Y);
|
||||
player.GroundedStateMachine?.Travel("Walk");
|
||||
}
|
||||
else {
|
||||
player.Velocity = new (
|
||||
Mathf.MoveToward(player.Velocity.X, player.MoveDir.X * player.Speed, player.Acceleration),
|
||||
player.Velocity.Y);
|
||||
player.GroundedStateMachine?.Travel("Sprint");
|
||||
|
||||
if (player.MoveDir.X < 0) {
|
||||
player.SprintStateMachine?.Travel("sprint_left");
|
||||
}
|
||||
else if (player.MoveDir.X > 0) {
|
||||
player.SprintStateMachine?.Travel("sprint_right");
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
player.Velocity = new (
|
||||
Mathf.MoveToward(player.Velocity.X, 0.0f, player.Friction),
|
||||
player.Velocity.Y);
|
||||
player.GroundedStateMachine?.Travel("Idle");
|
||||
}
|
||||
|
||||
if (Input.IsActionJustPressed("jump") && player.IsOnFloor()) {
|
||||
player.Velocity = new (player.Velocity.X, player.Jump);
|
||||
EmitSignal(SignalName.Transitioned, this, "PlayerAirborne");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://dv3sve5vfcn55
|
||||
61
gobbos_delivery/scripts/states/player/PlayerWallGrab.cs
Normal file
61
gobbos_delivery/scripts/states/player/PlayerWallGrab.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using Godot;
|
||||
|
||||
public partial class PlayerWallGrab : State {
|
||||
private Player player;
|
||||
|
||||
private float wallDirection;
|
||||
|
||||
public override void _Ready() {
|
||||
player = GetNode<Player>("../..");
|
||||
}
|
||||
|
||||
public override void Enter() {
|
||||
player.StateLabel?.Text = Name;
|
||||
player.InParachute = false;
|
||||
|
||||
Vector2 desiredPos = new (
|
||||
(player.WallCastLow.GetCollisionPoint().Snapped(new Vector2(4, 4)) + new Vector2(-2 * player.MoveDir.X, 0)).X,
|
||||
player.GlobalPosition.Y);
|
||||
var posTween = CreateTween().SetTrans(Tween.TransitionType.Sine);
|
||||
posTween.TweenProperty(player, "global_position", desiredPos, 0.05);
|
||||
wallDirection = player.MoveDir.X;
|
||||
}
|
||||
|
||||
public override void PhysicsUpdate(double delta) {
|
||||
var floatDelta = (float)delta;
|
||||
player.Velocity = new (0.0f, player.Velocity.Y);
|
||||
if (player.MoveDir.X != 0.0) {
|
||||
if (!player.WallCastLow.IsColliding() ||
|
||||
!player.WallCastMid.IsColliding() ||
|
||||
!player.WallCastTop.IsColliding()) {
|
||||
EmitSignal(SignalName.Transitioned, this, "PlayerAirborne");
|
||||
}
|
||||
}
|
||||
|
||||
if (player.MoveDir.Y != 0) {
|
||||
player.Velocity = new (
|
||||
player.Velocity.X,
|
||||
Mathf.MoveToward(player.Velocity.Y, player.MoveDir.Y * player.WallSpeed, player.Acceleration));
|
||||
}
|
||||
else {
|
||||
player.Velocity = new (
|
||||
player.Velocity.X,
|
||||
Mathf.MoveToward(player.Velocity.Y, 0.0f, floatDelta * player.WallFriction));
|
||||
}
|
||||
|
||||
if (Input.IsActionJustPressed("jump")) {
|
||||
player.WallCastTop?.Enabled = false;
|
||||
player.WallCastMid?.Enabled = false;
|
||||
player.WallCastLow?.Enabled = false;
|
||||
player.WallCastDelayTimer.Start();
|
||||
EmitSignal(SignalName.Transitioned, this, "PlayerAirborne");
|
||||
player.Velocity = new (player.Velocity.X, player.Jump * 0.85f);
|
||||
if (wallDirection > 0) {
|
||||
player.Velocity = new (-player.WallJump, player.Velocity.Y);
|
||||
}
|
||||
else {
|
||||
player.Velocity = new (player.WallJump, player.Velocity.Y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://dn3js76wk06we
|
||||
Reference in New Issue
Block a user