-
Notifications
You must be signed in to change notification settings - Fork 0
/
tile.go
107 lines (92 loc) · 2.44 KB
/
tile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
)
type Tile struct {
sprite Sprite
health int
solid bool
}
func (t Tile) Sprite() Sprite {
return t.sprite
}
func (t Tile) Solid() bool {
return t.solid
}
/*
* The following types & functions are for automatically parsing the
* data/tiles/tiles.json files and creating the prototypes
*/
type TileCreator struct {
prototypemap map[string]JSON_prototype
idmap map[int]string
spr *SpriteManager
}
func InitTileCreator(spr *SpriteManager) TileCreator {
json_bytes, err := ioutil.ReadFile(GetDataPath() + "tiles/tiles.json")
if err != nil {
fmt.Println("Error while loading JSON Tile Description")
panic(err)
}
var json_data_arr []JSON_prototype
err = json.Unmarshal(json_bytes, &json_data_arr)
if err != nil {
fmt.Println("Error while unmarshalling Tile data")
panic(err)
}
prototypemap := make(map[string]JSON_prototype)
idmap := make(map[int]string)
for _, prototype := range json_data_arr {
if _, contains := prototypemap[prototype.Name]; contains {
errstr := fmt.Sprintf("%s is not a unique tile name, please check tile.json",
prototype.Name)
err := errors.New(errstr)
panic(err)
}
prototypemap[prototype.Name] = prototype
if _, contains := idmap[prototype.Id]; contains {
errstr := fmt.Sprintf("%d is not a unique tile id, please check tile.json",
prototype.Id)
err := errors.New(errstr)
panic(err)
}
idmap[prototype.Id] = prototype.Name
}
return TileCreator{prototypemap, idmap, spr}
}
func (tc TileCreator) tileFromPrototype(prototype JSON_prototype) Tile {
sprite := tc.spr.GetSprite(prototype.Sprite)
return Tile{sprite, prototype.Health, prototype.Solid}
}
/* for programmatically creating levels */
func (tc TileCreator) TileByName(protoname string) Tile {
if prototype, contains := tc.prototypemap[protoname]; contains {
return tc.tileFromPrototype(prototype)
} else {
errstr := fmt.Sprintf("TileCreator: requested prototype %s does not exist\n",
protoname)
err := errors.New(errstr)
panic(err)
}
}
/* for loading tiles from a file */
func (tc TileCreator) TileById(id int) Tile {
if protoname, contains := tc.idmap[id]; contains {
return tc.TileByName(protoname)
} else {
errstr := fmt.Sprintf("TileCreator: requested id %d does not exist\n",
id)
err := errors.New(errstr)
panic(err)
}
}
type JSON_prototype struct {
Health int
Id int
Name string
Sprite string
Solid bool
}