Skip to content

Commit

Permalink
Added DeleteComponent function
Browse files Browse the repository at this point in the history
  • Loading branch information
unitoftime committed Jan 9, 2024
1 parent ce8e32f commit fab9d51
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
23 changes: 23 additions & 0 deletions world.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,29 @@ func Delete(world *World, id Id) bool {
return true
}

// Deletes specific components from an entity in the world
// Skips all work if the entity doesn't exist
// Skips deleting components that the entity doesn't have
// If no components remain after the delete, the entity will be completely removed
func DeleteComponent(world *World, id Id, comp ...Component) {
archId, ok := world.arch.Get(id)
if !ok {
return
}

ent := world.engine.ReadEntity(archId, id)
for i := range comp {
ent.Delete(comp[i])
}

world.arch.Delete(id)
world.engine.TagForDeletion(archId, id)

if len(ent.comp) > 0 {
world.Write(id, ent.comp...)
}
}

// Returns true if the entity exists in the world else it returns false
func (world *World) Exists(id Id) bool {
return world.arch.Has(id)
Expand Down
83 changes: 83 additions & 0 deletions world_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,89 @@ func TestWorldWriteDelete(t *testing.T) {
}
}

func TestWorldDeleteComponent(t *testing.T) {
world := NewWorld()
ids := make([]Id, 0)
for i := 0; i < 1e2; i++ {
id := world.NewId()
v := float64(id)
pos := position{v, v, v}
vel := velocity{v, v, v}
Write(world, id, C(pos), C(vel))
ids = append(ids, id)
}

// Verify they are all correct
for _, id := range ids {
expected := float64(id)

posOut, ok := Read[position](world, id)
check(t, ok)
compare(t, posOut, position{expected, expected, expected})

velOut, ok := Read[velocity](world, id)
check(t, ok)
compare(t, velOut, velocity{expected, expected, expected})
}

// different deletes for different modulos
for i, id := range ids {
if i % 2 == 0 {
DeleteComponent(world, id, C(position{}))
} else if i % 3 == 0 {
DeleteComponent(world, id, C(velocity{}))
} else if i % 5 == 0 {
DeleteComponent(world, id, C(position{}), C(velocity{}))
} else if i % 7 == 0 {
DeleteComponent(world, id, C(velocity{}))
DeleteComponent(world, id, C(position{}))
} else if i % 13 == 0 {
DeleteComponent(world, id, C(radius{})) // Note: This shouldn't do anything
}
}

// Verify they are all correct
for i, id := range ids {
expected := float64(id)

if i % 2 == 0 {
// Expect these to be deleted in the world
_, ok := Read[position](world, id)
check(t, !ok) // Expect to be false because we've deleted this

velOut, ok := Read[velocity](world, id)
check(t, ok)
compare(t, velOut, velocity{expected, expected, expected})
} else if i % 3 == 0 {
// Expect these to still exist in the world
posOut, ok := Read[position](world, id)
check(t, ok)
compare(t, posOut, position{expected, expected, expected})

_, ok = Read[velocity](world, id)
check(t, !ok) // Expect to be false because we've deleted this
} else if i % 5 == 0 {
_, ok := Read[position](world, id)
check(t, !ok) // Expect to be false because we've deleted this
_, ok = Read[velocity](world, id)
check(t, !ok) // Expect to be false because we've deleted this
} else if i % 7 == 0 {
_, ok := Read[position](world, id)
check(t, !ok) // Expect to be false because we've deleted this
_, ok = Read[velocity](world, id)
check(t, !ok) // Expect to be false because we've deleted this
} else {
// Expect these to still exist in the world
posOut, ok := Read[position](world, id)
check(t, ok)
compare(t, posOut, position{expected, expected, expected})
velOut, ok := Read[velocity](world, id)
check(t, ok)
compare(t, velOut, velocity{expected, expected, expected})
}
}
}

func TestResources(t *testing.T) {
world := NewWorld()
p := position{1, 2, 3}
Expand Down

0 comments on commit fab9d51

Please sign in to comment.