Dynamic lighting manager
if camera bounds sensor detects a lamp hook, and the lamp hooks is not the same as the last hook detected,
add a lamp and parent it to the hook, save as last lamp
iterate through all lamp hooks (a stored list) if any are using the lamp you just added, delete that lamp
limitations = don’t use the same lamp in a area less then the draw distance apart.
So almost unlimited lights!
any way to improve this?
import bge
def main():
cont = bge.logic.getCurrentController()
own = cont.owner
sens = cont.sensors['Collision']
## build a list of all lamp hooks in level
if 'LampHooks' not in own:
LH=[]
for obj in bge.logic.getCurrentScene().objects:
if 'LightHook' in obj:
print('found')
LH.append(obj)
own['LampHooks']=LH
else:
##if your camera sensor collides with a light hook
if sens.positive:
##if the light hook was not the same as the last light hook
if sens.hitObject.name!=own['Last']:
if sens.hitObject['On']==False:
## add the lamp, position and parent it
added = bge.logic.getCurrentScene().addObject(sens.hitObje ct['LightHook'],sens.hitObject,0)
Pos = eval(sens.hitObject['AddPoint'])
added.worldPosition=Pos
added.setParent(sens.hitObject,0,1)
## set last
own['Last'] = sens.hitObject.name
sens.hitObject['On']=True
for objects in own['LampHooks']:
## remove any duplicates of the lamp
if objects.children:
if 'Lamp' in objects.children[0]:
if objects.children[0]['Lamp']==added['Lamp']:
if objects.children[0]!=added:
objects.children[0].endObject()
objects['On']=False
##Debug to make sure there is never more then 2 copies of a lamp
## one on the hidden layer, and 1 on a hook.
##count=0
##for objects in bge.logic.getCurrentScene().objects:
## if 'Lamp' in objects:
## count+=1
## own['Count']=count ##Debug property
main()
Attachments
WrectifiedCandyLightHooks.blend (657 KB)