tilemap example added
This commit is contained in:
parent
070f96227a
commit
1de5ec5af8
|
@ -0,0 +1 @@
|
|||
tilemap
|
|
@ -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 |
|
@ -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,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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,38,39,40,38,39,40,38,39,40,40,0,0,0,0,0,0,0
|
||||
</data>
|
||||
</layer>
|
||||
<layer name="foreground" 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,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,24,0,0,0,0,24,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,
|
||||
31,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,17,18,18,18,18,18,18,18,18,18,18,19,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,21,22,21,22,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
18,19,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,17,19,0,0,0,0,0,0,0,0,0,0,33,34,35,0,0,0,0,0,0,0,0,0,0,0,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8,9,10,0,0,0,0,0,25,0,0,0,0,2,3,11,12,8,8,8,8,9,0,0,0,38,39,40,2,3,11,12,8,8,8,8,8,8,8,9,0,0,0,0,0,7,8,9,0,38,39,40,0,0,2,11,12,8,8,8,8,8,8,8,8,8,9,0,0,32,0,2,11,12,8,8,8,8,8,8,8,8,8,9,0,0,0,31,37,39,37,32,39,21,22,39,37,0,2,3,3,3,3,3,
|
||||
3,3,3,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>
|
|
@ -0,0 +1,215 @@
|
|||
{
|
||||
"name": "desert01",
|
||||
"tiles": [
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 3,
|
||||
"Y": 0
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 3,
|
||||
"Y": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 2,
|
||||
"Y": 1
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 2,
|
||||
"Y": 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 3,
|
||||
"Y": 1
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 3,
|
||||
"Y": 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 4,
|
||||
"Y": 1
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 4,
|
||||
"Y": 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 2,
|
||||
"Y": 7
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 0,
|
||||
"Y": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 2,
|
||||
"Y": 7
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 1,
|
||||
"Y": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 2,
|
||||
"Y": 7
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 2,
|
||||
"Y": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 2,
|
||||
"Y": 7
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 3,
|
||||
"Y": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 2,
|
||||
"Y": 7
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 4,
|
||||
"Y": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 2,
|
||||
"Y": 7
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 5,
|
||||
"Y": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 2,
|
||||
"Y": 7
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 6,
|
||||
"Y": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 2,
|
||||
"Y": 6
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 0,
|
||||
"Y": -1
|
||||
}
|
||||
},
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 2,
|
||||
"Y": 6
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 1,
|
||||
"Y": -1
|
||||
}
|
||||
},
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 2,
|
||||
"Y": 6
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 2,
|
||||
"Y": -1
|
||||
}
|
||||
},
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 2,
|
||||
"Y": 6
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 3,
|
||||
"Y": -1
|
||||
}
|
||||
},
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 2,
|
||||
"Y": 6
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 4,
|
||||
"Y": -1
|
||||
}
|
||||
},
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 2,
|
||||
"Y": 6
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 5,
|
||||
"Y": -1
|
||||
}
|
||||
},
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 2,
|
||||
"Y": 6
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 6,
|
||||
"Y": -1
|
||||
}
|
||||
},
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 1,
|
||||
"Y": 2
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 1,
|
||||
"Y": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 3,
|
||||
"Y": 2
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 4,
|
||||
"Y": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"mapPos": {
|
||||
"X": 4,
|
||||
"Y": 2
|
||||
},
|
||||
"gamePos": {
|
||||
"X": 5,
|
||||
"Y": 1
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,140 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"image/png"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"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 tilemap *tmx.Map
|
||||
var sprites []*pixel.Sprite
|
||||
|
||||
// Tile maps a tilemap coordinate to be drawn at `GamePos`
|
||||
type Tile struct {
|
||||
MapPos pixel.Vec `json:"mapPos"`
|
||||
GamePos pixel.Vec `json:"gamePos"`
|
||||
}
|
||||
|
||||
// Level represents a single game scene composed of tiles
|
||||
// - Tiles []*tile
|
||||
type Level struct {
|
||||
Name string `json:"name"`
|
||||
Tiles []*Tile `json:"tiles"`
|
||||
}
|
||||
|
||||
func gameloop(win *pixelgl.Window, level *Level) {
|
||||
tm := tilemap.Tilesets[0]
|
||||
w := float64(tm.TileWidth)
|
||||
h := float64(tm.TileHeight)
|
||||
sprite := loadSprite(tm.Image.Source)
|
||||
|
||||
var iX, iY float64
|
||||
var fX = float64(tm.TileWidth)
|
||||
var fY = float64(tm.TileHeight)
|
||||
|
||||
for !win.Closed() {
|
||||
win.Clear(clearColor)
|
||||
|
||||
for _, coord := range level.Tiles {
|
||||
iX = coord.MapPos.X * w
|
||||
fX = iX + w
|
||||
iY = coord.MapPos.Y * h
|
||||
fY = iY + h
|
||||
sprite.Set(sprite.Picture(), pixel.R(iX, iY, fX, fY))
|
||||
pos := coord.GamePos.ScaledXY(pixel.V(w, h))
|
||||
sprite.Draw(win, pixel.IM.Moved(pos.Add(pixel.V(0, h))))
|
||||
}
|
||||
win.Update()
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
// Load the level from file
|
||||
level, err := ParseLevelFile("level.json")
|
||||
panicIfErr(err)
|
||||
|
||||
gameloop(win, level)
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
// ParseLevelFile reads a file from the disk at `path`
|
||||
// and unmarshals it to a `*Level`
|
||||
func ParseLevelFile(path string) (*Level, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
bytes, err := ioutil.ReadAll(f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var newLevel Level
|
||||
json.Unmarshal(bytes, &newLevel)
|
||||
return &newLevel, nil
|
||||
}
|
||||
|
||||
// Save serializes a level to a JSON data file
|
||||
func (level *Level) Save(path string) error {
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
b, err := json.MarshalIndent(level, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r := bytes.NewReader(b)
|
||||
_, err = io.Copy(f, r)
|
||||
return err
|
||||
}
|
||||
|
||||
func main() {
|
||||
pixelgl.Run(run)
|
||||
}
|
||||
|
||||
func panicIfErr(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 64 KiB |
Loading…
Reference in New Issue