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

Source Code for Module S2A

  1  '''  
  2  The Sensor-To-Actuator library 
  3  ============================== 
  4   
  5  This module provides functions to transfer parameters from sensors 
  6  to actuators, sets position or aligns to a vector 
  7  ''' 
  8   
  9  __version__ = "1.1" 
 10  __author__ = "Monster" 
 11  __package__ = "S2A" 
 12   
 13  #=============================================================================== 
 14  # getValueOfPositive 
 15  #=============================================================================== 
16 -def __getValueOfPositive(cont, attribute):
17 ''' 18 Finds one positive sensor with hitObject. 19 20 @param cont: the calling controller 21 @param attribute: the sensors attribute to be read 22 @return: the value of the sensors attribute 23 ''' 24 for sensor in cont.sensors: 25 if sensor.positive: 26 try: 27 return getattr(sensor, attribute) 28 except AttributeError: 29 continue
30 31 #=============================================================================== 32 # getValueOf 33 #===============================================================================
34 -def __getValueOf(cont, attribute):
35 ''' 36 Finds one sensor with hitObject. 37 38 @param cont: the calling controller 39 @param attribute: the sensors attribute to be read 40 @return: the value of the sensors attribute 41 ''' 42 for sensor in cont.sensors: 43 try: 44 return getattr(sensor, attribute) 45 except AttributeError: 46 continue
47 48 #=============================================================================== 49 # setValueTo 50 #===============================================================================
51 -def __setValueTo(cont, attribute, value):
52 '''Configure and activate all actuators.''' 53 for actuator in cont.actuators: 54 try: 55 setattr(actuator, attribute, value) 56 except AttributeError: 57 pass # ignore 58 cont.activate(actuator)
59 60 #=============================================================================== 61 # getParam 62 #===============================================================================
63 -def __getParam(obj, propName, default=None):
64 '''Retrieves the property from the object. If not present returns default.''' 65 try: 66 return obj[propName] 67 except KeyError: 68 return default
69 70 #=============================================================================== 71 # hitObjectToObject 72 #===============================================================================
73 -def hitObjectToObject(cont):
74 ''' 75 Takes the hitObject parameter of the sensor and 76 puts it to the object parameter of the actuator. 77 78 Supported sensor types: 79 ======================= 80 - MouseFocus 81 - Near 82 - Touch 83 - Radar 84 - Ray 85 86 Supported actuator types: 87 ========================= 88 - Camera 89 - Parent 90 - TrackTo 91 92 The actuators will be activated. 93 @param cont: controller running this function 94 ''' 95 hitObject = __getValueOfPositive(cont, "hitObject") 96 if hitObject == None: 97 return 98 99 __setValueTo(cont, "object", hitObject)
100 101 #=============================================================================== 102 # closestHitObjectToObject 103 #===============================================================================
104 -def closestHitObjectToObject(cont):
105 ''' 106 Takes the hitObjectList parameter of the sensor and 107 puts the closest object from that list 108 to the object parameter of the actuator. 109 110 Supported sensor types: 111 ======================= 112 - Near 113 - Touch 114 - Radar 115 116 Supported actuator types: 117 ========================= 118 - TrackTo 119 - Camera 120 - Parent 121 122 The actuators will be activated if one sensor is positive. 123 124 @param cont: controller running this function 125 ''' 126 hitObjectList = __getValueOfPositive(cont, "hitObjectList") 127 if hitObjectList == None: 128 return 129 130 closestObject = hitObjectList[0] 131 closestDistance = closestObject.getDistanceTo(cont.owner) 132 for hitObject in hitObjectList: 133 distance = hitObject.getDistanceTo(cont.owner) 134 if distance < closestDistance: 135 closestObject = hitObject 136 closestDistance = distance 137 138 __setValueTo(cont, "object", closestObject)
139 140 141 #=============================================================================== 142 # hitNormalToAlign 143 #===============================================================================
144 -def hitNormalToAlign(cont):
145 ''' 146 Takes the hitNormal parameter of the sensor and 147 aligns the orientation of the actuator owners accordingly. 148 149 Supported sensor types: 150 ======================= 151 - Mouse over 152 - Mouse over any 153 - Ray 154 155 Supported actuator types: 156 ========================= 157 - any 158 159 The actuators will not be activated or changed. 160 161 @keyword axis: (optional) the axis you want to align 162 - 0 = X axis 163 - 1 = Y axis 164 - 2 = Z axis (default) 165 @type axis: integer 166 @keyword factor: (optional) only rotate a feaction of the distance to the target vector (0.0 - 1.0) 167 @type factor: float 168 @param cont: controller running this function 169 ''' 170 hitNormal = __getValueOfPositive(cont, "hitNormal") 171 if hitNormal == None: 172 return 173 174 axis = __getParam(cont.owner, "axis", 0) 175 factor = __getParam(cont.owner, "factor", 1.0) 176 177 for actuator in cont.actuators: 178 actuator.owner.alignAxisToVect(hitNormal, axis, factor)
179 180 #=============================================================================== 181 # rayDirectionToAlign 182 #===============================================================================
183 -def rayDirectionToAlign(cont):
184 ''' 185 Takes the rayDirection parameter of the sensor and 186 aligns the orientation of the actuator owners accordingly. 187 188 Supported sensor types: 189 ======================= 190 - Mouse over 191 - Mouse over any 192 - Ray 193 194 Supported actuator types: 195 ========================= 196 - any 197 198 The actuators will not be activated or changed. 199 200 @keyword axis: (optional) the axis you want to align 201 - 0 = X axis 202 - 1 = Y axis 203 - 2 = Z axis (default) 204 @type axis: integer 205 @keyword factor: (optional) only rotate a feaction of the distance to the target vector (0.0 - 1.0) 206 @type factor: float 207 @param cont: controller running this function 208 ''' 209 rayDirection = __getValueOfPositive(cont, "rayDirection") 210 if rayDirection == None: 211 return 212 213 axis = __getParam(cont.owner, "axis", 0) 214 factor = __getParam(cont.owner, "factor", 1.0) 215 216 for actuator in cont.actuators: 217 actuator.owner.alignAxisToVect(rayDirection, axis, factor)
218 219 #=============================================================================== 220 # hitPositionToPosition 221 #===============================================================================
222 -def hitPositionToPosition(cont):
223 ''' 224 Takes the hitPosition parameter of the sensor and 225 sets the position of the actuator owners accordingly. 226 227 Supported sensor types: 228 ======================= 229 - MouseFocus 230 - Ray 231 232 Supported actuator types: 233 ========================= 234 - any 235 236 The actuators will not be activated or changed. 237 @param cont: controller running this function 238 ''' 239 hitPosition = __getValueOfPositive(cont, "hitPosition") 240 if hitPosition == None: 241 return 242 243 for actuator in cont.actuators: 244 actuator.owner.worldPosition = hitPosition
245 246 #=============================================================================== 247 # rayTargetToPosition 248 #===============================================================================
249 -def rayTargetToPosition(cont):
250 ''' 251 Takes the rayTarget parameter of the sensor and 252 sets the position of the actuator owners accordingly. 253 254 Supported sensor types: 255 ======================= 256 - MouseFocus 257 258 Supported actuator types: 259 ========================= 260 - any 261 262 The actuators will not be activated or changed. 263 @param cont: controller running this function 264 ''' 265 rayTarget = __getValueOf(cont, "rayTarget") 266 if rayTarget == None: 267 return 268 269 for actuator in cont.actuators: 270 actuator.owner.worldPosition = rayTarget
271 272 #=============================================================================== 273 # raySourceToPosition 274 #===============================================================================
275 -def raySourceToPosition(cont):
276 ''' 277 Takes the raySource parameter of the sensor and 278 sets the position of the actuator owners accordingly. 279 280 Supported sensor types: 281 ======================= 282 - MouseFocus 283 284 Supported actuator types: 285 ========================= 286 - any 287 288 The actuators will not be activated or changed. 289 @param cont: controller running this function 290 ''' 291 raySource = __getValueOf(cont, "raySource") 292 if raySource == None: 293 return 294 295 for actuator in cont.actuators: 296 actuator.owner.worldPosition = raySource
297 298 #=============================================================================== 299 # firstBodyToValue 300 #===============================================================================
301 -def firstBodyToValue(cont):
302 ''' 303 Puts the values of the first body of the sensor to 304 the actuators variables value 305 306 Supported sensor types: 307 ======================= 308 - NetworkMessage 309 310 Supported actuator types: 311 ========================= 312 - Property 313 314 The actuators will be activated. 315 @param cont: controller running this function 316 ''' 317 bodies = __getValueOf(cont, "bodies") 318 if bodies == None: 319 return 320 321 __setValueTo(cont, "value", bodies[0])
322