ebiten/example/game/sprites/sprites.go

137 lines
2.7 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 (
"github.com/hajimehoshi/go.ebiten/graphics"
2013-06-21 16:16:52 +02:00
"github.com/hajimehoshi/go.ebiten/graphics/matrix"
"image"
"image/color"
"math/rand"
"os"
"time"
2013-06-21 04:02:38 +02:00
)
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 {
ebitenTexture graphics.Texture
2013-06-21 16:16:52 +02:00
sprites []*Sprite
2013-06-21 04:02:38 +02:00
}
2013-06-22 16:16:01 +02:00
func New() *Sprites {
2013-06-21 04:02:38 +02:00
return &Sprites{}
}
2013-06-21 16:16:52 +02:00
func (game *Sprites) ScreenWidth() int {
return 256
}
func (game *Sprites) ScreenHeight() int {
return 240
}
2013-06-23 15:38:41 +02:00
func (game *Sprites) Fps() int {
return 60
}
2013-06-21 04:02:38 +02:00
func (game *Sprites) Init(tf graphics.TextureFactory) {
2013-06-21 16:16:52 +02:00
file, err := os.Open("ebiten.png")
if err != nil {
panic(err)
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
panic(err)
}
2013-06-23 09:06:32 +02:00
if game.ebitenTexture, err = tf.NewTextureFromImage(img); err != nil {
panic(err)
}
2013-06-21 16:16:52 +02:00
game.sprites = []*Sprite{}
for i := 0; i < 100; i++ {
2013-06-22 19:16:22 +02:00
sprite := newSprite(
2013-06-21 16:16:52 +02:00
game.ScreenWidth(),
game.ScreenHeight(),
2013-06-21 19:03:16 +02:00
game.ebitenTexture.Width,
game.ebitenTexture.Height)
2013-06-21 16:16:52 +02:00
game.sprites = append(game.sprites, sprite)
}
2013-06-21 04:02:38 +02:00
}
func (game *Sprites) Update() {
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.GraphicsContext, offscreen graphics.Texture) {
2013-06-21 16:16:52 +02:00
g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255})
// Draw the sprites
2013-06-22 19:16:22 +02:00
locations := make([]graphics.TexturePart, 0, len(game.sprites))
texture := game.ebitenTexture
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{
0, 0, texture.Width, texture.Height,
},
}
locations = append(locations, location)
2013-06-21 16:16:52 +02:00
}
2013-06-21 19:42:14 +02:00
geometryMatrix := matrix.IdentityGeometry()
g.DrawTextureParts(texture.ID, locations,
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
}