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

Source Code for Module MyGameActiveHidden

  1  from MyGameObjects import MY_GameObjectActiveHidden, MY_GameLight 
  2  from MyScene import MY_SingleScene 
  3  from time import clock 
  4  import math 
  5  from math import pi, cos, sin, fabs, floor 
  6  from random import randint 
  7  from MyGameWorld import MY_SingleGameWorld 
  8  from MySpecialLists import MY_SingleElementList, MY_SingleIdList 
  9  from MyGameTimer import MY_SingleTimer 
 10  from Mathutils import RotationMatrix, Vector, AngleBetweenVecs, Matrix 
 11  from MyGameUtils import GetGlobalOrientationToMe 
 12  from GameTypes import KX_GameObject 
 13   
 14  # sky light class 
15 -class MY_SkyLight(MY_GameLight):
16 """ 17 class of the sky lights to simulate a sun movement 18 19 @ivar fadeStart: the start time for fading lights (on/off) 20 @type fadeStart: Float 21 @ivar oldEnergy: the starting energy of the light object 22 @type oldEnergy: Float 23 @ivar isFading: whether the light is fading or not 24 @type isFading: Boolean 25 """ 26
27 - def __init__(self, scene, element, name):
28 """ 29 @type scene: KX_Scene 30 @param scene: the blender scene object which holds all objects 31 @type element: String 32 @param element: the name of the light object which shall included into the MY_SingleScene class 33 @type name: String 34 @param name: the name the object shall have in MY_SingleScene 35 """ 36 37 MY_GameLight.__init__(self, scene, element, name) 38 39 self.fadeStart = 0 40 self.oldEnergy = 0 41 self.isFading = False 42 myScene = MY_SingleScene() 43 myScene.addObject(self)
44
45 - def setEnergyFade(self, max_amount, duration):
46 """ 47 fades the energy of the light object to a given amount in a given time 48 49 @type max_amount: Float 50 @param max_amount: amount to fade to 51 @type duration: Float 52 @param duration: duration which shall be used to fade the light's energy 53 """ 54 55 cur_fade_time = MY_SingleTimer().NOW - self.fadeStart 56 57 if (cur_fade_time <= duration): 58 if not self.isFading: 59 self.isFading = True 60 self.oldEnergy = self.gObject.energy 61 62 cur_amount = cur_fade_time / duration * (max_amount - self.oldEnergy) 63 self.setEnergy(cur_amount) 64 else: 65 self.isFading = False
66
67 - def setRotation(self, plane, radius, pivot):
68 """ 69 set an infinty rotation for day night effect 70 rotation axis depends on plane 71 72 @type plane: Integer [0,1,2] 73 @param plane: the plane on which the light shall rotate 74 @type radius: Float 75 @param radius: the radius for the rotation 76 @type pivot: Vector 77 @param pivot: the middle of the circle movement 78 """ 79 80 rad = ((clock() * 0.1) % 2) * pi 81 cosinus = radius * cos(rad) 82 sinus = radius * sin(rad) 83 84 new_position = [0, 0, 0] 85 86 if plane == "xy": 87 new_position = Vector([cosinus, sinus, 0.0]) 88 elif plane == "xz": 89 new_position = Vector([cosinus, 0.0, sinus]) 90 elif plane == "yz": 91 new_position = Vector([0.0, cosinus, sinus]) 92 else: 93 print "wrong axis" 94 95 self.gObject.worldPosition = new_position + pivot
96 97 # item class
98 -class MY_GameItem(MY_GameObjectActiveHidden):
99 """ 100 class of the game items that can be collected by the player and are positioned onm top/bottom of platforms 101 102 @ivar rotationDuration: how long shall it take to make a full circle rotation 103 @type rotationDuration: Float 104 @ivar rotationStart: start time of the circle rotation 105 @type rotationStart: Float 106 @ivar positionRotationX: specifies the tilt of the item dependent on its ground plane 107 @type positionRotationX: Float 108 @ivar positionRotationY: specifies the tilt of the item dependent on its ground plane 109 @type positionRotationY: Float 110 @ivar parentObject: the items parent object to know which object can be relieved of the item 111 @type parentObject: MY_GameObject 112 @ivar worldPlane: holds the number of the world plane 113 @type worldPlane: Integer (0,1,2) 114 """ 115
116 - def __init__(self, scene, element_name, parent_object):
117 """ 118 @type scene: KX_Scene 119 @param scene: the blender scene object which holds all objects 120 @type element_name: String 121 @param element_name: the name of the light object which shall included into the MY_SingleScene class 122 @type parent_object: MY_GameObject 123 @param parent_object: the game object the item shall parented to 124 """ 125 126 MY_GameObjectActiveHidden.__init__(self, scene, element_name) 127 myItemList = MY_SingleElementList().itemList 128 item_choice = randint(0, len(myItemList)-1) 129 self.gObject.replaceMesh(scene.objectsInactive[myItemList[item_choice][0]].meshes[0]) 130 #self.gObject.replaceMesh(scene.objectsInactive["OBitem_sniper"].meshes[0]) 131 self.rotationDuration = 2.0 132 self.rotationStart = 0 133 self.positionRotationX = 0 134 self.positionRotationY = 0 135 self.parentObject = parent_object 136 self.worldPlane = parent_object.worldPlane 137 self.setPosition() 138 self.setParentStatus(True, self) 139 140 myScene = MY_SingleScene() 141 myScene.addObject(self)
142
143 - def getPosition(self):
144 """ 145 get the world position of the item 146 147 @rtype: Vector 148 @return: the vector of the current world position 149 """ 150 151 return Vector(self.gObject.worldPosition)
152
153 - def setPosition(self):
154 """ 155 set an item at top or bottom of a parented platform 156 """ 157 158 top = randint(0, 1) 159 sign = 1 160 if top: 161 sign = -1 162 163 # sets position adjustment dependent on the plane 164 if self.worldPlane == 0: 165 self.gObject.worldPosition = self.parentObject.getPosition() + (-2 * Vector([0, 0, sign * 1])) 166 elif self.worldPlane == 1: 167 self.gObject.worldPosition = self.parentObject.getPosition() + (-2 * Vector([0, sign * 1, 0])) 168 elif self.worldPlane == 2: 169 self.gObject.worldPosition = self.parentObject.getPosition() + (-2 * Vector([sign * 1, 0, 0])) 170 171 # sets the right rotation dependent on the actual orientation to the parent platform 172 side = GetGlobalOrientationToMe(self, self.parentObject) 173 174 if self.worldPlane == 0: 175 #self.gObject.worldPosition = self.parentObject.getPosition() + (-2 * Vector([0, 0, sign * 1])) 176 if side == "bottom": 177 self.positionRotationX = 180 178 elif self.worldPlane == 1: 179 #self.gObject.worldPosition = self.parentObject.getPosition() + (-2 * Vector([0, sign * 1, 0])) 180 if side == "back": 181 self.positionRotationX = 90 182 elif side == "front": 183 self.positionRotationX = -90 184 elif self.worldPlane == 2: 185 #self.gObject.worldPosition = self.parentObject.getPosition() + (-2 * Vector([sign * 1, 0, 0])) 186 if side == "left": 187 self.positionRotationY = -90 188 elif side == "right": 189 self.positionRotationY = 90 190 191 self.gObject.localOrientation = RotationMatrix(self.positionRotationX, 3, "x") * RotationMatrix(self.positionRotationY, 3, "y")
192
193 - def setRotation(self):
194 """ 195 sets the smooth infinite rotation in the right plane of the item object 196 """ 197 198 perc = (MY_SingleTimer().NOW - self.rotationStart) / self.rotationDuration 199 axis = "z" 200 201 self.gObject.localOrientation = RotationMatrix(self.positionRotationX, 3, "x") * RotationMatrix(self.positionRotationY, 3, "y") * RotationMatrix(perc * 360, 3, "z") 202 203 if perc >= 1: 204 self.rotationStart = MY_SingleTimer().NOW
205
206 - def setParentStatus(self, has_item=False, item_object=None):
207 """ 208 sets the staus of the parent object, if it has an item or not and when 'yes', assign the item to it 209 210 @type has_item: Boolean 211 @param has_item: True if the parent object will have an assigned item 212 @type item_object: MY_GameObject 213 @param item_object: normally this item (self) 214 """ 215 216 self.parentObject.hasItem = has_item 217 self.parentObject.item = item_object
218
219 - def deleteObject(self):
220 """ 221 deletes object, removes it from the scene and frees its id 222 """ 223 224 self.setParentStatus() 225 self.gObject.endObject() 226 freeId = MY_SingleIdList() 227 freeId.setElementId(self.elementId) 228 myScene = MY_SingleScene() 229 myScene.removeObject(self)
230 231 # merch item class
232 -class MY_GameMerchItem(MY_GameObjectActiveHidden):
233 """ 234 class of the game merchandise items that can be collected by the player 235 """ 236
237 - def __init__(self, scene, spawn_point):
238 """ 239 @type scene: KX_Scene 240 @param scene: the blender scene object which holds all objects 241 @type spawn_point: KX_GameObject 242 @param spawn_point: the object where the merch item shall be spawned 243 """ 244 245 myMerchList = MY_SingleElementList().merchList 246 merch_choice = randint(0, len(myMerchList)-1) 247 element_name = myMerchList[merch_choice].name 248 MY_GameObjectActiveHidden.__init__(self, scene, element_name[2:], spawn_point) 249 250 myScene = MY_SingleScene() 251 myScene.objectsMerch.append(self)
252
253 - def setPosition(self, position):
254 """ 255 set an item at top or bottom of a parented platform 256 257 @type position: Vector 258 @param position: the vector of the new world position 259 """ 260 self.gObject.worldPosition = position
261
262 - def deleteObject(self):
263 """ 264 deletes object, removes it from the scene and frees its id 265 """ 266 267 self.gObject.endObject() 268 freeId = MY_SingleIdList() 269 freeId.setElementId(self.elementId) 270 myScene = MY_SingleScene() 271 myScene.objectsMerch.remove(self)
272 273 274 # hurdle classhurdleGroundDetector.localOrientation = orientation
275 -class MY_GameHurdle(MY_GameObjectActiveHidden):
276 """ 277 class of the enemy (hurdle) with its artificial intelligence or something like that ;) 278 279 @type targetPoint: Vector 280 @ivar targetPoint: the point the hurdle has to go to 281 @type speed: Float 282 @ivar speed: the speed of the hurdle 283 @type groundObject: MY_GameObject 284 @ivar groundObject: the current ground object of the hurdle 285 """ 286
287 - def __init__(self, scene, element_name, spawn_point):
288 """ 289 @type scene: KX_Scene 290 @param scene: the blender scene object which holds all objects 291 @type element_name: String 292 @param element_name: the name of the hurdle object which shall included into the MY_SingleScene class 293 @type spawn_point: KX_GameObject 294 @param spawn_point: the object where the merch item shall be spawned 295 """ 296 297 MY_GameObjectActiveHidden.__init__(self, scene, element_name, spawn_point) 298 self.targetPoint = Vector([0, 0, 0]) 299 self.groundObject = None 300 self.speed = 5 301 302 myScene = MY_SingleScene() 303 myScene.addObject(self)
304
305 - def setPosition(self, position):
306 """ 307 sets the world position of the hurdle 308 309 @type position: Vector 310 @param position: the new position of the hurdle 311 """ 312 313 self.gObject.worldPosition = position
314
315 - def setOrientation(self, prota_pos):
316 """ 317 sets the hurdle z-axis to the world gravity orientation, so the hurdle looks right 318 319 @type prota_pos: Vector 320 @param prota_pos: the position of the player, where the hurdle looks at 321 """ 322 323 distance_vector = prota_pos - self.getPosition() 324 counter = 0 325 look_vector = Vector([0,0]) 326 for idx, gravity in enumerate(MY_SingleGameWorld().gravityVector): 327 if gravity == 0: 328 look_vector[counter] = distance_vector[idx] 329 counter += 1 330 norm_vector_plane = look_vector / math.sqrt(math.pow(look_vector[0],2) + math.pow(look_vector[1],2)) 331 332 plane_rotation_matrix = RotationMatrix(0,3,"x") 333 334 if MY_SingleGameWorld().gravityVector == Vector([0,0,-1]): 335 if norm_vector_plane[1] >= 0: 336 angle = -(180 / math.pi) * math.acos(norm_vector_plane[0]) - 90 337 else: 338 angle = (180 / math.pi) * math.acos(norm_vector_plane[0]) - 90 339 elif MY_SingleGameWorld().gravityVector == Vector([0,0,1]): 340 plane_rotation_matrix = RotationMatrix(180,3,"x") 341 if norm_vector_plane[1] >= 0: 342 angle = (180 / math.pi) * math.acos(norm_vector_plane[0]) - 90 343 else: 344 angle = -(180 / math.pi) * math.acos(norm_vector_plane[0]) - 90 345 elif MY_SingleGameWorld().gravityVector == Vector([0,-1,0]): 346 plane_rotation_matrix = RotationMatrix(90,3,"x") 347 if norm_vector_plane[1] >= 0: 348 angle = (180 / math.pi) * math.acos(norm_vector_plane[0]) - 90 349 else: 350 angle = -(180 / math.pi) * math.acos(norm_vector_plane[0]) - 90 351 elif MY_SingleGameWorld().gravityVector == Vector([0,1,0]): 352 plane_rotation_matrix = RotationMatrix(-90,3,"x") 353 if norm_vector_plane[1] >= 0: 354 angle = -(180 / math.pi) * math.acos(norm_vector_plane[0]) - 90 355 else: 356 angle = (180 / math.pi) * math.acos(norm_vector_plane[0]) - 90 357 elif MY_SingleGameWorld().gravityVector == Vector([-1,0,0]): 358 plane_rotation_matrix = RotationMatrix(-90,3,"y") 359 if norm_vector_plane[1] >= 0: 360 angle = -(180 / math.pi) * math.acos(norm_vector_plane[0]) + 180 361 else: 362 angle = (180 / math.pi) * math.acos(norm_vector_plane[0]) + 180 363 elif MY_SingleGameWorld().gravityVector == Vector([1,0,0]): 364 plane_rotation_matrix = RotationMatrix(90,3,"y") 365 if norm_vector_plane[1] >= 0: 366 angle = (180 / math.pi) * math.acos(norm_vector_plane[0]) + 180 367 else: 368 angle = -(180 / math.pi) * math.acos(norm_vector_plane[0]) + 180 369 370 self.gObject.worldOrientation = plane_rotation_matrix * RotationMatrix(angle, 3, "z")
371
372 - def setHurdle(self, prota_pos):
373 """ 374 sets the hurdle onto the right way to the player 375 376 1. collision sphere (touch) detects items to eat and bullet hits 377 378 @type prota_pos: Vector 379 @param prota_pos: the position of the object the hurdle is after 380 381 @rtype: String 382 @return: 383 1. 'nothing', if hurdle is up in the air and the 'bullet ohysic engine' takes over 384 2. 'item', if hurdle hits an item 385 3. 'ground', if hurdle hits the ground 386 4. 'dead', if hurdle was hit by a bullet 387 """ 388 389 # the hurdle collision sphere (is deprecated, maybe it makes errors in the future) 390 # can't get the right controller by name so i hope it will be the same sequence all the time 391 touch = self.gObject.controllers[2].sensors["hurdle_touch"] 392 result = "nothing" 393 394 if not self.gObject["fly"]: 395 if touch.positive: 396 hit_object_name = touch.hitObject.name[2:] 397 hit_object = touch.hitObject 398 399 if hit_object_name == "delegate_item": 400 item = MY_SingleScene().getMyActiveTempObjectById(hit_object["id"]) 401 item.deleteObject() 402 result = "item" 403 404 elif hit_object_name == "platform" or hit_object_name == "single_step" or touch.hitObject.name[2:7] == "stair": 405 hitObject = MY_SingleScene().getMyPassiveObjectById(hit_object["id"]) 406 407 if hitObject != self.groundObject: 408 if self.groundObject != None: 409 self.groundObject.hurdleObject = None 410 self.groundObject = hitObject 411 hitObject.hurdleObject = self 412 413 414 if hitObject.rootConnectObject != None: 415 self.targetPoint = hitObject.rootConnectObject.midPoint 416 else: 417 self.targetPoint = prota_pos 418 419 self.setSpeedToPoint() 420 421 result = "ground" 422 423 elif hit_object_name == "bullet": 424 if hit_object.getLinearVelocity(0) != [0,0,0]: 425 print "bullet hit" 426 result = "bullet" 427 elif hit_object_name == "projectile": 428 if hit_object.getLinearVelocity(0) != [0,0,0]: 429 #print "projectile hit" 430 result = "projectile" 431 432 return result
433
434 - def setSpeedToPoint(self):
435 """ 436 sets the vectored speed of the hurdle 437 """ 438 439 speed_vector = Vector([0, 0, 0]) 440 441 point = self.targetPoint 442 443 max_ground_speed = 5 444 jump_speed = 7 445 446 ground_speed = [] 447 448 for i,k in enumerate(MY_SingleGameWorld().gravityVector): 449 if fabs(k) < 1: 450 speed_vector[i] = (point[i] - self.getPosition()[i]) #plane movement 451 ground_speed.append([i, speed_vector[i]]) 452 else: 453 speed_vector[i] = k * -jump_speed #jump 454 455 full_ground_speed = fabs(ground_speed[0][1]) + fabs(ground_speed[1][1]) 456 sign = 1 457 if ground_speed[0][1] < 0: 458 sign = -1 459 speed_vector[ground_speed[0][0]] = fabs(ground_speed[0][1]) / full_ground_speed * sign * max_ground_speed 460 sign = 1 461 if ground_speed[1][1] < 0: 462 sign = -1 463 speed_vector[ground_speed[1][0]] = fabs(ground_speed[1][1]) / full_ground_speed * sign * max_ground_speed 464 465 self.gObject.setLinearVelocity(speed_vector, 0)
466
467 - def deleteObject(self):
468 """ 469 deletes object, removes it from the scene and frees its id 470 """ 471 472 if self.groundObject != None: 473 self.groundObject.hurdleObject = None 474 self.gObject.endObject() 475 freeId = MY_SingleIdList() 476 freeId.setElementId(self.elementId) 477 myScene = MY_SingleScene() 478 myScene.removeObject(self)
479