mirror of
https://github.com/Kugelschieber/go-game.git
synced 2026-01-18 06:40:28 +00:00
Convenience function to addd and remove actors to or from systems.
This commit is contained in:
@@ -24,8 +24,7 @@ func (g *Game) Setup() {
|
||||
}
|
||||
|
||||
sprite := goga.NewSprite(tex)
|
||||
renderer := goga.GetSystemByName("spriteRenderer")
|
||||
renderer.Add(sprite)
|
||||
goga.AddActor(sprite)
|
||||
}
|
||||
|
||||
func (g *Game) Update(delta float64) {
|
||||
|
||||
11
sprite.go
11
sprite.go
@@ -101,20 +101,13 @@ func (s *SpriteRenderer) Add(actor interface{}) bool {
|
||||
|
||||
// Removes sprite from renderer.
|
||||
func (s *SpriteRenderer) Remove(actor interface{}) bool {
|
||||
sprite, ok := actor.(Sprite)
|
||||
sprite, ok := actor.(*Sprite)
|
||||
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
for i, sp := range s.sprites {
|
||||
if sp == sprite {
|
||||
s.sprites = append(s.sprites[:i], s.sprites[i+1:]...)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
return s.RemoveById(sprite.Actor.GetId())
|
||||
}
|
||||
|
||||
// Removes sprite from renderer by ID.
|
||||
|
||||
45
system.go
45
system.go
@@ -75,3 +75,48 @@ func updateSystems(delta float64) {
|
||||
system.Update(delta)
|
||||
}
|
||||
}
|
||||
|
||||
// Takes an actor and tries to add it to all systems that accept it.
|
||||
// This maybe not as performant as directly adding it to the right system.
|
||||
// Returns true if it could be added to at least one system, else false.
|
||||
func AddActor(actor interface{}) bool {
|
||||
accepted := false
|
||||
|
||||
for _, system := range systems {
|
||||
if system.Add(actor) {
|
||||
accepted = true
|
||||
}
|
||||
}
|
||||
|
||||
return accepted
|
||||
}
|
||||
|
||||
// Removes an actor from all systems.
|
||||
// This maybe not as performant as directly removing it from the right system.
|
||||
// Returns true if it could be removed from at least one system, else false.
|
||||
func RemoveActor(actor interface{}) bool {
|
||||
removed := false
|
||||
|
||||
for _, system := range systems {
|
||||
if system.Remove(actor) {
|
||||
removed = true
|
||||
}
|
||||
}
|
||||
|
||||
return removed
|
||||
}
|
||||
|
||||
// Removes an actor from all systems by ID.
|
||||
// This maybe not as performant as directly removing it from the right system.
|
||||
// Returns true if it could be removed from at least one system, else false.
|
||||
func RemoveActorById(id ActorId) bool {
|
||||
removed := false
|
||||
|
||||
for _, system := range systems {
|
||||
if system.RemoveById(id) {
|
||||
removed = true
|
||||
}
|
||||
}
|
||||
|
||||
return removed
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user