1
1
package gamemap
2
2
3
+ import (
4
+ "encoding/json"
5
+ "io/ioutil"
6
+ "strings"
7
+ )
8
+
9
+ var GameMaps = make (map [string ]GameMap )
10
+
11
+ type GameMapSize struct {
12
+ Width int `json:"width"`
13
+ Height int `json:"height"`
14
+ }
15
+
16
+ type GameMapObject struct {
17
+ NoNomAreas []GameMapNoNomArea `json:"noNomAreas"`
18
+ Walls []GameMapWall `json:"walls"`
19
+ }
20
+
21
+ type GameMapNoNomArea struct {
22
+ StartsAt int `json:"startsAt"`
23
+ EndsAt int `json:"endsAt"`
24
+ }
25
+
26
+ type GameMapWall struct {
27
+ X int `json:"x"`
28
+ Y int `json:"y"`
29
+ Width int `json:"width"`
30
+ Height int `json:"height"`
31
+ Type uint8 `json:"type"`
32
+ }
33
+
3
34
type GameMap struct {
4
- Name string `json:"name"`
5
- MapSize struct {
6
- Width int `json:"width"`
7
- Height int `json:"height"`
8
- } `json:"mapSize"`
9
- Objects []struct {
10
- NoNomAreas []struct {
11
- StartsAt int `json:"startsAt"`
12
- EndsAt int `json:"endsAt"`
13
- } `json:"noNomAreas"`
14
- Walls []struct {
15
- X int `json:"x"`
16
- Y int `json:"y"`
17
- Width int `json:"width"`
18
- Height int `json:"height"`
19
- Type uint8 `json:"type"`
20
- } `json:"walls"`
21
- } `json:"objects"`
22
- }
35
+ Name string `json:"name"`
36
+ MapSize GameMapSize `json:"mapSize"`
37
+ Objects GameMapObject `json:"objects"`
38
+ }
39
+
40
+ func LoadMaps () error {
41
+ files , err := ioutil .ReadDir ("maps" )
42
+ if err != nil {
43
+ return err
44
+ }
45
+
46
+ for _ , file := range files {
47
+ if strings .HasSuffix (file .Name (), ".json" ) { // only allow maps in JSON format
48
+ contents , err := ioutil .ReadFile ("maps/" + file .Name ())
49
+ if err != nil {
50
+ return err
51
+ }
52
+
53
+ var gameMap GameMap
54
+ err = json .Unmarshal (contents , & gameMap )
55
+ if err != nil {
56
+ return err
57
+ }
58
+ GameMaps [gameMap .Name ] = gameMap
59
+ }
60
+ }
61
+
62
+ return nil
63
+ }
0 commit comments