ichigo/game/bubble.go

91 lines
1.9 KiB
Go
Raw Normal View History

2021-09-16 14:38:21 +10:00
package game
import (
2021-09-17 11:13:39 +10:00
"fmt"
2021-09-16 15:07:14 +10:00
"image"
2021-09-16 14:53:23 +10:00
"math/rand"
2021-09-16 14:38:21 +10:00
"drjosh.dev/gurgle/engine"
2021-09-16 15:04:13 +10:00
"drjosh.dev/gurgle/geom"
2021-09-16 14:38:21 +10:00
)
2021-09-21 14:42:18 +10:00
var _ interface {
engine.Scanner
engine.Prepper
engine.Updater
} = &Bubble{}
2021-09-16 14:38:21 +10:00
type Bubble struct {
Life int
Sprite engine.Sprite
game *engine.Game
}
2021-09-16 15:07:14 +10:00
func NewBubble(pos geom.Int3) *Bubble {
return &Bubble{
Life: 60,
Sprite: engine.Sprite{
Actor: engine.Actor{
Pos: pos,
Bounds: geom.Box{
Min: geom.Pt3(-4, -4, -4),
Max: geom.Pt3(4, 4, 4),
},
},
DrawOffset: image.Pt(-4, -4),
Sheet: engine.Sheet{
AnimDefs: map[string]*engine.AnimDef{
"bubble": {
Steps: []engine.AnimStep{
{Cell: 0, Duration: 5},
{Cell: 1, Duration: 15},
{Cell: 2, Duration: 20},
{Cell: 3, Duration: 15},
{Cell: 4, Duration: 3},
{Cell: 5, Duration: 2},
},
OneShot: true,
},
},
CellSize: image.Pt(8, 8),
Src: engine.ImageRef{Path: "assets/bubble.png"},
},
},
}
}
2021-09-22 15:48:02 +10:00
func (b *Bubble) Scan(visit func(interface{}) error) error {
return visit(&b.Sprite)
2021-09-16 14:38:21 +10:00
}
2021-09-17 11:13:39 +10:00
func (b *Bubble) String() string {
return fmt.Sprintf("Bubble@%v", b.Sprite.Actor.Pos)
}
2021-09-16 14:38:21 +10:00
func (b *Bubble) Prepare(g *engine.Game) error {
b.game = g
return nil
}
func (b *Bubble) Update() error {
b.Life--
if b.Life <= 0 {
2021-09-21 17:09:49 +10:00
b.game.PathUnregister(b)
2021-09-16 14:38:21 +10:00
}
2021-09-22 17:04:27 +10:00
if true {
2021-09-21 16:53:04 +10:00
// not using MoveX/MoveY/... because collisions are unnecessary -
// this is an effect particle; if it overlaps a solid, who cares
2021-09-17 12:50:29 +10:00
b.Sprite.Actor.Pos = b.Sprite.Actor.Pos.Add(geom.Pt3(
2021-09-21 16:53:04 +10:00
//lint:ignore SA4000 one random minus another is not always zero...
2021-09-22 17:04:27 +10:00
rand.Intn(3)-1, -1, rand.Intn(2)-rand.Intn(2),
2021-09-17 12:50:29 +10:00
))
} else {
b.Sprite.Actor.MoveX(float64(rand.Intn(3)-1), nil)
2021-09-22 17:04:27 +10:00
b.Sprite.Actor.MoveY(-1, nil)
2021-09-21 16:53:04 +10:00
//lint:ignore SA4000 one random minus another is not always zero...
b.Sprite.Actor.MoveZ(float64(rand.Intn(2)-rand.Intn(2)), nil)
2021-09-17 12:50:29 +10:00
}
2021-09-16 14:38:21 +10:00
return nil
}