example/mascot: Use vx16 instead of dir

This commit is contained in:
Hajime Hoshi 2020-03-22 17:31:46 +09:00
parent 77b7e961ff
commit 37a8ae06c5

View File

@ -74,19 +74,12 @@ func init() {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
} }
type dir int
const (
right dir = iota
left
)
type mascot struct { type mascot struct {
x16 int x16 int
y16 int y16 int
vx16 int
vy16 int vy16 int
dir dir
count int count int
} }
@ -96,19 +89,15 @@ func (m *mascot) update(screen *ebiten.Image) error {
sw, sh := ebiten.ScreenSizeInFullscreen() sw, sh := ebiten.ScreenSizeInFullscreen()
ebiten.SetWindowPosition(m.x16/16, m.y16/16+sh-height) ebiten.SetWindowPosition(m.x16/16, m.y16/16+sh-height)
switch m.dir { if m.vx16 == 0 {
case right: m.vx16 = 64
m.x16 += 64
case left:
m.x16 -= 64
default:
panic("not reached")
} }
if m.x16/16 > sw-width && m.dir == right { m.x16 += m.vx16
m.dir = left if m.x16/16 > sw-width && m.vx16 > 0 {
m.vx16 = -64
} }
if m.x16 <= 0 && m.dir == left { if m.x16 <= 0 && m.vx16 < 0 {
m.dir = right m.vx16 = 64
} }
// Accelarate the mascot in the Y direction. // Accelarate the mascot in the Y direction.
@ -129,11 +118,7 @@ func (m *mascot) update(screen *ebiten.Image) error {
m.vy16 = -240 m.vy16 = -240
case 1: case 1:
// Turn. // Turn.
if m.dir == right { m.vx16 = -m.vx16
m.dir = left
} else {
m.dir = right
}
} }
} }
@ -153,7 +138,7 @@ func (m *mascot) update(screen *ebiten.Image) error {
} }
} }
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
if m.dir == left { if m.vx16 < 0 {
op.GeoM.Scale(-1, 1) op.GeoM.Scale(-1, 1)
op.GeoM.Translate(width, 0) op.GeoM.Translate(width, 0)
} }