pygamekit / gameobject.pxi

  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
519
520
521
522
523
524
525
526
527
528
#TODO: function for getting the appropiate python object from a gkGameObject pointer, instead of elifs everywhere
#TODO: separate callbacks, physics

from variable cimport *
from gameobject cimport *
from mathutils cimport *
cimport gkmath

OBJECT_TYPES = 'NULL', 'CAMERA', 'LIGHT', 'ENTITY', 'OBJECT', 'SKELETON', 'PARTICLES'

cdef CollisionSensor collision_callbacks = CollisionSensor()

cdef gkGameObject* cloneRecursiveImpl(gkGameObject* ob, int lifespan=0, bool instance=True):
    
    cdef gkScene *scn = ob.getOwner()
    cdef gkGameObjectArrayIterator it = ob.getChildren().iterator()
    cdef gkGameObject *ob2, *c, *c2
    cdef gkEntity *e
    
    if scn == NULL:
        return NULL
    
    ob2 = scn.cloneObject(ob, lifespan, instance)
    
    while it.hasMoreElements():
        c = it.getNext()
        ob2.removeChild(c)
        # warning: assumes every mesh child of skeleton has skeletal animation
        if ob.getType() == GK_SKELETON and c.getType() == GK_ENTITY and instance:
            c2 = cloneRecursiveImpl(c, lifespan, False)
            e = <gkEntity *>c2
            e.setSkeleton(ob2.getSkeleton())
            e.createInstance()
        else:
            c2 = cloneRecursiveImpl(c, lifespan, instance)
        ob2.addChild(c2)
        c2.setTransform(c.getTransform())
        
        
    return ob2

