removing broken ABC and refactoring enemy movement
This commit is contained in:
@@ -1,79 +1,68 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Movementtests.interfaces;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class FirstEnemy : CharacterBody3D, IDamageable, IDamageMaker, IHealthable, IKillable
|
||||
public partial class Enemy : CharacterBody3D, IDamageable, IDamageMaker, IHealthable, IKillable
|
||||
{
|
||||
[Export]
|
||||
public Node3D Target { get; set; }
|
||||
|
||||
[Export]
|
||||
public EnemyInitInputs Inputs;
|
||||
|
||||
[Export]
|
||||
public RDamage GetDamageDealt { get; set; }
|
||||
|
||||
|
||||
[Export]
|
||||
public CHealth CHealth { get; set; }
|
||||
|
||||
[Export]
|
||||
public CDamageable CDamage { get; set; }
|
||||
|
||||
[Export]
|
||||
public Node CMovement { get; set; }
|
||||
|
||||
[Export]
|
||||
public RDeathEffect[] DeathEffects { get; set; }
|
||||
|
||||
private RayCast3D _wallInFrontRayCast;
|
||||
private Area3D _damageBox;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_wallInFrontRayCast = GetNode<RayCast3D>("WallInFrontRayCast");
|
||||
_damageBox = GetNode<Area3D>("DamageBox");
|
||||
|
||||
_damageBox.BodyEntered += OnDamageBoxTriggered;
|
||||
|
||||
CDamage.DamageTaken += ReduceHealth;
|
||||
CHealth.Dead += Kill;
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
var target = Target.GlobalPosition;
|
||||
var direction = (target - GlobalPosition).Normalized();
|
||||
var targetPlanar = new Vector3(Target.GlobalPosition.X, GlobalPosition.Y, Target.GlobalPosition.Z);
|
||||
LookAt(targetPlanar);
|
||||
|
||||
var targetPlane = new Vector3(target.X, GlobalPosition.Y, target.Z);
|
||||
LookAt(targetPlane);
|
||||
if (CMovement is not IMoveable movement) return;
|
||||
|
||||
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;
|
||||
var inputs = new MovementInputs(
|
||||
Velocity: Velocity,
|
||||
TargetLocation: Target.GlobalPosition,
|
||||
isOnFloor: IsOnFloor(),
|
||||
gravity: GetGravity(),
|
||||
delta: delta
|
||||
);
|
||||
Velocity = movement!.ComputeVelocity(inputs);
|
||||
MoveAndSlide();
|
||||
}
|
||||
|
||||
public void OnDamageBoxTriggered(Node3D body)
|
||||
{
|
||||
GD.Print("Take this!");
|
||||
if(body is IDamageable damageable) damageable.TakeDamage(GetDamageDealt);
|
||||
}
|
||||
|
||||
public float TakeDamage(RDamage damage)
|
||||
{
|
||||
GD.Print("Ouch!");
|
||||
return CDamage.TakeDamage(damage);
|
||||
}
|
||||
|
||||
public float CurrentHealth
|
||||
{
|
||||
get => CHealth.CurrentHealth;
|
||||
set => CHealth.CurrentHealth = value;
|
||||
}
|
||||
|
||||
public void ReduceHealth(float amount)
|
||||
{
|
||||
CHealth.ReduceHealth(amount);
|
||||
@@ -1,18 +0,0 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class EnemyInitInputs : Resource
|
||||
{
|
||||
[Export(PropertyHint.Range, "0,10,0.1,or_greater")]
|
||||
public float Speed;
|
||||
|
||||
public EnemyInitInputs()
|
||||
{
|
||||
Speed = 5.0f;
|
||||
}
|
||||
public EnemyInitInputs(float speed)
|
||||
{
|
||||
Speed = speed;
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
uid://b2vdwkiqauhk3
|
||||
@@ -1,26 +1,13 @@
|
||||
[gd_scene load_steps=19 format=3 uid="uid://dxt0e2ugmttqq"]
|
||||
[gd_scene load_steps=17 format=3 uid="uid://dxt0e2ugmttqq"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://bn7sc6id7n166" path="res://scenes/enemies/first_enemy/FirstEnemy.cs" id="1_4yfjf"]
|
||||
[ext_resource type="Script" uid="uid://b2vdwkiqauhk3" path="res://scenes/enemies/first_enemy/EnemyInitInputs.cs" id="2_42xu3"]
|
||||
[ext_resource type="Script" uid="uid://bn7sc6id7n166" path="res://scenes/enemies/Enemy.cs" id="1_r6506"]
|
||||
[ext_resource type="Resource" uid="uid://otfc2snh8umc" path="res://scenes/enemies/first_enemy/first_enemy_damage.tres" id="2_6d4gl"]
|
||||
[ext_resource type="Script" uid="uid://bjwrpv3jpsc1e" path="res://scenes/health/CHealth.cs" id="2_gsmti"]
|
||||
[ext_resource type="Script" uid="uid://b6y3ugfydvch0" path="res://resource_definitions/RDamageModifier.cs" id="2_r3cnf"]
|
||||
[ext_resource type="Script" uid="uid://jitubgv6judn" path="res://resource_definitions/RDamage.cs" id="3_1tw73"]
|
||||
[ext_resource type="Script" uid="uid://b4cwruitopcee" path="res://resource_definitions/RDeathEffect.cs" id="3_onjoj"]
|
||||
[ext_resource type="Script" uid="uid://baiapod3csndf" path="res://resource_definitions/RHealth.cs" id="3_pvbr4"]
|
||||
[ext_resource type="Script" uid="uid://b0u23nkpaimyc" path="res://scenes/damage/CDamageable.cs" id="7_1tw73"]
|
||||
|
||||
[sub_resource type="Resource" id="Resource_66m7n"]
|
||||
script = ExtResource("2_42xu3")
|
||||
Speed = 2.0
|
||||
metadata/_custom_type_script = "uid://b2vdwkiqauhk3"
|
||||
|
||||
[sub_resource type="Resource" id="Resource_a6xb8"]
|
||||
script = ExtResource("3_1tw73")
|
||||
metadata/_custom_type_script = "uid://jitubgv6judn"
|
||||
|
||||
[sub_resource type="Resource" id="Resource_7gyf3"]
|
||||
script = ExtResource("3_onjoj")
|
||||
metadata/_custom_type_script = "uid://b4cwruitopcee"
|
||||
[ext_resource type="PackedScene" uid="uid://dbr7ioio158ew" path="res://scenes/movement/CGroundedMovement.tscn" id="7_qyswd"]
|
||||
[ext_resource type="Script" uid="uid://dtpxijlnb2c5" path="res://resource_definitions/RMovement.cs" id="8_6d4gl"]
|
||||
|
||||
[sub_resource type="Resource" id="Resource_r3cnf"]
|
||||
script = ExtResource("3_pvbr4")
|
||||
@@ -30,6 +17,11 @@ metadata/_custom_type_script = "uid://baiapod3csndf"
|
||||
script = ExtResource("2_r3cnf")
|
||||
metadata/_custom_type_script = "uid://b6y3ugfydvch0"
|
||||
|
||||
[sub_resource type="Resource" id="Resource_s01ng"]
|
||||
script = ExtResource("8_6d4gl")
|
||||
Speed = 3.0
|
||||
metadata/_custom_type_script = "uid://dtpxijlnb2c5"
|
||||
|
||||
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_62kkh"]
|
||||
|
||||
[sub_resource type="CapsuleMesh" id="CapsuleMesh_3uydm"]
|
||||
@@ -46,15 +38,15 @@ albedo_color = Color(0.06469653, 0.06469653, 0.06469653, 1)
|
||||
[sub_resource type="BoxShape3D" id="BoxShape3D_4yfjf"]
|
||||
size = Vector3(1, 2, 2)
|
||||
|
||||
[node name="FirstEnemy" type="CharacterBody3D" node_paths=PackedStringArray("CHealth", "CDamage")]
|
||||
[node name="FirstEnemy" type="CharacterBody3D" node_paths=PackedStringArray("CHealth", "CDamage", "CMovement")]
|
||||
collision_layer = 16
|
||||
collision_mask = 273
|
||||
script = ExtResource("1_4yfjf")
|
||||
Inputs = SubResource("Resource_66m7n")
|
||||
GetDamageDealt = SubResource("Resource_a6xb8")
|
||||
script = ExtResource("1_r6506")
|
||||
GetDamageDealt = ExtResource("2_6d4gl")
|
||||
CHealth = NodePath("CHealth")
|
||||
CDamage = NodePath("CDamageable")
|
||||
DeathEffects = Array[Object]([SubResource("Resource_7gyf3")])
|
||||
CMovement = NodePath("CGroundedMovement")
|
||||
DeathEffects = Array[Object]([])
|
||||
|
||||
[node name="CHealth" type="Node" parent="."]
|
||||
script = ExtResource("2_gsmti")
|
||||
@@ -66,6 +58,10 @@ script = ExtResource("7_1tw73")
|
||||
DamageModifiers = Array[Object]([SubResource("Resource_qj0ob")])
|
||||
metadata/_custom_type_script = "uid://b0u23nkpaimyc"
|
||||
|
||||
[node name="CGroundedMovement" parent="." node_paths=PackedStringArray("WallInFrontRayCast") instance=ExtResource("7_qyswd")]
|
||||
RMovement = SubResource("Resource_s01ng")
|
||||
WallInFrontRayCast = NodePath("../WallInFrontRayCast")
|
||||
|
||||
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
|
||||
shape = SubResource("CapsuleShape3D_62kkh")
|
||||
|
||||
@@ -1,116 +0,0 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using Movementtests.interfaces;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class FlyingEnemy : CharacterBody3D, IDamageable, IHealthable, IKnockbackable, IDamageMaker
|
||||
{
|
||||
[Export]
|
||||
public Node3D Target { get; set; }
|
||||
|
||||
[Export]
|
||||
public FlyingEnemyInputs Inputs { get; set; }
|
||||
|
||||
[Export]
|
||||
public RDamage GetDamageDealt { get; set; }
|
||||
|
||||
[Export]
|
||||
public CHealth CHealth { get; set; }
|
||||
[Export]
|
||||
public CDamageable CDamage { get; set; }
|
||||
|
||||
[Export]
|
||||
public RDeathEffect[] DeathEffects { get; set; }
|
||||
|
||||
private RayCast3D _groundDistanceRaycast;
|
||||
private Area3D _damageBox;
|
||||
|
||||
private bool _movingToDesiredHeight = true;
|
||||
private Vector3 _randomDirection;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_groundDistanceRaycast = GetNode<RayCast3D>("GroundDistance");
|
||||
_groundDistanceRaycast.TargetPosition = new Vector3(0, 0, 0);
|
||||
|
||||
_damageBox = GetNode<Area3D>("DamageBox");
|
||||
_damageBox.BodyEntered += OnDamageBoxTriggered;
|
||||
|
||||
_randomDirection = new Vector3(GD.RandRange(-1, 1), 1, GD.RandRange(-1, 1)).Normalized();
|
||||
|
||||
CDamage.DamageTaken += ReduceHealth;
|
||||
CHealth.Dead += Kill;
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
var spaceState = GetWorld3D().DirectSpaceState;
|
||||
var target = Target.GlobalPosition;
|
||||
var direction = (target - GlobalPosition).Normalized();
|
||||
Vector3 velocity = Velocity;
|
||||
|
||||
LookAt(target);
|
||||
|
||||
// Check if we have a direct line of sight to the player
|
||||
if (!_movingToDesiredHeight)
|
||||
{
|
||||
velocity = direction * Inputs.Speed;
|
||||
|
||||
var query = PhysicsRayQueryParameters3D.Create(GlobalPosition, target, _groundDistanceRaycast.CollisionMask);
|
||||
var result = spaceState.IntersectRay(query);
|
||||
if (result.Count > 0)
|
||||
{
|
||||
_movingToDesiredHeight = true;
|
||||
_randomDirection = new Vector3(GD.RandRange(-1, 1), 1, GD.RandRange(-1, 1)).Normalized();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
velocity = _randomDirection * Inputs.Speed;
|
||||
|
||||
var groundQuery = PhysicsRayQueryParameters3D.Create(GlobalPosition, GlobalPosition+Vector3.Down*Inputs.TargetHeight, _groundDistanceRaycast.CollisionMask);
|
||||
var groundResult = spaceState.IntersectRay(groundQuery);
|
||||
if (groundResult.Count == 0)
|
||||
{
|
||||
velocity.Y = 0;
|
||||
|
||||
var query = PhysicsRayQueryParameters3D.Create(GlobalPosition, target, _groundDistanceRaycast.CollisionMask);
|
||||
var result = spaceState.IntersectRay(query);
|
||||
if (result.Count == 0)
|
||||
{
|
||||
_movingToDesiredHeight = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Velocity = velocity;
|
||||
MoveAndSlide();
|
||||
}
|
||||
|
||||
public void OnDamageBoxTriggered(Node3D body)
|
||||
{
|
||||
if(body is IDamageable damageable) damageable.TakeDamage(GetDamageDealt);
|
||||
}
|
||||
|
||||
public event Action<float> DamageTaken;
|
||||
|
||||
|
||||
public float TakeDamage(RDamage damage)
|
||||
{
|
||||
return CDamage.TakeDamage(damage);
|
||||
}
|
||||
|
||||
public void ReduceHealth(float amount)
|
||||
{
|
||||
CHealth.ReduceHealth(amount);
|
||||
}
|
||||
|
||||
public void Kill()
|
||||
{
|
||||
foreach (var killable in DeathEffects.ToIKillables())
|
||||
{
|
||||
killable.Kill();
|
||||
}
|
||||
QueueFree();
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
uid://cmvep0qi7qlvf
|
||||
@@ -1,23 +0,0 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class FlyingEnemyInputs : Resource
|
||||
{
|
||||
[Export(PropertyHint.Range, "0,10,0.1,or_greater")]
|
||||
public float Speed;
|
||||
|
||||
[Export(PropertyHint.Range, "0,100,1,or_greater")]
|
||||
public float TargetHeight;
|
||||
|
||||
public FlyingEnemyInputs()
|
||||
{
|
||||
Speed = 5.0f;
|
||||
TargetHeight = 50.0f;
|
||||
}
|
||||
public FlyingEnemyInputs(float speed, float targetHeight)
|
||||
{
|
||||
Speed = speed;
|
||||
TargetHeight = targetHeight;
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
uid://do0dic4r3ri0s
|
||||
@@ -1,30 +1,14 @@
|
||||
[gd_scene load_steps=20 format=3 uid="uid://cmlud1hwkd6sv"]
|
||||
[gd_scene load_steps=18 format=3 uid="uid://cmlud1hwkd6sv"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://cmvep0qi7qlvf" path="res://scenes/enemies/flying_enemy/FlyingEnemy.cs" id="1_b46rq"]
|
||||
[ext_resource type="Script" uid="uid://bn7sc6id7n166" path="res://scenes/enemies/Enemy.cs" id="1_q8l7o"]
|
||||
[ext_resource type="Script" uid="uid://b6y3ugfydvch0" path="res://resource_definitions/RDamageModifier.cs" id="2_1bsgx"]
|
||||
[ext_resource type="Script" uid="uid://do0dic4r3ri0s" path="res://scenes/enemies/flying_enemy/FlyingEnemyInputs.cs" id="2_aarbi"]
|
||||
[ext_resource type="Script" uid="uid://jitubgv6judn" path="res://resource_definitions/RDamage.cs" id="2_irbqc"]
|
||||
[ext_resource type="Script" uid="uid://b4cwruitopcee" path="res://resource_definitions/RDeathEffect.cs" id="4_12h4s"]
|
||||
[ext_resource type="Resource" uid="uid://dgo65k2ceqfvy" path="res://scenes/enemies/flying_enemy/flying_enemy_damage.tres" id="2_on7rt"]
|
||||
[ext_resource type="Script" uid="uid://bjwrpv3jpsc1e" path="res://scenes/health/CHealth.cs" id="4_ys4jv"]
|
||||
[ext_resource type="Script" uid="uid://baiapod3csndf" path="res://resource_definitions/RHealth.cs" id="5_irbqc"]
|
||||
[ext_resource type="PackedScene" uid="uid://dmw5ibwrb483f" path="res://scenes/movement/CFlyingMovement.tscn" id="7_vaeds"]
|
||||
[ext_resource type="Script" uid="uid://dtpxijlnb2c5" path="res://resource_definitions/RMovement.cs" id="8_on7rt"]
|
||||
[ext_resource type="Script" uid="uid://b0u23nkpaimyc" path="res://scenes/damage/CDamageable.cs" id="8_uotso"]
|
||||
|
||||
[sub_resource type="Resource" id="Resource_7nxyi"]
|
||||
script = ExtResource("2_aarbi")
|
||||
Speed = 5.0
|
||||
TargetHeight = 10.0
|
||||
metadata/_custom_type_script = "uid://do0dic4r3ri0s"
|
||||
|
||||
[sub_resource type="Resource" id="Resource_o3msb"]
|
||||
script = ExtResource("2_irbqc")
|
||||
DamageDealt = 2.0
|
||||
DamageType = 2
|
||||
metadata/_custom_type_script = "uid://jitubgv6judn"
|
||||
|
||||
[sub_resource type="Resource" id="Resource_ys4jv"]
|
||||
script = ExtResource("4_12h4s")
|
||||
metadata/_custom_type_script = "uid://b4cwruitopcee"
|
||||
|
||||
[sub_resource type="Resource" id="Resource_aarbi"]
|
||||
script = ExtResource("5_irbqc")
|
||||
StartingHealth = 50.0
|
||||
@@ -39,6 +23,12 @@ script = ExtResource("2_1bsgx")
|
||||
DamageType = 1
|
||||
metadata/_custom_type_script = "uid://b6y3ugfydvch0"
|
||||
|
||||
[sub_resource type="Resource" id="Resource_ma2bq"]
|
||||
script = ExtResource("8_on7rt")
|
||||
Speed = 2.0
|
||||
TargetHeight = 10.0
|
||||
metadata/_custom_type_script = "uid://dtpxijlnb2c5"
|
||||
|
||||
[sub_resource type="SphereShape3D" id="SphereShape3D_b46rq"]
|
||||
|
||||
[sub_resource type="SphereMesh" id="SphereMesh_1bsgx"]
|
||||
@@ -55,16 +45,16 @@ albedo_color = Color(0.06469653, 0.06469653, 0.06469653, 1)
|
||||
[sub_resource type="BoxShape3D" id="BoxShape3D_4yfjf"]
|
||||
size = Vector3(1, 1, 1.5)
|
||||
|
||||
[node name="FlyingEnemy" type="CharacterBody3D" node_paths=PackedStringArray("CHealth", "CDamage")]
|
||||
[node name="FlyingEnemy" type="CharacterBody3D" node_paths=PackedStringArray("CHealth", "CDamage", "CMovement")]
|
||||
collision_layer = 16
|
||||
collision_mask = 273
|
||||
motion_mode = 1
|
||||
script = ExtResource("1_b46rq")
|
||||
Inputs = SubResource("Resource_7nxyi")
|
||||
GetDamageDealt = SubResource("Resource_o3msb")
|
||||
script = ExtResource("1_q8l7o")
|
||||
GetDamageDealt = ExtResource("2_on7rt")
|
||||
CHealth = NodePath("CHealth")
|
||||
CDamage = NodePath("CDamageable")
|
||||
DeathEffects = Array[Object]([SubResource("Resource_ys4jv")])
|
||||
CMovement = NodePath("CFlyingMovement")
|
||||
DeathEffects = Array[Object]([])
|
||||
|
||||
[node name="CHealth" type="Node" parent="."]
|
||||
script = ExtResource("4_ys4jv")
|
||||
@@ -76,6 +66,10 @@ script = ExtResource("8_uotso")
|
||||
DamageModifiers = Array[Object]([SubResource("Resource_jnv07"), SubResource("Resource_53j1c")])
|
||||
metadata/_custom_type_script = "uid://b0u23nkpaimyc"
|
||||
|
||||
[node name="CFlyingMovement" parent="." instance=ExtResource("7_vaeds")]
|
||||
RMovement = SubResource("Resource_ma2bq")
|
||||
TerrainCollision = 256
|
||||
|
||||
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
|
||||
shape = SubResource("SphereShape3D_b46rq")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user