2008-09-10 03:34:08 +00:00
|
|
|
#!BPY
|
|
|
|
"""
|
|
|
|
Name: 'GameLogic Example'
|
2009-06-08 20:08:19 +00:00
|
|
|
Blender: 249
|
2008-09-10 03:34:08 +00:00
|
|
|
Group: 'ScriptTemplate'
|
|
|
|
Tooltip: 'Script template with examples of how to use game logic'
|
|
|
|
"""
|
|
|
|
|
|
|
|
from Blender import Window
|
|
|
|
import bpy
|
|
|
|
|
|
|
|
script_data = \
|
|
|
|
'''
|
2009-02-22 10:22:49 +00:00
|
|
|
# This script must be assigned to a python controller
|
|
|
|
# where it can access the object that owns it and the sensors/actuators that it connects to.
|
|
|
|
|
2008-09-10 03:34:08 +00:00
|
|
|
# GameLogic has been added to the global namespace no need to import
|
|
|
|
|
|
|
|
# for keyboard event comparison
|
|
|
|
# import GameKeys
|
|
|
|
|
|
|
|
# support for Vector(), Matrix() types and advanced functions like AngleBetweenVecs(v1,v2) and RotationMatrix(...)
|
|
|
|
# import Mathutils
|
|
|
|
|
|
|
|
# for functions like getWindowWidth(), getWindowHeight()
|
|
|
|
# import Rasterizer
|
|
|
|
|
|
|
|
def main():
|
|
|
|
cont = GameLogic.getCurrentController()
|
|
|
|
|
|
|
|
# The KX_GameObject that owns this controller.
|
2009-06-08 20:08:19 +00:00
|
|
|
own = cont.owner
|
2008-09-10 03:34:08 +00:00
|
|
|
|
|
|
|
# for scripts that deal with spacial logic
|
2009-06-08 20:08:19 +00:00
|
|
|
own_pos = own.worldPosition
|
2008-09-10 03:34:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Some example functions, remove to write your own script.
|
|
|
|
# check for a positive sensor, will run on any object without errors.
|
2009-06-08 20:08:19 +00:00
|
|
|
print 'Logic info for KX_GameObject', own.name
|
2008-09-10 03:34:08 +00:00
|
|
|
input = False
|
|
|
|
|
2009-06-08 20:08:19 +00:00
|
|
|
for sens in cont.sensors:
|
2008-09-10 03:34:08 +00:00
|
|
|
# The sensor can be on another object, we may want to use it
|
2009-06-08 20:08:19 +00:00
|
|
|
own_sens = sens.owner
|
|
|
|
print ' sensor:', sens.name,
|
|
|
|
if sens.positive:
|
2008-09-10 03:34:08 +00:00
|
|
|
print '(true)'
|
|
|
|
input = True
|
|
|
|
else:
|
|
|
|
print '(false)'
|
|
|
|
|
2009-06-08 20:08:19 +00:00
|
|
|
for actu in cont.actuators:
|
2008-09-10 03:34:08 +00:00
|
|
|
# The actuator can be on another object, we may want to use it
|
2009-06-08 20:08:19 +00:00
|
|
|
own_actu = actu.owner
|
|
|
|
print ' actuator:', actu.name
|
2008-09-10 03:34:08 +00:00
|
|
|
|
|
|
|
# This runs the actuator or turns it off
|
|
|
|
# note that actuators will continue to run unless explicitly turned off.
|
|
|
|
if input:
|
2009-06-08 20:08:19 +00:00
|
|
|
cont.activate(actu)
|
2008-09-10 03:34:08 +00:00
|
|
|
else:
|
2009-06-08 20:08:19 +00:00
|
|
|
cont.deactivate(actu)
|
2008-09-10 03:34:08 +00:00
|
|
|
|
2009-06-08 20:08:19 +00:00
|
|
|
# Its also good practice to get sensors and actuators by name
|
|
|
|
# rather then index so any changes to their order wont break the script.
|
2008-09-10 03:34:08 +00:00
|
|
|
|
2009-06-08 20:08:19 +00:00
|
|
|
# sens_key = cont.sensors['key_sensor']
|
|
|
|
# actu_motion = cont.actuators['motion']
|
2008-09-10 03:34:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Loop through all other objects in the scene
|
|
|
|
sce = GameLogic.getCurrentScene()
|
2009-06-08 20:08:19 +00:00
|
|
|
print 'Scene Objects:', sce.name
|
|
|
|
for ob in sce.objects:
|
|
|
|
print ' ', ob.name, ob.worldPosition
|
2008-09-10 03:34:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Example where collision objects are checked for their properties
|
|
|
|
# adding to our objects "life" property
|
|
|
|
"""
|
2009-06-08 20:08:19 +00:00
|
|
|
actu_collide = cont.sensors['collision_sens']
|
|
|
|
for ob in actu_collide.objectHitList:
|
2008-09-10 03:34:08 +00:00
|
|
|
# Check to see the object has this property
|
2009-06-08 20:08:19 +00:00
|
|
|
if ob.has_key('life'):
|
|
|
|
own['life'] += ob['life']
|
|
|
|
ob['life'] = 0
|
|
|
|
print own['life']
|
2008-11-05 11:42:34 +00:00
|
|
|
"""
|
|
|
|
|
2008-09-10 03:34:08 +00:00
|
|
|
main()
|
|
|
|
'''
|
|
|
|
|
|
|
|
new_text = bpy.data.texts.new('gamelogic_example.py')
|
|
|
|
new_text.write(script_data)
|
|
|
|
bpy.data.texts.active = new_text
|
|
|
|
Window.RedrawAll()
|