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"
|
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics"
|
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
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-10-16 17:14:32 +02:00
|
|
|
ebitenTextureID graphics.TextureID
|
2013-10-17 17:45:12 +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{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Sprites) Init(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-10-16 17:14:32 +02:00
|
|
|
if game.ebitenTextureID, err = tf.NewTextureFromImage(img); err != nil {
|
2013-06-23 09:06:32 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
2013-06-21 04:02:38 +02:00
|
|
|
}
|
|
|
|
|
2013-07-05 14:02:17 +02:00
|
|
|
func (game *Sprites) Update(context ebiten.GameContext) {
|
2013-10-11 15:17:49 +02:00
|
|
|
if game.sprites == nil {
|
|
|
|
game.sprites = []*Sprite{}
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
sprite := newSprite(
|
|
|
|
context.ScreenWidth(),
|
|
|
|
context.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
|
|
|
}
|
|
|
|
|
2013-07-04 16:57:53 +02:00
|
|
|
func (game *Sprites) Draw(g graphics.Context) {
|
2013-10-11 20:20:13 +02:00
|
|
|
g.Fill(128, 128, 255)
|
2013-06-21 17:50:55 +02:00
|
|
|
|
|
|
|
// 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{
|
2013-06-21 18:52:10 +02:00
|
|
|
LocationX: sprite.x,
|
|
|
|
LocationY: sprite.y,
|
|
|
|
Source: graphics.Rect{
|
2013-10-16 16:20:07 +02:00
|
|
|
0, 0, ebitenTextureWidth, ebitenTextureHeight,
|
2013-06-21 17:50:55 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
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-16 17:14:32 +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
|
|
|
}
|