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

Source Code for Module MyGamePassiveParts

  1  from MyGameObjects import MY_GameObjectPassive 
  2  from MyScene import MY_SingleScene 
  3  from Mathutils import Vector, Matrix, RotationMatrix 
  4  from random import randint 
  5  from math import cos, sin, pi, ceil 
  6  from MyGameUtils import * 
  7   
  8  
 
9 -class MY_GamePlatform(MY_GameObjectPassive):
10 """ 11 class of the platform objects in the game for right rotation and translation etc. 12 13 @ivar freeSidesPosition: a list element that consists of the connected side and the position where the new object can be connected to 14 @type freeSidesPosition: List [Integer (0,1,2,3), Vector]; not very attractive :( 15 @ivar origScale: the original scaling factor 16 @type origScale: Vector 17 @ivar midPoint: the center of the object (at least the right pivot, because at creation point of some elements the pivot is wrong. so you have to use this one to get the right rotations) 18 @type midPoint: Vector 19 """ 20
21 - def __init__(self, scene):
22 """ 23 @type scene: KX_Scene 24 @param scene: the blender scene object where all other objects are connected to 25 """ 26 27 MY_GameObjectPassive.__init__(self, scene, "platform") 28 29 if self.elementId >= 0: 30 self.freeSidesPosition = [] 31 self.origScale = Vector(self.gObject.worldScale) 32 self.gObject.localScale = [0, 0, 0] 33 self.midPoint = Vector([0, 0, 0]) 34 myScene = MY_SingleScene() 35 myScene.addObject(self)
36
37 - def setElement(self, side_location, prev_plane, rotated, direct=0):
38 """ 39 initiates the calculation of position, orientation, midpoint, boundingbox and worldplane 40 41 @type side_location: List[Integer (0,1,2,3), Vector] 42 @param side_location: to know exactly where the connection point is; that means what direction and at what position 43 @type prev_plane: Integer (0,1,2) 44 @param prev_plane: you have to know the previous plane (the plane of the object that this object is connected to) because of ninety degree turns 45 @type rotated: Integer (0,1,2) 46 @param rotated: whether th object is rotated (0=up, 1=down) or it is just connected in the same plane (2=normal) 47 @type direct: Boolean 48 @param direct: True if the object shall be spawned even ther is no other object to connect to (for example at the beginning) 49 """ 50 51 connected_dir = side_location[0] 52 location = side_location[1] 53 54 self.isRotated = rotated 55 56 self.setLocation(connected_dir, location, prev_plane, direct) 57 self.setOrientation(connected_dir, location, prev_plane) 58 59 if self.isRotated != 2: 60 self.setNewPlaneOnRotation(prev_plane, connected_dir) 61 else: 62 self.worldPlane = prev_plane 63 64 self.midPoint = self.getPosition() 65 self.boundingBox = self.getBoundingBox()
66
67 - def setLocation(self, connected_dir, location, prev_plane, direct=0):
68 """ 69 sets the location by use of the MyGameUtils interface 70 71 @type connected_dir: Integer (0,1,2,3) 72 @param connected_dir: one of the four directions an object can be connected to 73 @type prev_plane: Integer (0,1,2) 74 @param prev_plane: you have to know the previous plane (the plane of the object that this object is connected to) because of ninety degree turns 75 @type location: Vector 76 @param location: the position where the object shall be spawned 77 @type direct: Boolean 78 @param direct: True if the object shall be spawned even ther is no other object to connect to (for example at the beginning) 79 """ 80 81 half_width = self.gObject["width"] / 2 82 half_height = self.gObject["height"] / 2 83 84 self.gObject.worldPosition = location 85 86 if self.isRotated != 2: 87 self.gObject.worldPosition = GetPlatfVertTransDelta(location, connected_dir, prev_plane, half_width, half_height, self.isRotated) 88 else: 89 self.gObject.worldPosition = GetPlatfNormTransDelta(location, connected_dir, prev_plane, half_width, direct)
90
91 - def setOrientation(self, connected_dir, location, prev_plane):
92 """ 93 sets the orientation by use of the MyGameUtils interface 94 95 @type connected_dir: Integer (0,1,2,3) 96 @param connected_dir: one of the four directions an object can be connected to 97 @type prev_plane: Integer (0,1,2) 98 @param prev_plane: you have to know the previous plane (the plane of the object that this object is connected to) because of ninety degree turns 99 @type location: Vector 100 @param location: the position where the object shall be spawned 101 """ 102 103 if self.isRotated != 2: 104 self.gObject.localOrientation = GetVertRotation(connected_dir, prev_plane) 105 else: 106 self.gObject.localOrientation = GetNormRotation(connected_dir, prev_plane)
107
108 - def getFreeSidesPositions(self):
109 """ 110 gets the positions of the not already connected sides of the object 111 112 @rtype: List[Integer (0,1,2,3), Vector] 113 @return: returns a list of all the free sides (direction and position) 114 """ 115 116 half_width = self.gObject["width"] / 2 117 half_depth = self.gObject["depth"] / 2 118 119 free_sides = [] 120 121 if self.connectedSides[0] == None: 122 north = Vector([0, half_depth, 0]) 123 free_sides.append([0, north]) 124 if self.connectedSides[1] == None: 125 east = Vector([half_width, 0, 0]) 126 free_sides.append([1, east]) 127 if self.connectedSides[2] == None: 128 south = Vector([0, -half_depth, 0]) 129 free_sides.append([2, south]) 130 if self.connectedSides[3] == None: 131 west = Vector([-half_width, 0, 0]) 132 free_sides.append([3, west]) 133 134 if self.worldPlane == 0: 135 for vector in free_sides: 136 t_vector = vector[1] 137 vector[1] = Vector(self.gObject.worldPosition) + t_vector 138 elif self.worldPlane == 1: 139 xrot_matrix = RotationMatrix(-90, 3, "x") 140 for vector in free_sides: 141 t_vector = xrot_matrix * Vector(vector[1]) 142 vector[1] = Vector(self.gObject.worldPosition) + t_vector 143 elif self.worldPlane == 2: 144 yrot_matrix = RotationMatrix(90, 3, "y") 145 zrot_matrix = RotationMatrix(90, 3, "z") 146 for vector in free_sides: 147 t_vector = yrot_matrix * zrot_matrix * Vector(vector[1]) 148 vector[1] = Vector(self.gObject.worldPosition) + t_vector 149 150 return free_sides # direction and position
151
152 - def getBoundingBox(self):
153 """ 154 gets the object's bounding box (the cubic hull of the object) 155 156 @rtype: List 157 @return: returns a list of vector points that represents the bounding box 158 """ 159 160 pos_x = self.gObject.worldPosition[0] 161 pos_y = self.gObject.worldPosition[1] 162 pos_z = self.gObject.worldPosition[2] 163 164 half_width = self.gObject["width"] / 2 165 half_depth = self.gObject["depth"] / 2 166 half_height = self.gObject["height"] / 2 167 168 bounding_box = [] 169 bounding_box.append(Vector([pos_x - half_width, pos_y - half_depth, pos_z - half_height])) 170 bounding_box.append(Vector([pos_x - half_width, pos_y - half_depth, pos_z + half_height])) 171 bounding_box.append(Vector([pos_x - half_width, pos_y + half_depth, pos_z - half_height])) 172 bounding_box.append(Vector([pos_x - half_width, pos_y + half_depth, pos_z + half_height])) 173 bounding_box.append(Vector([pos_x + half_width, pos_y - half_depth, pos_z - half_height])) 174 bounding_box.append(Vector([pos_x + half_width, pos_y - half_depth, pos_z + half_height])) 175 bounding_box.append(Vector([pos_x + half_width, pos_y + half_depth, pos_z - half_height])) 176 bounding_box.append(Vector([pos_x + half_width, pos_y + half_depth, pos_z + half_height])) 177 178 if self.worldPlane == 1: 179 xrot_matrix = RotationMatrix(90, 3, "x") 180 for point in bounding_box: 181 new_point = xrot_matrix * point 182 point = new_point 183 elif self.worldPlane == 2: 184 yrot_matrix = RotationMatrix(90, 3, "y") 185 for point in bounding_box: 186 new_point = yrot_matrix * point 187 point = new_point 188 189 return bounding_box
190 191
192 -class MY_GameStair(MY_GameObjectPassive):
193 """ 194 class of all stair-like objects to set the right location and orientation etc. 195 196 @ivar isUp: True if the stair object goes upwards (will be initialized by a random for the lazy ones) 197 @type isUp: Boolean 198 @ivar origScale: the original scaling factor 199 @type origScale: Vector 200 @ivar midPoint: the center of the object (at least the right pivot, because at creation point of some elements the pivot is wrong. so you have to use this one to get the right rotations) 201 @type midPoint: Vector 202 """ 203
204 - def __init__(self, scene, name, scale):
205 """ 206 @type scene: KX_Scene 207 @param scene: the blender scene object where all other objects are connected to 208 @type name: String 209 @param name: the name the object shall have in MY_SingleScene 210 @type scale: Integer 211 @param scale: the new scaling factor to increase the variance of the stair objects 212 """ 213 214 MY_GameObjectPassive.__init__(self, scene, name) 215 216 if self.elementId >= 0: 217 self.isUp = randint(0, 1) 218 self.origScale = Vector([self.gObject.worldScale[0], self.gObject.worldScale[1] * scale, self.gObject.worldScale[2]]) 219 self.gObject.localScale = [0, 0, 0] 220 self.midPoint = Vector([0, 0, 0]) 221 myScene = MY_SingleScene() 222 myScene.addObject(self)
223
224 - def setElement(self, side_location, prev_plane):
225 """ 226 initiates the calculation of position, orientation, midpoint, boundingbox and worldplane 227 228 @type side_location: List[Integer (0,1,2,3), Vector] 229 @param side_location: to know exactly where the connection point is; that means what direction and at what position 230 @type prev_plane: Integer (0,1,2) 231 @param prev_plane: you have to know the previous plane (the plane of the object that this object is connected to) because of ninety degree turns 232 """ 233 234 connected_dir = side_location[0] 235 location = side_location[1] 236 237 self.worldDirection = connected_dir 238 self.worldPlane = prev_plane 239 240 self.gObject.worldPosition = location 241 self.gObject.localOrientation = GetNormRotation(connected_dir, prev_plane, self.isUp) 242 self.setMidPoint() 243 self.boundingBox = self.getBoundingBox()
244
245 - def getFreeSidesPositions(self):
246 """ 247 gets the positions of the not already connected sides of the object 248 249 @rtype: List[Integer (0,1,2,3), Vector] 250 @return: returns the free sides (direction and position) 251 """ 252 253 o_dir = self.worldDirection 254 255 north = self.connectedSides[0] 256 east = self.connectedSides[1] 257 south = self.connectedSides[2] 258 west = self.connectedSides[3] 259 260 if self.isUp: 261 d_height = (self.gObject["height"] - 2.0) / 2.0 262 else: 263 d_height = ((self.gObject["height"] - 2.0) * -1) / 2.0 264 265 y_scaling = self.origScale[1] 266 d_depth = (self.gObject["depth"]/ 2.0) * y_scaling 267 268 free_sides = [] 269 270 front_point = Vector([0, d_depth, d_height]) 271 back_point = Vector([0, -d_depth, -d_height]) 272 if o_dir == 0: 273 if north == None: 274 free_sides.append([0, front_point]) 275 if south == None: 276 free_sides.append([2, back_point]) 277 elif o_dir == 1: 278 if east == None: 279 free_sides.append([1, front_point]) 280 if west == None: 281 free_sides.append([3, back_point]) 282 elif o_dir == 2: 283 if north == None: 284 free_sides.append([0, back_point]) 285 if south == None: 286 free_sides.append([2, front_point]) 287 elif o_dir == 3: 288 if east == None: 289 free_sides.append([1, back_point]) 290 if west == None: 291 free_sides.append([3, front_point]) 292 293 for vector in free_sides: 294 vector[1] = self.getRightPlanePoint(vector[1]) 295 296 return free_sides # direction and position
297
298 - def getRightPlanePoint(self, point):
299 """ 300 gets the right point for a connection after all the creepy rotations of stairs and single steps :) 301 302 @type point: Vector 303 @param point: the point that is calculated in the xy-plane before any rotation and translation of the object 304 305 @rtype: Vector 306 @return: returns the right free side point 307 """ 308 309 zangle = 0 310 if self.worldDirection == 1: 311 zangle = 90 312 elif self.worldDirection == 2: 313 zangle = 180 314 elif self.worldDirection == 3: 315 zangle = -90 316 317 point = RotationMatrix(zangle, 3, "z") * point 318 319 rot_matrix = RotationMatrix(0, 3, "z") 320 if self.worldPlane == 1: 321 if self.worldDirection == 0 or self.worldDirection == 2: 322 zrot_matrix = RotationMatrix(180, 3, "z") 323 xrot_matrix = RotationMatrix(90, 3, "x") 324 rot_matrix = xrot_matrix * zrot_matrix 325 else: 326 rot_matrix = RotationMatrix(90, 3, "x") 327 elif self.worldPlane == 2: 328 if self.worldDirection == 0 or self.worldDirection == 2: 329 crank_rot = RotationMatrix(180, 3, "x") 330 else: 331 crank_rot = RotationMatrix(0, 3, "x") 332 yrot_matrix = RotationMatrix(-90, 3, "y") 333 zrot_matrix = RotationMatrix(90, 3, "z") 334 rot_matrix = crank_rot * yrot_matrix * zrot_matrix 335 336 point = rot_matrix * point 337 338 point += Vector(self.midPoint) 339 340 return point
341
342 - def getBoundingBox(self):
343 """ 344 gets the bounding box 345 346 @rtype: List 347 @return: returns a list of all the vector points that represents the bounding box 348 """ 349 350 sign = 1 351 if not self.isUp: 352 sign = -1 353 354 depth_scaling = self.origScale[1] 355 depth = (self.gObject["depth"] / 2.0) * depth_scaling 356 357 half_width = self.gObject["width"] / 2 358 359 height = sign * (self.gObject["height"]/ 2.0) 360 361 t_bounding_box = [] 362 t_bounding_box.append(Vector([-half_width, -depth, -height])) 363 t_bounding_box.append(Vector([-half_width, -depth, height])) 364 t_bounding_box.append(Vector([-half_width, depth, -height])) 365 t_bounding_box.append(Vector([-half_width, depth, height])) 366 t_bounding_box.append(Vector([+half_width, -depth, -height])) 367 t_bounding_box.append(Vector([+half_width, -depth, height])) 368 t_bounding_box.append(Vector([+half_width, depth, -height])) 369 t_bounding_box.append(Vector([+half_width, depth, height])) 370 371 bounding_box = [] 372 373 for point in t_bounding_box: 374 new_point = self.getRightPlanePoint(point) 375 bounding_box.append(new_point) 376 377 return bounding_box
378
379 - def setMidPoint(self):
380 """ 381 sets th mid point for rotation (stair objects rotation point is at the first face!) 382 """ 383 384 sign = 1 385 if not self.isUp: 386 sign = -1 387 388 point = Vector([0, self.gObject["depth"] * 0.5 * self.origScale[1], sign * ((self.gObject["height"] * 0.5) - self.gObject["step_height"] * 0.5)]) 389 390 self.midPoint = self.getRightPlanePoint(point) 391 self.midPoint += self.getPosition()
392