added C#
This commit is contained in:
parent
ab79ace226
commit
00b76b6b25
63 changed files with 1172 additions and 1548 deletions
169
src/creatures/birb/Birb.cs
Normal file
169
src/creatures/birb/Birb.cs
Normal file
|
|
@ -0,0 +1,169 @@
|
|||
using System;
|
||||
using Chickensoft.LogicBlocks;
|
||||
using Chickensoft.LogicBlocks.Auto;
|
||||
|
||||
namespace Dancer;
|
||||
|
||||
[Meta(typeof(IAutoNode))]
|
||||
public sealed partial class Birb : CharacterBody2D, IAttackable
|
||||
{
|
||||
public override void _Notification(int what) => this.Notify(what);
|
||||
|
||||
[Export] public float Speed { get; set; } = 60f;
|
||||
[Export] public float AttackDistance { get; set; } = 32f;
|
||||
[Export] public float ThinkTime { get; set; } = 0.2f;
|
||||
[Export] public float Weight { get; set; } = 20f;
|
||||
[Export] public float MaxHp { get; set; } = 4f;
|
||||
|
||||
[Node] private ProgressBar HealthBar { get; set; } = null!;
|
||||
[Node] private AnimatedSprite2D BodySprite { get; set; } = null!;
|
||||
|
||||
private float _hp;
|
||||
private float _timeThought;
|
||||
private Node2D? _target;
|
||||
private float _targetVelocity;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_hp = MaxHp;
|
||||
HealthBar.MinValue = 0f;
|
||||
HealthBar.MaxValue = MaxHp;
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
_timeThought += (float)delta;
|
||||
if (_timeThought >= ThinkTime)
|
||||
{
|
||||
_timeThought -= ThinkTime;
|
||||
if (_target is not null)
|
||||
{
|
||||
var xDifference = _target.Position.X - Position.X;
|
||||
var distance = Mathf.Abs(xDifference);
|
||||
if (Mathf.Abs(distance - AttackDistance) > Speed * ThinkTime)
|
||||
_targetVelocity = Mathf.Sign(xDifference) * Mathf.Sign(distance - AttackDistance) * Speed;
|
||||
else
|
||||
_targetVelocity = 0f;
|
||||
BodySprite.FlipH = xDifference < 0;
|
||||
}
|
||||
else
|
||||
_targetVelocity = 0f;
|
||||
}
|
||||
|
||||
Velocity = Velocity with
|
||||
{
|
||||
X = Mathf.MoveToward(Velocity.X, _targetVelocity, (float)delta * 500f)
|
||||
};
|
||||
|
||||
if (!IsOnFloor()) Velocity += GetGravity() * (float)delta;
|
||||
|
||||
MoveAndSlide();
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
HealthBar.Value = _hp;
|
||||
HealthBar.Visible = _hp < MaxHp;
|
||||
}
|
||||
|
||||
public void OnDetectionBodyEntered(Node2D body) => _target = body;
|
||||
|
||||
public void OnChaseBodyExited(Node2D body)
|
||||
{
|
||||
if (body == _target) _target = null;
|
||||
}
|
||||
|
||||
public void GetAttacked(in Attack attack)
|
||||
{
|
||||
_hp -= attack.Damage;
|
||||
Velocity += attack.Impulse / Weight;
|
||||
if (_hp <= 0.01) QueueFree();
|
||||
}
|
||||
|
||||
[Meta]
|
||||
public sealed partial class Logic : AutoBlock
|
||||
{
|
||||
public sealed record Data
|
||||
{
|
||||
public required Birb Birb { get; set; }
|
||||
public float DurationLeft { get; set; }
|
||||
public Node2D? Target { get; set; }
|
||||
}
|
||||
|
||||
public Logic(Birb birb)
|
||||
{
|
||||
Set(new Data { Birb = birb });
|
||||
Preallocate<Root>();
|
||||
}
|
||||
|
||||
public static class Input
|
||||
{
|
||||
public readonly record struct TimerFinished;
|
||||
}
|
||||
|
||||
[Meta, StateDiagram]
|
||||
public abstract partial record Root : LogicBlockState
|
||||
{
|
||||
public virtual void OnPhysicsProcess(double delta)
|
||||
{
|
||||
}
|
||||
|
||||
[Meta]
|
||||
public sealed partial record Idle : Root;
|
||||
|
||||
[Meta]
|
||||
public abstract partial record Fight : Root
|
||||
{
|
||||
[Meta]
|
||||
public sealed partial record Think : Fight, IGet<Input.TimerFinished>
|
||||
{
|
||||
public Think()
|
||||
{
|
||||
this.OnEnter(() => Get<Data>().DurationLeft = (float)GD.RandRange(0.3, 0.6));
|
||||
}
|
||||
|
||||
public override void OnPhysicsProcess(double delta)
|
||||
{
|
||||
var data = Get<Data>();
|
||||
data.DurationLeft -= (float)delta;
|
||||
if (data.DurationLeft <= 0f) Input(new Input.TimerFinished());
|
||||
}
|
||||
|
||||
public Type On(in Input.TimerFinished input)
|
||||
{
|
||||
var data = Get<Data>();
|
||||
if (data.Target is not { } target) return To<Idle>();
|
||||
return target.GlobalPosition.DistanceTo(data.Birb.GlobalPosition) <
|
||||
data.Birb.AttackDistance
|
||||
? To<PrepareStrike>()
|
||||
: To<StepToPosition>();
|
||||
}
|
||||
}
|
||||
|
||||
[Meta]
|
||||
public sealed partial record StepToPosition : Fight;
|
||||
|
||||
[Meta]
|
||||
public sealed partial record PrepareStrike : Fight, IGet<Input.TimerFinished>
|
||||
{
|
||||
public PrepareStrike()
|
||||
{
|
||||
this.OnEnter(() => Get<Data>().DurationLeft = (float)GD.RandRange(0.7, 0.9));
|
||||
}
|
||||
|
||||
public override void OnPhysicsProcess(double delta)
|
||||
{
|
||||
var data = Get<Data>();
|
||||
data.DurationLeft -= (float)delta;
|
||||
if (data.DurationLeft <= 0f) Input(new Input.TimerFinished());
|
||||
}
|
||||
|
||||
public Type On(in Input.TimerFinished input) => To<Stab>();
|
||||
}
|
||||
|
||||
[Meta]
|
||||
public sealed partial record Stab : Fight;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue