2024-02-10 19:45:06 +01:00
|
|
|
package systems
|
|
|
|
|
|
|
|
|
|
import (
|
2024-04-07 14:24:28 +02:00
|
|
|
"openquell/assets"
|
|
|
|
|
"openquell/components"
|
2024-02-10 19:45:06 +01:00
|
|
|
. "openquell/components"
|
2024-04-07 14:24:28 +02:00
|
|
|
"openquell/config"
|
|
|
|
|
"openquell/util"
|
|
|
|
|
|
|
|
|
|
"log"
|
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 GridSystem struct {
|
2024-02-23 18:47:15 +01:00
|
|
|
World *ecs.World
|
|
|
|
|
Selector *generic.Filter3[Renderable, Position, Solid]
|
|
|
|
|
UseCache bool
|
|
|
|
|
Cache *ebiten.Image
|
|
|
|
|
Count int // register tile count, invalidates cache
|
|
|
|
|
Background *ebiten.Image
|
2024-04-07 14:24:28 +02:00
|
|
|
Animate components.Animation
|
|
|
|
|
BackgroundAnimated bool
|
2024-02-23 18:47:15 +01:00
|
|
|
Width, Height, Tilesize int
|
2024-02-10 19:45:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewGridSystem(world *ecs.World, width, height,
|
2024-04-07 14:24:28 +02:00
|
|
|
tilesize int, background string) System {
|
2024-02-10 19:45:06 +01:00
|
|
|
|
|
|
|
|
cache := ebiten.NewImage(width, height)
|
|
|
|
|
|
2024-04-07 14:24:28 +02:00
|
|
|
if !util.Exists(assets.Assets, background) {
|
|
|
|
|
log.Fatalf("no background %s in assets", background)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-10 19:45:06 +01:00
|
|
|
system := &GridSystem{
|
|
|
|
|
Selector: generic.NewFilter3[Renderable, Position, Solid](),
|
|
|
|
|
UseCache: false,
|
|
|
|
|
Cache: cache,
|
|
|
|
|
Width: width,
|
|
|
|
|
Height: height,
|
|
|
|
|
Tilesize: tilesize,
|
2024-04-07 14:24:28 +02:00
|
|
|
Background: assets.Assets[background],
|
2024-02-10 19:45:06 +01:00
|
|
|
World: world,
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-07 14:24:28 +02:00
|
|
|
if util.Exists(assets.Animations, background+"-animated") {
|
|
|
|
|
system.BackgroundAnimated = true
|
|
|
|
|
|
|
|
|
|
system.Animate = components.Animation{
|
2024-04-07 19:10:27 +02:00
|
|
|
Active: true,
|
|
|
|
|
Loop: true,
|
|
|
|
|
Sprites: assets.Animations[background+"-animated"].Sprites,
|
2024-04-07 14:24:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
system.Animate.Timer.Start(config.ANIMATION_LOOPWAIT)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-10 19:45:06 +01:00
|
|
|
return system
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-01 10:55:14 +01:00
|
|
|
func (system *GridSystem) Update() error {
|
2024-04-07 14:24:28 +02:00
|
|
|
if system.BackgroundAnimated {
|
|
|
|
|
if system.Animate.Timer.IsReady() {
|
|
|
|
|
switch {
|
|
|
|
|
// animation shows from earlier tick, animate
|
2024-04-07 19:10:27 +02:00
|
|
|
case system.Animate.Index > -1 && system.Animate.Index < len(system.Animate.Sprites)-1:
|
2024-04-07 14:24:28 +02:00
|
|
|
system.Animate.Index += 1
|
2024-04-07 19:10:27 +02:00
|
|
|
system.Animate.Timer.Start(system.Animate.GetDuration())
|
2024-04-07 14:24:28 +02:00
|
|
|
default:
|
|
|
|
|
// last sprite reached
|
|
|
|
|
if system.Animate.Loop {
|
|
|
|
|
system.Animate.Index = 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
system.Animate.Timer.Update()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-01 10:55:14 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
2024-02-10 19:45:06 +01:00
|
|
|
|
|
|
|
|
func (system *GridSystem) Draw(screen *ebiten.Image) {
|
|
|
|
|
op := &ebiten.DrawImageOptions{}
|
2024-02-22 14:33:01 +01:00
|
|
|
query := system.Selector.Query(system.World)
|
2024-03-01 10:55:14 +01:00
|
|
|
|
2024-04-07 14:24:28 +02:00
|
|
|
if !system.UseCache || query.Count() != system.Count || system.BackgroundAnimated {
|
2024-02-10 19:45:06 +01:00
|
|
|
// map not cached or cacheable, write it to the cache
|
2024-04-07 14:24:28 +02:00
|
|
|
if system.BackgroundAnimated {
|
2024-04-07 19:10:27 +02:00
|
|
|
system.Cache.DrawImage(system.Animate.GetSprite(), op)
|
2024-04-07 14:24:28 +02:00
|
|
|
} else {
|
|
|
|
|
system.Cache.DrawImage(system.Background, op)
|
|
|
|
|
}
|
2024-02-10 19:45:06 +01:00
|
|
|
|
2024-02-22 14:33:01 +01:00
|
|
|
system.Count = query.Count()
|
2024-03-01 10:55:14 +01:00
|
|
|
counter := 0
|
2024-02-10 19:45:06 +01:00
|
|
|
for query.Next() {
|
|
|
|
|
sprite, pos, _ := query.Get()
|
2024-03-31 20:16:15 +02:00
|
|
|
|
2024-03-01 10:55:14 +01:00
|
|
|
counter++
|
|
|
|
|
op.GeoM.Reset()
|
|
|
|
|
op.GeoM.Translate(float64(pos.X), float64(pos.Y))
|
|
|
|
|
system.Cache.DrawImage(sprite.Image, op)
|
2024-03-31 20:16:15 +02:00
|
|
|
|
2024-02-10 19:45:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
op.GeoM.Reset()
|
|
|
|
|
screen.DrawImage(system.Cache, op)
|
|
|
|
|
|
|
|
|
|
system.UseCache = true
|
2024-03-01 10:55:14 +01:00
|
|
|
|
2024-02-10 19:45:06 +01:00
|
|
|
} else {
|
|
|
|
|
// use the cached map
|
|
|
|
|
op.GeoM.Reset()
|
|
|
|
|
screen.DrawImage(system.Cache, op)
|
2024-02-22 14:33:01 +01:00
|
|
|
query.Close()
|
2024-02-10 19:45:06 +01:00
|
|
|
}
|
|
|
|
|
}
|