Remove in-progress work that isn't working well
This commit is contained in:
150
src/enemy/Enemy.cs
Normal file
150
src/enemy/Enemy.cs
Normal file
@@ -0,0 +1,150 @@
|
||||
using Chickensoft.AutoInject;
|
||||
using Chickensoft.GodotNodeInterfaces;
|
||||
using Chickensoft.Introspection;
|
||||
using Godot;
|
||||
using System.Linq;
|
||||
|
||||
namespace GameJamDungeon;
|
||||
|
||||
public interface IEnemy : ICharacterBody3D
|
||||
{
|
||||
public IEnemyLogic EnemyLogic { get; }
|
||||
|
||||
public int CurrentHP { get; set; }
|
||||
|
||||
public Resource EnemyStats { get; set; }
|
||||
}
|
||||
|
||||
[Meta(typeof(IAutoNode))]
|
||||
public partial class Enemy : CharacterBody3D, IEnemy, IProvide<IEnemyLogic>
|
||||
{
|
||||
public override void _Notification(int what) => this.Notify(what);
|
||||
|
||||
public IEnemyLogic EnemyLogic { get; set; } = default!;
|
||||
|
||||
IEnemyLogic IProvide<IEnemyLogic>.Value() => EnemyLogic;
|
||||
|
||||
public EnemyLogic.IBinding EnemyBinding { get; set; } = default!;
|
||||
|
||||
[Dependency] IGameRepo GameRepo => this.DependOn<IGameRepo>();
|
||||
|
||||
[Export]
|
||||
public Resource EnemyStats { get; set; } = new();
|
||||
|
||||
[Node] public Area3D DetectionSphere { get; set; } = default!;
|
||||
|
||||
[Node] public Area3D AlertedSphere { get; set; } = default!;
|
||||
|
||||
public static PackedScene CollisionDetectorScene => GD.Load<PackedScene>("res://src/enemy/CollisionDetector.tscn");
|
||||
|
||||
public static Area3D CollisionDetector { get; set; } = default!;
|
||||
|
||||
public int CurrentHP { get; set; }
|
||||
|
||||
public void Setup()
|
||||
{
|
||||
EnemyLogic = new EnemyLogic();
|
||||
EnemyLogic.Set(EnemyStats);
|
||||
EnemyLogic.Set(this as IEnemy);
|
||||
EnemyLogic.Set(GameRepo);
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
var enemyResource = EnemyStats as EnemyStats;
|
||||
CurrentHP = enemyResource.MaximumHP;
|
||||
DetectionSphere.BodyEntered += OnDetectionSphereEntered;
|
||||
AlertedSphere.BodyExited += OnAlertedSphereExited;
|
||||
}
|
||||
|
||||
public void OnResolved()
|
||||
{
|
||||
EnemyBinding = EnemyLogic.Bind();
|
||||
|
||||
EnemyBinding
|
||||
.Handle((in EnemyLogic.Output.MovementComputed output) =>
|
||||
{
|
||||
var spriteNode = GetChildren().OfType<AnimatedSprite3D>();
|
||||
|
||||
if (spriteNode.Any())
|
||||
PlayMovementAnimations(spriteNode.Single(), Velocity);
|
||||
|
||||
MoveAndCollide(output.Velocity);
|
||||
})
|
||||
.Handle((in EnemyLogic.Output.Die output) =>
|
||||
{
|
||||
CollisionDetector.Dispose();
|
||||
QueueFree();
|
||||
})
|
||||
.Handle((in EnemyLogic.Output.HitByPlayer output) =>
|
||||
{
|
||||
});
|
||||
|
||||
this.Provide();
|
||||
|
||||
EnemyLogic.Start();
|
||||
}
|
||||
|
||||
public void PlayMovementAnimations(AnimatedSprite3D sprite, Vector3 velocity)
|
||||
{
|
||||
if (sprite != null && velocity.Length() > 0.2f)
|
||||
{
|
||||
var lookdir = (GlobalPosition).Normalized();
|
||||
var sign = lookdir.Sign();
|
||||
if (lookdir.MaxAxisIndex() == Vector3.Axis.X && sign.X == 1)
|
||||
sprite.Play("walk_right");
|
||||
if (lookdir.MaxAxisIndex() == Vector3.Axis.X && sign.X == -1)
|
||||
sprite.Play("walk_left");
|
||||
if (lookdir.MaxAxisIndex() == Vector3.Axis.Z && sign.Z == 1)
|
||||
sprite.Play("walk_forward");
|
||||
if (lookdir.MaxAxisIndex() == Vector3.Axis.Z && sign.Z == -1)
|
||||
sprite.Play("walk_backward");
|
||||
}
|
||||
if (sprite != null && velocity.IsZeroApprox())
|
||||
sprite.Stop();
|
||||
}
|
||||
|
||||
|
||||
public void OnPhysicsProcess(double delta)
|
||||
{
|
||||
EnemyLogic.Input(new EnemyLogic.Input.PhysicsTick(delta));
|
||||
}
|
||||
|
||||
public void OnPlayerHitboxEntered(Area3D body)
|
||||
{
|
||||
if (body.GetParent() is IPlayer)
|
||||
{
|
||||
if (CurrentHP > 0)
|
||||
{
|
||||
GD.Print("Enemy Hit");
|
||||
EnemyLogic.Input(new EnemyLogic.Input.HitByPlayer());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnDetectionSphereEntered(Node3D body)
|
||||
{
|
||||
GD.Print($"Detected {body.Name}...");
|
||||
EnemyLogic.Input(new EnemyLogic.Input.Alerted());
|
||||
}
|
||||
|
||||
public void OnAlertedSphereExited(Node3D body)
|
||||
{
|
||||
GD.Print($"Lost track of {body.Name}...");
|
||||
EnemyLogic.Input(new EnemyLogic.Input.LostPlayer());
|
||||
}
|
||||
|
||||
public void OnReady()
|
||||
{
|
||||
SetPhysicsProcess(true);
|
||||
CollisionDetector = CollisionDetectorScene.Instantiate<Area3D>();
|
||||
CollisionDetector.AreaEntered += OnPlayerHitboxEntered;
|
||||
AddChild(CollisionDetector);
|
||||
}
|
||||
|
||||
public void OnExitTree()
|
||||
{
|
||||
EnemyLogic.Stop();
|
||||
EnemyBinding.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user