ebiten/example/game/sprites/sprites.go

148 lines
3.6 KiB
Go
Raw Normal View History

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 (
2013-07-04 16:57:53 +02:00
"github.com/hajimehoshi/go.ebiten"
2013-06-21 04:02:38 +02:00
"github.com/hajimehoshi/go.ebiten/graphics"
2013-06-21 16:16:52 +02:00
"github.com/hajimehoshi/go.ebiten/graphics/matrix"
"image"
"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{}
}
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 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(),
game.ebitenTexture.Width(),
game.ebitenTexture.Height())
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)
// 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{
2013-07-12 18:36:01 +02:00
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()
2013-07-12 18:36:01 +02:00
g.DrawTextureParts(texture.ID(), 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
}