2024-02-10 19:45:06 +01:00
|
|
|
package systems
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2024-02-11 13:00:56 +01:00
|
|
|
"openquell/assets"
|
|
|
|
|
"openquell/components"
|
2024-02-10 19:45:06 +01:00
|
|
|
. "openquell/components"
|
2024-02-12 17:27:52 +01:00
|
|
|
"openquell/config"
|
2024-02-11 13:00:56 +01:00
|
|
|
"openquell/observers"
|
2024-02-10 19:45:06 +01:00
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
|
"github.com/mlange-42/arche/ecs"
|
|
|
|
|
"github.com/mlange-42/arche/generic"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type CollectibleSystem struct {
|
2024-02-11 13:00:56 +01:00
|
|
|
World *ecs.World
|
|
|
|
|
Selector *generic.Filter3[Position, Collectible, Renderable]
|
2024-02-10 19:45:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewCollectibleSystem(world *ecs.World) *CollectibleSystem {
|
|
|
|
|
system := &CollectibleSystem{
|
|
|
|
|
Selector: generic.NewFilter3[Position, Collectible, Renderable](),
|
|
|
|
|
World: world,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return system
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-11 13:00:56 +01:00
|
|
|
func (system *CollectibleSystem) Update() {
|
|
|
|
|
playerobserver := observers.GetPlayerObserver(system.World)
|
2024-02-10 19:45:06 +01:00
|
|
|
|
2024-02-11 13:00:56 +01:00
|
|
|
posID := ecs.ComponentID[components.Position](system.World)
|
|
|
|
|
veloID := ecs.ComponentID[components.Velocity](system.World)
|
2024-02-12 17:27:52 +01:00
|
|
|
|
2024-02-11 13:00:56 +01:00
|
|
|
particlepositions := []*components.Position{}
|
|
|
|
|
EntitiesToRemove := []ecs.Entity{}
|
2024-02-10 19:45:06 +01:00
|
|
|
|
|
|
|
|
query := system.Selector.Query(system.World)
|
2024-02-11 14:24:30 +01:00
|
|
|
numcollectibles := query.Count()
|
|
|
|
|
|
|
|
|
|
if numcollectibles == 0 {
|
|
|
|
|
query.Close()
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-02-10 19:45:06 +01:00
|
|
|
|
|
|
|
|
for query.Next() {
|
|
|
|
|
colposition, collectible, _ := query.Get()
|
|
|
|
|
|
2024-02-11 13:00:56 +01:00
|
|
|
for player := range playerobserver.Entities {
|
|
|
|
|
playerposition := (*Position)(system.World.Get(player, posID))
|
|
|
|
|
playervelocity := (*Velocity)(system.World.Get(player, veloID))
|
|
|
|
|
|
|
|
|
|
ok, _ := playerposition.Intersects(colposition, playervelocity)
|
|
|
|
|
if ok {
|
|
|
|
|
fmt.Printf("bumped into collectible %v\n", collectible)
|
|
|
|
|
particlepositions = append(particlepositions, colposition)
|
|
|
|
|
EntitiesToRemove = append(EntitiesToRemove, query.Entity())
|
|
|
|
|
}
|
2024-02-10 19:45:06 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-11 13:00:56 +01:00
|
|
|
for _, pos := range particlepositions {
|
|
|
|
|
system.AddParticle(pos)
|
|
|
|
|
}
|
2024-02-10 19:45:06 +01:00
|
|
|
|
2024-02-11 13:00:56 +01:00
|
|
|
for _, entity := range EntitiesToRemove {
|
2024-02-10 19:45:06 +01:00
|
|
|
system.World.RemoveEntity(entity)
|
2024-02-11 14:24:30 +01:00
|
|
|
numcollectibles--
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if numcollectibles == 0 {
|
|
|
|
|
// winner, winner, chicken dinner!
|
|
|
|
|
game := observers.GetGameObserver(system.World)
|
|
|
|
|
game.Score++
|
|
|
|
|
game.CurrentLevel++
|
2024-02-10 19:45:06 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (system *CollectibleSystem) Draw(screen *ebiten.Image) {
|
|
|
|
|
// write the movable tiles
|
|
|
|
|
op := &ebiten.DrawImageOptions{}
|
|
|
|
|
query := system.Selector.Query(system.World)
|
|
|
|
|
|
|
|
|
|
for query.Next() {
|
|
|
|
|
pos, _, sprite := query.Get()
|
|
|
|
|
|
|
|
|
|
op.GeoM.Reset()
|
|
|
|
|
op.GeoM.Translate(float64(pos.X), float64(pos.Y))
|
|
|
|
|
|
|
|
|
|
screen.DrawImage(sprite.Image, op)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-11 13:00:56 +01:00
|
|
|
|
|
|
|
|
func (system *CollectibleSystem) AddParticle(position *components.Position) {
|
|
|
|
|
particleobserver := observers.GetParticleObserver(system.World)
|
|
|
|
|
|
2024-02-12 17:27:52 +01:00
|
|
|
ptmapper := generic.NewMap3[
|
2024-02-11 13:00:56 +01:00
|
|
|
components.Position,
|
|
|
|
|
components.Particle,
|
2024-02-12 17:27:52 +01:00
|
|
|
components.Timer,
|
2024-02-11 13:00:56 +01:00
|
|
|
](system.World)
|
|
|
|
|
|
|
|
|
|
entity := ptmapper.New()
|
2024-02-12 17:27:52 +01:00
|
|
|
pos, particle, timer := ptmapper.Get(entity)
|
2024-02-11 13:00:56 +01:00
|
|
|
particleobserver.AddEntity(entity)
|
|
|
|
|
|
|
|
|
|
particle.Index = assets.Tiles['*'].Particle
|
|
|
|
|
particle.Particles = assets.Tiles['*'].Particles
|
|
|
|
|
|
|
|
|
|
pos.Update(
|
|
|
|
|
position.X-(16), // FIXME: use global tilesize!
|
|
|
|
|
position.Y-(16),
|
|
|
|
|
64,
|
|
|
|
|
)
|
2024-02-12 17:27:52 +01:00
|
|
|
|
|
|
|
|
timer.Start(config.PARTICLE_LOOPWAIT)
|
2024-02-11 13:00:56 +01:00
|
|
|
}
|