cdef class GameObject(pywrap):
    
    @property
    def type(self):
        return OBJECT_TYPES[(<gkGameObject*>self._p).getType()]
            
    @property
    def name(self):
        cdef Ogre_SceneNode *n = (<gkGameObject*>self._p).getNode()
        if n!=NULL: return str(n.getName().c_str())
        return "<no name>"
    
    # Properties, accessed like this: object['myProperty']
    def __getitem__(self,item):
        cdef gkVariable *v
        cdef int t
        cdef std_string s = to_stdstring(item)
        cdef gkGameObject* o = <gkGameObject*>self._p
        
        if o.hasVariable(s):
            v = o.getVariable(s)
            t = v.getType()
            if t==VAR_REAL:
                return v.getValueReal()
            elif t==VAR_BOOL:
                return v.getValueBool()
            elif t==VAR_INT:
                return v.getValueInt()
            else:
                return str(v.getValueString().c_str())
            
        raise KeyError("This GameObject doesn't have property "+str(item))
        
    def __setitem__(self,item,value):
        cdef gkVariable *v
        cdef int t
        cdef std_string s = to_stdstring(item)
        cdef gkGameObject* o = <gkGameObject*>self._p
        
        if o.hasVariable(s):
            v = o.getVariable(s)
        else:
            v = o.createVariable(s, 0)
            
        if type(value)==float:
            v.setValue(<Real>value)
        elif type(value)==int:
            v.setValue(<int>value)
        elif type(value)==type(True):
            v.setValue(<bool>value)
        elif type(value)==str:
            v.setValue(to_stdstring(value))
        else:
            raise TypeError("Unsupported type for property "+str(item)+" (type is "+str(type(value))+")")
        
    def __contains__(self,item):
        cdef gkGameObject* o = <gkGameObject*>self._p
        
        if o.hasVariable(to_stdstring(item)): return True
        return False
        
    def __hash__(self):
        return str(self).__hash__()
        
    def clone(self, name):
        cdef std_string *s = new std_string()
        return GameObject().P((<gkGameObject*>self._p).clone(s.assign(name)))
        #TODO: use scn.cloneObject() instead
        
    def cloneRecursive(self, lifespan=0):
        cdef gkGameObject *ob = cloneRecursiveImpl(<gkGameObject*>self._p, lifespan)
        if ob:
            return GameObject().P(ob)

    property parent:

        def __get__(self):
            cdef gkGameObject* o = (<gkGameObject*>self._p).getParent()
            if o!=NULL: return GameObject().P(o)
            return None
            
        def __set__(self, GameObject ob):
            if ob:
                (<gkGameObject*>self._p).setParent(<gkGameObject*>ob._p)
            else:
                (<gkGameObject*>self._p).clearParent()
                
    @property
    def children(self):
        return GameObjectArray().P(&((<gkGameObject*>self._p).getChildren()))
        
    property position:
        
        def __get__(self):
            cdef gkVector3 v = (<gkGameObject*>self._p).getPosition()
            return Vector(v.x, v.y, v.z)
            
        def __set__(self,pos):
            cdef gkVector3 v = gkVector3()
            v.x = pos[0]
            v.y = pos[1]
            v.z = pos[2]

            (<gkGameObject*>self._p).setPosition(v)

    property positionWorld:
        
        def __get__(self):
            cdef gkVector3 v = (<gkGameObject*>self._p).getWorldPosition()
            return Vector(v.x, v.y, v.z)

    property rotation:

        def __get__(self):
            cdef gkEuler r = (<gkGameObject*>self._p).getRotation()
            return Vector(r.x.valueRadians(), r.y.valueRadians(), r.z.valueRadians())

        def __set__(self,rot):
            cdef gkEuler r = gkEuler()
            r.x = gkRadian(rot[0])
            r.y = gkRadian(rot[1])
            r.z = gkRadian(rot[2])
            (<gkGameObject*>self._p).setOrientation(r)
            
    property rotationWorld:

        def __get__(self):
            cdef gkEuler r = (<gkGameObject*>self._p).getWorldRotation()
            return Vector(r.x.valueRadians(), r.y.valueRadians(), r.z.valueRadians())

    property orientation:
        
        def __get__(self):
            cdef Quaternion q = Quaternion()
            (q.q).assign((<gkGameObject*>self._p).getOrientation())
            return q
            
        def __set__(self, Quaternion q):
            (<gkGameObject*>self._p).setOrientation(<gkmath.Quaternion>(q.q)[0])
            
    property scale:
        
        def __get__(self):
            cdef gkVector3 v = (<gkGameObject*>self._p).getScale()
            return Vector(v.x, v.y, v.z)
            
        def __set__(self,pos):
            cdef gkVector3 v = gkVector3()
            v.x = pos[0]
            v.y = pos[1]
            v.z = pos[2]
            (<gkGameObject*>self._p).setScale(v)

    property linearVelocity:
        
        def __get__(self):
            cdef gkVector3 v = (<gkGameObject*>self._p).getLinearVelocity()
            return Vector(v.x, v.y, v.z)
            
        def __set__(self,pos):
            cdef gkVector3 v = gkVector3()
            v.x = pos[0]
            v.y = pos[1]
            v.z = pos[2]
            (<gkGameObject*>self._p).setLinearVelocity(v,TRANSFORM_PARENT)
            
    property angularVelocity:
        
        def __get__(self):
            cdef gkVector3 v = (<gkGameObject*>self._p).getAngularVelocity()
            return Vector(v.x, v.y, v.z)
            
        def __set__(self,pos):
            cdef gkVector3 v = gkVector3()
            v.x = pos[0]
            v.y = pos[1]
            v.z = pos[2]
            (<gkGameObject*>self._p).setAngularVelocity(v,TRANSFORM_PARENT)
            
    def translate(self, pos):
        cdef gkVector3 v = gkVector3()
        v.x = pos[0]
        v.y = pos[1]
        v.z = pos[2]

        (<gkGameObject*>self._p).translate(v, TRANSFORM_PARENT)

    def translateLocal(self, pos):
        cdef gkVector3 v = gkVector3()
        v.x = pos[0]
        v.y = pos[1]
        v.z = pos[2]

        (<gkGameObject*>self._p).translate(v, TRANSFORM_LOCAL)

    def translateWorld(self, pos):
        cdef gkVector3 v = gkVector3()
        v.x = pos[0]
        v.y = pos[1]
        v.z = pos[2]

        (<gkGameObject*>self._p).translate(v, TRANSFORM_WORLD)

    def rotate(self, drot):
        cdef gkEuler r = gkEuler()
        r.x = gkRadian(drot[0])
        r.y = gkRadian(drot[1])
        r.z = gkRadian(drot[2])
        (<gkGameObject*>self._p).rotate(r, TRANSFORM_PARENT)

    def rotateLocal(self, drot):
        cdef gkEuler r = gkEuler()
        r.x = gkRadian(drot[0])
        r.y = gkRadian(drot[1])
        r.z = gkRadian(drot[2])
        (<gkGameObject*>self._p).rotate(r, TRANSFORM_LOCAL)

    def rotateWorld(self, drot):
        cdef gkEuler r = gkEuler()
        r.x = gkRadian(drot[0])
        r.y = gkRadian(drot[1])
        r.z = gkRadian(drot[2])
        (<gkGameObject*>self._p).rotate(r, TRANSFORM_WORLD)
        
    # Rigid body stuff
    
    def applyTorque(self, t):
        cdef gkVector3 v = gkVector3()
        v.x = t[0]
        v.y = t[1]
        v.z = t[2]

        (<gkGameObject*>self._p).applyTorque(v, TRANSFORM_PARENT)

    def applyTorqueLocal(self, t):
        cdef gkVector3 v = gkVector3()
        v.x = t[0]
        v.y = t[1]
        v.z = t[2]

        (<gkGameObject*>self._p).applyTorque(v, TRANSFORM_LOCAL)

    def applyTorqueWorld(self, t):
        cdef gkVector3 v = gkVector3()
        v.x = t[0]
        v.y = t[1]
        v.z = t[2]

        (<gkGameObject*>self._p).applyTorque(v, TRANSFORM_WORLD)

    def applyForce(self, f):
        cdef gkVector3 v = gkVector3()
        v.x = f[0]
        v.y = f[1]
        v.z = f[2]

        (<gkGameObject*>self._p).applyForce(v, TRANSFORM_PARENT)

    def applyForceLocal(self, f):
        cdef gkVector3 v = gkVector3()
        v.x = f[0]
        v.y = f[1]
        v.z = f[2]

        (<gkGameObject*>self._p).applyForce(v, TRANSFORM_LOCAL)

    def applyForceWorld(self, f):
        cdef gkVector3 v = gkVector3()
        v.x = f[0]
        v.y = f[1]
        v.z = f[2]

        (<gkGameObject*>self._p).applyForce(v, TRANSFORM_WORLD)

    def suspendDynamics(self):
        (<gkGameObject*>self._p).getPhysicsController().suspend(True)

    def resumeDynamics(self):
        (<gkGameObject*>self._p).getPhysicsController().suspend(False)

    @property
    def animations(self):
        return AnimationList().P(self._p)
        
    def collisionSensor(self, prop):
        return CollisionSensor()._init(<gkGameObject*>self._p, prop)
        
