-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.py
518 lines (394 loc) · 12.2 KB
/
entity.py
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#!/usr/bin/python
# -*- coding: utf-8 -*-
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
from glhelper import *
import gamestatemanager
import game
import math
import itertools
from particle import *
from Foundation.vector import Vector
import json
import io
global_id = 0
def minimum_distance(v, w, p):
len2 = (v-w).get_length_sqrd();
if len2 == 0.0:
return (p - v).length,Vector(0,0,0)
t = (p - v).dot(w-v) / len2
if t < 0.:
return (p - v).length,Vector(0,0,0)
if t > 1.:
return (p - w).length,Vector(0,0,0)
projection = v + t * (w - v)
le = (p - projection).length
v2 = projection.cross(Vector(0,0,1))
return le, v2
class Entity(object):
__receipe = {}
__tex = 0
__position = Vector(0.,0.,0.)
__velocity = Vector(0.,0.,0.)
__rotation = 0.
__rotationto = 0.
__texture_slicex = 0
__texture_slicey = 0
__size = []
__offset = []
__frame = 0
__id = 0
__emitter = []
__animation = None
__frame = 0.
__animation_pause = False
__animation_dir = 1.
__transparent = False
__can_be_removed = False
__alpha = 1.
__owner = None
__collectible = False
__collected = False
__collidable = True
__inventory = []
def __init__(self,game, receipe):
global global_id
self.__receipe = json.load(io.open(receipe))
self.__script = None
self.__game = game
if "script" in self.__receipe:
self.__script = compile(io.open(self.__receipe["script"]).read(),"<string>","exec")
self.__tex = loadTexture(self.__receipe.get("texture","defaultTexturePathMissing"))
self.__texture_slicex = self.__receipe.get("texture_slice_x",1)
self.__texture_slicey = self.__receipe.get("texture_slice_y",1)
self.__size = self.__receipe.get("size",[32,32])
self.__offset = self.__receipe.get("offset",[-0.5,-0.5,-0.5])
self.__frame = 0
self.__id = global_id + 1
self.__emitter = []
self.__animation = None
self.__frame = 0.
self.__animation_pause = False
self.__animation_dir = 1.
self.__velocity = Vector(0,0,0)
self.__position = Vector(0,0,0)
self.__rotation = 0.
self.__transparent = self.__receipe.get("transparent",False)
self.__can_be_removed = False
self.__alpha = 1.
self.__owner = None
self.__collectible = self.__receipe.get("collectible",False)
self.__collected = False
self.__collidable = True
self.__inventory = []
self.__texture2d = None
self.__glow = self.__receipe.get("glow",False)
path2d = self.__receipe.get("texture2d",None)
if path2d != None:
self.__texture2d = loadTexture(path2d)
global_id += 1
self.call("onInit")
def setOwner(self, owner):
self.__owner = owner
def getOwner(self):
return self.__owner
def getInventory(self):
return self.__inventory
def addItem(self, ent):
self.__inventory.append(ent)
def call(self, method, *args):
if method in self.__script.co_names:
#game = self.__game
gamestate = self.__game.getGameStateManager().getCurrentGameState()
entitymanager = gamestate.getEntityManager()
keys = gamestate.keys
specialkeys = gamestate.specialkeys
mouseL = gamestate.mouseL
mouseR = gamestate.mouseR
mouseM = gamestate.mouseM
cursorX = gamestate.cursorX
cursorY = gamestate.cursorY
cursorZ = gamestate.cursorZ
current_animation = self.__animation
position = self.__position
velocity = self.__velocity
alpha = self.__alpha
exec(self.__script,locals(),globals())
call_method = method+"("+(",".join(itertools.chain(["self"],["args["+str(x)+"]" for x in range(len(args))])))+")"
exec(call_method,locals(),globals())
def isPlayable(self):
return self.__receipe.get("playable",False)
def getName(self):
return self.__receipe.get("name","unknown")
def setName(self, name):
self.__name = name
def remove(self):
self.__can_be_removed = True
def getPosition(self):
return self.__position
def isCollectible(self):
return self.__collectible
def isCollidable(self):
return self.__collidable
def collect(self, by):
self.call("onCollectedBy",by)
self.__owner = by
self.__collected = True
self.__collidable = False
def drop(self):
self.__position = self.__owner.getPosition()
self.__collected = False
self.__owner = None
self.__collidable = True
def getVelocity(self):
return self.__velocity
def setPosition(self, pos):
self.__position = pos
def setVelocity(self, vel):
self.__velocity = vel
def setRotation(self, rot):
self.__rotation = self.__rotationto = rot
def rotateTo(self,rot):
self.__rotationto = rot
def getRotation(self):
return self.__rotation
def isMoving(self):
return self.__velocity.length > 0.01
def startAnimation(self, name):
for a in self.__receipe["animations"]:
if a.get("name","unknown") == name:
self.__animation = a
self.__frame = a["start"]
self.__animation_pause = False
def pauseAnimation(self):
self.__animation_pause = True
def resumeAnimation(self):
self.__animation_pause = False
def setAlpha(self, alpha):
self.__alpha = alpha
def canBeRemoved(self):
return self.__can_be_removed
def display3D(self,delta,pazz="default"):
if self.__can_be_removed or self.__collected:
return
if pazz == "glow" and not self.__glow:
return
elif self.__glow and pazz != "glow":
return
glColor4f(1,1,1,self.__alpha)
if self.__transparent:
glDepthMask(GL_FALSE)
glPushMatrix()
#if self.isMoving():
#self.__rotationto = math.degrees(math.atan2(self.__velocity[1],self.__velocity[0]))+90
torotate = self.__rotationto - self.__rotation
if torotate > 180:
torotate -= 360
self.__rotation += torotate * delta * 10.
glTranslate(self.__position.x, self.__position.y, self.__position.z)
glRotatef(self.__rotation,0,0,1)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, self.__tex)
u,v,u2,v2 = atlasUV(round(self.__frame), self.__texture_slicex, self.__texture_slicey)
s = self.__size
x,y,z = self.__offset
glBegin(GL_QUADS)
glTexCoord(u,v2)
glVertex(x,y,z)
glTexCoord(u2,v2)
glVertex(x+s[0],y,z)
glTexCoord(u2,v)
glVertex(x+s[0],y+s[1],z)
glTexCoord(u,v)
glVertex(x,y+s[1],z)
glEnd()
glPopMatrix()
self.call("onRender",delta)
for e in self.__emitter:
e.display3D(delta)
if self.__transparent:
glDepthMask(GL_TRUE)
def updateCollision(self, delta):
# TODO: make Bounding box more arbritary
bbsizex, bbsizey = self.__receipe["bounding_box"]
bbsizex /= 2.
bbsizey /= 2.
ldist = min(bbsizex, bbsizey)
noff = [[bbsizex,bbsizey],[-bbsizex,-bbsizey],[bbsizex,-bbsizey],[-bbsizex,bbsizey]]
current_map = self.__game.getGameStateManager().getCurrentGameState().getMap()
walls = current_map.getWalls()
props = current_map.getTile(self.__position + Vector(0,0,current_map.LAYERHEIGHT))
epsilon = 0.26
newpos = Vector(self.__position)
newpos.x += self.__velocity.x * delta * 0.5
solid = False
impdist = 5
hit = Vector(0,0,0)
def nearwall(p1,p2,pos):
x1 = p1.x
x2 = p2.x
if x1 > x2:
x1,x2 = x2,x1
y1 = p1.y
y2 = p2.y
if y1 > y2:
y1,y2 = y2,y1
return pos.inPlanarRect([x1-1.5,y1-1.5,x2+1.5,y2+1.5])
for off in noff:
checkpos = newpos + Vector(off[0],off[1],current_map.LAYERHEIGHT*1.1)
if current_map.isSolid(checkpos):
solid = True
for w2 in walls:
for w in w2["colllines"]:
p1 = Vector(w[0])
p2 = Vector(w[1])
if not nearwall(p1,p2,newpos):
continue
if abs(round(p1.z / current_map.LAYERHEIGHT) - round(checkpos.z / current_map.LAYERHEIGHT)) - 1.0 > epsilon:
continue
p1.z = 0
p2.z = 0
checkpos2 = Vector(checkpos)
checkpos2.z = 0
diff = p2 - p1
length = diff.length
dist,ort = minimum_distance(p1,p2,checkpos2)
#print dist
if dist < ldist:
#hit = ort
solid = True
if not solid:
self.__position = newpos
else:
self.call("onCollision",newpos)
newpos = Vector(self.__position)
newpos.y += self.__velocity.y * delta + hit.y * delta * 0.1
solid = False
hit = Vector(0,0,0)
for off in noff:
checkpos = newpos + Vector(off[0],off[1],current_map.LAYERHEIGHT)
if current_map.isSolid(checkpos):
solid = True
for w2 in walls:
for w in w2["colllines"]:
p1 = Vector(w[0])
p2 = Vector(w[1])
if not nearwall(p1,p2,newpos):
continue
if abs(round(p1.z / current_map.LAYERHEIGHT) - round(checkpos.z / current_map.LAYERHEIGHT)) - 1.0 > epsilon:
continue
p1.z = 0
p2.z = 0
checkpos2 = Vector(checkpos)
checkpos2.z = 0
diff = p2 - p1
length = diff.length
dist, ort = minimum_distance(p1,p2,checkpos2)
#print dist
if dist < ldist:
#hit = ort
solid = True
if not solid:
self.__position = newpos
else:
self.call("onCollision",newpos)
newpos = Vector(self.__position)
newpos.x += self.__velocity.x * delta * 0.5 + hit.x * delta * 0.1
solid = False
hit = Vector(0,0,0)
for off in noff:
checkpos = newpos + Vector(off[0],off[1],current_map.LAYERHEIGHT)
if current_map.isSolid(checkpos):
solid = True
for w2 in walls:
for w in w2["colllines"]:
p1 = Vector(w[0])
p2 = Vector(w[1])
if not nearwall(p1,p2,newpos):
continue
if abs(round(p1.z / current_map.LAYERHEIGHT) - round(checkpos.z / current_map.LAYERHEIGHT)) - 1.0 > epsilon:
continue
p1.z = 0
p2.z = 0
checkpos2 = Vector(checkpos)
checkpos2.z = 0
diff = p2 - p1
length = diff.length
dist,ort = minimum_distance(p1,p2,checkpos2)
#print dist
if dist < ldist:
#hit = ort
solid = True
if not solid:
self.__position = newpos
else:
self.call("onCollision",newpos)
gamestate = self.__game.getGameStateManager().getCurrentGameState()
entitymanager = gamestate.getEntityManager()
for e in entitymanager.getEntities():
if not e.isCollidable():
continue
if e == self:
continue
# todo coll detect improve
dist = (e.getPosition() - self.getPosition()).length
if dist < 0.5:
self.call("onCollision",e)
# if len(noff) != 0:
# newpos = Vector(self.__position)
# newpos.z += -1.0 * delta
# solid = False
# for off in noff:
# checkpos = self.__position + Vector(off[0],off[1],current_map.LAYERHEIGHT*0.5)
# if current_map.isSolid(checkpos):
# solid = True
# if not solid:
# self.__position = newpos
self.__position.z = 0.0
def shoot(self,_dir,speed=10.):
emitter = ParticleEmitter(loadTexture("Textures/tex1.png"),1)
emitter.setPosition(self.getPosition() + Vector(0,0,0.25))
emitter.setScale(.15)
emitter.setStartVelocity(_dir.normalized() * 10)
emitter.emit()
self.__emitter.append(emitter)
def updateAnimation(self, delta):
self.__frame += delta * self.__animation["fps"] * self.__animation_dir
anim_type = self.__animation.get("type","default")
if anim_type == "default":
if self.__frame >= self.__animation["end"]:
self.__frame = self.__animation["start"]
elif anim_type == "pingpong":
if self.__frame > self.__animation["end"]:
self.__animation_dir = -1.
elif self.__frame <= self.__animation["start"]:
self.__animation_dir = 1.
def display2D(self, delta):
if self.__texture2d == None:
return
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D,self.__texture2d)
glPushMatrix()
glBegin(GL_QUADS)
glTexCoord(0,0)
glVertex(0,0,0)
glTexCoord(1,0)
glVertex(32,0,0)
glTexCoord(1,1)
glVertex(32,32,0)
glTexCoord(0,1)
glVertex(0,32,0)
glEnd()
glPopMatrix()
def update(self, delta):
if self.__can_be_removed or self.__collected:
return
self.call("onUpdate",delta)
self.__emitter = [item for item in self.__emitter if not item.isDead()]
for e in self.__emitter:
e.update(delta)
if "bounding_box" in self.__receipe:
self.updateCollision(delta)
if "animations" in self.__receipe and self.__animation != None and not self.__animation_pause:
self.updateAnimation( delta)