spawning
This commit is contained in:
@@ -3,28 +3,34 @@ using Godot;
|
||||
using Movementtests.interfaces;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class Enemy : CharacterBody3D, IDamageable, IDamageMaker, IHealthable, IKillable
|
||||
public partial class Enemy : CharacterBody3D, IDamageable, IDamageMaker, IHealthable, IKillable, IMoveable
|
||||
{
|
||||
public event Action<IDamageable, float> DamageTaken;
|
||||
|
||||
public event Action<IHealthable, float> HealthChanged;
|
||||
public event Action<IHealthable> HealthDepleted;
|
||||
|
||||
[Export]
|
||||
public Node3D Target { get; set; }
|
||||
|
||||
[Export]
|
||||
public RDamage GetDamageDealt { get; set; }
|
||||
public Node3D Target { get; set; }
|
||||
|
||||
[Export]
|
||||
public Node CHealth { get; set; }
|
||||
[Export]
|
||||
public RHealth RHealth { get; set; }
|
||||
|
||||
public float CurrentHealth { get; set; }
|
||||
|
||||
[Export]
|
||||
public Node CDamage { get; set; }
|
||||
|
||||
[Export]
|
||||
public RDamage RDamage { get; set; }
|
||||
|
||||
[Export]
|
||||
public Node CMovement { get; set; }
|
||||
|
||||
[Export]
|
||||
public RMovement RMovement { get; set; }
|
||||
|
||||
|
||||
[Export]
|
||||
public RDeathEffect[] DeathEffects { get; set; }
|
||||
|
||||
@@ -38,13 +44,21 @@ public partial class Enemy : CharacterBody3D, IDamageable, IDamageMaker, IHealth
|
||||
if (CDamage is IDamageable damageable) damageable.DamageTaken += ReduceHealth;
|
||||
if (CHealth is IHealthable healthable) healthable.HealthDepleted += Kill;
|
||||
}
|
||||
|
||||
public void Setup()
|
||||
{
|
||||
if (CMovement is IMoveable moveable && RMovement != null) moveable.RMovement = RMovement;
|
||||
if (CHealth is IHealthable healthable && RHealth != null)
|
||||
{
|
||||
healthable.RHealth = RHealth;
|
||||
healthable.CurrentHealth = RHealth.StartingHealth;
|
||||
}
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
var targetPlanar = new Vector3(Target.GlobalPosition.X, GlobalPosition.Y, Target.GlobalPosition.Z);
|
||||
LookAt(targetPlanar);
|
||||
|
||||
if (CMovement is not IMoveable movement) return;
|
||||
|
||||
var inputs = new MovementInputs(
|
||||
Velocity: Velocity,
|
||||
@@ -53,15 +67,21 @@ public partial class Enemy : CharacterBody3D, IDamageable, IDamageMaker, IHealth
|
||||
gravity: GetGravity(),
|
||||
delta: delta
|
||||
);
|
||||
Velocity = movement!.ComputeVelocity(inputs);
|
||||
Velocity = ComputeVelocity(inputs);
|
||||
MoveAndSlide();
|
||||
}
|
||||
|
||||
public Vector3 ComputeVelocity(MovementInputs inputs)
|
||||
{
|
||||
if (CMovement is not IMoveable movement) return Vector3.Zero;
|
||||
return movement!.ComputeVelocity(inputs);
|
||||
}
|
||||
|
||||
public void OnDamageBoxTriggered(Node3D body)
|
||||
{
|
||||
GD.Print("Take this!");
|
||||
if (body is not IDamageable damageable) return;
|
||||
damageable.TakeDamage(GetDamageDealt);
|
||||
damageable.TakeDamage(RDamage);
|
||||
}
|
||||
|
||||
public float TakeDamage(RDamage damage)
|
||||
|
||||
@@ -2,18 +2,14 @@
|
||||
|
||||
[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="Resource" uid="uid://dg1xbjhyhgnnk" path="res://scenes/enemies/flying_enemy/flying_enemy_health.tres" id="2_ma2bq"]
|
||||
[ext_resource type="Resource" uid="uid://dgo65k2ceqfvy" path="res://scenes/enemies/flying_enemy/flying_enemy_damage.tres" id="2_on7rt"]
|
||||
[ext_resource type="Resource" uid="uid://bwqjaom4k7rc3" path="res://scenes/enemies/flying_enemy/flying_enemy_movement.tres" id="4_dejyg"]
|
||||
[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_aarbi"]
|
||||
script = ExtResource("5_irbqc")
|
||||
StartingHealth = 50.0
|
||||
metadata/_custom_type_script = "uid://baiapod3csndf"
|
||||
|
||||
[sub_resource type="Resource" id="Resource_jnv07"]
|
||||
script = ExtResource("2_1bsgx")
|
||||
metadata/_custom_type_script = "uid://b6y3ugfydvch0"
|
||||
@@ -50,15 +46,17 @@ collision_layer = 16
|
||||
collision_mask = 273
|
||||
motion_mode = 1
|
||||
script = ExtResource("1_q8l7o")
|
||||
GetDamageDealt = ExtResource("2_on7rt")
|
||||
CHealth = NodePath("CHealth")
|
||||
RHealth = ExtResource("2_ma2bq")
|
||||
CDamage = NodePath("CDamageable")
|
||||
RDamage = ExtResource("2_on7rt")
|
||||
CMovement = NodePath("CFlyingMovement")
|
||||
RMovement = ExtResource("4_dejyg")
|
||||
DeathEffects = Array[Object]([])
|
||||
|
||||
[node name="CHealth" type="Node" parent="."]
|
||||
script = ExtResource("4_ys4jv")
|
||||
HealthResource = SubResource("Resource_aarbi")
|
||||
RHealth = ExtResource("2_ma2bq")
|
||||
metadata/_custom_type_script = "uid://bjwrpv3jpsc1e"
|
||||
|
||||
[node name="CDamageable" type="Node" parent="."]
|
||||
|
||||
9
scenes/enemies/flying_enemy/flying_enemy_movement.tres
Normal file
9
scenes/enemies/flying_enemy/flying_enemy_movement.tres
Normal file
@@ -0,0 +1,9 @@
|
||||
[gd_resource type="Resource" script_class="RMovement" load_steps=2 format=3 uid="uid://bwqjaom4k7rc3"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://dtpxijlnb2c5" path="res://resource_definitions/RMovement.cs" id="1_3yq0a"]
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_3yq0a")
|
||||
Speed = 3.0
|
||||
TargetHeight = 10.0
|
||||
metadata/_custom_type_script = "uid://dtpxijlnb2c5"
|
||||
@@ -1,18 +1,15 @@
|
||||
[gd_scene load_steps=17 format=3 uid="uid://dxt0e2ugmttqq"]
|
||||
|
||||
[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="Resource" uid="uid://otfc2snh8umc" path="res://scenes/enemies/grounded_enemy/grounded_enemy_damage.tres" id="2_bn56u"]
|
||||
[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://baiapod3csndf" path="res://resource_definitions/RHealth.cs" id="3_pvbr4"]
|
||||
[ext_resource type="Resource" uid="uid://bohbojc68j7y1" path="res://scenes/enemies/grounded_enemy/grounded_enemy_health.tres" id="2_w4lm8"]
|
||||
[ext_resource type="Resource" uid="uid://bqq6uukbdfysr" path="res://scenes/enemies/grounded_enemy/grounded_enemy_movement.tres" id="4_na24f"]
|
||||
[ext_resource type="Script" uid="uid://b0u23nkpaimyc" path="res://scenes/damage/CDamageable.cs" id="7_1tw73"]
|
||||
[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")
|
||||
metadata/_custom_type_script = "uid://baiapod3csndf"
|
||||
|
||||
[sub_resource type="Resource" id="Resource_qj0ob"]
|
||||
script = ExtResource("2_r3cnf")
|
||||
metadata/_custom_type_script = "uid://b6y3ugfydvch0"
|
||||
@@ -38,19 +35,21 @@ 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", "CMovement")]
|
||||
[node name="GroundedEnemy" type="CharacterBody3D" node_paths=PackedStringArray("CHealth", "CDamage", "CMovement")]
|
||||
collision_layer = 16
|
||||
collision_mask = 273
|
||||
script = ExtResource("1_r6506")
|
||||
GetDamageDealt = ExtResource("2_6d4gl")
|
||||
CHealth = NodePath("CHealth")
|
||||
RHealth = ExtResource("2_w4lm8")
|
||||
CDamage = NodePath("CDamageable")
|
||||
RDamage = ExtResource("2_bn56u")
|
||||
CMovement = NodePath("CGroundedMovement")
|
||||
RMovement = ExtResource("4_na24f")
|
||||
DeathEffects = Array[Object]([])
|
||||
|
||||
[node name="CHealth" type="Node" parent="."]
|
||||
script = ExtResource("2_gsmti")
|
||||
HealthResource = SubResource("Resource_r3cnf")
|
||||
RHealth = ExtResource("2_w4lm8")
|
||||
metadata/_custom_type_script = "uid://bjwrpv3jpsc1e"
|
||||
|
||||
[node name="CDamageable" type="Node" parent="."]
|
||||
@@ -0,0 +1,8 @@
|
||||
[gd_resource type="Resource" script_class="RMovement" load_steps=2 format=3 uid="uid://bqq6uukbdfysr"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://dtpxijlnb2c5" path="res://resource_definitions/RMovement.cs" id="1_hsy8g"]
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_hsy8g")
|
||||
Speed = 5.0
|
||||
metadata/_custom_type_script = "uid://dtpxijlnb2c5"
|
||||
@@ -9,17 +9,18 @@ public partial class CHealth : Node, IHealthable
|
||||
public event Action<IHealthable> HealthDepleted;
|
||||
|
||||
[Export]
|
||||
public RHealth HealthResource;
|
||||
public RHealth RHealth { get; set; }
|
||||
|
||||
public float CurrentHealth { get; set; }
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
CurrentHealth = HealthResource.StartingHealth;
|
||||
CurrentHealth = RHealth.StartingHealth;
|
||||
}
|
||||
|
||||
public void ReduceHealth(IDamageable source, float amount)
|
||||
{
|
||||
GD.Print(CurrentHealth);
|
||||
CurrentHealth -= amount;
|
||||
HealthChanged?.Invoke(this, CurrentHealth);
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Movementtests.scenes.movement;
|
||||
|
||||
public partial class CFlyingMovement : Node3D, IMoveable
|
||||
{
|
||||
[Export] public RMovement RMovement;
|
||||
[Export] public RMovement RMovement { get; set; }
|
||||
|
||||
[Export(PropertyHint.Layers3DPhysics)]
|
||||
public uint TerrainCollision { get; set; } = 1;
|
||||
@@ -15,10 +15,9 @@ public partial class CFlyingMovement : Node3D, IMoveable
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
|
||||
_randomDirection = new Vector3(GD.RandRange(-1, 1), 1, GD.RandRange(-1, 1)).Normalized();
|
||||
}
|
||||
|
||||
|
||||
public Vector3 ComputeVelocity(MovementInputs inputs)
|
||||
{
|
||||
var velocity = inputs.Velocity;
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Movementtests.scenes.movement;
|
||||
|
||||
public partial class CGroundedMovement : Node3D, IMoveable
|
||||
{
|
||||
[Export] public RMovement RMovement;
|
||||
[Export] public RMovement RMovement { get; set; }
|
||||
|
||||
[Export]
|
||||
public RayCast3D WallInFrontRayCast { get; set; }
|
||||
|
||||
@@ -4,10 +4,15 @@ using System;
|
||||
public partial class Spawner : Node3D
|
||||
{
|
||||
[Export(PropertyHint.NodeType)]
|
||||
public PackedScene SceneToSpawn { get; set; }
|
||||
public PackedScene EnemyToSpawn { get; set; }
|
||||
|
||||
[Export]
|
||||
public Resource Inputs;
|
||||
public RMovement MovementInputs { get; set; }
|
||||
[Export]
|
||||
public RHealth HealthInputs { get; set; }
|
||||
[Export]
|
||||
public RDamage DamageInputs { get; set; }
|
||||
|
||||
[Export]
|
||||
public Node3D Target { get; set; }
|
||||
|
||||
@@ -35,13 +40,16 @@ public partial class Spawner : Node3D
|
||||
|
||||
public void Spawn()
|
||||
{
|
||||
if (SceneToSpawn == null || !SceneToSpawn.CanInstantiate()) return;
|
||||
|
||||
var spawnedInstance = SceneToSpawn.Instantiate() as Enemy;
|
||||
if (spawnedInstance == null) return;
|
||||
|
||||
// spawnedInstance.Inputs = Inputs as EnemyInitInputs;
|
||||
if (EnemyToSpawn == null || !EnemyToSpawn.CanInstantiate()) return;
|
||||
|
||||
if (EnemyToSpawn.Instantiate() is not Enemy spawnedInstance) return;
|
||||
|
||||
spawnedInstance.Target = Target;
|
||||
spawnedInstance.RMovement = MovementInputs;
|
||||
spawnedInstance.RDamage = DamageInputs;
|
||||
spawnedInstance.RHealth = HealthInputs;
|
||||
spawnedInstance.Setup();
|
||||
|
||||
GetTree().GetCurrentScene().AddChild(spawnedInstance);
|
||||
spawnedInstance.GlobalPosition = GlobalPosition;
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
[gd_scene load_steps=3 format=3 uid="uid://c305mfrtumcyq"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://djyspwixi01j5" path="res://scenes/spawners/Spawner.cs" id="1_2otbo"]
|
||||
[ext_resource type="PackedScene" uid="uid://dxt0e2ugmttqq" path="res://scenes/enemies/first_enemy/first_enemy.tscn" id="2_2otbo"]
|
||||
[ext_resource type="PackedScene" uid="uid://dxt0e2ugmttqq" path="res://scenes/enemies/grounded_enemy/grounded_enemy.tscn" id="2_2otbo"]
|
||||
|
||||
[node name="Spawner" type="Node3D"]
|
||||
script = ExtResource("1_2otbo")
|
||||
|
||||
Reference in New Issue
Block a user