2013-06-21 04:02:38 +02:00
|
|
|
package game
|
|
|
|
|
|
|
|
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 {
|
|
|
|
texture graphics.Texture
|
|
|
|
ch chan bool
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
vx int
|
|
|
|
vy int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSprite(screenWidth, screenHeight int,
|
|
|
|
texture graphics.Texture) *Sprite {
|
|
|
|
maxX := screenWidth - texture.Width
|
|
|
|
maxY := screenHeight - texture.Height
|
|
|
|
sprite := &Sprite{
|
|
|
|
texture: texture,
|
|
|
|
ch: make(chan bool),
|
|
|
|
x: rand.Intn(maxX),
|
|
|
|
y: rand.Intn(maxY),
|
|
|
|
vx: rand.Intn(2)*2 - 1,
|
|
|
|
vy: rand.Intn(2)*2 - 1,
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return sprite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sprite *Sprite) Update() {
|
|
|
|
sprite.ch <- true
|
|
|
|
<-sprite.ch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sprite *Sprite) Draw(g graphics.GraphicsContext) {
|
|
|
|
geometryMatrix := matrix.IdentityGeometry()
|
|
|
|
geometryMatrix.Translate(float64(sprite.x), float64(sprite.y))
|
|
|
|
|
|
|
|
g.DrawTexture(sprite.texture.ID,
|
|
|
|
0, 0, sprite.texture.Width, sprite.texture.Height,
|
|
|
|
geometryMatrix,
|
|
|
|
matrix.IdentityColor())
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func NewSprites() *Sprites {
|
|
|
|
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-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)
|
|
|
|
}
|
|
|
|
game.ebitenTexture = tf.NewTextureFromImage(img)
|
|
|
|
game.sprites = []*Sprite{}
|
|
|
|
for i := 0; i < 200; i++ {
|
|
|
|
sprite := NewSprite(
|
|
|
|
game.ScreenWidth(),
|
|
|
|
game.ScreenHeight(),
|
|
|
|
game.ebitenTexture)
|
|
|
|
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})
|
|
|
|
for _, sprite := range game.sprites {
|
|
|
|
sprite.Draw(g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
2013-06-21 04:02:38 +02:00
|
|
|
}
|