This repository has been archived on 2020-09-30. You can view files and clone it, but cannot push or open issues or pull requests.
odyssey-old/Actors/Objects/Lightbulb/Lightbulb.gd

74 lines
1.7 KiB
GDScript3
Raw Normal View History

2020-07-07 00:47:29 +00:00
tool
extends Area2D
2020-07-07 11:01:12 +00:00
class_name GameObjectLightbulb
2020-07-07 00:47:29 +00:00
enum Direction { LEFT, RIGHT, UP, DOWN }
export(Direction) var direction = Direction.DOWN setget set_direction
export var lit = true setget set_lit
2020-07-07 07:40:20 +00:00
onready var activationRange = $ActivationRange as ActivationRange
2020-07-10 00:09:54 +00:00
onready var manager = $PowerManager as PowerManager
2020-07-07 07:40:20 +00:00
2020-07-07 00:47:29 +00:00
func _ready():
2020-07-07 08:38:32 +00:00
if not Engine.editor_hint:
activationRange.visible = true
2020-07-10 00:09:54 +00:00
refresh_sprite()
2020-07-07 00:47:29 +00:00
remotesync func set_direction(dir):
2020-07-07 00:47:29 +00:00
direction = dir
refresh_sprite()
remotesync func set_lit(val):
2020-07-07 00:47:29 +00:00
lit = val
2020-07-10 00:09:54 +00:00
update_light()
2020-07-07 00:47:29 +00:00
refresh_sprite()
func refresh_sprite():
2020-07-07 07:40:20 +00:00
var rot = 0
2020-07-07 00:47:29 +00:00
var lit_offset = 0
2020-07-10 08:48:19 +00:00
var powered = Engine.editor_hint or (manager != null and manager.powered)
if not lit or not powered:
2020-07-07 00:47:29 +00:00
lit_offset = 32
match direction:
Direction.DOWN:
$light.region_rect.position = Vector2(32, lit_offset)
rot = 0
2020-07-07 00:47:29 +00:00
Direction.UP:
$light.region_rect.position = Vector2(0, lit_offset)
2020-07-07 07:40:20 +00:00
rot = PI
2020-07-07 00:47:29 +00:00
Direction.LEFT:
$light.region_rect.position = Vector2(96, lit_offset)
2020-07-07 12:20:04 +00:00
rot = -PI/2
2020-07-07 00:47:29 +00:00
Direction.RIGHT:
$light.region_rect.position = Vector2(64, lit_offset)
2020-07-07 12:20:04 +00:00
rot = PI/2
2020-07-07 07:40:20 +00:00
$Light2D.rotation = rot
$ActivationRange.rotation = rot
2020-07-07 00:47:29 +00:00
func _input_event(_viewport, event, _shape_idx):
2020-07-07 00:47:29 +00:00
if Engine.editor_hint:
return
if event is InputEventMouseButton and event.pressed:
2020-07-07 07:40:20 +00:00
if activationRange.in_range():
rpc("set_lit", !lit)
2020-07-10 00:09:54 +00:00
func _power_status_changed(_powered: bool) -> void:
2020-07-10 00:09:54 +00:00
update_light()
refresh_sprite()
func update_light():
$Light2D.enabled = lit and (manager != null and manager.powered)
func serialize():
return {
"direction": direction,
"lit": lit
}
func deserialize(data):
set_direction(data["direction"])
set_lit(data["lit"])