cdef class Entity(GameObject):
    
    def buildTangentVectors(self, sourceTexCoordSet):
        #print (<gkEntity*>self._p).getNode().getAttachedObject(0).getMovableType().c_str()
        #="Entity"
        (<Ogre_Entity*>(<gkEntity*>self._p).getNode().getAttachedObject(0)).buildTangentVectors(VES_TANGENT, sourceTexCoordSet, 0, True, False, True)
    
cdef class Camera(GameObject):
    
    pass

cdef class Skeleton(GameObject):
    
    def something(self, a, b, c, d):
        cdef gkBone *b1 = (<gkSkeleton*>self._p).getBone(to_stdstring('tizq'))
        cdef gkBone *b2 = (<gkSkeleton*>self._p).getBone(to_stdstring('tder'))
        cdef gkTransformState t1 = gkTransformState()
        cdef gkTransformState t2 = gkTransformState()
        cdef gkVector3 v1, v2
        t1.loc.x = a
        t1.loc.z = b
        t2.loc.x = c
        t2.loc.z = d
        t1.loc.y=0.5
        t2.loc.y=0.5
        b1.applyChannelTransform(t1, 1)
        b2.applyChannelTransform(t2, 1)
        v1 = b1.getPose().loc
        v2 = b2.getPose().loc
        #print v1.x, v1.y, v1.z, "   ", v2.x, v2.y, v2.z

    def getBone(self, name):
        cdef gkBone *b = (<gkSkeleton*>self._p).getBone(to_stdstring(name))
        if b:
            return Bone().P(b)
        return None

cdef class Bone(pywrap):
    
    pass

cdef class GameObjectList(pywrap):
        
    def __list__(self):
        cdef int hack1 "h;/*" = 0
        cdef gkGameObjectHashMapIterator it, hack2 "gkGameObjectHashMap::Iterator __pyx_v_it /**/"
        
        hack2 = (<gkScene*>self._p).getObjects().iterator()
        
        ret = []
        
        while it.hasMoreElements():
            ret.append(GameObject().P(it.getNext().second))
            
        return ret
        
    def __iter__(self):
        return self.__list__().__iter__() #TODO: can be made more efficient
        
    def __getitem__(self,item):
        cdef gkGameObject *ob = (<gkScene*>self._p).getObject(to_stdstring(item))
        if ob:
            t = ob.getType()
            if t == GK_ENTITY:
                return Entity().P(ob)
            if t == GK_SKELETON:
                return Skeleton().P(ob)
            else:
                return GameObject().P(ob)
        else:
            return None

    def __len__(self):
        return (<gkScene*>self._p).getObjects().size()
    
