diff --git a/community/tilemap/.gitignore b/community/tilemap/.gitignore new file mode 100644 index 0000000..912305e --- /dev/null +++ b/community/tilemap/.gitignore @@ -0,0 +1 @@ +tilemap diff --git a/community/tilemap/README.md b/community/tilemap/README.md new file mode 100644 index 0000000..766dd7b --- /dev/null +++ b/community/tilemap/README.md @@ -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) diff --git a/community/tilemap/gameart2d-desert.png b/community/tilemap/gameart2d-desert.png new file mode 100644 index 0000000..89170fd Binary files /dev/null and b/community/tilemap/gameart2d-desert.png differ diff --git a/community/tilemap/gameart2d-desert.tmx b/community/tilemap/gameart2d-desert.tmx new file mode 100644 index 0000000..3102285 --- /dev/null +++ b/community/tilemap/gameart2d-desert.tmx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,25,25,36,37,36,23,23,37,25,23,25,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,40,2,3,4,0,0,0,0,38,39,40,0,0,0,0,0,0,0,0,0,0,0,0,0,2,11,12,8,8,8,8,8,8,8,13,13,20,0,0,0,32,2,11,12,8,8,8,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,37,0,26,0,0,0,0,17,19,0,0,0,0,0,0,0,0,0,0,0,0,0,0,29,30,0,0,0,0,0,0,0,0,0,0,0,0,40,2,3,11,12,8,9,0,0,0,0,0,2,3,4,0,0,0,0,0,0,0,0,0,0,2,11,12,8,8,8,8,8,13,13,13,13,20,0,0,0,0,2,11,12,8,8,8,8,8,9,0,0,0,17,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,1,2,3,3,3,3,4,5,0,0,0,0,0,0,0,0,0,0,0,23,1,2,3,3,3,3,4,0,0,0,33,34,35,0,0,40,2,3,11,12,8,8,8,8,9,0,0,0,0,0,7,8,9,0,33,34,35,0,0,0,0,2,11,12,8,8,8,8,8,8,8,9,0,0,0,0,0,0,2,11,12,8,8,8,8,8,8,8,9,0,0,0,0,33,34,35,33,34,35,33,34,35,0,0,0,0,0,27,0, +27,0,0,37,37,0,0,0,0,2,3,11,12,8,8,8,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 + + + diff --git a/community/tilemap/level.json b/community/tilemap/level.json new file mode 100644 index 0000000..47f06d3 --- /dev/null +++ b/community/tilemap/level.json @@ -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 + } + } + ] +} \ No newline at end of file diff --git a/community/tilemap/main.go b/community/tilemap/main.go new file mode 100644 index 0000000..f9722f7 --- /dev/null +++ b/community/tilemap/main.go @@ -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) + } +} diff --git a/community/tilemap/screenshot.png b/community/tilemap/screenshot.png new file mode 100644 index 0000000..32a28a9 Binary files /dev/null and b/community/tilemap/screenshot.png differ