84 lines
2 KiB
GDScript
84 lines
2 KiB
GDScript
tool
|
|
|
|
extends Area2D
|
|
|
|
class_name ElectricSocket
|
|
|
|
enum Direction { LEFT, RIGHT, UP, DOWN }
|
|
enum Flow { SOURCE, SINK, BIDIRECTIONAL }
|
|
|
|
export(Direction) var direction := Direction.DOWN setget set_direction
|
|
|
|
export(Array, NodePath) var connectionPaths := []
|
|
var connections := []
|
|
|
|
export(Color) var source_color
|
|
export(Color) var sink_color
|
|
export(Color) var bidirectional_color
|
|
|
|
export(Flow) var flow = Flow.SINK setget set_flow
|
|
|
|
onready var socket = $socket
|
|
|
|
var network = null
|
|
|
|
func _ready() -> void:
|
|
socket.material = socket.material.duplicate()
|
|
refresh_sprite()
|
|
if Engine.editor_hint:
|
|
return
|
|
for connection in connectionPaths:
|
|
add_connection(get_node(connection))
|
|
|
|
func add_connection(node: Node) -> void:
|
|
var manager := node.get_node("PowerManager") as PowerManager
|
|
manager.socket = self
|
|
connections.append(manager)
|
|
|
|
func set_direction(dir) -> void:
|
|
direction = dir
|
|
refresh_sprite()
|
|
|
|
func set_flow(val) -> void:
|
|
flow = val
|
|
refresh_sprite()
|
|
|
|
func refresh_sprite() -> void:
|
|
if socket == null:
|
|
return
|
|
match direction:
|
|
Direction.DOWN:
|
|
socket.region_rect.position = Vector2(0, 0)
|
|
Direction.UP:
|
|
socket.region_rect.position = Vector2(32, 0)
|
|
Direction.LEFT:
|
|
socket.region_rect.position = Vector2(32, 32)
|
|
Direction.RIGHT:
|
|
socket.region_rect.position = Vector2(0, 32)
|
|
match flow:
|
|
Flow.SOURCE:
|
|
socket.material.set_shader_param("cable_color", source_color)
|
|
Flow.SINK:
|
|
socket.material.set_shader_param("cable_color", sink_color)
|
|
Flow.BIDIRECTIONAL:
|
|
socket.material.set_shader_param("cable_color", bidirectional_color)
|
|
|
|
func serialize():
|
|
var connection_paths := []
|
|
for conn in connections:
|
|
connection_paths.append({ "path": get_path_to(conn), "powered": conn.powered })
|
|
|
|
return {
|
|
"direction": direction,
|
|
"flow": flow,
|
|
"connections": connection_paths
|
|
}
|
|
|
|
func deserialize(data):
|
|
set_direction(data["direction"])
|
|
set_flow(data["flow"])
|
|
for node in data["connections"]:
|
|
var manager := get_node(node.path) as PowerManager
|
|
manager.socket = self
|
|
manager.powered = node.powered
|
|
connections.append(manager)
|