-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.go
177 lines (164 loc) · 3.96 KB
/
object.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
package game
import (
"encoding/json"
"errors"
"github.com/kettek/morogue/id"
"github.com/vmihailenco/msgpack/v5"
)
// ObjectType is a key that represents an object's type. This is used
// for safely marshalling/unmarshalling the Object interface.
type ObjectType string
// Object is an interface intended for location objects. This includes
// players, items, and enemies.
type Object interface {
Type() ObjectType
SetWID(id.WID) // SetWID should only be called by the world.
GetWID() id.WID
SetPosition(Position)
GetPosition() Position
GetArchetypeID() id.UUID
SetArchetype(a Archetype)
GetArchetype() Archetype
GetContainerWID() id.WID
SetContainerWID(id.WID)
}
func CreateObjectFromArchetype(a Archetype) Object {
switch a := a.(type) {
case CharacterArchetype:
return &Character{
Objectable: Objectable{
ArchetypeID: a.GetID(),
Archetype: a,
},
Slots: a.Slots.ToMap(),
}
case WeaponArchetype:
return &Weapon{
Objectable: Objectable{
ArchetypeID: a.GetID(),
Archetype: a,
},
}
case ArmorArchetype:
return &Armor{
Objectable: Objectable{
ArchetypeID: a.GetID(),
Archetype: a,
},
}
case DoorArchetype:
return &Door{
Objectable: Objectable{
ArchetypeID: a.GetID(),
Archetype: a,
},
Blockable: Blockable{
BlockType: a.BlockType,
},
}
case FoodArchetype:
return &Food{
Objectable: Objectable{
ArchetypeID: a.GetID(),
Archetype: a,
},
Edible: Edible{
Calories: a.Calories,
CurrentCalories: a.Calories,
},
}
}
return nil
}
type RawMessage []byte
func (m RawMessage) MarshalMsgpack() ([]byte, error) {
return msgpack.Marshal((msgpack.RawMessage)(m))
}
func (m *RawMessage) UnmarshalMsgpack(b []byte) error {
return msgpack.Unmarshal(b, (*msgpack.RawMessage)(m))
}
func (m RawMessage) MarshalJSON() ([]byte, error) {
return json.Marshal((json.RawMessage)(m))
}
func (m *RawMessage) UnmarshalJSON(b []byte) error {
return json.Unmarshal(b, (*json.RawMessage)(m))
}
// ObjectWrapper wraps an Object interface for msgpack marshal and unmarshal.
type ObjectWrapper struct {
Type ObjectType `msgpack:"t"`
Data RawMessage `msgpack:"d"`
}
// Object returns the wrapped Object. To add additional types, they must be
// handled here and ObjectJSON.
func (ow ObjectWrapper) Object() (Object, error) {
switch ow.Type {
case (Item{}).Type():
var o *Item
if err := msgpack.Unmarshal(ow.Data, &o); err != nil {
return nil, err
}
return o, nil
case (Character{}).Type():
var c *Character
if err := msgpack.Unmarshal(ow.Data, &c); err != nil {
return nil, err
}
return c, nil
case (Weapon{}).Type():
var w *Weapon
if err := msgpack.Unmarshal(ow.Data, &w); err != nil {
return nil, err
}
return w, nil
case (Armor{}).Type():
var a *Armor
if err := msgpack.Unmarshal(ow.Data, &a); err != nil {
return nil, err
}
return a, nil
case (Food{}).Type():
var f *Food
if err := msgpack.Unmarshal(ow.Data, &f); err != nil {
return nil, err
}
return f, nil
}
return nil, errors.New("unknown object type: " + string(ow.Type))
}
// ObjectJSON returns the wrapped Object. To add additional types, they must be
// handled here and Object.
func (ow ObjectWrapper) ObjectJSON() (Object, error) {
switch ow.Type {
case (Item{}).Type():
var o *Item
if err := json.Unmarshal(ow.Data, &o); err != nil {
return nil, err
}
return o, nil
case (Character{}).Type():
var c *Character
if err := json.Unmarshal(ow.Data, &c); err != nil {
return nil, err
}
return c, nil
case (Weapon{}).Type():
var w *Weapon
if err := json.Unmarshal(ow.Data, &w); err != nil {
return nil, err
}
return w, nil
case (Armor{}).Type():
var a *Armor
if err := json.Unmarshal(ow.Data, &a); err != nil {
return nil, err
}
return a, nil
case (Food{}).Type():
var f *Food
if err := json.Unmarshal(ow.Data, &f); err != nil {
return nil, err
}
return f, nil
}
return nil, errors.New("unknown object type: " + string(ow.Type))
}