2020-07-07 11:01:12 +00:00
|
|
|
tool
|
|
|
|
|
|
|
|
extends StaticBody2D
|
|
|
|
|
|
|
|
class_name GameObjectEngine
|
|
|
|
|
|
|
|
const LIGHT_STRENGTH_MULT = 0.002
|
|
|
|
|
|
|
|
const MAX_ENERGY = 2
|
2020-07-16 12:37:21 +00:00
|
|
|
const MAX_USAGE = 0.5
|
2020-07-07 11:01:12 +00:00
|
|
|
|
|
|
|
enum Direction { LEFT, RIGHT, UP, DOWN }
|
|
|
|
|
|
|
|
export(Direction) var direction = Direction.DOWN setget set_direction
|
|
|
|
|
|
|
|
onready var activationRange = $ActivationRange as ActivationRange
|
2020-07-14 21:28:13 +00:00
|
|
|
onready var manager = $PowerManager as PowerManager
|
2020-07-07 11:01:12 +00:00
|
|
|
|
|
|
|
export var strength = 1.0 setget set_strength
|
2020-07-16 15:08:30 +00:00
|
|
|
export var max_force = 0.05
|
2020-07-16 12:37:21 +00:00
|
|
|
|
|
|
|
var force = 0
|
2020-07-07 11:01:12 +00:00
|
|
|
|
2020-07-14 21:28:13 +00:00
|
|
|
func _ready() -> void:
|
2020-07-07 11:01:12 +00:00
|
|
|
if not Engine.editor_hint:
|
|
|
|
activationRange.visible = true
|
2020-07-16 12:37:21 +00:00
|
|
|
$Light2D.visible = true
|
2020-07-07 11:01:12 +00:00
|
|
|
|
2020-07-14 21:28:13 +00:00
|
|
|
func set_direction(dir) -> void:
|
2020-07-07 11:01:12 +00:00
|
|
|
direction = dir
|
|
|
|
refresh_sprite()
|
|
|
|
|
2020-07-14 21:28:13 +00:00
|
|
|
func set_strength(val: float) -> void:
|
2020-07-07 11:01:12 +00:00
|
|
|
strength = val
|
|
|
|
$Light2D.energy = val * LIGHT_STRENGTH_MULT
|
|
|
|
|
2020-07-14 21:28:13 +00:00
|
|
|
func refresh_sprite() -> void:
|
2020-07-07 11:01:12 +00:00
|
|
|
var rot = 0
|
|
|
|
match direction:
|
|
|
|
Direction.DOWN:
|
|
|
|
$engine.region_rect.position = Vector2(0, 0)
|
|
|
|
rot = 0
|
|
|
|
Direction.UP:
|
|
|
|
$engine.region_rect.position = Vector2(0, 96)
|
|
|
|
rot = PI
|
|
|
|
Direction.LEFT:
|
|
|
|
$engine.region_rect.position = Vector2(96, 96)
|
|
|
|
rot = PI/2
|
|
|
|
Direction.RIGHT:
|
|
|
|
$engine.region_rect.position = Vector2(96, 0)
|
|
|
|
rot = -PI/2
|
|
|
|
$Light2D.rotation = rot
|
|
|
|
$ActivationRange.rotation = rot
|
2020-07-14 21:28:13 +00:00
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
2020-07-16 12:37:21 +00:00
|
|
|
if Engine.editor_hint:
|
|
|
|
return
|
|
|
|
manager.power_usage = max(1e-3, MAX_USAGE * strength)
|
2020-07-07 11:01:12 +00:00
|
|
|
|
2020-07-07 16:08:56 +00:00
|
|
|
func _input_event(_viewport, event, _shape_idx):
|
2020-07-07 11:01:12 +00:00
|
|
|
if Engine.editor_hint:
|
|
|
|
return
|
|
|
|
if event is InputEventMouseButton and event.pressed:
|
|
|
|
if activationRange.in_range():
|
|
|
|
# TODO
|
|
|
|
pass
|
2020-07-13 19:01:36 +00:00
|
|
|
|
|
|
|
func serialize():
|
|
|
|
return {
|
|
|
|
"direction": direction,
|
|
|
|
"strength": strength
|
|
|
|
}
|
|
|
|
|
|
|
|
func deserialize(data):
|
|
|
|
set_direction(data["direction"])
|
|
|
|
set_strength(data["strength"])
|
2020-07-16 12:37:21 +00:00
|
|
|
|
|
|
|
func _power_status_changed(powered):
|
|
|
|
if powered:
|
|
|
|
force = max_force
|
|
|
|
$Light2D.enabled = true
|
|
|
|
else:
|
|
|
|
force = 0
|
|
|
|
$Light2D.enabled = false
|