Skip to content

Commit c822905

Browse files
authored
feat: check if entity exists (#25)
Always check if an entity exists before add/remove a component. Small improvements by calling hasComponents instead of HasComponents inside internal code.
1 parent efc2101 commit c822905

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

component.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@ func ConfigureComponent[T ComponentInterface](world *World, conf any) T {
4646
// - the entity has the component
4747
// - an internal error occurs
4848
func AddComponent[T ComponentInterface](world *World, entityId EntityId, component T) error {
49-
componentId := component.GetComponentId()
50-
if world.HasComponents(entityId, componentId) {
51-
return fmt.Errorf("the entity %d already owns the component %d", entityId, componentId)
52-
}
53-
5449
entityRecord, ok := world.entities[entityId]
5550
if !ok {
5651
return fmt.Errorf("entity %v does not exist", entityId)
5752
}
5853

54+
componentId := component.GetComponentId()
55+
if world.hasComponents(entityRecord, componentId) {
56+
return fmt.Errorf("the entity %d already owns the component %d", entityId, componentId)
57+
}
58+
5959
archetype := world.getNextArchetype(entityRecord, world.getComponentsIds(component)...)
6060
err := addComponentsToArchetype1(world, entityRecord, archetype, component)
6161
if err != nil {
@@ -354,7 +354,12 @@ func addComponents8[A, B, C, D, E, F, G, H ComponentInterface](world *World, ent
354354
// - the componentId is not registered in the World
355355
// - an internal error occurs
356356
func (world *World) AddComponent(entityId EntityId, componentId ComponentId, conf any) error {
357-
if world.HasComponents(entityId, componentId) {
357+
entityRecord, ok := world.entities[entityId]
358+
if !ok {
359+
return fmt.Errorf("entity %v does not exist", entityId)
360+
}
361+
362+
if world.hasComponents(entityRecord, componentId) {
358363
return fmt.Errorf("the entity %d already owns the component %d", entityId, componentId)
359364
}
360365

@@ -380,12 +385,17 @@ func (world *World) AddComponent(entityId EntityId, componentId ComponentId, con
380385
// - the componentsIds are not registered in the World
381386
// - an internal error occurs
382387
func (world *World) AddComponents(entityId EntityId, componentsIdsConfs ...ComponentIdConf) error {
388+
entityRecord, ok := world.entities[entityId]
389+
if !ok {
390+
return fmt.Errorf("entity %v does not exist", entityId)
391+
}
392+
383393
var componentsIds []ComponentId
384394
for _, componentIdConf := range componentsIdsConfs {
385395
componentsIds = append(componentsIds, componentIdConf.ComponentId)
386396
}
387397

388-
if world.HasComponents(entityId, componentsIds...) {
398+
if world.hasComponents(entityRecord, componentsIds...) {
389399
return fmt.Errorf("the entity %d already owns the components %v", entityId, componentsIds)
390400
}
391401

@@ -412,7 +422,11 @@ func (world *World) AddComponents(entityId EntityId, componentsIdsConfs ...Compo
412422
func RemoveComponent[T ComponentInterface](world *World, entityId EntityId) error {
413423
var t T
414424
componentId := t.GetComponentId()
415-
entityRecord := world.entities[entityId]
425+
426+
entityRecord, ok := world.entities[entityId]
427+
if !ok {
428+
return fmt.Errorf("entity %v does not exist", entityId)
429+
}
416430

417431
if !world.hasComponents(entityRecord, componentId) {
418432
return fmt.Errorf("the entity %d doesn't own the component %d", entityId, componentId)

0 commit comments

Comments
 (0)