>>13374900
Currently it only supports omni-directional lights. Moving lights should be fine, not sure about colors, but brightness (energy in Godot) is taken into account. Each light is a scene set up as pic related, then just instanced into the main scene. A great feature of Godot is that I can change the energy, range, attenuation, and other properties inside the editor without affecting other instances. Here's the script for the light with some quick comments:
extends OmniLight
var player
# player has a function called light(), which affects an array of lights
func _ready():
# make detection area the size of the range
var area_s = SphereShape.new()
area_s.radius = self.omni_range
get_node("Area/CollisionShape").shape = area_s
set_fixed_process(false)
# player enters detection area
func _on_Area_body_entered( body ):
if body.is_in_group("player"):
player = body
get_node("RayCast").enabled = true
set_fixed_process(true)
# player exits detection area
func _on_Area_body_exited( body ):
if body.is_in_group("player"):
player.light_remove(self.get_instance_ID())
get_node("RayCast").enabled = false
set_fixed_process(false)
func _fixed_process(dt):
# cast the ray from the light origin to the player
get_node("RayCast").cast_to = player.translation + Vector3(0,1,0) - self.translation
if get_node("RayCast").is_colliding():
if get_node("RayCast").get_collider().is_in_group("player"):
# the light can "see" the player, calculate appropriate light level and update self in the player's light list
var amount = omni_range - self.translation.distance_to(player.translation+Vector3(0,1,0))
if amount > 0:
amount /= omni_range
amount = pow( amount, omni_attenuation )
amount *= light_energy
else:
amount = 0
player.light(self.get_instance_ID(), amount)
else:
# there's an obstruction b/t the player and the light, remove self from the player's light list
player.light(self.get_instance_ID(), 0)
And here's the relevant parts of the player script:
# there is a dictionary called lights in the player class
# lights are stored by their unique instance ID, and updated when needed
func light(id, amount):
# change
lights[id] = amount
update_light_level()
func light_remove(id):
lights.erase(id)
update_light_level()
func update_light_level():
light_level = 0
for l in lights:
light_level += lights[l] # this is probably physically inaccurate
# hud updating below
>>13374915
He asked me.