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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
1
src/creatures/birb/Birb.cs.uid
Normal file
1
src/creatures/birb/Birb.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://daowp76saltic
|
||||
BIN
src/creatures/birb/birb.aseprite
Normal file
BIN
src/creatures/birb/birb.aseprite
Normal file
Binary file not shown.
21
src/creatures/birb/birb.aseprite.import
Normal file
21
src/creatures/birb/birb.aseprite.import
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
[remap]
|
||||
|
||||
importer="aseprite_wizard.plugin.spriteframes"
|
||||
type="SpriteFrames"
|
||||
uid="uid://cyf7ij04grglc"
|
||||
path="res://.godot/imported/birb.aseprite-70cec5e9f1931c6cd2bc1e2936bb8cc0.res"
|
||||
|
||||
[deps]
|
||||
|
||||
source_file="res://src/creatures/birb/birb.aseprite"
|
||||
dest_files=["res://.godot/imported/birb.aseprite-70cec5e9f1931c6cd2bc1e2936bb8cc0.res"]
|
||||
|
||||
[params]
|
||||
|
||||
layer/exclude_layers_pattern=""
|
||||
layer/only_visible_layers=false
|
||||
sheet/sheet_type="packed"
|
||||
sheet/sheet_columns=12
|
||||
sheet/frame_padding=0
|
||||
sheet/scale=1
|
||||
animation/round_fps=true
|
||||
195
src/creatures/birb/birb.tscn
Normal file
195
src/creatures/birb/birb.tscn
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
[gd_scene format=3 uid="uid://bosbrhc3oprau"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://daowp76saltic" path="res://src/creatures/birb/Birb.cs" id="1_lyo8d"]
|
||||
[ext_resource type="SpriteFrames" uid="uid://cyf7ij04grglc" path="res://src/creatures/birb/birb.aseprite" id="2_ba6cv"]
|
||||
|
||||
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_feb5d"]
|
||||
radius = 5.0
|
||||
height = 14.0
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_lyo8d"]
|
||||
radius = 52.0
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_ba6cv"]
|
||||
radius = 110.0
|
||||
|
||||
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_lyo8d"]
|
||||
bg_color = Color(0.15, 0.15, 0.15, 0.34509805)
|
||||
|
||||
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_ba6cv"]
|
||||
bg_color = Color(0.73, 0.10949998, 0.14052509, 0.8666667)
|
||||
|
||||
[sub_resource type="Animation" id="Animation_lyo8d"]
|
||||
length = 0.001
|
||||
step = 0.1
|
||||
tracks/0/type = "value"
|
||||
tracks/0/imported = false
|
||||
tracks/0/enabled = true
|
||||
tracks/0/path = NodePath("BodySprite:animation")
|
||||
tracks/0/interp = 1
|
||||
tracks/0/loop_wrap = true
|
||||
tracks/0/keys = {
|
||||
"times": PackedFloat32Array(0),
|
||||
"transitions": PackedFloat32Array(1),
|
||||
"update": 1,
|
||||
"values": [&"idle"]
|
||||
}
|
||||
tracks/1/type = "value"
|
||||
tracks/1/imported = false
|
||||
tracks/1/enabled = true
|
||||
tracks/1/path = NodePath("BodySprite:frame")
|
||||
tracks/1/interp = 1
|
||||
tracks/1/loop_wrap = true
|
||||
tracks/1/keys = {
|
||||
"times": PackedFloat32Array(0),
|
||||
"transitions": PackedFloat32Array(1),
|
||||
"update": 1,
|
||||
"values": [0]
|
||||
}
|
||||
|
||||
[sub_resource type="Animation" id="Animation_ba6cv"]
|
||||
resource_name = "idle"
|
||||
loop_mode = 1
|
||||
step = 0.1
|
||||
tracks/0/type = "value"
|
||||
tracks/0/imported = false
|
||||
tracks/0/enabled = true
|
||||
tracks/0/path = NodePath("BodySprite:animation")
|
||||
tracks/0/interp = 1
|
||||
tracks/0/loop_wrap = true
|
||||
tracks/0/keys = {
|
||||
"times": PackedFloat32Array(0),
|
||||
"transitions": PackedFloat32Array(1),
|
||||
"update": 1,
|
||||
"values": [&"idle"]
|
||||
}
|
||||
tracks/1/type = "value"
|
||||
tracks/1/imported = false
|
||||
tracks/1/enabled = true
|
||||
tracks/1/path = NodePath("BodySprite:frame")
|
||||
tracks/1/interp = 1
|
||||
tracks/1/loop_wrap = true
|
||||
tracks/1/keys = {
|
||||
"times": PackedFloat32Array(0),
|
||||
"transitions": PackedFloat32Array(1),
|
||||
"update": 1,
|
||||
"values": [0]
|
||||
}
|
||||
|
||||
[sub_resource type="Animation" id="Animation_1i6dk"]
|
||||
resource_name = "prepare_attack"
|
||||
step = 0.1
|
||||
tracks/0/type = "value"
|
||||
tracks/0/imported = false
|
||||
tracks/0/enabled = true
|
||||
tracks/0/path = NodePath("BodySprite:animation")
|
||||
tracks/0/interp = 1
|
||||
tracks/0/loop_wrap = true
|
||||
tracks/0/keys = {
|
||||
"times": PackedFloat32Array(0),
|
||||
"transitions": PackedFloat32Array(1),
|
||||
"update": 1,
|
||||
"values": [&"prepare_attack"]
|
||||
}
|
||||
tracks/1/type = "value"
|
||||
tracks/1/imported = false
|
||||
tracks/1/enabled = true
|
||||
tracks/1/path = NodePath("BodySprite:frame")
|
||||
tracks/1/interp = 1
|
||||
tracks/1/loop_wrap = true
|
||||
tracks/1/keys = {
|
||||
"times": PackedFloat32Array(0),
|
||||
"transitions": PackedFloat32Array(1),
|
||||
"update": 1,
|
||||
"values": [0]
|
||||
}
|
||||
|
||||
[sub_resource type="Animation" id="Animation_3ly8m"]
|
||||
resource_name = "stab"
|
||||
step = 0.1
|
||||
tracks/0/type = "value"
|
||||
tracks/0/imported = false
|
||||
tracks/0/enabled = true
|
||||
tracks/0/path = NodePath("BodySprite:animation")
|
||||
tracks/0/interp = 1
|
||||
tracks/0/loop_wrap = true
|
||||
tracks/0/keys = {
|
||||
"times": PackedFloat32Array(0),
|
||||
"transitions": PackedFloat32Array(1),
|
||||
"update": 1,
|
||||
"values": [&"stab"]
|
||||
}
|
||||
tracks/1/type = "value"
|
||||
tracks/1/imported = false
|
||||
tracks/1/enabled = true
|
||||
tracks/1/path = NodePath("BodySprite:frame")
|
||||
tracks/1/interp = 1
|
||||
tracks/1/loop_wrap = true
|
||||
tracks/1/keys = {
|
||||
"times": PackedFloat32Array(0),
|
||||
"transitions": PackedFloat32Array(1),
|
||||
"update": 1,
|
||||
"values": [0]
|
||||
}
|
||||
|
||||
[sub_resource type="AnimationLibrary" id="AnimationLibrary_1i6dk"]
|
||||
_data = {
|
||||
&"RESET": SubResource("Animation_lyo8d"),
|
||||
&"idle": SubResource("Animation_ba6cv"),
|
||||
&"prepare_attack": SubResource("Animation_1i6dk"),
|
||||
&"stab": SubResource("Animation_3ly8m")
|
||||
}
|
||||
|
||||
[node name="Birb" type="CharacterBody2D" unique_id=428810294]
|
||||
collision_layer = 4
|
||||
script = ExtResource("1_lyo8d")
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="BodySprite" type="AnimatedSprite2D" parent="." unique_id=581143480]
|
||||
unique_name_in_owner = true
|
||||
position = Vector2(2, -4)
|
||||
sprite_frames = ExtResource("2_ba6cv")
|
||||
animation = &"idle"
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1744954023]
|
||||
position = Vector2(0, 1)
|
||||
shape = SubResource("CapsuleShape2D_feb5d")
|
||||
|
||||
[node name="Detection" type="Area2D" parent="." unique_id=151830843]
|
||||
collision_layer = 0
|
||||
collision_mask = 2
|
||||
monitorable = false
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Detection" unique_id=1217467707]
|
||||
shape = SubResource("CircleShape2D_lyo8d")
|
||||
debug_color = Color(0, 0.6, 0.69803923, 0.16862746)
|
||||
|
||||
[node name="Chase" type="Area2D" parent="." unique_id=172000915]
|
||||
collision_layer = 0
|
||||
collision_mask = 2
|
||||
monitorable = false
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Chase" unique_id=567870323]
|
||||
shape = SubResource("CircleShape2D_ba6cv")
|
||||
debug_color = Color(0, 0.6, 0.69803923, 0)
|
||||
|
||||
[node name="HealthBar" type="ProgressBar" parent="." unique_id=369348156]
|
||||
unique_name_in_owner = true
|
||||
anchors_preset = 5
|
||||
anchor_left = 0.5
|
||||
anchor_right = 0.5
|
||||
offset_left = -10.0
|
||||
offset_top = -12.0
|
||||
offset_right = 10.0
|
||||
offset_bottom = -10.0
|
||||
grow_horizontal = 2
|
||||
theme_override_styles/background = SubResource("StyleBoxFlat_lyo8d")
|
||||
theme_override_styles/fill = SubResource("StyleBoxFlat_ba6cv")
|
||||
value = 50.0
|
||||
show_percentage = false
|
||||
|
||||
[node name="AnimationPlayer" type="AnimationPlayer" parent="." unique_id=695394949]
|
||||
libraries/ = SubResource("AnimationLibrary_1i6dk")
|
||||
|
||||
[connection signal="body_entered" from="Detection" to="." method="OnDetectionBodyEntered"]
|
||||
[connection signal="body_exited" from="Chase" to="." method="OnChaseBodyExited"]
|
||||
Loading…
Add table
Add a link
Reference in a new issue