Merge pull request #13 from zcking/feature/tilemap-example

tilemap example added
This commit is contained in:
Michal Štrba 2019-02-04 01:05:29 +01:00 committed by GitHub
commit d8a6e4b962
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 217 additions and 0 deletions

1
community/tilemap/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
tilemap

View File

@ -0,0 +1,11 @@
# Pixel Tiles Example
This is a very simple example of how you might load a tilemap generated with [Tiled](https://www.mapeditor.org/) (i.e. a .tmx file),
in a game with [Pixel](https://github.com/faiface/pixel) for Go.
The tilemap used in this example is humbly borrowed from James Bowman's repo here: [https://github.com/jamesbowman/tiled-maps](https://github.com/jamesbowman/tiled-maps).
## Preview
![Screenshot of Tiles Window](./screenshot.png)
## Tilemap
![Tilemap](./gameart2d-desert.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 196 KiB

View File

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="120" height="18" tilewidth="128" tileheight="128" nextobjectid="1">
<tileset firstgid="1" name="gameart2d-desert" tilewidth="128" tileheight="128" tilecount="40" columns="5">
<image source="gameart2d-desert.png" width="640" height="1024"/>
<tile id="2">
<properties>
<property name="type" type="int" value="1"/>
</properties>
</tile>
<tile id="7">
<properties>
<property name="type" type="int" value="1"/>
</properties>
</tile>
</tileset>
<layer name="background" width="120" height="18">
<data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,3,3,3,3,3,11,12,8,8,8,8,8,8,8,14,15,3,3,3,3,3,3,3,3,3,11,12,8,8,8,8,8,8,8,9,0,0,2,3,3,11,12,8,8,8,8,8,8,8,8,8,8,9,0,0,0,0,0,7,8,14,15,3,3,3,3,11,12,8,8,8,8,8,8,8,8,8,8,8,14,15,3,3,11,12,8,8,8,8,8,8,8,8,8,8,8,9,0,0,0,17,18,18,18,18,18,18,18,18,19,0,7,8,8,8,8,8
</data>
</layer>
</map>

145
community/tilemap/main.go Normal file
View File

@ -0,0 +1,145 @@
package main
import (
"fmt"
"image/png"
"math"
"os"
"time"
"golang.org/x/image/colornames"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"github.com/salviati/go-tmx/tmx"
)
var clearColor = colornames.Skyblue
var sprites []*pixel.Sprite
func gameloop(win *pixelgl.Window, tilemap *tmx.Map) {
// Load the sprites
sprites := make(map[string]*pixel.Sprite)
for _, tileset := range tilemap.Tilesets {
if _, alreadyLoaded := sprites[tileset.Image.Source]; !alreadyLoaded {
sprites[tileset.Image.Source] = loadSprite(tileset.Image.Source)
}
}
var (
camPos = pixel.ZV
camSpeed = 1000.0
camZoom = 0.2
camZoomSpeed = 1.2
)
last := time.Now()
for !win.Closed() {
dt := time.Since(last).Seconds()
last = time.Now()
// Camera movement
cam := pixel.IM.Scaled(camPos, camZoom).Moved(win.Bounds().Center().Sub(camPos))
win.SetMatrix(cam)
if win.Pressed(pixelgl.KeyLeft) {
camPos.X -= camSpeed * dt
}
if win.Pressed(pixelgl.KeyRight) {
camPos.X += camSpeed * dt
}
if win.Pressed(pixelgl.KeyDown) {
camPos.Y -= camSpeed * dt
}
if win.Pressed(pixelgl.KeyUp) {
camPos.Y += camSpeed * dt
}
camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y)
win.Clear(clearColor)
// Draw tiles
for _, layer := range tilemap.Layers {
for tileIndex, tile := range layer.DecodedTiles {
ts := layer.Tileset
tID := int(tile.ID)
if tID == 0 {
// Tile ID 0 means blank, skip it.
continue
}
// Calculate the framing for the tile within its tileset's source image
numRows := ts.Tilecount / ts.Columns
x, y := tileIDToCoord(tID, ts.Columns, numRows)
gamePos := indexToGamePos(tileIndex, tilemap.Width, tilemap.Height)
iX := float64(x) * float64(ts.TileWidth)
fX := iX + float64(ts.TileWidth)
iY := float64(y) * float64(ts.TileHeight)
fY := iY + float64(ts.TileHeight)
sprite := sprites[ts.Image.Source]
sprite.Set(sprite.Picture(), pixel.R(iX, iY, fX, fY))
pos := gamePos.ScaledXY(pixel.V(float64(ts.TileWidth), float64(ts.TileHeight)))
sprite.Draw(win, pixel.IM.Moved(pos))
}
}
win.Update()
}
}
func tileIDToCoord(tID int, numColumns int, numRows int) (x int, y int) {
x = tID % numColumns
y = numRows - (tID / numColumns) - 1
return
}
func indexToGamePos(idx int, width int, height int) pixel.Vec {
gamePos := pixel.V(
float64(idx%width)-1,
float64(height)-float64(idx/width),
)
return gamePos
}
func run() {
// Create the window with OpenGL
cfg := pixelgl.WindowConfig{
Title: "Pixel Tilemaps",
Bounds: pixel.R(0, 0, 800, 600),
VSync: true,
}
win, err := pixelgl.NewWindow(cfg)
panicIfErr(err)
// Initialize art assets (i.e. the tilemap)
tilemap, err := tmx.ReadFile("gameart2d-desert.tmx")
panicIfErr(err)
fmt.Println("use WASD to move camera around")
gameloop(win, tilemap)
}
func loadSprite(path string) *pixel.Sprite {
f, err := os.Open(path)
panicIfErr(err)
img, err := png.Decode(f)
panicIfErr(err)
pd := pixel.PictureDataFromImage(img)
return pixel.NewSprite(pd, pd.Bounds())
}
func main() {
pixelgl.Run(run)
}
func panicIfErr(err error) {
if err != nil {
panic(err)
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB