62 lines
1.5 KiB
C#
62 lines
1.5 KiB
C#
using Godot;
|
|
using System;
|
|
using Movementtests.interfaces;
|
|
|
|
|
|
[GlobalClass]
|
|
public partial class FirstEnemy : CharacterBody3D, IDamageable, IKillable, IKnockbackable, IDamageMaker
|
|
{
|
|
[Export]
|
|
public Node3D Target { get; set; }
|
|
|
|
[Export]
|
|
public EnemyInitInputs Inputs;
|
|
|
|
[Export]
|
|
public RDamage GetDamageDealt { get; set; }
|
|
[Export]
|
|
public RDamageModifier[] DamageModifiers { get; set; }
|
|
|
|
private RayCast3D _wallInFrontRayCast;
|
|
private Area3D _damageBox;
|
|
|
|
public override void _Ready()
|
|
{
|
|
_wallInFrontRayCast = GetNode<RayCast3D>("WallInFrontRayCast");
|
|
_damageBox = GetNode<Area3D>("DamageBox");
|
|
_damageBox.BodyEntered += OnDamageBoxTriggered;
|
|
}
|
|
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
var target = Target.GlobalPosition;
|
|
var direction = (target - GlobalPosition).Normalized();
|
|
|
|
var targetPlane = new Vector3(target.X, GlobalPosition.Y, target.Z);
|
|
LookAt(targetPlane);
|
|
|
|
var velocity = Velocity;
|
|
velocity.X = direction.X * Inputs.Speed;
|
|
velocity.Z = direction.Z * Inputs.Speed;
|
|
|
|
if (_wallInFrontRayCast.IsColliding())
|
|
velocity.Y = Inputs.Speed;
|
|
else if (!IsOnFloor())
|
|
velocity += GetGravity() * (float)delta;
|
|
|
|
Velocity = velocity;
|
|
MoveAndSlide();
|
|
}
|
|
|
|
public void OnDamageBoxTriggered(Node3D body)
|
|
{
|
|
if(body is IDamageable damageable) damageable.TakeDamage(GetDamageDealt);
|
|
}
|
|
|
|
public void TakeDamage(RDamage damage)
|
|
{
|
|
foreach (var damageable in DamageModifiers.ToIDamageables())
|
|
damageable.TakeDamage(damage);
|
|
}
|
|
}
|