ebiten/example/game/sprites/sprites.go

158 lines
3.1 KiB
Go
Raw Normal View History

2013-06-22 16:16:01 +02:00
package sprites
2013-06-21 04:02:38 +02:00
import (
2013-10-14 04:34:58 +02:00
"github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
"github.com/hajimehoshi/go-ebiten/ui"
2013-06-21 16:16:52 +02:00
"image"
"math/rand"
"os"
"time"
2013-06-21 04:02:38 +02:00
)
2013-10-16 16:20:07 +02:00
const (
2013-10-17 17:45:12 +02:00
ebitenTextureWidth = 57
2013-10-16 16:20:07 +02:00
ebitenTextureHeight = 26
)
2013-06-21 16:16:52 +02:00
type Sprite struct {
2013-06-21 19:42:14 +02:00
width int
2013-06-21 19:03:16 +02:00
height int
2013-06-21 19:42:14 +02:00
ch chan bool
x int
y int
vx int
vy int
2013-06-21 16:16:52 +02:00
}
2013-06-22 19:16:22 +02:00
func newSprite(screenWidth, screenHeight, width, height int) *Sprite {
2013-06-21 19:03:16 +02:00
maxX := screenWidth - width
maxY := screenHeight - height
2013-06-21 16:16:52 +02:00
sprite := &Sprite{
2013-06-21 19:42:14 +02:00
width: width,
2013-06-21 19:03:16 +02:00
height: height,
2013-06-21 19:42:14 +02:00
ch: make(chan bool),
x: rand.Intn(maxX),
y: rand.Intn(maxY),
vx: rand.Intn(2)*2 - 1,
vy: rand.Intn(2)*2 - 1,
2013-06-21 16:16:52 +02:00
}
2013-06-21 16:22:46 +02:00
go sprite.update(screenWidth, screenHeight)
2013-06-21 16:16:52 +02:00
return sprite
}
2013-06-21 16:22:46 +02:00
func (sprite *Sprite) update(screenWidth, screenHeight int) {
2013-06-21 19:03:16 +02:00
maxX := screenWidth - sprite.width
maxY := screenHeight - sprite.height
2013-06-21 16:22:46 +02:00
for {
<-sprite.ch
sprite.x += sprite.vx
sprite.y += sprite.vy
if sprite.x < 0 || maxX <= sprite.x {
sprite.vx = -sprite.vx
}
if sprite.y < 0 || maxY <= sprite.y {
sprite.vy = -sprite.vy
}
sprite.ch <- true
}
}
2013-06-21 16:16:52 +02:00
func (sprite *Sprite) Update() {
sprite.ch <- true
<-sprite.ch
}
2013-06-21 04:02:38 +02:00
type Sprites struct {
2013-12-01 13:42:41 +01:00
ebitenTextureId graphics.TextureId
sprites []*Sprite
screenSizeUpdatedCh chan ui.ScreenSizeUpdatedEvent
2013-11-29 20:24:52 +01:00
screenWidth int
screenHeight int
2013-06-21 04:02:38 +02:00
}
2013-06-22 16:16:01 +02:00
func New() *Sprites {
2013-11-29 20:24:52 +01:00
return &Sprites{
screenSizeUpdatedCh: make(chan ui.ScreenSizeUpdatedEvent),
2013-11-29 20:24:52 +01:00
}
}
2013-10-22 18:08:01 +02:00
func (game *Sprites) OnScreenSizeUpdated(e ui.ScreenSizeUpdatedEvent) {
2013-12-01 13:23:03 +01:00
go func() {
e := e
game.screenSizeUpdatedCh <- e
}()
2013-06-21 04:02:38 +02:00
}
2013-10-20 08:51:26 +02:00
func (game *Sprites) InitTextures(tf graphics.TextureFactory) {
2013-07-02 18:27:04 +02:00
file, err := os.Open("images/ebiten.png")
2013-06-21 16:16:52 +02:00
if err != nil {
panic(err)
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
panic(err)
}
2013-11-28 18:38:18 +01:00
if game.ebitenTextureId, err = tf.CreateTextureFromImage(img); err != nil {
2013-06-23 09:06:32 +02:00
panic(err)
}
2013-06-21 04:02:38 +02:00
}
2013-11-29 20:24:52 +01:00
func (game *Sprites) Update() {
events:
for {
select {
case e := <-game.screenSizeUpdatedCh:
game.screenWidth, game.screenHeight = e.Width, e.Height
default:
break events
}
}
if game.screenWidth == 0 || game.screenHeight == 0 {
return
}
2013-10-11 15:17:49 +02:00
if game.sprites == nil {
game.sprites = []*Sprite{}
for i := 0; i < 100; i++ {
sprite := newSprite(
2013-11-29 20:24:52 +01:00
game.screenWidth,
game.screenHeight,
2013-10-16 16:20:07 +02:00
ebitenTextureWidth,
ebitenTextureHeight)
2013-10-11 15:17:49 +02:00
game.sprites = append(game.sprites, sprite)
}
}
2013-06-21 16:16:52 +02:00
for _, sprite := range game.sprites {
sprite.Update()
}
2013-06-21 04:02:38 +02:00
}
func (game *Sprites) Draw(g graphics.Canvas) {
2013-10-11 20:20:13 +02:00
g.Fill(128, 128, 255)
// Draw the sprites
2013-06-22 19:16:22 +02:00
locations := make([]graphics.TexturePart, 0, len(game.sprites))
2013-06-21 16:16:52 +02:00
for _, sprite := range game.sprites {
2013-06-22 19:16:22 +02:00
location := graphics.TexturePart{
LocationX: sprite.x,
LocationY: sprite.y,
Source: graphics.Rect{
2013-10-16 16:20:07 +02:00
0, 0, ebitenTextureWidth, ebitenTextureHeight,
},
}
locations = append(locations, location)
2013-06-21 16:16:52 +02:00
}
2013-06-21 19:42:14 +02:00
geometryMatrix := matrix.IdentityGeometry()
2013-10-21 15:18:10 +02:00
g.DrawTextureParts(game.ebitenTextureId, locations,
2013-06-21 19:42:14 +02:00
geometryMatrix, matrix.IdentityColor())
2013-06-21 16:16:52 +02:00
}
func init() {
rand.Seed(time.Now().UnixNano())
2013-06-21 04:02:38 +02:00
}