basic ECS spawner
This commit is contained in:
9
GECS/components/c_health.gd
Normal file
9
GECS/components/c_health.gd
Normal file
@@ -0,0 +1,9 @@
|
||||
class_name C_Health
|
||||
extends Component
|
||||
|
||||
@export var current: float = 100.0
|
||||
@export var maximum: float = 100.0
|
||||
|
||||
func _init(max_health: float = 100.0):
|
||||
maximum = max_health
|
||||
current = max_health
|
||||
1
GECS/components/c_health.gd.uid
Normal file
1
GECS/components/c_health.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bg653v2p104l1
|
||||
15
GECS/components/c_spawn_point.gd
Normal file
15
GECS/components/c_spawn_point.gd
Normal file
@@ -0,0 +1,15 @@
|
||||
class_name C_SpawnPoint
|
||||
extends Component
|
||||
|
||||
@export var spawn_prefab: PackedScene
|
||||
@export var spawn_frequency: float = 1
|
||||
var spawn_cooldown: float = 0
|
||||
|
||||
func _init():
|
||||
pass
|
||||
|
||||
func should_spawn() -> bool:
|
||||
return spawn_cooldown < 0
|
||||
|
||||
func start_spawn_cooldown() -> void:
|
||||
spawn_cooldown = spawn_frequency
|
||||
1
GECS/components/c_spawn_point.gd.uid
Normal file
1
GECS/components/c_spawn_point.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cmflg422miab6
|
||||
7
GECS/components/c_transform.gd
Normal file
7
GECS/components/c_transform.gd
Normal file
@@ -0,0 +1,7 @@
|
||||
class_name C_Transform
|
||||
extends Component
|
||||
|
||||
@export var position: Vector3 = Vector3.ZERO
|
||||
|
||||
func _init(pos: Vector3 = Vector3.ZERO):
|
||||
position = pos
|
||||
1
GECS/components/c_transform.gd.uid
Normal file
1
GECS/components/c_transform.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://c4ihfoefv2vwd
|
||||
7
GECS/components/c_velocity.gd
Normal file
7
GECS/components/c_velocity.gd
Normal file
@@ -0,0 +1,7 @@
|
||||
class_name C_Velocity
|
||||
extends Component
|
||||
|
||||
@export var velocity: Vector3 = Vector3.ZERO
|
||||
|
||||
func _init(vel: Vector3 = Vector3.ZERO):
|
||||
velocity = vel
|
||||
1
GECS/components/c_velocity.gd.uid
Normal file
1
GECS/components/c_velocity.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cnqotfu7xgxwa
|
||||
36
GECS/entities/BasicEnemy/BasicEnemy.tscn
Normal file
36
GECS/entities/BasicEnemy/BasicEnemy.tscn
Normal file
@@ -0,0 +1,36 @@
|
||||
[gd_scene load_steps=11 format=3 uid="uid://bct6stmj0qyp2"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://ci3kmg1eck6gb" path="res://GECS/entities/BasicEnemy/e_basic_enemy.gd" id="1_h3v2l"]
|
||||
[ext_resource type="Script" uid="uid://b6k13gc2m4e5s" path="res://addons/gecs/ecs/component.gd" id="2_gsgoc"]
|
||||
[ext_resource type="Script" uid="uid://bg653v2p104l1" path="res://GECS/components/c_health.gd" id="3_unr0g"]
|
||||
[ext_resource type="Script" uid="uid://c4ihfoefv2vwd" path="res://GECS/components/c_transform.gd" id="4_07lse"]
|
||||
[ext_resource type="Script" uid="uid://cnqotfu7xgxwa" path="res://GECS/components/c_velocity.gd" id="5_vhe2b"]
|
||||
|
||||
[sub_resource type="Resource" id="Resource_vas4o"]
|
||||
script = ExtResource("3_unr0g")
|
||||
metadata/_custom_type_script = "uid://bg653v2p104l1"
|
||||
|
||||
[sub_resource type="Resource" id="Resource_yuljp"]
|
||||
script = ExtResource("4_07lse")
|
||||
metadata/_custom_type_script = "uid://c4ihfoefv2vwd"
|
||||
|
||||
[sub_resource type="Resource" id="Resource_6hf1n"]
|
||||
script = ExtResource("5_vhe2b")
|
||||
velocity = Vector3(2, 0, 0)
|
||||
metadata/_custom_type_script = "uid://cnqotfu7xgxwa"
|
||||
|
||||
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_ocib1"]
|
||||
|
||||
[sub_resource type="CapsuleMesh" id="CapsuleMesh_4ju8b"]
|
||||
|
||||
[node name="BasicEnemy" type="CharacterBody3D"]
|
||||
script = ExtResource("1_h3v2l")
|
||||
component_resources = Array[ExtResource("2_gsgoc")]([SubResource("Resource_vas4o"), SubResource("Resource_yuljp"), SubResource("Resource_6hf1n")])
|
||||
|
||||
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
|
||||
shape = SubResource("CapsuleShape3D_ocib1")
|
||||
|
||||
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
|
||||
mesh = SubResource("CapsuleMesh_4ju8b")
|
||||
10
GECS/entities/BasicEnemy/e_basic_enemy.gd
Normal file
10
GECS/entities/BasicEnemy/e_basic_enemy.gd
Normal file
@@ -0,0 +1,10 @@
|
||||
@tool
|
||||
class_name BasicEnemy
|
||||
extends Entity
|
||||
|
||||
|
||||
func on_ready():
|
||||
# Sync the entity's scene position to the Transform component
|
||||
if has_component(C_Transform):
|
||||
var c_trs = get_component(C_Transform) as C_Transform
|
||||
self.global_position = c_trs.position
|
||||
1
GECS/entities/BasicEnemy/e_basic_enemy.gd.uid
Normal file
1
GECS/entities/BasicEnemy/e_basic_enemy.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://ci3kmg1eck6gb
|
||||
25
GECS/entities/Spawner/SpawnPoint.tscn
Normal file
25
GECS/entities/Spawner/SpawnPoint.tscn
Normal file
@@ -0,0 +1,25 @@
|
||||
[gd_scene load_steps=7 format=3 uid="uid://d0075ch03hfri"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://ckqlc1tqyh7gm" path="res://GECS/entities/Spawner/e_spawn_point.gd" id="1_sp3sh"]
|
||||
[ext_resource type="Script" uid="uid://b6k13gc2m4e5s" path="res://addons/gecs/ecs/component.gd" id="2_ksqpe"]
|
||||
[ext_resource type="Script" uid="uid://cmflg422miab6" path="res://GECS/components/c_spawn_point.gd" id="3_ksqpe"]
|
||||
[ext_resource type="PackedScene" uid="uid://bct6stmj0qyp2" path="res://GECS/entities/BasicEnemy/BasicEnemy.tscn" id="4_2ixog"]
|
||||
|
||||
[sub_resource type="Resource" id="Resource_dgltp"]
|
||||
script = ExtResource("3_ksqpe")
|
||||
spawn_prefab = ExtResource("4_2ixog")
|
||||
metadata/_custom_type_script = "uid://cmflg422miab6"
|
||||
|
||||
[sub_resource type="SphereShape3D" id="SphereShape3D_sp3sh"]
|
||||
|
||||
[node name="SpawnPoint" type="CharacterBody3D"]
|
||||
collision_layer = 0
|
||||
collision_mask = 0
|
||||
motion_mode = 1
|
||||
script = ExtResource("1_sp3sh")
|
||||
component_resources = Array[ExtResource("2_ksqpe")]([SubResource("Resource_dgltp")])
|
||||
|
||||
[node name="Marker3D" type="Marker3D" parent="."]
|
||||
|
||||
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
|
||||
shape = SubResource("SphereShape3D_sp3sh")
|
||||
14
GECS/entities/Spawner/e_spawn_point.gd
Normal file
14
GECS/entities/Spawner/e_spawn_point.gd
Normal file
@@ -0,0 +1,14 @@
|
||||
@tool
|
||||
class_name E_SpawnPoint
|
||||
extends Entity
|
||||
|
||||
@export var spawn_prefab: PackedScene
|
||||
@export var spawn_frequency: float = -1
|
||||
|
||||
func on_ready():
|
||||
if has_component(C_SpawnPoint):
|
||||
var c_spawn_point = get_component(C_SpawnPoint) as C_SpawnPoint
|
||||
if spawn_prefab != null && spawn_prefab.can_instantiate():
|
||||
c_spawn_point.spawn_prefab = spawn_prefab
|
||||
if spawn_frequency > 0:
|
||||
c_spawn_point.spawn_frequency = spawn_frequency
|
||||
1
GECS/entities/Spawner/e_spawn_point.gd.uid
Normal file
1
GECS/entities/Spawner/e_spawn_point.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://ckqlc1tqyh7gm
|
||||
25
GECS/systems/default_systems.tscn
Normal file
25
GECS/systems/default_systems.tscn
Normal file
@@ -0,0 +1,25 @@
|
||||
[gd_scene load_steps=4 format=3 uid="uid://b2v1bngfh5te"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://b3vi2ingux88g" path="res://addons/gecs/lib/system_group.gd" id="1_sk1vy"]
|
||||
[ext_resource type="Script" uid="uid://dpglt5gt5ijrn" path="res://GECS/systems/s_movement.gd" id="2_aqda8"]
|
||||
[ext_resource type="Script" uid="uid://cb2kev6dsctfu" path="res://GECS/systems/s_spawner.gd" id="2_hdbau"]
|
||||
|
||||
[node name="Systems" type="Node"]
|
||||
|
||||
[node name="gameplay" type="Node" parent="."]
|
||||
script = ExtResource("1_sk1vy")
|
||||
metadata/_custom_type_script = "uid://b3vi2ingux88g"
|
||||
|
||||
[node name="SpawnSystem" type="Node" parent="gameplay"]
|
||||
script = ExtResource("2_hdbau")
|
||||
group = &"gameplay"
|
||||
metadata/_custom_type_script = "uid://cb2kev6dsctfu"
|
||||
|
||||
[node name="physics" type="Node" parent="."]
|
||||
script = ExtResource("1_sk1vy")
|
||||
metadata/_custom_type_script = "uid://b3vi2ingux88g"
|
||||
|
||||
[node name="MovementSystem" type="Node" parent="physics"]
|
||||
script = ExtResource("2_aqda8")
|
||||
group = &"physics"
|
||||
metadata/_custom_type_script = "uid://dpglt5gt5ijrn"
|
||||
25
GECS/systems/s_movement.gd
Normal file
25
GECS/systems/s_movement.gd
Normal file
@@ -0,0 +1,25 @@
|
||||
class_name MovementSystem
|
||||
extends System
|
||||
|
||||
func query():
|
||||
# Find all entities that have both transform and velocity
|
||||
return q.with_all([C_Velocity])
|
||||
|
||||
func process(entities: Array[Entity], _components: Array, delta: float):
|
||||
for entity in entities:
|
||||
var c_velocity = entity.get_component(C_Velocity) as C_Velocity
|
||||
|
||||
if entity.velocity.length() == 0:
|
||||
entity.velocity = c_velocity.velocity
|
||||
|
||||
# Add the gravity.
|
||||
if not entity.is_on_floor():
|
||||
entity.velocity += entity.get_gravity() * delta
|
||||
|
||||
# Bounce off screen edges (simple example)
|
||||
if entity.global_position.x > 10 && entity.velocity.x > 0:
|
||||
entity.velocity = -c_velocity.velocity
|
||||
if entity.global_position.x < -10 && entity.velocity.x < 0:
|
||||
entity.velocity = c_velocity.velocity
|
||||
|
||||
entity.move_and_slide()
|
||||
1
GECS/systems/s_movement.gd.uid
Normal file
1
GECS/systems/s_movement.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://dpglt5gt5ijrn
|
||||
21
GECS/systems/s_spawner.gd
Normal file
21
GECS/systems/s_spawner.gd
Normal file
@@ -0,0 +1,21 @@
|
||||
class_name SpawnSystem
|
||||
extends System
|
||||
|
||||
func query():
|
||||
# Find all entities that have both transform and velocity
|
||||
return q.with_all([C_SpawnPoint]).enabled()
|
||||
|
||||
func process(entities: Array[Entity], _components: Array, delta: float):
|
||||
for entity in entities:
|
||||
var spawn_point = entity.get_component(C_SpawnPoint) as C_SpawnPoint
|
||||
|
||||
if not spawn_point.should_spawn():
|
||||
spawn_point.spawn_cooldown -= delta
|
||||
continue
|
||||
|
||||
var spawned = spawn_point.spawn_prefab.instantiate() as Entity
|
||||
get_tree().current_scene.add_child(spawned)
|
||||
ECS.world.add_entity(spawned)
|
||||
|
||||
spawned.global_position = entity.global_position
|
||||
spawn_point.start_spawn_cooldown()
|
||||
1
GECS/systems/s_spawner.gd.uid
Normal file
1
GECS/systems/s_spawner.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cb2kev6dsctfu
|
||||
Reference in New Issue
Block a user