zorldo

Goofing around with Ebiten
git clone git://bsandro.tech/zorldo
Log | Files | Refs

commit 4d2add529204e37537c830ed40b169095a8d1255
parent 9e499efa47b8b2ce1634f4a61dbd78716390077e
Author: bsandro <brian.drosan@gmail.com>
Date:   Sun,  3 Oct 2021 13:41:06 +0300

Game and map and tilesets init from the single entry point and config

Diffstat:
MZorldo.tiled-session | 3+++
Mmain.go | 120++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
Mmap0.json | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mmap0.tmx | 5+++++
4 files changed, 143 insertions(+), 41 deletions(-)

diff --git a/Zorldo.tiled-session b/Zorldo.tiled-session @@ -22,6 +22,9 @@ "map0.tmx#npc_test": { "scaleInEditor": 1 }, + "map0.tmx#overworld": { + "scaleInEditor": 1 + }, "npc_test.tsx": { "scaleInDock": 1, "scaleInEditor": 2 diff --git a/main.go b/main.go @@ -2,26 +2,35 @@ package main import ( "encoding/json" + "errors" "fmt" "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/ebitenutil" "image" _ "image/png" "log" - _ "math" "os" ) const ( - flipHoriz uint32 = 0b10000000000000000000000000000000 - flipVert uint32 = 0b01000000000000000000000000000000 - flipDiag uint32 = 0b00100000000000000000000000000000 - screenW = 1024 - screenH = 768 - tileSize = 16 - tileXCount = 40 + flipHoriz uint32 = 0b10000000000000000000000000000000 + flipVert uint32 = 0b01000000000000000000000000000000 + flipDiag uint32 = 0b00100000000000000000000000000000 + screenW = 1024 + screenH = 768 ) +type Tileset struct { + FirstGid uint32 `json:firstgid` + Image string `json:image` + Name string `json:name` + TileWidth int `json:tilewidth` + TileHeight int `json:tileheight` + Columns int `json:columns` + + Atlas *ebiten.Image +} + type Gamemap struct { Height int `json:height` Width int `json:width` @@ -34,42 +43,65 @@ type Gamemap struct { X int `json:x` Y int `json:y` } `json:layers` - Tilesets []struct { - Firstgid uint32 `json:firstgid` - Source string `json:source` - Name string `json:name` - } `json:tilesets` + Tilesets []*Tileset `json:tilesets` } -type Game struct{} - -var tilemap *ebiten.Image -var gamemap *Gamemap +type Game struct { + Inited bool + Map *Gamemap +} -func init() { +func (t *Tileset) Init() error { var err error - gamemap = new(Gamemap) - tilemap, _, err = ebitenutil.NewImageFromFile("Overworld.png") + t.Atlas, _, err = ebitenutil.NewImageFromFile(t.Image) + return err +} + +func (m *Gamemap) Init(filename string) error { + data, err := os.ReadFile(filename) if err != nil { - log.Fatal(err) + return err } - - data, err := os.ReadFile("map0.json") + err = json.Unmarshal(data, m) if err != nil { - log.Fatal(err) + return err + } + + for _, tileset := range m.Tilesets { + if err := tileset.Init(); err != nil { + return err + } + } + + return nil +} + +func (g *Game) Init() error { + if g.Inited { + return errors.New("Game is already initialized") } - err = json.Unmarshal(data, gamemap) + // init base map + g.Map = &Gamemap{} + var err error = g.Map.Init("map0.json") if err != nil { - log.Fatal(err) + return err } + + return nil } -func findFirstGid(gid uint32) uint32 { - var ret uint32 = 0 - for _, tileset := range gamemap.Tilesets { - if gid >= tileset.Firstgid && ret < tileset.Firstgid { - ret = tileset.Firstgid +func init() { + +} + +func (m *Gamemap) findTileset(gid uint32) *Tileset { + var ret *Tileset = nil + for _, tileset := range m.Tilesets { + if gid >= tileset.FirstGid { + if (ret != nil && ret.FirstGid < tileset.FirstGid) || ret == nil { + ret = tileset + } } } return ret @@ -80,7 +112,7 @@ func (g *Game) Update() error { } func (g *Game) Draw(screen *ebiten.Image) { - for _, layer := range gamemap.Layers { + for _, layer := range g.Map.Layers { if layer.Type == "tilelayer" { for i, t := range layer.Data { if t > 0 { @@ -88,13 +120,15 @@ func (g *Game) Draw(screen *ebiten.Image) { is_flipped_ver := (t & flipVert) >> 30 is_flipped_diag := (t & flipDiag) >> 29 t &= ^(flipHoriz | flipVert | flipDiag) // cleanup flags - first_gid := findFirstGid(t) - if first_gid == 0 { + tileset := g.Map.findTileset(t) + if tileset == nil { log.Fatal("Invalid tile gid:", t) } - t1 := int(t - first_gid) + t_width := tileset.TileWidth + t_height := tileset.TileHeight + t1 := int(t - tileset.FirstGid) op := &ebiten.DrawImageOptions{} - op.GeoM.Translate(-float64(tileSize)/2, -float64(tileSize)/2) + op.GeoM.Translate(-float64(t_width)/2, -float64(t_height)/2) if is_flipped_diag == 1 { ret := ebiten.GeoM{} ret.SetElement(0, 0, op.GeoM.Element(1, 0)) @@ -111,10 +145,10 @@ func (g *Game) Draw(screen *ebiten.Image) { if is_flipped_ver == 1 { op.GeoM.Scale(1, -1) } - op.GeoM.Translate(float64((i%gamemap.Width)*tileSize), float64((i/gamemap.Width)*tileSize)) - sx := (t1 % tileXCount) * tileSize - sy := (t1 / tileXCount) * tileSize - var tile *ebiten.Image = tilemap.SubImage(image.Rect(sx, sy, sx+tileSize, sy+tileSize)).(*ebiten.Image) + op.GeoM.Translate(float64((i%g.Map.Width)*t_width), float64((i/g.Map.Width)*t_height)) + sx := (t1 % tileset.Columns) * t_width + sy := (t1 / tileset.Columns) * t_height + var tile *ebiten.Image = tileset.Atlas.SubImage(image.Rect(sx, sy, sx+t_width, sy+t_height)).(*ebiten.Image) screen.DrawImage(tile, op) } } @@ -128,14 +162,18 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeigh } func main() { + var err error ebiten.SetWindowSize(screenW, screenH) ebiten.SetWindowTitle("Zorldo") ebiten.SetMaxTPS(60) ebiten.SetRunnableOnUnfocused(true) var game *Game = &Game{} + if err = game.Init(); err != nil { + log.Fatal(err) + } - if err := ebiten.RunGame(game); err != nil { + if err = ebiten.RunGame(game); err != nil { log.Fatal(err) } } diff --git a/map0.json b/map0.json @@ -115,6 +115,62 @@ }, { "id":203, + "objectgroup": + { + "draworder":"index", + "id":2, + "name":"", + "objects":[ + { + "height":0, + "id":1, + "name":"", + "polygon":[ + { + "x":0, + "y":0 + }, + { + "x":-3.03000491883915, + "y":1.77078209542548 + }, + { + "x":-3.89572060993605, + "y":4.01377274963109 + }, + { + "x":-3.89572060993605, + "y":11.1756025577964 + }, + { + "x":-2.83325135268077, + "y":13.1824889326119 + }, + { + "x":0.118052139695032, + "y":15.2680767338908 + }, + { + "x":9.52287260206591, + "y":15.2680767338908 + }, + { + "x":9.52287260206591, + "y":-0.157402852926709 + }], + "rotation":0, + "type":"", + "visible":true, + "width":0, + "x":6.33546483030005, + "y":0.511559272011805 + }], + "opacity":1, + "type":"objectgroup", + "visible":true, + "x":0, + "y":0 + }, "properties":[ { "name":"is_movable", diff --git a/map0.tmx b/map0.tmx @@ -18,6 +18,11 @@ <properties> <property name="is_movable" type="bool" value="true"/> </properties> + <objectgroup draworder="index" id="2"> + <object id="1" x="6.33546" y="0.511559"> + <polygon points="0,0 -3.03,1.77078 -3.89572,4.01377 -3.89572,11.1756 -2.83325,13.1825 0.118052,15.2681 9.52287,15.2681 9.52287,-0.157403"/> + </object> + </objectgroup> </tile> <tile id="204" type="log"> <properties>