Изменена логика доспеха

This commit is contained in:
Евгений Титаренко 2023-08-19 11:51:46 +03:00
parent 429fe9b62f
commit 1fe08a596d
4 changed files with 133 additions and 43 deletions

View file

@ -1,5 +1,6 @@
using Godot;
using System;
using System.Collections.Generic;
public partial class LivingArmor : CharacterBody2D
{
@ -9,7 +10,7 @@ public partial class LivingArmor : CharacterBody2D
Moving,
Attack
}
public enum SideFace
{
Left,
@ -17,10 +18,10 @@ public partial class LivingArmor : CharacterBody2D
Right,
Down
}
[Export] public SideFace Facing = SideFace.Down;
[Export] public float MovingSpeed = 16f;
public State CurrentState
{
get => _state;
@ -46,7 +47,11 @@ public partial class LivingArmor : CharacterBody2D
private float _timeSinceState;
private AnimatedSprite2D _sprite;
private bool _isLitUp;
//private readonly List<WeakRef> _bodiesInSight = new List<WeakRef>();
private readonly List<Node2D> _bodiesInSight = new List<Node2D>();
private readonly List<Node2D> _bodiesNearBy = new List<Node2D>();
private Node2D _target = null;
public override void _Ready()
{
_sprite = (AnimatedSprite2D)FindChild("AnimatedSprite2D");
@ -66,6 +71,7 @@ public partial class LivingArmor : CharacterBody2D
animationName = "down_walk";
break;
}
_sprite.Play(animationName);
_sprite.Stop();
}
@ -87,7 +93,42 @@ public partial class LivingArmor : CharacterBody2D
break;
case State.Moving:
var direction = (Player.Instance.Position - Position).Normalized();
foreach (var body in _bodiesNearBy)
{
switch (body)
{
case Wretched wretched:
wretched.Kill(this);
break;
case Player player:
player.Kill(this);
break;
}
}
SearchTarget();
switch (_target)
{
case Player player:
if (!player.Alive)
{
_bodiesInSight.Remove(player);
_target = null;
}
break;
case Wretched wretched:
if (!wretched.IsAlive)
{
_bodiesInSight.Remove(wretched);
_target = null;
}
break;
}
if (_target is null)
{
_state = State.Waiting;
return;
}
var direction = (_target.Position - Position).Normalized();
Velocity = direction * MovingSpeed;
var animationName = "side_walk";
@ -106,9 +147,39 @@ public partial class LivingArmor : CharacterBody2D
//MoveAndCollide(direction);
break;
}
CheckIfLitUp();
}
private void SearchTarget()
{
float targetDistance = -1;
foreach (var body in _bodiesInSight)
{
var distance = (body.Position - Position).Length();
GD.Print($"{body.Name}");
switch (body)
{
case Wretched wretched:
if (targetDistance < 0 || targetDistance > distance)
{
targetDistance = distance;
_target = wretched;
}
break;
case Player player:
if (_target is Wretched)
{
continue;
}
_target = player;
break;
}
}
}
private void _OnLightEntered(Area2D area)
{
if (area.GetParentOrNull<GameCamera>() is null)
@ -120,13 +191,7 @@ public partial class LivingArmor : CharacterBody2D
private void _OnPlayerCollision(Node2D body)
{
if (body is not Player player)
return;
if (CurrentState is State.Waiting)
return;
player.Kill(this);
_bodiesNearBy.Add(body);
}
private void _OnLightExited(Area2D area)
@ -139,6 +204,7 @@ public partial class LivingArmor : CharacterBody2D
private void _OnPlayerCollisionExited(Node2D body)
{
_bodiesNearBy.Remove(body);
}
void CheckIfLitUp()
@ -148,10 +214,31 @@ public partial class LivingArmor : CharacterBody2D
CurrentState = State.Waiting;
return;
}
if (CurrentState is State.Moving or State.Attack)
return;
CurrentState = State.Moving;
}
private void _OnBodyEntered(Node2D body)
{
if (body is not Wretched and not Player)
return;
_bodiesInSight.Add(body);
}
private void _OnBodyExited(Node2D body)
{
if (body is not Wretched and not Player)
return;
if (body == _target)
{
_target = null;
}
_bodiesInSight.Remove(body);
}
}