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-09-24 10:56:53 +00:00
|
|
|
static func editor_info():
|
|
|
|
var editor_icon = AtlasTexture.new()
|
|
|
|
editor_icon.atlas = preload("res://Graphics/tgstation/light.png")
|
|
|
|
editor_icon.region = Rect2(32, 0, 32, 32)
|
|
|
|
return {
|
|
|
|
"name": "Light fixture",
|
|
|
|
"scene": load("res://Actors/Objects/Lightbulb/Lightbulb.tscn"),
|
|
|
|
"icon": editor_icon
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-07-13 19:01:36 +00:00
|
|
|
remotesync func set_direction(dir):
|
2020-07-07 00:47:29 +00:00
|
|
|
direction = dir
|
|
|
|
refresh_sprite()
|
|
|
|
|
2020-07-13 19:01:36 +00:00
|
|
|
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)
|
2020-07-13 19:01:36 +00:00
|
|
|
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
|
|
|
|
2020-07-07 16:08:56 +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():
|
2020-07-13 19:01:36 +00:00
|
|
|
rpc("set_lit", !lit)
|
2020-07-10 00:09:54 +00:00
|
|
|
|
2020-07-10 08:45:57 +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)
|
2020-07-13 19:01:36 +00:00
|
|
|
|
|
|
|
func serialize():
|
|
|
|
return {
|
|
|
|
"direction": direction,
|
|
|
|
"lit": lit
|
|
|
|
}
|
|
|
|
|
|
|
|
func deserialize(data):
|
|
|
|
set_direction(data["direction"])
|
|
|
|
set_lit(data["lit"])
|