/
gameobject.go
282 lines (217 loc) · 4.94 KB
/
gameobject.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Core game object interfaces
package game
import (
"bytes"
"encoding/gob"
"fmt"
"github.com/nsf/termbox-go"
"github.com/nsf/tulib"
// uuid "github.com/nu7hatch/gouuid"
"image"
//"log"
"sync"
"time"
)
var (
idchan chan int
)
func init() {
gob.Register(&GameObject{})
gob.Register(&GameObjectMap{})
idchan = make(chan int)
go func() {
var id int
id = 0
for {
id++
idchan <- id
}
}()
}
// TODO: remove need for this when drawing terrain with camera.Draw
type Renderable interface {
// Draw this object on the buffer at pos
Draw(buf *tulib.Buffer, pos image.Point)
}
type Object interface {
// Setter/getter for ID
SetID(id int)
GetID() int
SetName(name string)
GetName() string
// Setter/getter for position
SetPos(x, y int) bool
GetPos() (x, y int)
// Setter/getter for the glyph
SetGlyph(termbox.Cell)
GetGlyph() termbox.Cell
// Setter/getter for tags
SetTag(tag string, val bool) bool // returns old value
GetTag(tag string) bool
GetSubObjects() *GameObjectMap
AddSubObject(obj Object)
RemoveSubObject(obj Object) Object
// update this object with delta
Update(delta time.Duration)
Renderable
}
type GameObject struct {
ID int // game object id
ItemID int // id if from game/data/itemdb.lua, else -1
Name string // object name
Pos image.Point // object world coordinates
Glyph termbox.Cell // character for this object
Tags map[string]bool // object tags
SubObjects *GameObjectMap // objects associated with this one
m sync.Mutex // lock, ew
}
func NewGameObject(name string) Object {
gob := &GameObject{
ID: <-idchan,
ItemID: -1,
Name: name,
Pos: image.ZP,
Glyph: termbox.Cell{'¡', termbox.ColorRed, termbox.ColorDefault},
Tags: make(map[string]bool),
SubObjects: NewGameObjectMap(),
}
return gob
}
func (gob GameObject) String() string {
gob.m.Lock()
defer gob.m.Unlock()
var buf bytes.Buffer
for key, value := range gob.Tags {
buf.WriteString(fmt.Sprintf(" %s:%t", key, value))
}
return fmt.Sprintf("%s (%c) %s %d tags:%s", gob.Name, gob.Glyph.Ch,
gob.Pos, gob.ID, buf.String())
}
func (gob *GameObject) SetID(id int) {
gob.m.Lock()
defer gob.m.Unlock()
gob.ID = id
}
func (gob *GameObject) GetID() int {
gob.m.Lock()
defer gob.m.Unlock()
return gob.ID
}
func (gob *GameObject) SetName(name string) {
gob.m.Lock()
defer gob.m.Unlock()
gob.Name = name
}
func (gob *GameObject) GetName() string {
gob.m.Lock()
defer gob.m.Unlock()
return gob.Name
}
func (gob *GameObject) SetPos(x, y int) bool {
gob.m.Lock()
defer gob.m.Unlock()
gob.Pos.X = x
gob.Pos.Y = y
return true
}
func (gob *GameObject) GetPos() (x, y int) {
gob.m.Lock()
defer gob.m.Unlock()
return gob.Pos.X, gob.Pos.Y
}
func (gob *GameObject) SetGlyph(glyph termbox.Cell) {
gob.m.Lock()
defer gob.m.Unlock()
gob.Glyph = glyph
}
func (gob *GameObject) GetGlyph() termbox.Cell {
gob.m.Lock()
defer gob.m.Unlock()
return gob.Glyph
}
func (gob *GameObject) SetTag(tag string, val bool) (old bool) {
gob.m.Lock()
defer gob.m.Unlock()
old = gob.Tags[tag]
gob.Tags[tag] = val
return
}
func (gob *GameObject) GetTag(tag string) bool {
gob.m.Lock()
defer gob.m.Unlock()
return gob.Tags[tag]
}
func (gob *GameObject) GetSubObjects() *GameObjectMap {
gob.m.Lock()
defer gob.m.Unlock()
return gob.SubObjects
}
func (gob *GameObject) AddSubObject(obj Object) {
gob.SubObjects.Add(obj)
}
func (gob *GameObject) RemoveSubObject(obj Object) Object {
gob.SubObjects.RemoveObject(obj)
return obj
}
func (gob *GameObject) Update(delta time.Duration) {
}
func (gob *GameObject) Draw(buf *tulib.Buffer, pos image.Point) {
buf.Set(pos.X, pos.Y, gob.Glyph)
}
// handy interface for a collection of game objects
type GameObjectMap struct {
Objs map[int]Object
m sync.Mutex
}
func NewGameObjectMap() *GameObjectMap {
g := GameObjectMap{Objs: make(map[int]Object)}
return &g
}
func (gom *GameObjectMap) Add(obj Object) {
// make sure we don't double insert
gom.m.Lock()
if _, ok := gom.Objs[obj.GetID()]; !ok {
gom.Objs[obj.GetID()] = obj
}
gom.m.Unlock()
}
func (gom *GameObjectMap) RemoveObject(obj Object) {
gom.m.Lock()
delete(gom.Objs, obj.GetID())
gom.m.Unlock()
}
func (gom *GameObjectMap) FindObjectByID(id int) Object {
gom.m.Lock()
defer gom.m.Unlock()
o, ok := gom.Objs[id]
if !ok {
return nil
}
return o
}
func (gom *GameObjectMap) Chan() <-chan Object {
gom.m.Lock()
defer gom.m.Unlock()
ch := make(chan Object, len(gom.Objs))
for _, o := range gom.Objs {
ch <- o
}
close(ch)
return ch
}
// return a slice containing the objects
// XXX: crappy hack so lua can iterate the contents
func (gom *GameObjectMap) GetSlice() []Object {
gom.m.Lock()
defer gom.m.Unlock()
r := make([]Object, 1)
for _, o := range gom.Objs {
r = append(r, o)
}
return r
}
func SamePos(ob1, ob2 Object) bool {
x1, y1 := ob1.GetPos()
x2, y2 := ob2.GetPos()
return x1 == x2 && y1 == y2
}