animated tiles gogo
This commit is contained in:
parent
2bb2635120
commit
def1dc1daa
2 changed files with 77 additions and 21 deletions
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tilemap struct {
|
type Tilemap struct {
|
||||||
Map [][]int
|
Map [][]Tile
|
||||||
Src *ebiten.Image // must be a horizontal tile set
|
Src *ebiten.Image // must be a horizontal tile set
|
||||||
TileSize int
|
TileSize int
|
||||||
GeoM ebiten.GeoM
|
GeoM ebiten.GeoM
|
||||||
|
@ -22,9 +22,55 @@ func (t *Tilemap) Draw(screen *ebiten.Image) {
|
||||||
op.GeoM.Translate(float64(i*t.TileSize), float64(j*t.TileSize))
|
op.GeoM.Translate(float64(i*t.TileSize), float64(j*t.TileSize))
|
||||||
op.GeoM.Concat(t.GeoM)
|
op.GeoM.Concat(t.GeoM)
|
||||||
|
|
||||||
sx := tile * t.TileSize
|
sx := tile.TileIndex() * t.TileSize
|
||||||
src := t.Src.SubImage(image.Rect(sx, 0, sx+t.TileSize, t.TileSize)).(*ebiten.Image)
|
src := t.Src.SubImage(image.Rect(sx, 0, sx+t.TileSize, t.TileSize)).(*ebiten.Image)
|
||||||
screen.DrawImage(src, &op)
|
screen.DrawImage(src, &op)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Tilemap) Update() error {
|
||||||
|
for j := range t.Map {
|
||||||
|
for i := range t.Map[j] {
|
||||||
|
if tile, ok := t.Map[j][i].(Updater); ok {
|
||||||
|
if err := tile.Update(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type Tile interface {
|
||||||
|
TileIndex() int
|
||||||
|
}
|
||||||
|
|
||||||
|
type StaticTile int
|
||||||
|
|
||||||
|
func (s StaticTile) TileIndex() int { return int(s) }
|
||||||
|
|
||||||
|
type AnimatedTile struct {
|
||||||
|
Frame int // if Tile in AnimationDefs, index
|
||||||
|
DurationTicks int // time spent showing this frame
|
||||||
|
AnimDef []TileAnimFrameDef
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AnimatedTile) TileIndex() int { return a.AnimDef[a.Frame].Tile }
|
||||||
|
|
||||||
|
func (a *AnimatedTile) Update() error {
|
||||||
|
a.DurationTicks++
|
||||||
|
if a.DurationTicks >= a.AnimDef[a.Frame].DurationTicks {
|
||||||
|
a.DurationTicks = 0
|
||||||
|
a.Frame++
|
||||||
|
}
|
||||||
|
if a.Frame >= len(a.AnimDef) {
|
||||||
|
a.Frame = 0
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type TileAnimFrameDef struct {
|
||||||
|
Tile int // show this tile
|
||||||
|
DurationTicks int // show it for this long
|
||||||
|
}
|
||||||
|
|
18
main.go
18
main.go
|
@ -29,8 +29,7 @@ func main() {
|
||||||
}
|
}
|
||||||
boxesFile.Close()
|
boxesFile.Close()
|
||||||
|
|
||||||
tiles := &engine.Tilemap{
|
staticTiles := [][]int{
|
||||||
Map: [][]int{
|
|
||||||
{0, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1},
|
{0, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1},
|
||||||
{9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9},
|
{9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9},
|
||||||
{9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9},
|
{9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9},
|
||||||
|
@ -46,7 +45,18 @@ func main() {
|
||||||
{9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9},
|
{9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9},
|
||||||
{9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9},
|
{9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9},
|
||||||
{2, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 3},
|
{2, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 3},
|
||||||
},
|
}
|
||||||
|
|
||||||
|
tiles := make([][]engine.Tile, len(staticTiles))
|
||||||
|
for j := range staticTiles {
|
||||||
|
tiles[j] = make([]engine.Tile, len(staticTiles[j]))
|
||||||
|
for i := range staticTiles[j] {
|
||||||
|
tiles[j][i] = engine.StaticTile(staticTiles[j][i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tilemap := &engine.Tilemap{
|
||||||
|
Map: tiles,
|
||||||
Src: ebiten.NewImageFromImage(boxesPNG),
|
Src: ebiten.NewImageFromImage(boxesPNG),
|
||||||
TileSize: 16,
|
TileSize: 16,
|
||||||
}
|
}
|
||||||
|
@ -55,7 +65,7 @@ func main() {
|
||||||
ScreenHeight: screenHeight,
|
ScreenHeight: screenHeight,
|
||||||
ScreenWidth: screenWidth,
|
ScreenWidth: screenWidth,
|
||||||
Components: []interface{}{
|
Components: []interface{}{
|
||||||
tiles,
|
tilemap,
|
||||||
engine.PerfDisplay{},
|
engine.PerfDisplay{},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue