save
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using Godot;
|
||||
|
||||
public partial class Enemy : PathFollow2D
|
||||
{
|
||||
[Export] public EnemyType Type;
|
||||
[Export] public Health Health;
|
||||
|
||||
public event Action<Enemy> Died;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Health.Death += _ => Died?.Invoke(this);
|
||||
}
|
||||
|
||||
public void ResetEnemy()
|
||||
{
|
||||
GlobalPosition = Vector2.Zero;
|
||||
Rotation = 0f;
|
||||
Visible = true;
|
||||
ProcessMode = ProcessModeEnum.Inherit;
|
||||
|
||||
Health.Reset();
|
||||
// reset velocity, animation, AI, etc here
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://bj52mq7uip7di
|
||||
@@ -0,0 +1,6 @@
|
||||
using Godot;
|
||||
|
||||
public partial class EnemyArea : Area2D
|
||||
{
|
||||
[Export] public Enemy Enemy { get; set; }
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://c247m8m3qmlk0
|
||||
@@ -3,24 +3,18 @@ using System;
|
||||
|
||||
public partial class EnemyMovement : Node
|
||||
{
|
||||
[Export] private NavigationAgent2D _agent2D;
|
||||
[Export] private CharacterBody2D _body2D;
|
||||
[Export] private PathFollow2D _pathFollow2D;
|
||||
[Export] private float _speed;
|
||||
|
||||
private double _time = 0;
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
|
||||
public override void _EnterTree()
|
||||
{
|
||||
_time += delta;
|
||||
if (_time > 0.2)
|
||||
{
|
||||
_time = 0;
|
||||
_agent2D.SetTargetPosition(GameController.Instance.Player.GlobalPosition);
|
||||
}
|
||||
// _parent.Position = _agent2D.GetNextPathPosition();
|
||||
var gpos = _agent2D.GetNextPathPosition();
|
||||
var dir = (gpos - _body2D.GlobalPosition).Normalized();
|
||||
// _body2D.SetVelocity(dir * _speed * (float)delta);
|
||||
_body2D.GlobalPosition += dir * _speed * (float)delta;
|
||||
_pathFollow2D.ProgressRatio = 0;
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
_pathFollow2D.ProgressRatio += (float)delta * (_speed / 1000f);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
using Godot;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public enum EnemyType
|
||||
{
|
||||
Stone,
|
||||
Stump,
|
||||
Liquid
|
||||
}
|
||||
|
||||
public partial class EnemyPool : Node
|
||||
{
|
||||
[Export] private Godot.Collections.Dictionary<EnemyType, PackedScene> _scenes;
|
||||
[Export] private int _poolSize = 10;
|
||||
|
||||
private readonly Dictionary<EnemyType, Queue<Enemy>> _pool = new();
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
foreach (var kvp in _scenes)
|
||||
{
|
||||
var q = new Queue<Enemy>();
|
||||
_pool[kvp.Key] = q;
|
||||
|
||||
for (int i = 0; i < _poolSize; i++)
|
||||
q.Enqueue(CreateEnemy(kvp.Key));
|
||||
}
|
||||
}
|
||||
|
||||
private Enemy CreateEnemy(EnemyType type)
|
||||
{
|
||||
var e = _scenes[type].Instantiate<Enemy>();
|
||||
e.Died += ReturnToPool;
|
||||
return e;
|
||||
}
|
||||
|
||||
public Enemy Get(EnemyType type)
|
||||
{
|
||||
if (!_pool.TryGetValue(type, out var q))
|
||||
return null;
|
||||
|
||||
var e = q.Count > 0 ? q.Dequeue() : CreateEnemy(type);
|
||||
e.ResetEnemy();
|
||||
return e;
|
||||
}
|
||||
|
||||
private void ReturnToPool(Enemy e)
|
||||
{
|
||||
e.GetParent()?.RemoveChild(e);
|
||||
e.ProcessMode = ProcessModeEnum.Disabled;
|
||||
_pool[e.Type].Enqueue(e);
|
||||
}
|
||||
|
||||
public EnemyType GetRandomType()
|
||||
{
|
||||
var keys = new List<EnemyType>(_pool.Keys);
|
||||
return keys[GD.RandRange(0, keys.Count - 1)];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://bhxyvnirfeipr
|
||||
@@ -0,0 +1,39 @@
|
||||
using Godot;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public partial class EnemySpawner : Node
|
||||
{
|
||||
[Export] private Node _pathParent;
|
||||
[Export] private EnemyPool _pool;
|
||||
|
||||
private readonly List<Path2D> _paths = new();
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
foreach (var c in _pathParent.GetChildren())
|
||||
if (c is Path2D p)
|
||||
_paths.Add(p);
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
SpawnWithDelay(i * 0.33f);
|
||||
}
|
||||
|
||||
private async void SpawnWithDelay(float t)
|
||||
{
|
||||
await ToSignal(GetTree().CreateTimer(t), SceneTreeTimer.SignalName.Timeout);
|
||||
Spawn();
|
||||
}
|
||||
|
||||
private void Spawn()
|
||||
{
|
||||
if (_paths.Count == 0) return;
|
||||
|
||||
var path = _paths[GD.RandRange(0, _paths.Count - 1)];
|
||||
var type = _pool.GetRandomType();
|
||||
var enemy = _pool.Get(type);
|
||||
|
||||
if (enemy == null) return;
|
||||
|
||||
path.AddChild(enemy);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://cds2rrl4yjexf
|
||||
Reference in New Issue
Block a user