carotidartillery/creep.go

58 lines
879 B
Go
Raw Normal View History

2021-10-06 04:05:02 +00:00
package main
import (
"math/rand"
"sync"
"github.com/hajimehoshi/ebiten/v2"
)
2021-10-06 14:18:38 +00:00
type gameCreep struct {
2021-10-06 04:05:02 +00:00
x, y float64
sprite *ebiten.Image
moveX, moveY float64
tick int
nextAction int
2021-10-06 14:18:38 +00:00
level *Level
2021-10-06 04:05:02 +00:00
sync.Mutex
}
2021-10-06 14:18:38 +00:00
func NewCreep(sprite *ebiten.Image, level *Level) *gameCreep {
return &gameCreep{
x: float64(1 + rand.Intn(64)),
y: float64(1 + rand.Intn(64)),
sprite: sprite,
level: level,
}
}
func (c *gameCreep) doNextAction() {
2021-10-06 04:05:02 +00:00
c.moveX = (rand.Float64() - 0.5) / 10
c.moveY = (rand.Float64() - 0.5) / 10
c.nextAction = 400 + rand.Intn(1000)
}
2021-10-06 14:18:38 +00:00
func (c *gameCreep) Update() {
2021-10-06 04:05:02 +00:00
c.Lock()
defer c.Unlock()
c.tick++
if c.tick >= c.nextAction {
c.doNextAction()
c.tick = 0
}
2021-10-06 14:18:38 +00:00
c.x, c.y = c.level.Clamp(c.x+c.moveX, c.y+c.moveY)
2021-10-06 04:05:02 +00:00
}
2021-10-06 14:18:38 +00:00
func (c *gameCreep) Position() (float64, float64) {
2021-10-06 04:05:02 +00:00
c.Lock()
defer c.Unlock()
return c.x, c.y
}