ichigo/game/bubble.go

82 lines
1.7 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
)
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-16 14:38:21 +10:00
func (b *Bubble) Scan() []interface{} {
return []interface{}{&b.Sprite}
}
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 {
b.game.Unregister(b)
}
2021-09-17 12:50:29 +10:00
if false { // not using MoveX/MoveY/... because collisions are unnecessary -
// this is an effect particle, if it overlaps a solid, who cares
b.Sprite.Actor.Pos = b.Sprite.Actor.Pos.Add(geom.Pt3(
// --lint:ignore SA4000 one random minus another is not always zero...
rand.Intn(3)-1, rand.Intn(2)-1, 0, // rand.Intn(2)-rand.Intn(2),
))
} else {
b.Sprite.Actor.MoveX(float64(rand.Intn(3)-1), nil)
b.Sprite.Actor.MoveY(float64(rand.Intn(2)-1), nil)
}
2021-09-16 14:38:21 +10:00
return nil
}