/
gameobject.go
283 lines (253 loc) · 7.21 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
283
package shootbase
import (
"github.com/kasworld/go4game"
"time"
)
func (o GameObject) String() string {
return o.SPObj.String()
}
type GameObject struct {
*SPObj
colcount int64
enabled bool
startTime time.Time
endTime time.Time
accelVector go4game.Vector3D
targetObjID int64
targetTeamID int64
lastMoveTime time.Time
moveByTimeFn GameObjectActFn
borderActionFn GameObjectActFn
collisionActionFn GameObjectActFn
expireActionFn GameObjectActFn
}
func NewGameObject(teamID int64) *GameObject {
o := GameObject{
SPObj: &SPObj{
ID: <-go4game.IdGenCh,
TeamID: teamID,
},
enabled: true,
startTime: time.Now(),
lastMoveTime: time.Now(),
moveByTimeFn: moveByTimeFn_accel,
borderActionFn: borderActionFn_Bounce,
collisionActionFn: collisionFn_default,
expireActionFn: expireFn_default,
}
return &o
}
func (o *GameObject) ToOctreeObj() go4game.OctreeObjI {
if o == nil {
return nil
}
rtn := *o.SPObj
return &rtn
}
func (o *GameObject) ToSPObj() *SPObj {
if o == nil {
return nil
}
rtn := *o.SPObj
return &rtn
}
type ActionFnEnvInfo struct {
frameTime time.Time
world *World
o *GameObject
clist go4game.IDList
}
func (e *ActionFnEnvInfo) doPartMainObj(oo go4game.OctreeObjI) bool {
v := oo.(*SPObj)
e.o.colcount++
if (e.o.TeamID != v.TeamID) && e.o.SPObj.IsCollision(v) {
e.clist = append(e.clist, v.TeamID)
}
return false
}
func (e *ActionFnEnvInfo) doPartOtherObj(oo go4game.OctreeObjI) bool {
v := oo.(*SPObj)
e.o.colcount++
if (e.o.TeamID != v.TeamID) && e.o.SPObj.IsCollision(v) {
return true
}
return false
}
func (o *GameObject) ActByTime(world *World, t time.Time) go4game.IDList {
defer func() {
o.lastMoveTime = t
}()
envInfo := ActionFnEnvInfo{
frameTime: t,
world: world,
o: o,
clist: make(go4game.IDList, 0),
}
// check expire
if !o.endTime.IsZero() && o.endTime.Before(t) {
if o.expireActionFn != nil {
ok := o.expireActionFn(o, &envInfo)
if ok != true {
return envInfo.clist
}
}
}
var isCollision bool
hr := go4game.NewHyperRectByCR(o.PosVector, GameConst.Radius[o.ObjType]+GameConst.MaxObjectRadius)
if o.ObjType == GameObjMain {
envInfo.world.octree.QueryByHyperRect(envInfo.doPartMainObj, hr)
} else {
isCollision = envInfo.world.octree.QueryByHyperRect(envInfo.doPartOtherObj, hr)
}
if isCollision || len(envInfo.clist) > 0 {
if o.collisionActionFn != nil {
ok := o.collisionActionFn(o, &envInfo)
if ok != true {
return envInfo.clist
}
}
}
if o.moveByTimeFn != nil {
// change PosVector by movevector
ok := o.moveByTimeFn(o, &envInfo)
if ok != true {
return envInfo.clist
}
}
if o.borderActionFn != nil {
// check wall action ( wrap, bounce )
ok := o.borderActionFn(o, &envInfo)
if ok != true {
return envInfo.clist
}
}
return envInfo.clist
}
type GameObjectActFn func(m *GameObject, envInfo *ActionFnEnvInfo) bool
func expireFn_default(m *GameObject, envInfo *ActionFnEnvInfo) bool {
m.enabled = false
return false
}
func collisionFn_default(m *GameObject, envInfo *ActionFnEnvInfo) bool {
m.enabled = false
return false
}
func moveByTimeFn_none(m *GameObject, envInfo *ActionFnEnvInfo) bool {
return true
}
func moveByTimeFn_accel(m *GameObject, envInfo *ActionFnEnvInfo) bool {
dur := float64(envInfo.frameTime.Sub(m.lastMoveTime)) / float64(time.Second)
m.MoveVector = m.MoveVector.Add(m.accelVector.Imul(dur))
if m.MoveVector.Abs() > GameConst.MoveLimit[m.ObjType] {
m.MoveVector = m.MoveVector.NormalizedTo(GameConst.MoveLimit[m.ObjType])
}
m.PosVector = m.PosVector.Add(m.MoveVector.Imul(dur))
return true
}
func moveByTimeFn_home(m *GameObject, envInfo *ActionFnEnvInfo) bool {
dur := float64(envInfo.frameTime.Sub(m.lastMoveTime)) / float64(time.Second)
m.accelVector = GameConst.WorldCube.RandVector()
m.MoveVector = m.MoveVector.Add(m.accelVector.Imul(dur))
if m.MoveVector.Abs() > GameConst.MoveLimit[m.ObjType] {
m.MoveVector = m.MoveVector.NormalizedTo(GameConst.MoveLimit[m.ObjType])
}
m.PosVector = m.PosVector.Add(m.MoveVector.Imul(dur))
return true
}
func moveByTimeFn_shield(m *GameObject, envInfo *ActionFnEnvInfo) bool {
mo := envInfo.world.Teams[m.TeamID].findMainObj()
if mo == nil {
return false
}
dur := float64(envInfo.frameTime.Sub(m.startTime)) / float64(time.Second)
//axis := &go4game.Vector3D{0, math.Copysign(20, m.MoveVector[0]), 0}
axis := mo.MoveVector // .NormalizedTo(20)
//p := m.accelVector.NormalizedTo(20)
p := mo.MoveVector.Cross(m.MoveVector).NormalizedTo(20)
m.PosVector = mo.PosVector.Add(p.RotateAround(axis, dur+m.accelVector.Abs()))
return true
}
func moveByTimeFn_clock(m *GameObject, envInfo *ActionFnEnvInfo) bool {
dur := float64(envInfo.frameTime.Sub(m.startTime)) / float64(time.Second)
p := m.MoveVector.Cross(m.accelVector)
//m.PosVector = m.MoveVector.NormalizedTo(30).RotateAround(p, dur*5)
m.PosVector = m.MoveVector.RotateAround(p, dur)
return true
}
func moveByTimeFn_homming(m *GameObject, envInfo *ActionFnEnvInfo) bool {
// how to other team obj pos? without panic
targetTeam := envInfo.world.Teams[m.targetTeamID]
if targetTeam == nil {
m.enabled = false
return false
}
targetobj := targetTeam.GameObjs[m.targetObjID]
if targetobj == nil {
m.enabled = false
return false
}
m.accelVector = targetobj.PosVector.Sub(m.PosVector).NormalizedTo(GameConst.MoveLimit[m.ObjType])
return moveByTimeFn_accel(m, envInfo)
}
func borderActionFn_Bounce(m *GameObject, envInfo *ActionFnEnvInfo) bool {
for i := range m.PosVector {
if m.PosVector[i] > GameConst.WorldCube.Max[i] {
m.accelVector[i] = -m.accelVector[i]
m.MoveVector[i] = -m.MoveVector[i]
m.PosVector[i] = GameConst.WorldCube.Max[i]
}
if m.PosVector[i] < GameConst.WorldCube.Min[i] {
m.accelVector[i] = -m.accelVector[i]
m.MoveVector[i] = -m.MoveVector[i]
m.PosVector[i] = GameConst.WorldCube.Min[i]
}
}
return true
}
func borderActionFn_Block(m *GameObject, envInfo *ActionFnEnvInfo) bool {
for i := range m.PosVector {
if m.PosVector[i] > GameConst.WorldCube.Max[i] {
m.accelVector[i] = 0
m.MoveVector[i] = 0
m.PosVector[i] = GameConst.WorldCube.Max[i]
}
if m.PosVector[i] < GameConst.WorldCube.Min[i] {
m.accelVector[i] = 0
m.MoveVector[i] = 0
m.PosVector[i] = GameConst.WorldCube.Min[i]
}
}
return true
}
func borderActionFn_Wrap(m *GameObject, envInfo *ActionFnEnvInfo) bool {
for i := range m.PosVector {
if m.PosVector[i] > GameConst.WorldCube.Max[i] {
m.PosVector[i] = GameConst.WorldCube.Min[i]
}
if m.PosVector[i] < GameConst.WorldCube.Min[i] {
m.PosVector[i] = GameConst.WorldCube.Max[i]
}
}
return true
}
func borderActionFn_Disable(m *GameObject, envInfo *ActionFnEnvInfo) bool {
for i := range m.PosVector {
if (m.PosVector[i] > GameConst.WorldCube.Max[i]) || (m.PosVector[i] < GameConst.WorldCube.Min[i]) {
m.enabled = false
return false
}
}
return true
}
func borderActionFn_B2_Disable(m *GameObject, envInfo *ActionFnEnvInfo) bool {
for i := range m.PosVector {
if (m.PosVector[i] > GameConst.WorldCube2.Max[i]) || (m.PosVector[i] < GameConst.WorldCube2.Min[i]) {
m.enabled = false
return false
}
}
return true
}
func borderActionFn_None(m *GameObject, envInfo *ActionFnEnvInfo) bool {
return true
}