Re-introduce prototype code
This commit is contained in:
130
src/player/Player.cs
Normal file
130
src/player/Player.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
using Chickensoft.AutoInject;
|
||||
using Chickensoft.GodotNodeInterfaces;
|
||||
using Chickensoft.Introspection;
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
namespace GameJamDungeon
|
||||
{
|
||||
public interface IPlayer : ICharacterBody3D
|
||||
{
|
||||
PlayerLogic PlayerLogic { get; }
|
||||
|
||||
PlayerData PlayerData { get; }
|
||||
|
||||
public Vector3 GetGlobalInputVector();
|
||||
}
|
||||
|
||||
[Meta(typeof(IAutoNode))]
|
||||
public partial class Player : CharacterBody3D, IPlayer, IProvide<PlayerLogic>
|
||||
{
|
||||
public override void _Notification(int what) => this.Notify(what);
|
||||
|
||||
PlayerLogic IProvide<PlayerLogic>.Value() => PlayerLogic;
|
||||
|
||||
[Dependency] public IAppRepo AppRepo => this.DependOn<IAppRepo>();
|
||||
[Dependency] public IGameRepo GameRepo => this.DependOn<IGameRepo>();
|
||||
|
||||
/// <summary>Rotation speed (quaternions?/sec).</summary>
|
||||
[Export(PropertyHint.Range, "0, 100, 0.1")]
|
||||
public float RotationSpeed { get; set; } = 0.5f;
|
||||
|
||||
/// <summary>Player speed (meters/sec).</summary>
|
||||
[Export(PropertyHint.Range, "0, 100, 0.1")]
|
||||
public float MoveSpeed { get; set; } = 2f;
|
||||
|
||||
/// <summary>Player speed (meters^2/sec).</summary>
|
||||
[Export(PropertyHint.Range, "0, 100, 0.1")]
|
||||
public float Acceleration { get; set; } = 1f;
|
||||
|
||||
public PlayerLogic.Settings Settings { get; set; } = default!;
|
||||
|
||||
public PlayerLogic PlayerLogic { get; set; } = default!;
|
||||
|
||||
public PlayerData PlayerData { get; set; } = default!;
|
||||
|
||||
public PlayerLogic.IBinding PlayerBinding { get; set; } = default!;
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Setup()
|
||||
{
|
||||
Settings = new PlayerLogic.Settings(
|
||||
RotationSpeed,
|
||||
MoveSpeed);
|
||||
|
||||
PlayerLogic = new PlayerLogic();
|
||||
PlayerLogic.Set(this as IPlayer);
|
||||
PlayerLogic.Set(Settings);
|
||||
PlayerLogic.Set(AppRepo);
|
||||
PlayerLogic.Set(GameRepo);
|
||||
PlayerLogic.Set(PlayerData);
|
||||
|
||||
GameRepo.PlayerGlobalPosition.Sync += OnPlayerPositionUpdated;
|
||||
}
|
||||
|
||||
private void OnPlayerPositionUpdated(Vector3 globalPosition)
|
||||
{
|
||||
GlobalPosition = globalPosition;
|
||||
}
|
||||
|
||||
public void OnResolved()
|
||||
{
|
||||
PlayerBinding = PlayerLogic.Bind();
|
||||
|
||||
PlayerBinding
|
||||
.Handle((in PlayerLogic.Output.MovementComputed output) =>
|
||||
{
|
||||
Transform = Transform with { Basis = output.Rotation };
|
||||
Velocity = output.Velocity;
|
||||
})
|
||||
.Handle((in PlayerLogic.Output.Animations.Attack output) =>
|
||||
{
|
||||
});
|
||||
|
||||
this.Provide();
|
||||
PlayerLogic.Start();
|
||||
}
|
||||
|
||||
public void OnReady()
|
||||
{
|
||||
SetPhysicsProcess(true);
|
||||
}
|
||||
|
||||
public void OnPhysicsProcess(double delta)
|
||||
{
|
||||
PlayerLogic.Input(new PlayerLogic.Input.PhysicsTick(delta));
|
||||
|
||||
var attackIsPressed = Input.IsActionJustPressed(GameInputs.Attack);
|
||||
if (attackIsPressed)
|
||||
PlayerLogic.Input(new PlayerLogic.Input.Attack());
|
||||
|
||||
MoveAndSlide();
|
||||
|
||||
PlayerLogic.Input(new PlayerLogic.Input.Moved(GlobalPosition));
|
||||
}
|
||||
|
||||
public Vector3 GetGlobalInputVector()
|
||||
{
|
||||
var rawInput = Input.GetVector(GameInputs.MoveLeft, GameInputs.MoveRight, GameInputs.MoveUp, GameInputs.MoveDown);
|
||||
|
||||
var input = new Vector3
|
||||
{
|
||||
X = rawInput.X,
|
||||
Z = rawInput.Y
|
||||
};
|
||||
return input with { Y = 0f };
|
||||
}
|
||||
|
||||
public void OnExitTree()
|
||||
{
|
||||
PlayerLogic.Stop();
|
||||
AppRepo.Dispose();
|
||||
GameRepo.Dispose();
|
||||
PlayerBinding.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user