added hud system, turned all observers into one central observer
This commit is contained in:
@@ -4,17 +4,33 @@ import (
|
||||
"openquell/components"
|
||||
|
||||
"github.com/mlange-42/arche/ecs"
|
||||
"github.com/mlange-42/arche/ecs/event"
|
||||
"github.com/mlange-42/arche/generic"
|
||||
"github.com/mlange-42/arche/listener"
|
||||
)
|
||||
|
||||
// Used for global game state
|
||||
// Used for global game state. Also stores mobile entities of the
|
||||
// current level. The Entities map will be reset on each level
|
||||
// initialization in grid/grid.go
|
||||
type GameObserver struct {
|
||||
World *ecs.World
|
||||
CurrentLevel, Width int
|
||||
Height, Cellsize, Score int
|
||||
StopTimer *components.Timer
|
||||
Lost bool // set to true if player is struck or something, by default: win!
|
||||
Retry bool
|
||||
NextlevelText string
|
||||
Entities map[ecs.ID]map[ecs.Entity]int
|
||||
}
|
||||
|
||||
func (observer *GameObserver) GetListenerCallback(comp ecs.ID) listener.Callback {
|
||||
return listener.NewCallback(
|
||||
func(world *ecs.World, event ecs.EntityEvent) {
|
||||
observer.RemoveEntity(event.Entity, comp)
|
||||
},
|
||||
event.EntityRemoved,
|
||||
comp,
|
||||
)
|
||||
}
|
||||
|
||||
func NewGameObserver(world *ecs.World, startlevel, width, height, cellsize int) *GameObserver {
|
||||
@@ -24,11 +40,33 @@ func NewGameObserver(world *ecs.World, startlevel, width, height, cellsize int)
|
||||
Width: width,
|
||||
Height: height,
|
||||
Cellsize: cellsize,
|
||||
World: world,
|
||||
}
|
||||
|
||||
resmanger := generic.NewResource[GameObserver](world)
|
||||
resmanger.Add(observer)
|
||||
|
||||
playerID := ecs.ComponentID[components.Player](world)
|
||||
obstacleID := ecs.ComponentID[components.Obstacle](world)
|
||||
particleID := ecs.ComponentID[components.Particle](world)
|
||||
|
||||
playerListener := observer.GetListenerCallback(playerID)
|
||||
obstacleListener := observer.GetListenerCallback(obstacleID)
|
||||
particleListener := observer.GetListenerCallback(particleID)
|
||||
|
||||
listen := listener.NewDispatch(
|
||||
&playerListener,
|
||||
&obstacleListener,
|
||||
&particleListener,
|
||||
)
|
||||
|
||||
world.SetListener(&listen)
|
||||
|
||||
observer.Entities = make(map[ecs.ID]map[ecs.Entity]int)
|
||||
observer.Entities[playerID] = make(map[ecs.Entity]int)
|
||||
observer.Entities[obstacleID] = make(map[ecs.Entity]int)
|
||||
observer.Entities[particleID] = make(map[ecs.Entity]int)
|
||||
|
||||
return observer
|
||||
}
|
||||
|
||||
@@ -41,3 +79,45 @@ func GetGameObserver(world *ecs.World) *GameObserver {
|
||||
func (observer *GameObserver) Gameover() {
|
||||
observer.Lost = true
|
||||
}
|
||||
|
||||
func (observer *GameObserver) AddEntity(entity ecs.Entity, comp ecs.ID) {
|
||||
observer.Entities[comp][entity] = 1
|
||||
}
|
||||
|
||||
func (observer *GameObserver) RemoveEntity(entity ecs.Entity, comp ecs.ID) {
|
||||
delete(observer.Entities[comp], entity)
|
||||
}
|
||||
|
||||
func (observer *GameObserver) GetEntities(comp ecs.ID) []ecs.Entity {
|
||||
keys := make([]ecs.Entity, 0, len(observer.Entities[comp]))
|
||||
for k, _ := range observer.Entities[comp] {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
func (observer *GameObserver) GetPlayers() []ecs.Entity {
|
||||
playerID := ecs.ComponentID[components.Player](observer.World)
|
||||
return observer.GetEntities(playerID)
|
||||
}
|
||||
|
||||
func (observer *GameObserver) GetObstacles() []ecs.Entity {
|
||||
obstacleID := ecs.ComponentID[components.Obstacle](observer.World)
|
||||
return observer.GetEntities(obstacleID)
|
||||
}
|
||||
|
||||
func (observer *GameObserver) GetParticles() []ecs.Entity {
|
||||
particleID := ecs.ComponentID[components.Particle](observer.World)
|
||||
return observer.GetEntities(particleID)
|
||||
}
|
||||
|
||||
func (observer *GameObserver) RemoveEntities() {
|
||||
playerID := ecs.ComponentID[components.Player](observer.World)
|
||||
obstacleID := ecs.ComponentID[components.Obstacle](observer.World)
|
||||
particleID := ecs.ComponentID[components.Particle](observer.World)
|
||||
|
||||
observer.Entities = make(map[ecs.ID]map[ecs.Entity]int)
|
||||
observer.Entities[playerID] = make(map[ecs.Entity]int)
|
||||
observer.Entities[obstacleID] = make(map[ecs.Entity]int)
|
||||
observer.Entities[particleID] = make(map[ecs.Entity]int)
|
||||
}
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
package observers
|
||||
|
||||
import (
|
||||
"github.com/mlange-42/arche/ecs"
|
||||
"github.com/mlange-42/arche/ecs/event"
|
||||
"github.com/mlange-42/arche/generic"
|
||||
"github.com/mlange-42/arche/listener"
|
||||
)
|
||||
|
||||
// will be added as an ecs.Resource to the world so we can use it in
|
||||
// CollisionSystem to dynamically make it appear or disappear
|
||||
type ObstacleObserver struct {
|
||||
// we only have one obstacle so far, if we use multiple ones, turn
|
||||
// this in to a map, see player_observer.go for an example
|
||||
Entities map[ecs.Entity]int
|
||||
}
|
||||
|
||||
// Create a new resource of type PlayerObserver which will hold all
|
||||
// obstacle entities. We also add a Listener which looks for
|
||||
// EntityRemoved events and if a player gets removed, we also remove
|
||||
// it from the Entity map in our observer.
|
||||
func NewObstacleObserver(world *ecs.World) {
|
||||
observer := &ObstacleObserver{}
|
||||
observer.Entities = make(map[ecs.Entity]int)
|
||||
|
||||
resmanger := generic.NewResource[ObstacleObserver](world)
|
||||
resmanger.Add(observer)
|
||||
|
||||
listen := listener.NewCallback(
|
||||
func(world *ecs.World, event ecs.EntityEvent) {
|
||||
observerID := ecs.ResourceID[ObstacleObserver](world)
|
||||
observer := world.Resources().Get(observerID).(*ObstacleObserver)
|
||||
observer.RemoveEntity(event.Entity)
|
||||
},
|
||||
|
||||
event.EntityRemoved,
|
||||
)
|
||||
|
||||
world.SetListener(&listen)
|
||||
}
|
||||
|
||||
func GetObstacleObserver(world *ecs.World) *ObstacleObserver {
|
||||
observerID := ecs.ResourceID[ObstacleObserver](world)
|
||||
observer := world.Resources().Get(observerID).(*ObstacleObserver)
|
||||
return observer
|
||||
}
|
||||
|
||||
func (observer *ObstacleObserver) AddEntity(entity ecs.Entity) {
|
||||
observer.Entities[entity] = 1
|
||||
}
|
||||
|
||||
func (observer *ObstacleObserver) RemoveEntity(entity ecs.Entity) {
|
||||
observer.Entities = make(map[ecs.Entity]int)
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package observers
|
||||
|
||||
import (
|
||||
"github.com/mlange-42/arche/ecs"
|
||||
"github.com/mlange-42/arche/ecs/event"
|
||||
"github.com/mlange-42/arche/generic"
|
||||
"github.com/mlange-42/arche/listener"
|
||||
)
|
||||
|
||||
// will be added as an ecs.Resource to the world so we can use it in
|
||||
// CollisionSystem to dynamically make it appear or disappear
|
||||
type ParticleObserver struct {
|
||||
// we only have one particle so far, if we use multiple ones, turn
|
||||
// this in to a map, see player_observer.go for an example
|
||||
Entity ecs.Entity
|
||||
}
|
||||
|
||||
// Create a new resource of type PlayerObserver which will hold all
|
||||
// player entities. We also add a Listener which looks for
|
||||
// EntityRemoved events and if a player gets removed, we also remove
|
||||
// it from the Entity map in our observer.
|
||||
func NewParticleObserver(world *ecs.World) {
|
||||
observer := &ParticleObserver{}
|
||||
|
||||
resmanger := generic.NewResource[ParticleObserver](world)
|
||||
resmanger.Add(observer)
|
||||
|
||||
listen := listener.NewCallback(
|
||||
func(world *ecs.World, event ecs.EntityEvent) {
|
||||
observerID := ecs.ResourceID[ParticleObserver](world)
|
||||
observer := world.Resources().Get(observerID).(*ParticleObserver)
|
||||
observer.RemoveEntity(event.Entity)
|
||||
},
|
||||
|
||||
event.EntityRemoved,
|
||||
)
|
||||
|
||||
world.SetListener(&listen)
|
||||
}
|
||||
|
||||
func GetParticleObserver(world *ecs.World) *ParticleObserver {
|
||||
observerID := ecs.ResourceID[ParticleObserver](world)
|
||||
observer := world.Resources().Get(observerID).(*ParticleObserver)
|
||||
return observer
|
||||
}
|
||||
|
||||
func (observer *ParticleObserver) AddEntity(entity ecs.Entity) {
|
||||
observer.Entity = entity
|
||||
}
|
||||
|
||||
func (observer *ParticleObserver) RemoveEntity(entity ecs.Entity) {
|
||||
observer.Entity = ecs.Entity{}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package observers
|
||||
|
||||
import (
|
||||
"github.com/mlange-42/arche/ecs"
|
||||
"github.com/mlange-42/arche/ecs/event"
|
||||
"github.com/mlange-42/arche/generic"
|
||||
"github.com/mlange-42/arche/listener"
|
||||
)
|
||||
|
||||
// will be added as an ecs.Resource to the world so we can use it in
|
||||
// CollisionSystem to check for player collisions.
|
||||
type PlayerObserver struct {
|
||||
Entities map[ecs.Entity]int
|
||||
}
|
||||
|
||||
// Create a new resource of type PlayerObserver which will hold all
|
||||
// player entities. We also add a Listener which looks for
|
||||
// EntityRemoved events and if a player gets removed, we also remove
|
||||
// it from the Entity map in our observer.
|
||||
func NewPlayerObserver(world *ecs.World) {
|
||||
observer := &PlayerObserver{}
|
||||
observer.Entities = make(map[ecs.Entity]int)
|
||||
|
||||
resmanger := generic.NewResource[PlayerObserver](world)
|
||||
resmanger.Add(observer)
|
||||
|
||||
listen := listener.NewCallback(
|
||||
func(world *ecs.World, event ecs.EntityEvent) {
|
||||
observerID := ecs.ResourceID[PlayerObserver](world)
|
||||
observer := world.Resources().Get(observerID).(*PlayerObserver)
|
||||
observer.RemoveEntity(event.Entity)
|
||||
},
|
||||
|
||||
event.EntityRemoved,
|
||||
)
|
||||
|
||||
world.SetListener(&listen)
|
||||
}
|
||||
|
||||
func GetPlayerObserver(world *ecs.World) *PlayerObserver {
|
||||
observerID := ecs.ResourceID[PlayerObserver](world)
|
||||
observer := world.Resources().Get(observerID).(*PlayerObserver)
|
||||
return observer
|
||||
}
|
||||
|
||||
func (observer *PlayerObserver) AddEntity(entity ecs.Entity) {
|
||||
observer.Entities[entity] = 1
|
||||
}
|
||||
|
||||
func (observer *PlayerObserver) RemoveEntity(entity ecs.Entity) {
|
||||
delete(observer.Entities, entity)
|
||||
}
|
||||
|
||||
func (observer *PlayerObserver) RemoveEntities() {
|
||||
observer.Entities = make(map[ecs.Entity]int)
|
||||
}
|
||||
Reference in New Issue
Block a user