-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
executable file
·189 lines (168 loc) · 3.7 KB
/
Copy pathtypes.go
File metadata and controls
executable file
·189 lines (168 loc) · 3.7 KB
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
package main
import (
"encoding/json"
"sync"
)
type (
// Server info
Server struct {
Mode int `yaml:"mode"`
Host string `yaml:"host"`
Port string `yaml:"port"`
Debug bool `yaml:"debug"`
StaticDir string `yaml:"static-directory"`
}
// Game data
GameData struct {
sync.RWMutex
Globals map[string]map[string]Value
Users map[string]*UserValue
Objects map[string]Object
}
// Generic value
Value struct {
Value string `yaml:"value"`
Type string `yaml:"type"`
}
// User value
UserValue struct {
X float32
Y float32
Z float32
Orientation float32
Other map[string]interface{}
}
// Message container
Message struct {
Type int `yaml:"type"`
ID string `yaml:"id"`
Other map[string]interface{} `yaml:"other"`
Coordinates Coordinates `yaml:"coordinates"`
Orientation float32 `yaml:"orientation"`
}
// Store player coordinates (2d)
Coordinates struct {
X float32 `yaml:"x"`
Y float32 `yaml:"y"`
Z float32 `yaml:"z"`
}
// Store objects
Object struct {
Coordinates Coordinates `yaml:"coordinates"`
Other map[string]interface{} `yaml:"other"`
}
// Test player info
TestPlayer struct {
Starting Coordinates `yaml:"starting"`
Movements movement `yaml:"movement"`
Reference *UserValue
}
// Movement for test players
movement struct {
Active bool `yaml:"active"`
Actions []action `yaml:"actions"`
}
// Actions for test players' movement
action struct {
From float32 `yaml:"from"`
To float32 `yaml:"to"`
Axis int `yaml:"axis"`
Speed float32 `yaml:"speed"`
}
)
func (g *GameData) SetGlobals(globals map[string]map[string]Value) {
g.Lock()
defer g.Unlock()
g.Globals = globals
}
func (g *GameData) SetUserData(id string, x, y, z, orientation float32, other map[string]interface{}) {
g.Lock()
defer g.Unlock()
g.Users[id] = &UserValue{
X: x,
Y: y,
Z: z,
Orientation: orientation,
Other: other,
}
}
func (g *GameData) SetObject(id string, x, y, z float32, other map[string]interface{}) {
g.Lock()
defer g.Unlock()
g.Objects[id] = Object{
Coordinates: Coordinates{
X: x,
Y: y,
Z: z,
},
Other: other,
}
}
func (g *GameData) CreateTestUser(id string, x, y, z float32) *UserValue {
g.Lock()
defer g.Unlock()
g.Users[id] = &UserValue{
X: x,
Y: y,
Z: z,
Orientation: 0,
Other: make(map[string]interface{}),
}
return g.Users[id]
}
func (g *GameData) DeleteUserData(id string) {
g.Lock()
defer g.Unlock()
delete(g.Users, id)
}
func (g *GameData) DeleteObject(id string) {
g.Lock()
defer g.Unlock()
delete(g.Objects, id)
}
func (g *GameData) GetAllData() ([]byte, error) {
g.Lock()
defer g.Unlock()
j, err := json.Marshal(struct {
Globals map[string]map[string]Value
Users map[string]*UserValue
Objects map[string]Object
}{
Globals: g.Globals,
Users: g.Users,
Objects: g.Objects,
})
if err != nil {
return nil, err
}
return j, nil
}
func (u UserValue) equals (u2 UserValue) bool {
if u.X != u2.X {return false}
if u.Y != u2.Y {return false}
if u.Z != u2.Z {return false}
if len(u.Other) != len(u2.Other) {return false}
return true
}
func (t *TestPlayer) Move (id int) {
for i, a := range t.Movements.Actions {
switch a.Axis {
case 0:
t.Reference.X += a.Speed
break
case 1:
t.Reference.Y += a.Speed
break
case 2:
t.Reference.Z += a.Speed
break
}
if (t.Reference.X <= a.From || t.Reference.X >= a.To) && a.Axis == 0 {
t.Movements.Actions[i].Speed = -a.Speed
} else if (t.Reference.Y <= a.From || t.Reference.Y >= a.To) && a.Axis == 1 {
t.Movements.Actions[i].Speed = -a.Speed
} else if (t.Reference.Z <= a.From || t.Reference.Z >= a.To) && a.Axis == 2 {
t.Movements.Actions[i].Speed = -a.Speed
}
}
}