basic ECS spawner
This commit is contained in:
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