Module MyGameObjects
[hide private]
[frames] | no frames]

Source Code for Module MyGameObjects

  1  from MySpecialLists import MY_SingleIdList, MY_SingleElementList 
  2  from GameTypes import KX_GameObject 
  3  from MyScene import MY_SingleScene 
  4  from random import randint 
  5  from Mathutils import RotationMatrix, Matrix, Vector 
  6  from MyGameTimer import MY_SingleTimer 
  7  from MyGameWorld import MY_SingleGameWorld 
  8  from math import sin, cos, pi, fabs, floor 
  9  
 
 10  
 
11 -class MY_GameObjectActive():
12 """ 13 base class of all the objects that are dynamic and NOT temporary 14 15 @ivar gObject: a blender game object 16 @type gObject: KX_GameObject 17 @ivar name: the name that represents the object in MY_SingleScene 18 @type name: String 19 @ivar type: a name which identifies the object as a specific class in MY_SingleScene 20 @type type: String 21 """ 22
23 - def __init__(self, scene, element):
24 """ 25 @type scene: KX_Scene 26 @param scene: the blender scene object which holds all objects 27 @type element: String 28 @param element: the name of the light object which shall included into the MY_SingleScene class 29 """ 30 31 self.gObject = scene.objects["OB" + element] 32 self.name = element 33 self.type = "objectActive"
34
35 - def setLocalOrientation(self, orientation):
36 """ 37 sets the local orientation 38 39 @type orientation: Matrix 40 @param orientation: the new local orientation matrix 41 """ 42 43 self.gObject.localOrientation = orientation
44
45 - def getWorldOrientation(self):
46 """ 47 gets the world orientation 48 49 @rtype: Matrix 50 @return: a 3x3 matrix of the current orientation 51 """ 52 53 return self.gObject.worldOrientation
54
55 - def getPosition(self):
56 """ 57 gets the world position 58 59 @rtype: Vector 60 @return: a vector of the current world position 61 """ 62 63 return Vector(self.gObject.worldPosition)
64
65 - def setPosition(self, position):
66 """ 67 sets the world position 68 69 @type position: Vector 70 @param position: the new world position of the object 71 """ 72 73 self.gObject.worldPosition = position
74
75 - def getLinearVelocity(self, local):
76 """ 77 if 'local = True' it gets the local linear velocity 78 otherwise it gets the global linear velocity 79 80 @type local: Boolean 81 @param local: True if you want the local linear velocity, otherwise it is global 82 83 @rtype: List [vx, vy, vz] 84 @return: the current linear velocity in all three directions 85 """ 86 87 return self.gObject.getLinearVelocity(local)
88
89 - def setLinearVelocity(self, speed, local=0):
90 """ 91 if 'local = True' it sets the local linear velocity 92 otherwise it sets the global linear velocity 93 94 @type speed: Vector 95 @param speed: the max speed for all three directions 96 @type local: Boolean 97 @param local: True if the local velocity shall be set, otherwise global 98 """ 99 100 self.gObject.setLinearVelocity(speed, local)
101
102 - def deleteObject(self):
103 """ 104 deletes object and removes it from the scene 105 """ 106 107 self.gObject.endObject() 108 myScene = MY_SingleScene() 109 myScene.removeObject(self)
110 111
112 -class MY_GameObjectActiveHidden():
113 """ 114 base class of all the objects that are dynamic but have a lifetime, so they are temporary ingame 115 116 @ivar gObject: a blender game object 117 @type gObject: KX_GameObject 118 @ivar name: the name that represents the object in MY_SingleScene 119 @type name: String 120 @ivar type: a name which identifies the object as a specific class in MY_SingleScene 121 @type type: String 122 @ivar elementId: the unique identifier for this object 123 @type elementId: Integer 124 @ivar worldPlane: the number of the its current plane 125 @type worldPlane: Integer (0,1,2) 126 @ivar lifeTime: specifies the lifetime of the spawned item (negative = infinite) 127 @type lifeTime: Integer 128 """ 129
130 - def __init__(self, scene, element, spawn_point = 0):
131 """ 132 @type scene: KX_Scene 133 @param scene: the blender scene object which holds all objects 134 @type element: the name of the object 135 @param element: String 136 @type spawn_point: KX_GameObject 137 @param spawn_point: the object where the object shall be spawned 138 """ 139 140 if spawn_point == 0: 141 __center = scene.objects["OBcenter"] 142 spawn_point = __center 143 144 __freeId = MY_SingleIdList() 145 146 self.gObject = scene.addObject(scene.objectsInactive["OB" + element], spawn_point) 147 self.name = element 148 self.type = "objectActiveTemp" 149 self.elementId = __freeId.getElementId() 150 self.gObject["id"] = self.elementId 151 self.worldPlane = 0 152 self.lifeTime = -1
153
154 - def getAlive(self, prota_pos):
155 """ 156 checks if object's lifetime is expired or object is out of game view 157 158 @rtype: Boolean 159 @return: True if the item is still alive, otherwise False 160 """ 161 162 result = True 163 164 if self.lifeTime == -1: 165 pass 166 elif floor(MY_SingleTimer().NOW) > floor(MY_SingleTimer().AGO) and (self.lifeTime-1) != -1: 167 self.lifeTime -= 1 168 169 distance_vector = self.getPosition() - prota_pos 170 171 if self.lifeTime == 0 or distance_vector.length > 200: 172 result = False 173 174 return result
175
176 - def getPosition(self):
177 """ 178 gets the world position 179 180 @rtype: Vector 181 @return: the current world position of the object 182 """ 183 184 return Vector(self.gObject.worldPosition)
185
186 - def setLinearVelocity(self, speed, local=True):
187 """ 188 just a forwarding of the blender setLinearVelocity 189 190 @type speed: Vector 191 @param speed: a vector with the speed of every single direction 192 @type local: Boolean 193 @param local: True if velocity has effect just locally, else globally 194 """ 195 196 self.gObject.setLinearVelocity(speed, local)
197
198 - def deleteObject(self):
199 """ 200 deletes object, removes it from the scene and frees its id 201 """ 202 203 self.gObject.endObject() 204 freeId = MY_SingleIdList() 205 freeId.setElementId(self.elementId) 206 myScene = MY_SingleScene() 207 myScene.removeObject(self)
208 209
210 -class MY_GameObjectPassive():
211 """ 212 base class of all the objects in the game that are part of the environment, like platforms and stairs 213 214 @ivar gObject: a blender game object 215 @type gObject: KX_GameObject 216 @ivar name: the name of the object 217 @type name: String 218 @ivar type: a name which identifies the object as a specific class in MY_SingleScene 219 @type type: String 220 @ivar elementId: a unique identifier 221 @type elementId: Integer 222 @ivar connectedSides: a dictionary (in form of a quadrupel) which contains the connected objects (otherwise: None) in specific way {north=+y, east=+x, south=-y, west=-x} 223 @type connectedSides: Dict 224 @ivar rootConnectObject: the connected object that leads to the players ground object 225 @type rootConnectObject: MY_GameObject 226 @ivar isRotated: three states: 0 = up; 1 = down; 2 = normal (in the xy-plane) 227 @type isRotated: Integer (0,1,2) 228 @ivar worldPlane: the objects plane information (0=xy; 1=xz; 2=yz) 229 @type worldPlane: Integer (0,1,2) 230 @ivar worldDirection: the global adjustment of the object (plane 0: 0=+y; 1=+x; 2=-y; 3=+x / plane 1: 0=+z; 1=+x; 2=-z; 3=+x / plane 2: 0=+z; 1=+y; 2=-z; 3=+y) 231 @type worldDirection: Integer (0,1,2,3) 232 @ivar popStart: the start time of popping 233 @type popStart: Float 234 @ivar popSteps: the number of steps for popping 235 @type popSteps: Integer 236 @ivar curPopStep: the current pop step 237 @type curPopStep: Integer 238 @ivar origScale: the original scale factor 239 @type origScale: Vector 240 @ivar curScale: the current scale factor 241 @type curScale: Vector 242 @ivar popDuration: the duration of popping 243 @type popDuration: Integer 244 @ivar deleteDelay: the amount of the delay for deleting the object; set to '<0' for no deletion 245 @type deleteDelay: Integer 246 @ivar boundingBox: contains all points of the bounding box which delimitates the object 247 @type boundingBox: List 248 @ivar hasItem: True if the object has an item to administrate while deletion, otherwise False 249 @type hasItem: Boolean 250 @ivar item: the item object that is on top/bottom of tis object 251 @type item: MY_GameObject 252 @ivar alive: True if object is destined for deletion (in the future) 253 @type alive: Boolean 254 @ivar hurdleObject: the hurdle object that is on THIS object (for oneighty turns), if no hurdle is near this variable is 'None' 255 @type hurdleObject: MY_GameObject 256 """ 257
258 - def __init__(self, scene, element):
259 """ 260 @type scene: KX_Scene 261 @param scene: the blender scene object which holds all objects 262 @type element: String 263 @param element: the name of the object 264 """ 265 266 self.type = "objectPassive" 267 self.elementId = -1 268 self.gObject = 0 269 self.name = element 270 self.connectedSides = {0:None, 1:None, 2:None, 3:None} #[-1, -1, -1, -1] # list with the id's of connected objects 271 self.rootConnectObject = None 272 self.isRotated = 2 273 self.worldPlane = 0 274 self.worldDirection = 0 275 self.popStart = 0 276 self.popSteps = 0 277 self.curPopStep = 0 278 self.origScale = Vector([1, 1, 1]) 279 self.curScale = Vector([1, 1, 1]) 280 self.popDuration = 0 281 self.deleteDelay = -1 282 self.boundingBox = [] 283 self.hasItem = False 284 self.item = None 285 self.alive = True 286 self.hurdleObject = None 287 288 __obj_found = False 289 __freeId = MY_SingleIdList() 290 291 __center = scene.objects["OBcenter"] 292 293 if element == "platform": 294 for obj in scene.objectsInactive: 295 if obj.name[2:10] == element: 296 self.gObject = scene.addObject(obj, __center) 297 __obj_found =True 298 elif element == "single_step": 299 for obj in scene.objectsInactive: 300 if obj.name[2:13] == element: 301 self.gObject = scene.addObject(obj, __center) 302 __obj_found =True 303 elif element == "stair": 304 myStairList = MY_SingleElementList() 305 choice = randint(0, len(myStairList.stairList)-1) 306 if len(myStairList.stairList) > 0: 307 obj = scene.objectsInactive[myStairList.stairList[choice]] 308 self.gObject = scene.addObject(obj, __center) 309 __obj_found =True 310 else: 311 print "no game element catched" 312 313 if __obj_found and __freeId: 314 self.elementId = __freeId.getElementId() 315 self.gObject["id"] = self.elementId
316
317 - def getPlane(self):
318 """ 319 gets the world plane the object is in 320 321 @rtype: Integer (0,1,2) 322 @return: 323 - returns '0' for xy plane 324 - returns '1' for xz plane 325 - returns '2' for yz plane 326 """ 327 328 return self.worldPlane
329
330 - def setNewPlaneOnRotation(self, prev_plane, connected_dir):
331 """ 332 sets new plane after orthogonal rotation dependent on the plane of connected object and its connected direction 333 334 @type prev_plane: Integer (0,1,2) 335 @param prev_plane: the plane of the connected object after ninety degree turn (dont know how to explain) 336 @type connected_dir: Integer (0,1,2,3) 337 @param connected_dir: on which side is the connection (0:+y, 1:+x, 2:-y, 3:-x) 338 """ 339 340 new_plane = 0 341 if prev_plane == 0: 342 if connected_dir == 0 or connected_dir == 2: 343 new_plane = 1 344 else: 345 new_plane = 2 346 elif prev_plane == 1: 347 if connected_dir == 0 or connected_dir == 2: 348 new_plane = 0 349 else: 350 new_plane = 2 351 elif prev_plane == 2: 352 if connected_dir == 0 or connected_dir == 2: 353 new_plane = 0 354 else: 355 new_plane = 1 356 357 self.worldPlane = new_plane
358
359 - def getPosition(self):
360 """ 361 gets the world position 362 363 @rtype: Vector 364 @return: the vector of the current world position 365 """ 366 367 return Vector(self.gObject.worldPosition)
368
369 - def setConnectedSide(self, connected_side, connected_object):
370 """ 371 sets list for connected sides with the right id at the right position in the array 372 373 @type connected_side: Integer (0,1,2,3) 374 @param connected_side: on which side is the connection (0:+y, 1:+x, 2:-y, 3:-x) 375 @type connected_object: MY_GameObject 376 @param connected_object: the object which is connected to THIS object 377 """ 378 379 self.connectedSides[connected_side] = connected_object
380
381 - def delConnectedSide(self, connected_object):
382 """ 383 deletes entry out of the connected side list 384 385 @type connected_object: MY_GameObject 386 @param connected_object: the connected object that was deleted 387 """ 388 389 for idx, obj in self.connectedSides.iteritems(): 390 if obj == connected_object: 391 self.connectedSides[idx] = None
392
393 - def initPop(self, pop_steps, pop_start, duration):
394 """ 395 initialize popping when object is spawned 396 397 @type pop_steps: Integer (> 0) 398 @param pop_steps: how many steps are needed to pop up/down the object 399 @type pop_start: Float 400 @param pop_start: start time of popping 401 @type duration: Float 402 @param duration: for how long shall the object pop 403 """ 404 405 self.popSteps = pop_steps 406 self.popStart = pop_start 407 self.popDuration = duration 408 self.gObject.localScale = [0, 0, 0] 409 self.curScale = self.gObject.localScale
410
411 - def pop(self, do_pop, natal=True):
412 """ 413 pops the object with its initialized parameters 414 415 @type do_pop: Boolean 416 @param do_pop: do the pop (True) or not (False) 417 @type natal: Boolean 418 @param natal: whether it is the objects birth (True) or dead (False) 419 """ 420 421 if do_pop: 422 myTimer = MY_SingleTimer() 423 424 step_duration = self.popDuration / (self.curPopStep + 1) 425 perc = ((myTimer.NOW * 1000) - (self.popStart * 1000)) / step_duration 426 427 num_pi = 2 * pi * perc 428 if natal: 429 scale = cos(num_pi) * self.origScale 430 else: 431 scale = sin(num_pi) * self.origScale 432 433 if self.curPopStep != self.popSteps: 434 if natal: 435 if num_pi > pi/2 and num_pi < 3.0/2.0 * pi: 436 scale *= -1 437 else: 438 if num_pi > pi: 439 scale *= -1 440 441 self.gObject.localScale = Vector(self.curScale) + scale 442 else: 443 self.popSteps = 0 444 445 if num_pi >= 2* pi: 446 self.curPopStep += 1 447 self.popStart = myTimer.NOW 448 else: 449 self.gObject.localScale = self.origScale
450
451 - def getChildren(self):
452 """ 453 gets the direct children which are connected to the object 454 455 @rtype: List 456 @return: the list of the objects children 457 """ 458 459 children_list = [] 460 461 for idx,obj in self.connectedSides.iteritems(): 462 if obj != None and obj != self.rootConnectObject: 463 recDelObj = obj 464 465 if not recDelObj.gObject.invalid: 466 children_list.append(recDelObj) 467 return children_list
468
469 - def setAlive(self, alive):
470 """ 471 sets the object and its children to dead if necessary or the object to alive 472 473 @type alive: Boolean 474 @param alive: True if the object will live further more 475 """ 476 477 if not alive: 478 if self.alive: 479 480 self.deleteDelay = MY_SingleGameWorld().deleteDelay 481 self.alive = False 482 483 for i in self.getChildren(): 484 i.setAlive(False) 485 else: 486 self.deleteDelay = -1 487 self.alive = True
488
489 - def deleteObject(self, now=False, recursive_delete=True):
490 """ 491 deletes the object. if a delay is given, the object will be deleted after the delay. with 'now' deletion can be forced 492 all children can be deleted by setting the recursive_delete variable 493 494 @type now: Boolean 495 @param now: False if the deletion will be delayed, otherwise delete it NOW 496 @type recursive_delete: Boolean 497 @param recursive_delete: True if children will also been deleted 498 499 @rtype: Boolean 500 @return: returns 'True' if object was deleted, otherwise 'False' 501 """ 502 503 if self.deleteDelay > 0: 504 myTimer = MY_SingleTimer() 505 if floor(myTimer.NOW) > floor(myTimer.AGO): 506 self.deleteDelay -= 1 507 508 if self.deleteDelay == 0 or now: 509 self.gObject.endObject() 510 freeId = MY_SingleIdList() 511 freeId.setElementId(self.elementId) 512 myScene = MY_SingleScene() 513 514 if self.name == "platform" and self.hasItem != 0: 515 self.item.deleteObject() 516 self.hasItem = 0 517 518 for idx, obj in self.connectedSides.iteritems(): 519 if obj != None: 520 freedObject = obj 521 522 if not freedObject.gObject.invalid: 523 freedObject.delConnectedSide(self) 524 525 if recursive_delete: 526 for j in self.getChildren(): 527 j.deleteObject(True) 528 529 myScene.removeObject(self) 530 531 return True 532 else: 533 return False
534 535
536 -class MY_GameCamera():
537 """ 538 class for all the cameras used in the game 539 540 @ivar gObject: a blender game object 541 @type gObject: KX_GameObject 542 @ivar name: the name that represents the object in MY_SingleScene 543 @type name: String 544 @ivar type: a name which identifies the object as a specific class in MY_SingleScene 545 @type type: String 546 """ 547
548 - def __init__(self, scene, element):
549 """ 550 @type scene: KX_Scene 551 @param scene: the blender scene object which holds all objects 552 @type element: String 553 @param element: the name of the object 554 """ 555 556 self.gObject = scene.cameras["OB" + element] 557 self.type = "objectCamera" 558 self.name = element 559 560 if self.gObject: 561 myScene = MY_SingleScene() 562 myScene.addObject(self)
563
564 - def pointInsideFrustum(self, point):
565 """ 566 checks if a given point is inside the cameras frustum 567 568 @type point: Vector 569 @param point: the point that will be checked if it is inside the camera frustum 570 571 @rtype: String 572 @return: 573 - returns OUTSIDE, if point is outside 574 - returns INSIDE, if point is inside 575 """ 576 577 return self.gObject.pointInsideFrustum(point)
578
579 - def boxInsideFrustum(self, box):
580 """ 581 checks if a given box is inside/intersect/outside the cameras frustum 582 583 @type box: List of Vectors 584 @param box: the box parameters that will be checked 585 586 @rtype: Integer 587 @return: 588 - returns 0, if box is outside 589 - returns 2, if box intersects 590 - returns 1, if box is inside 591 """ 592 593 if self.gObject.boxInsideFrustum(box) == self.gObject.OUTSIDE: 594 return 0 595 elif self.gObject.boxInsideFrustum(box) == self.gObject.INSIDE: 596 return 1 597 elif self.gObject.boxInsideFrustum(box) == self.gObject.INTERSECT: 598 return 2
599
600 - def setLocalOrientation(self, orientation):
601 """ 602 sets the local orientation 603 604 @type orientation: Matrix 605 @param orientation: the new 3x3 orientation matrix 606 """ 607 608 self.gObject.localOrientation = orientation
609
610 - def getPosition(self):
611 """ 612 gets the local position 613 614 @rtype: Vector 615 @return: the vector of the current local position 616 """ 617 618 return Vector(self.gObject.localPosition)
619
620 - def deleteObject(self):
621 """ 622 deletes the object and removes it from the scene 623 """ 624 625 self.gObject.endObject() 626 myScene = MY_SingleScene() 627 myScene.removeObject(self)
628 629
630 -class MY_GameLight():
631 """ 632 base class of all the light objects in the game 633 634 @ivar gObject: a blender game object 635 @type gObject: KX_GameObject 636 @ivar name: the name that represents the object in MY_SingleScene 637 @type name: String 638 @ivar type: a name which identifies the object as a specific class in MY_SingleScene 639 @type type: String 640 """ 641
642 - def __init__(self, scene, element, name):
643 """ 644 @type scene: KX_Scene 645 @param scene: the blender scene object which holds all objects 646 @type element: String 647 @param element: the name of the object 648 @type name: String 649 @param name: the name that represents the object in MY_SingleScene 650 """ 651 652 __center = scene.objects["OBcenter"] 653 self.gObject = scene.addObject(scene.lights["OB" + element], __center) 654 self.name = name 655 self.type = "objectLight"
656
657 - def setEnergy(self, energy):
658 """ 659 sets the energy of the lamp 660 661 @type energy: Float 662 @param energy: the energy of the light object 663 """ 664 665 self.gObject.energy = energy
666
667 - def setOrientation(self, angle, axis):
668 """ 669 sets the local orientation 670 671 @type angle: Float 672 @param angle: the new rotation angle 673 @type axis: String (x,y,z) 674 @param axis: the rotation axis 675 """ 676 677 self.gObject.localOrientation = RotationMatrix(angle, 3, axis)
678
679 - def setParent(self, parent_object):
680 """ 681 sets objects as children of given parent object 682 683 @type parent_object: MY_GameObject 684 @param parent_object: 685 """ 686 687 self.gObject.worldPosition = parent_object.gObject.worldPosition 688 self.gObject.setParent(parent_object.gObject, 0, 1)
689
690 - def getPosition(self, local=1):
691 """ 692 gets the local position, if 'local' is True, otherwise it gets the global position 693 694 @type local: Boolean 695 @param local: True if you want to get the local position of the object, otherwise global 696 697 @rtype: Vector 698 @return: the vector of the current global/local position 699 """ 700 701 if local: 702 position = Vector(self.gObject.localPosition) 703 else: 704 position = Vector(self.gObject.worldPosition) 705 706 return position
707
708 - def deleteObject(self):
709 """ 710 deletes the object and removes it from the scene 711 """ 712 713 self.gObject.endObject() 714 myScene = MY_SingleScene() 715 myScene.removeObject(self)
716