Source

pygamekit / gameobject.pxi

Full commit
  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

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


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

cdef to_GameObject(gkGameObject *ob):
    if ob!=NULL:
        t = ob.getType()
        if t == GK_ENTITY:
            return Entity().P(ob)
        elif t == GK_SKELETON:
            return Skeleton().P(ob)
        else:
            return GameObject().P(ob)
    else:
        return None

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):
    
    #cdef public Vector look_vector
    #cdef public Vector up_vector
    
    @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 __delitem__(self,item):
        (<gkGameObject*>self._p).removeVariable(to_stdstring(item))
        
    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 clone_recursive(self, lifespan=0):
        cdef gkGameObject *ob = cloneRecursiveImpl(<gkGameObject*>self._p, lifespan)
        return to_GameObject(ob)

    property parent:

        def __get__(self):
            cdef gkGameObject* ob = (<gkGameObject*>self._p).getParent()
            return to_GameObject(ob)
            
        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 gkmath.Vector3 v = (<gkGameObject*>self._p).getPosition()
            return Vector(v.x, v.y, v.z)
            
        def __set__(self,pos):
            cdef gkmath.Vector3 v = gkmath.Vector3()
            v.x = pos[0]
            v.y = pos[1]
            v.z = pos[2]

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

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

    property rotation:

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

        def __set__(self,rot):
            cdef gkmath.Euler r = gkmath.Euler()
            r.x = gkmath.Radian(rot[0])
            r.y = gkmath.Radian(rot[1])
            r.z = gkmath.Radian(rot[2])
            (<gkGameObject*>self._p).setOrientation(r)
            
    property rotation_world:

        def __get__(self):
            cdef gkmath.Euler 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 gkmath.Vector3 v = (<gkGameObject*>self._p).getScale()
            return Vector(v.x, v.y, v.z)
            
        def __set__(self,pos):
            cdef gkmath.Vector3 v = gkmath.Vector3()
            v.x = pos[0]
            v.y = pos[1]
            v.z = pos[2]
            (<gkGameObject*>self._p).setScale(v)

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

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

    def translate_local(self, pos):
        cdef gkmath.Vector3 v = gkmath.Vector3()
        v.x = pos[0]
        v.y = pos[1]
        v.z = pos[2]

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

    def translate_world(self, pos):
        cdef gkmath.Vector3 v = gkmath.Vector3()
        v.x = pos[0]
        v.y = pos[1]
        v.z = pos[2]

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

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

    def rotate_local(self, drot):
        cdef gkmath.Euler r = gkmath.Euler()
        r.x = gkmath.Radian(drot[0])
        r.y = gkmath.Radian(drot[1])
        r.z = gkmath.Radian(drot[2])
        (<gkGameObject*>self._p).rotate(r, TRANSFORM_LOCAL)

    def rotate_world(self, drot):
        cdef gkmath.Euler r = gkmath.Euler()
        r.x = gkmath.Radian(drot[0])
        r.y = gkmath.Radian(drot[1])
        r.z = gkmath.Radian(drot[2])
        (<gkGameObject*>self._p).rotate(r, TRANSFORM_WORLD)
        
    # Rigid body stuff
    
    def apply_torque(self, t):
        cdef gkmath.Vector3 v = gkmath.Vector3()
        v.x = t[0]
        v.y = t[1]
        v.z = t[2]

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

    def apply_torque_local(self, t):
        cdef gkmath.Vector3 v = gkmath.Vector3()
        v.x = t[0]
        v.y = t[1]
        v.z = t[2]

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

    def apply_torque_world(self, t):
        cdef gkmath.Vector3 v = gkmath.Vector3()
        v.x = t[0]
        v.y = t[1]
        v.z = t[2]

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

    def apply_force(self, f):
        cdef gkmath.Vector3 v = gkmath.Vector3()
        v.x = f[0]
        v.y = f[1]
        v.z = f[2]

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

    def apply_force_local(self, f):
        cdef gkmath.Vector3 v = gkmath.Vector3()
        v.x = f[0]
        v.y = f[1]
        v.z = f[2]

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

    def apply_force_world(self, f):
        cdef gkmath.Vector3 v = gkmath.Vector3()
        v.x = f[0]
        v.y = f[1]
        v.z = f[2]

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

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

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

    @property
    def animations(self):
        return AnimationList().P(self._p)
        
    def add_sensor(self, Sensor sensor):
        old = getattr(sensor,'owner',None)
        if old is not None and old!=self:
            sensor = sensor.copy()
        sensor._set_owner(self)
        return sensor
        
    def add_actuator(self, actuator):
        old = getattr(actuator,'owner',None)
        if old is not None and old!=self:
            actuator = actuator.copy()
        actuator._set_owner(self)
        return actuator
        
    def __richcmp__(self, other, op):
        if isinstance(other, GameObject):
            if op==2:   # __eq__
                return (<GameObject>other)._p == <void *>self._p
            elif op==3: # __ne__
                return (<GameObject>other)._p != <void *>self._p
        return False
        
    property visible:
        def __get__(self):
            return not (<gkGameObject*>self._p).getProperties().isInvisible()
        
        def __set__(self, bool v):
            cdef gkGameObjectProperties *prop = &((<gkGameObject*>self._p).getProperties())
            prop.m_mode |= GK_INVISIBLE
            if v:
                prop.m_mode -= GK_INVISIBLE
            (<gkGameObject*>self._p).getNode().setVisible(v, False)
        
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 gkmath.Vector3 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

    property bones:
        def __get__(self):
            return BoneList().P(self._p)

cdef class Bone(pywrap):
    
    pass

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

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(to_GameObject(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))
        return to_GameObject(ob)

    def __len__(self):
        return (<gkScene*>self._p).getObjects().size()
    
cdef class GameObjectArray(pywrap):
    
    cdef list list
        
    def __list__(self):
        cdef gkGameObjectArrayIterator it = (<gkGameObjectArray*>self._p).iterator()
        
        if self.list: return self.list  # TODO: what I was going to do with this?
        
        ret = []
        
        while (it.hasMoreElements()):
            ret.append(to_GameObject(it.getNext()))
            
        return ret
        
    def __iter__(self):
        return self.__list__().__iter__() #TODO: can be made more efficient
        
    def __getitem__(self, item):
        return self.__list__().__getitem__(item)

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