cdef class GameObjectArray(pywrap):
        
    def __list__(self):
        cdef gkGameObjectArrayIterator it = (<gkGameObjectArray*>self._p).iterator()
        
        ret = []
        
        while (it.hasMoreElements()):
            ret.append(GameObject().P(it.getNext()))
            
        return ret
        
    def __iter__(self):
        return self.__list__().__iter__() #TODO: can be made more efficient

    def __len__(self):
        return (<gkGameObjectArray*>self._p).size()
    
# This wraps gkAnimationPlayer
cdef class Animation(pywrap):
    
    cdef gkGameObject *ob
    
    def play(self, blend = 1.0):
        self.ob.playAnimation(<gkAnimationPlayer*>self._p, blend)
        
    def stop(self, blend = 1.0):
        self.ob.stopAnimation(<gkAnimationPlayer*>self._p)
        
    property weight:
        def __get__(self):
            return (<gkAnimationPlayer*>self._p).getWeight()
        def __set__(self,  v):
            (<gkAnimationPlayer*>self._p).setWeight(v)
            
    property length:
        def __get__(self):
            return (<gkAnimationPlayer*>self._p).getLength()
        #def __set__(self,  v):
            #(<gkAnimationPlayer*>self._p).setLength(v)
            
    property speed_factor:
        def __get__(self):
            return (<gkAnimationPlayer*>self._p).getSpeedFactor()
        def __set__(self, v):
            (<gkAnimationPlayer*>self._p).setSpeedFactor(v)
    

cdef class AnimationList(pywrap):
        
    def aaa__dict__(self): # this should returna dict with all the animations, but it only returns the active one.
        cdef int hack1 "h;/*" = 0
        cdef gkAnimationsIterator it, hack2 "utHashTable<gkHashedString, gkAnimationPlayer*>::Iterator __pyx_v_it /**/"
        hack2 = (<gkGameObject*>self._p).getAnimations().iterator()
        
        cdef Animation a
        ret = {}
        
        while it.hasMoreElements():
            a = Animation().P(it.peekNextValue())
            a.ob = <gkGameObject*>self._p
            ret[it.peekNextKey().str().c_str()] = a
            it.next()
        
        return ret
        
    def __iter__(self):
        return self.__dict__().__iter__() #TODO: can be made more efficient
        
    def __getitem__(self,item):
        cdef Animation a
        cdef gkAnimationPlayer *ap
        ap = (<gkGameObject*>self._p).getAnimationPlayer(to_stdstring(item))
        if ap==NULL:
            ap = (<gkGameObject*>self._p).addAnimation(to_stdstring(item))
            if ap==NULL:
                return None
        a = Animation().P(ap)
        a.ob = <gkGameObject*>self._p
        return a

    def __len__(self):
        return (<gkGameObject*>self._p).getAnimations().size()
        

cdef class CollisionSensor(Sensor):
    
    cdef gkPhysicsController *pc
    cdef std_string *prop
    cdef gkGameObjectArray *objlist
    
    cdef _init(self, gkGameObject *ob, prop):
        self.prop = new std_string(prop) #is this freed?
        self.pc = ob.getPhysicsController()
        self.objlist = new gkGameObjectArray()
        self.callback_list = collision_callbacks
        return self
    
    def __nonzero__(self):
        return self.pc.sensorCollides(self.prop[0], to_stdstring(""), False, False, self.objlist)
            
    property hitObjects:
        
        def __get__(self):
            return GameObjectArray().P(self.objlist)
            
    def __str__(self):
        if self: return "<CollisionSensor Active>"
        else: return "<CollisionSensor>"
        
cdef void evaluate_collision_callbacks() except *:
    cdef bool s
    cdef CollisionSensor e
    
    e = collision_callbacks.next_non_empty()
    while e != None:
        if e.pc:
            s = e.pc.sensorCollides(e.prop[0], to_stdstring(""), False, False, <gkGameObjectArray*>e.objlist)
            if not e.prevstate and s:
                e.execute()
            e.prevstate = s
            
        e = e.next_non_empty()
        #TODO: where the callbacks are removed and freed?
        
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.