two enemy types, ready to refactor

This commit is contained in:
2026-01-17 10:10:14 +01:00
parent 63529a11ae
commit 5908494977
19 changed files with 256 additions and 32 deletions

View File

@@ -1,10 +1,10 @@
using Godot;
using System;
using Movementtests.player_controller.Scripts;
using Movementtests.interfaces;
[GlobalClass]
public partial class FirstEnemy : CharacterBody3D, ISpawnable
public partial class FirstEnemy : CharacterBody3D, IDamageable, IKillable, IKnockbackable
{
[Export]
public Node3D Target { get; set; }
@@ -13,20 +13,18 @@ public partial class FirstEnemy : CharacterBody3D, ISpawnable
public EnemyInitInputs Inputs;
private RayCast3D _wallInFrontRayCast;
private Area3D _damageBox;
public override void _Ready()
{
_wallInFrontRayCast = GetNode<RayCast3D>("WallInFrontRayCast");
_damageBox = GetNode<Area3D>("DamageBox");
_damageBox.BodyEntered += OnDamageBoxTriggered;
}
public Resource GetSpawnInitResource()
public void OnDamageBoxTriggered(Node3D body)
{
return Inputs;
}
public void TestMethod()
{
GD.Print("I'm an enemy");
if(body is IDamageable damageable) damageable.TakeDamage();
}
public override void _PhysicsProcess(double delta)
@@ -49,4 +47,9 @@ public partial class FirstEnemy : CharacterBody3D, ISpawnable
Velocity = velocity;
MoveAndSlide();
}
public void TakeDamage()
{
GD.Print("Emotional daaamaaage!");
}
}

View File

@@ -0,0 +1,85 @@
using Godot;
using System;
using Movementtests.interfaces;
[GlobalClass]
public partial class FlyingEnemy : CharacterBody3D, IDamageable, IKillable, IKnockbackable
{
[Export]
public Node3D Target { get; set; }
[Export]
public FlyingEnemyInputs Inputs;
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, -Inputs.TargetHeight, 0);
_damageBox = GetNode<Area3D>("DamageBox");
_damageBox.BodyEntered += OnDamageBoxTriggered;
_randomDirection = new Vector3(GD.RandRange(-1, 1), 1, GD.RandRange(-1, 1)).Normalized();
}
public void OnDamageBoxTriggered(Node3D body)
{
if(body is IDamageable damageable) damageable.TakeDamage();
}
public override void _PhysicsProcess(double delta)
{
var target = Target.GlobalPosition;
var direction = (target - GlobalPosition).Normalized();
Vector3 velocity = Velocity;
var targetPlane = new Vector3(target.X, GlobalPosition.Y, target.Z);
LookAt(targetPlane);
// Check if we have a direct line of sight to the player
if (!_movingToDesiredHeight)
{
velocity = direction * Inputs.Speed;
var spaceState = GetWorld3D().DirectSpaceState;
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;
if (!_groundDistanceRaycast.IsColliding())
{
velocity.Y = 0;
var spaceState = GetWorld3D().DirectSpaceState;
var query = PhysicsRayQueryParameters3D.Create(GlobalPosition, target, _groundDistanceRaycast.CollisionMask);
var result = spaceState.IntersectRay(query);
if (result.Count == 0)
{
_movingToDesiredHeight = false;
}
}
}
Velocity = velocity;
MoveAndSlide();
}
public void TakeDamage()
{
GD.Print("Oh no I'm falling");
}
}

View File

@@ -0,0 +1 @@
uid://cmvep0qi7qlvf

View File

@@ -0,0 +1,23 @@
using Godot;
using System;
[GlobalClass]
public partial class FlyingEnemyInputs : Resource
{
[Export(PropertyHint.Range, "0,10,0.1,or_greater")]
public float Speed = 5.0f;
[Export(PropertyHint.Range, "0,100,1,or_greater")]
public float TargetHeight = 50.0f;
public FlyingEnemyInputs()
{
Speed = 5.0f;
TargetHeight = 50.0f;
}
public FlyingEnemyInputs(float speed, float targetHeight)
{
Speed = speed;
TargetHeight = targetHeight;
}
}

View File

@@ -0,0 +1 @@
uid://do0dic4r3ri0s

View File

@@ -1,4 +1,4 @@
[gd_scene load_steps=6 format=3 uid="uid://dxt0e2ugmttqq"]
[gd_scene load_steps=7 format=3 uid="uid://dxt0e2ugmttqq"]
[ext_resource type="Script" uid="uid://bn7sc6id7n166" path="res://scenes/enemies/FirstEnemy.cs" id="1_4yfjf"]
@@ -15,7 +15,10 @@ rings = 4
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_4yfjf"]
albedo_color = Color(0.06469653, 0.06469653, 0.06469653, 1)
[node name="CharacterBody3D" type="CharacterBody3D"]
[sub_resource type="BoxShape3D" id="BoxShape3D_4yfjf"]
size = Vector3(1, 2, 2)
[node name="FirstEnemy" type="CharacterBody3D"]
collision_layer = 16
collision_mask = 273
script = ExtResource("1_4yfjf")
@@ -49,3 +52,11 @@ surface_material_override/0 = SubResource("StandardMaterial3D_4yfjf")
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.1, 0)
target_position = Vector3(0, 0, -1.5)
collision_mask = 272
[node name="DamageBox" type="Area3D" parent="."]
collision_layer = 0
monitorable = false
[node name="CollisionShape3D" type="CollisionShape3D" parent="DamageBox"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, -0.5)
shape = SubResource("BoxShape3D_4yfjf")

View File

@@ -0,0 +1,60 @@
[gd_scene load_steps=7 format=3 uid="uid://cmlud1hwkd6sv"]
[ext_resource type="Script" uid="uid://cmvep0qi7qlvf" path="res://scenes/enemies/FlyingEnemy.cs" id="1_b46rq"]
[sub_resource type="SphereShape3D" id="SphereShape3D_b46rq"]
[sub_resource type="SphereMesh" id="SphereMesh_1bsgx"]
[sub_resource type="SphereMesh" id="SphereMesh_4yfjf"]
radius = 0.05
height = 0.1
radial_segments = 4
rings = 4
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_4yfjf"]
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"]
collision_layer = 16
collision_mask = 273
motion_mode = 1
script = ExtResource("1_b46rq")
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
shape = SubResource("SphereShape3D_b46rq")
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
layers = 33
mesh = SubResource("SphereMesh_1bsgx")
[node name="MeshInstance3D2" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.2, 0, -0.5)
layers = 33
cast_shadow = 0
ignore_occlusion_culling = true
mesh = SubResource("SphereMesh_4yfjf")
surface_material_override/0 = SubResource("StandardMaterial3D_4yfjf")
[node name="MeshInstance3D3" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.2, 0, -0.5)
layers = 33
cast_shadow = 0
ignore_occlusion_culling = true
mesh = SubResource("SphereMesh_4yfjf")
surface_material_override/0 = SubResource("StandardMaterial3D_4yfjf")
[node name="GroundDistance" type="RayCast3D" parent="."]
target_position = Vector3(0, 0, 0)
collision_mask = 272
[node name="DamageBox" type="Area3D" parent="."]
collision_layer = 0
monitorable = false
[node name="CollisionShape3D" type="CollisionShape3D" parent="DamageBox"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.75)
shape = SubResource("BoxShape3D_4yfjf")