2020-07-08 12:30:46 +00:00
|
|
|
tool
|
|
|
|
|
|
|
|
extends Area2D
|
|
|
|
|
2020-07-09 14:50:34 +00:00
|
|
|
class_name ElectricSocket
|
|
|
|
|
2020-07-08 12:30:46 +00:00
|
|
|
enum Direction { LEFT, RIGHT, UP, DOWN }
|
2020-07-08 22:12:14 +00:00
|
|
|
enum Flow { SOURCE, SINK, BIDIRECTIONAL }
|
2020-07-08 12:30:46 +00:00
|
|
|
|
|
|
|
export(Direction) var direction = Direction.DOWN setget set_direction
|
|
|
|
|
2020-07-08 22:12:14 +00:00
|
|
|
export(Array, NodePath) var connectionPaths = []
|
|
|
|
var connections = []
|
2020-07-08 12:30:46 +00:00
|
|
|
|
|
|
|
export(Color) var source_color
|
|
|
|
export(Color) var sink_color
|
2020-07-08 22:12:14 +00:00
|
|
|
export(Color) var bidirectional_color
|
2020-07-08 12:30:46 +00:00
|
|
|
|
|
|
|
export(Flow) var flow = Flow.SINK setget set_flow
|
|
|
|
|
2020-07-08 22:12:14 +00:00
|
|
|
onready var socket = $socket
|
|
|
|
|
2020-07-09 07:19:17 +00:00
|
|
|
var network = null
|
2020-07-08 12:30:46 +00:00
|
|
|
|
2020-07-09 14:50:34 +00:00
|
|
|
func _ready() -> void:
|
2020-07-08 22:12:14 +00:00
|
|
|
socket.material = socket.material.duplicate()
|
2020-07-08 12:30:46 +00:00
|
|
|
refresh_sprite()
|
2020-07-09 14:50:34 +00:00
|
|
|
if Engine.editor_hint:
|
|
|
|
return
|
2020-07-08 22:12:14 +00:00
|
|
|
for connection in connectionPaths:
|
|
|
|
add_connection(get_node(connection))
|
|
|
|
|
2020-07-09 14:50:34 +00:00
|
|
|
func add_connection(node: Node) -> void:
|
2020-07-08 22:12:14 +00:00
|
|
|
var manager = node.get_node("PowerManager") as PowerManager
|
2020-07-09 07:19:17 +00:00
|
|
|
manager.socket = self
|
2020-07-09 14:50:34 +00:00
|
|
|
connections.append(manager)
|
2020-07-08 12:30:46 +00:00
|
|
|
|
2020-07-09 14:50:34 +00:00
|
|
|
func set_direction(dir) -> void:
|
2020-07-08 12:30:46 +00:00
|
|
|
direction = dir
|
2020-07-08 22:12:14 +00:00
|
|
|
refresh_sprite()
|
2020-07-08 12:30:46 +00:00
|
|
|
|
2020-07-09 14:50:34 +00:00
|
|
|
func set_flow(val) -> void:
|
2020-07-08 12:30:46 +00:00
|
|
|
flow = val
|
2020-07-08 22:12:14 +00:00
|
|
|
refresh_sprite()
|
2020-07-08 12:30:46 +00:00
|
|
|
|
2020-07-09 14:50:34 +00:00
|
|
|
func refresh_sprite() -> void:
|
2020-07-08 22:12:14 +00:00
|
|
|
if socket == null:
|
|
|
|
return
|
2020-07-08 12:30:46 +00:00
|
|
|
match direction:
|
|
|
|
Direction.DOWN:
|
2020-07-08 22:12:14 +00:00
|
|
|
socket.region_rect.position = Vector2(0, 0)
|
2020-07-08 12:30:46 +00:00
|
|
|
Direction.UP:
|
2020-07-08 22:12:14 +00:00
|
|
|
socket.region_rect.position = Vector2(32, 0)
|
2020-07-08 12:30:46 +00:00
|
|
|
Direction.LEFT:
|
2020-07-08 22:12:14 +00:00
|
|
|
socket.region_rect.position = Vector2(32, 32)
|
2020-07-08 12:30:46 +00:00
|
|
|
Direction.RIGHT:
|
2020-07-08 22:12:14 +00:00
|
|
|
socket.region_rect.position = Vector2(0, 32)
|
2020-07-08 12:30:46 +00:00
|
|
|
match flow:
|
|
|
|
Flow.SOURCE:
|
2020-07-08 22:12:14 +00:00
|
|
|
socket.material.set_shader_param("cable_color", source_color)
|
2020-07-08 12:30:46 +00:00
|
|
|
Flow.SINK:
|
2020-07-08 22:12:14 +00:00
|
|
|
socket.material.set_shader_param("cable_color", sink_color)
|
|
|
|
Flow.BIDIRECTIONAL:
|
|
|
|
socket.material.set_shader_param("cable_color", bidirectional_color)
|
2020-07-13 19:01:36 +00:00
|
|
|
|
|
|
|
func serialize():
|
2020-07-13 20:18:02 +00:00
|
|
|
var connection_paths = []
|
|
|
|
for conn in connections:
|
|
|
|
connection_paths.append({ "path": get_path_to(conn), "powered": conn.powered })
|
|
|
|
|
2020-07-13 19:01:36 +00:00
|
|
|
return {
|
|
|
|
"direction": direction,
|
|
|
|
"flow": flow,
|
2020-07-13 20:18:02 +00:00
|
|
|
"connections": connection_paths
|
2020-07-13 19:01:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func deserialize(data):
|
|
|
|
set_direction(data["direction"])
|
|
|
|
set_flow(data["flow"])
|
2020-07-13 20:18:02 +00:00
|
|
|
for node in data["connections"]:
|
|
|
|
var manager = get_node(node.path) as PowerManager
|
|
|
|
manager.socket = self
|
|
|
|
manager.powered = node.powered
|
|
|
|
connections.append(manager)
|