examples/sprites: Rotate sprites

This commit is contained in:
Hajime Hoshi 2019-02-24 02:09:38 +09:00
parent 3f888f85cc
commit fc43868f21

View File

@ -64,10 +64,11 @@ func init() {
type sprite struct { type sprite struct {
count int count int
maxCount int maxCount int
angle float64 dir float64
img *ebiten.Image img *ebiten.Image
scale float64 scale float64
angle float64
alpha float64 alpha float64
} }
@ -91,13 +92,14 @@ func (s *sprite) draw(screen *ebiten.Image) {
ox = screenWidth / 2 ox = screenWidth / 2
oy = screenHeight / 2 oy = screenHeight / 2
) )
x := math.Cos(s.angle) * float64(s.maxCount-s.count) x := math.Cos(s.dir) * float64(s.maxCount-s.count)
y := math.Sin(s.angle) * float64(s.maxCount-s.count) y := math.Sin(s.dir) * float64(s.maxCount-s.count)
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
sx, sy := s.img.Size() sx, sy := s.img.Size()
op.GeoM.Translate(-float64(sx)/2, -float64(sy)/2) op.GeoM.Translate(-float64(sx)/2, -float64(sy)/2)
op.GeoM.Rotate(s.angle)
op.GeoM.Scale(s.scale, s.scale) op.GeoM.Scale(s.scale, s.scale)
op.GeoM.Translate(x, y) op.GeoM.Translate(x, y)
op.GeoM.Translate(ox, oy) op.GeoM.Translate(ox, oy)
@ -114,13 +116,12 @@ func (s *sprite) draw(screen *ebiten.Image) {
alpha *= s.alpha alpha *= s.alpha
op.ColorM.Scale(1, 1, 1, alpha) op.ColorM.Scale(1, 1, 1, alpha)
op.Filter = ebiten.FilterLinear
screen.DrawImage(s.img, op) screen.DrawImage(s.img, op)
} }
func newSprite(img *ebiten.Image) *sprite { func newSprite(img *ebiten.Image) *sprite {
c := rand.Intn(50) + 300 c := rand.Intn(50) + 300
dir := rand.Float64() * 2 * math.Pi
a := rand.Float64() * 2 * math.Pi a := rand.Float64() * 2 * math.Pi
s := rand.Float64()*0.1 + 0.4 s := rand.Float64()*0.1 + 0.4
return &sprite{ return &sprite{
@ -128,9 +129,11 @@ func newSprite(img *ebiten.Image) *sprite {
maxCount: c, maxCount: c,
count: c, count: c,
angle: a, dir: dir,
scale: s,
alpha: 0.5, angle: a,
scale: s,
alpha: 0.5,
} }
} }