2013-07-01 15:25:41 +02:00
|
|
|
// Copyright 2013 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
// SOFTWARE.
|
|
|
|
|
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-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-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{}
|
2013-06-23 14:51:23 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-07-01 17:42:47 +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
|
|
|
}
|
|
|
|
|
2013-06-27 17:33:03 +02:00
|
|
|
func (game *Sprites) Draw(g graphics.Context, offscreen graphics.Texture) {
|
2013-06-21 16:16:52 +02:00
|
|
|
g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 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 17:50:55 +02:00
|
|
|
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{
|
2013-06-21 18:52:10 +02:00
|
|
|
LocationX: sprite.x,
|
|
|
|
LocationY: sprite.y,
|
|
|
|
Source: graphics.Rect{
|
|
|
|
0, 0, texture.Width, texture.Height,
|
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()
|
|
|
|
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
|
|
|
}
|