Skip to content

Commit

Permalink
Merge branch 'zombies-ate-my-neighbors' of https://github.com/cspranc…
Browse files Browse the repository at this point in the history
…e/gecs into zombies-ate-my-neighbors
  • Loading branch information
csprance committed Nov 29, 2024
2 parents bb9d753 + f5d1de3 commit 4ce9bf5
Showing 1 changed file with 21 additions and 31 deletions.
52 changes: 21 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,22 @@ Before diving into the usage of the GECS addon, it's important to understand the

- **Component**: A data container that holds specific attributes or properties. Components do not contain game logic.

- **System**: A system contains the logic that operates on entities with specific components.
- **Archetypes**: A specific named Entity that has a specific set of components (at ready) and usually is using script inheritance to take advantage of any godot nodes features. OR... just a named Entity. Very handy for relationship queries.

- **System**: A system contains two parts. The query which defines which entities to operate on and the logic that operates on each entity with specific components.

- **World**: The context in which entities and systems exist and interact.

- **Query**: A way to query for specific entities in the world based on the components they contain.
- **Query**: A way to query for specific entities in the world based on the components they contain or the relationships they have.

- **Relationship**: A resource that represents a relationship between a target and a source based on a relation. See more: [Relations.md](./RELATIONSHIPS.md)

- **ECS Singleton**: Provides global access to the current `World` instance and offers utility functions for processing.


## Creating Components

Components in GECS are resources that extend the `Component` class. They are simple data containers without any logic.
Components in GECS are resources that extend the `Component` class. They are simple data containers without any logic. They may contain functions but only for getting/setting properties on the component.

Here's how to create a new component:

Expand All @@ -84,7 +89,7 @@ extends Component
@export var should_bounce := false
```

2. **Define Properties**: Add any properties that represent the data for this component. Use the `@export` keyword to make them editable in the inspector.
2. **Define Properties**: Add any properties that represent the data for this component. Use the `@export` keyword to make them editable in the inspector. This allows you to use them in the godot editor to design entities with components and modify those properties.

## Creating Entities

Expand All @@ -103,7 +108,7 @@ func on_ready() -> void:
Utils.sync_transform(self)
```

3. **Initialize Components**: In the `_ready()` function, components listed in `component_resources` are automatically added to the entity.
3. **Initialize Components**: In the `_ready()` function, components listed in `component_resources` are automatically added to the entity but you can also override a method called `define_components()` and return an array of components.

## Creating Systems

Expand All @@ -117,6 +122,7 @@ class_name BounceSystem
extends System
func query():
# All entities that all have transform, velocity and bounce components
return q.with_all([Transform, Velocity, Bounce])
func process(entity: Entity, delta: float):
Expand Down Expand Up @@ -151,8 +157,8 @@ func _ready() -> void:
ECS.world = world
func _process(delta):
# Process only systems in the "physics" group
ECS.process(delta, "physics")
# Process only systems in the "gameplay" group
ECS.process(delta, "gameplay")
```

## Example Project
Expand Down Expand Up @@ -191,9 +197,7 @@ extends Component
class_name Transform
extends Component
@export var position := Vector2.ZERO
@export var rotation := 0.0
@export var scale := Vector2.ONE
@export var transform: Transform2D
```

### Entities
Expand Down Expand Up @@ -258,7 +262,7 @@ func process(entity: Entity, delta: float):
var velocity: Velocity = entity.get_component(Velocity)
var transform: Transform = entity.get_component(Transform)
var velocity_vector: Vector2 = velocity.direction.normalized() * velocity.speed
transform.position += velocity_vector * delta
transform.transform.origin += velocity_vector * delta
```

- **Transform2DSystem**: Synchronizes the `Transform` component with the entity's actual transform.
Expand All @@ -272,10 +276,7 @@ func query():
return q.with_all([Transform])
func process(entity: Entity, delta):
var transform: Transform = entity.get_component(Transform)
entity.position = transform.position
entity.rotation = transform.rotation
entity.scale = transform.scale
Utils.sync_transform(entity)
```

## Advanced Usage
Expand All @@ -289,11 +290,15 @@ q
.with_all([]) # Find entities that have all these components
.with_any([]) # Find entities that have any of these components
.with_none([]) # Exclude entities that have these components
.has_relationship([])
.has_reverse_relationship([])
```

- **with_all**: Entities must have all of these components.
- **with_any**: Entities must have at least one of these components.
- **with_none**: Entities must not have any of these components.
- **has_relationship**: Entities must have these relationships
- **has_reverse_relationship**: This finds the entities of reverse relationships (aka the target of the relationship, not the source)

**Example**:

Expand All @@ -318,17 +323,6 @@ This will only process systems that are in the "physics" group in the physics pr

To process all active systems regardless of their group, omit the `group` parameter:

### Configuring Entity Base Type

GECS provides flexibility in choosing the base class for your entities, allowing you to work seamlessly with both 2D and 3D nodes.

To set the base type for entities:

1. Open `Project > Project Settings > GECS`.
2. Set `Entity Base Type` to either `Node2D` or `Node3D`.

This setting updates the base class of the `Entity` to match your selection, ensuring compatibility with your project's node structure.

### Processing Systems by Group

Systems can be assigned to specific groups, enabling you to control when and how they are processed. This is particularly useful for separating logic that should run at different times or frequencies.
Expand All @@ -347,8 +341,4 @@ Feel free to explore and expand upon the example project provided, and refer to
- Build a better GUI for seeing Systems and their groups
- GUI For Seeing all Systems executing
- Gui for seeing all Entities and Components and Values in those components
- GUI TO see all Components by type
- Relationships
- q.with_all([C_Health, C_Damage, Rel([C_DamagedBy, Player])]).with_none([Rel(C_ImmuneTo, RedTeam)])
- Entities that all have health, damage, and were damaged by the player entity, without a relationship of immunity to red team entity
- enemy_entity.add_relationship(Rel([C_DamagedBy, player_entity]))
- GUI TO see all Components by type

0 comments on commit 4ce9bf5

Please sign in to comment.