refactoring to systems complete. also added observers for collision checks
This commit is contained in:
@@ -2,8 +2,10 @@ package systems
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"openquell/assets"
|
||||
"openquell/components"
|
||||
. "openquell/components"
|
||||
"openquell/observers"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/mlange-42/arche/ecs"
|
||||
@@ -11,9 +13,8 @@ import (
|
||||
)
|
||||
|
||||
type CollectibleSystem struct {
|
||||
World *ecs.World
|
||||
Selector *generic.Filter3[Position, Collectible, Renderable]
|
||||
EntitiesToRemove []ecs.Entity
|
||||
World *ecs.World
|
||||
Selector *generic.Filter3[Position, Collectible, Renderable]
|
||||
}
|
||||
|
||||
func NewCollectibleSystem(world *ecs.World) *CollectibleSystem {
|
||||
@@ -25,38 +26,39 @@ func NewCollectibleSystem(world *ecs.World) *CollectibleSystem {
|
||||
return system
|
||||
}
|
||||
|
||||
func (system *CollectibleSystem) CheckPlayerCollision(
|
||||
playerposition *Position,
|
||||
playervelocity *Velocity) (bool, image.Point) {
|
||||
func (system *CollectibleSystem) Update() {
|
||||
playerobserver := observers.GetPlayerObserver(system.World)
|
||||
|
||||
particle_pos := image.Point{}
|
||||
var bumped bool
|
||||
posID := ecs.ComponentID[components.Position](system.World)
|
||||
veloID := ecs.ComponentID[components.Velocity](system.World)
|
||||
particlepositions := []*components.Position{}
|
||||
EntitiesToRemove := []ecs.Entity{}
|
||||
|
||||
query := system.Selector.Query(system.World)
|
||||
|
||||
for query.Next() {
|
||||
colposition, collectible, _ := query.Get()
|
||||
|
||||
ok, _ := playerposition.Intersects(colposition, playervelocity)
|
||||
if ok {
|
||||
fmt.Printf("bumped into collectible %v\n", collectible)
|
||||
system.EntitiesToRemove = append(system.EntitiesToRemove, query.Entity())
|
||||
particle_pos.X = colposition.X
|
||||
particle_pos.Y = colposition.Y
|
||||
bumped = true
|
||||
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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return bumped, particle_pos
|
||||
}
|
||||
|
||||
func (system *CollectibleSystem) Update() {
|
||||
// remove collectible if collected
|
||||
for _, entity := range system.EntitiesToRemove {
|
||||
system.World.RemoveEntity(entity)
|
||||
for _, pos := range particlepositions {
|
||||
system.AddParticle(pos)
|
||||
}
|
||||
|
||||
system.EntitiesToRemove = []ecs.Entity{}
|
||||
for _, entity := range EntitiesToRemove {
|
||||
system.World.RemoveEntity(entity)
|
||||
}
|
||||
}
|
||||
|
||||
func (system *CollectibleSystem) Draw(screen *ebiten.Image) {
|
||||
@@ -73,3 +75,25 @@ func (system *CollectibleSystem) Draw(screen *ebiten.Image) {
|
||||
screen.DrawImage(sprite.Image, op)
|
||||
}
|
||||
}
|
||||
|
||||
func (system *CollectibleSystem) AddParticle(position *components.Position) {
|
||||
particleobserver := observers.GetParticleObserver(system.World)
|
||||
|
||||
ptmapper := generic.NewMap2[
|
||||
components.Position,
|
||||
components.Particle,
|
||||
](system.World)
|
||||
|
||||
entity := ptmapper.New()
|
||||
pos, particle := ptmapper.Get(entity)
|
||||
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,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package systems
|
||||
|
||||
import (
|
||||
"image"
|
||||
. "openquell/components"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
@@ -25,34 +24,29 @@ func NewParticleSystem(world *ecs.World, cellsize int) *ParticleSystem {
|
||||
return system
|
||||
}
|
||||
|
||||
func (system *ParticleSystem) Update(detonate bool, position *image.Point) {
|
||||
func (system *ParticleSystem) Update() {
|
||||
// display debris after collecting
|
||||
EntitiesToRemove := []ecs.Entity{}
|
||||
|
||||
query := system.Selector.Query(system.World)
|
||||
|
||||
for query.Next() {
|
||||
// we loop, but it's only one anyway
|
||||
ptposition, particle := query.Get()
|
||||
_, particle := query.Get()
|
||||
|
||||
if detonate {
|
||||
// particle appears
|
||||
ptposition.Update(
|
||||
position.X-(system.Cellsize/2),
|
||||
position.Y-(system.Cellsize/2),
|
||||
64,
|
||||
)
|
||||
|
||||
particle.Index = 0 // start displaying the particle
|
||||
} else {
|
||||
switch {
|
||||
// particle shows from earlier tick, animate
|
||||
case particle.Index > -1 && particle.Index < len(particle.Particles)-1:
|
||||
particle.Index++
|
||||
default:
|
||||
// last sprite reached, remove it
|
||||
particle.Index = -1
|
||||
}
|
||||
switch {
|
||||
// particle shows from earlier tick, animate
|
||||
case particle.Index > -1 && particle.Index < len(particle.Particles)-1:
|
||||
particle.Index++
|
||||
default:
|
||||
// last sprite reached, remove it
|
||||
EntitiesToRemove = append(EntitiesToRemove, query.Entity())
|
||||
}
|
||||
}
|
||||
|
||||
for _, entity := range EntitiesToRemove {
|
||||
system.World.RemoveEntity(entity)
|
||||
}
|
||||
}
|
||||
|
||||
func (system *ParticleSystem) Draw(screen *ebiten.Image) {
|
||||
@@ -63,11 +57,8 @@ func (system *ParticleSystem) Draw(screen *ebiten.Image) {
|
||||
for query.Next() {
|
||||
pos, particle := query.Get()
|
||||
|
||||
if particle.Index > -1 {
|
||||
op.GeoM.Reset()
|
||||
op.GeoM.Translate(float64(pos.X), float64(pos.Y))
|
||||
screen.DrawImage(particle.Particles[particle.Index], op)
|
||||
}
|
||||
op.GeoM.Reset()
|
||||
op.GeoM.Translate(float64(pos.X), float64(pos.Y))
|
||||
screen.DrawImage(particle.Particles[particle.Index], op)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package systems
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
. "openquell/components"
|
||||
. "openquell/config"
|
||||
|
||||
@@ -34,9 +33,6 @@ func NewPlayerSystem(world *ecs.World, grid *GridSystem) *PlayerSystem {
|
||||
func (system PlayerSystem) Update() error {
|
||||
query := system.Selector.Query(system.World)
|
||||
|
||||
var bumped bool
|
||||
var particle_pos image.Point
|
||||
|
||||
for query.Next() {
|
||||
playerposition, velocity, _, _ := query.Get()
|
||||
|
||||
@@ -74,14 +70,12 @@ func (system PlayerSystem) Update() error {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bumped, particle_pos = system.Collectible.CheckPlayerCollision(playerposition, velocity)
|
||||
}
|
||||
|
||||
playerposition.Move(velocity)
|
||||
}
|
||||
|
||||
system.Particle.Update(bumped, &particle_pos)
|
||||
system.Particle.Update()
|
||||
system.Collectible.Update()
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user