Calculate source/usage

This commit is contained in:
Hamcha 2020-07-09 09:19:17 +02:00
parent 75b760086d
commit db74d4135d
8 changed files with 50 additions and 24 deletions

View file

@ -5,6 +5,18 @@ class_name PowerManager
signal power_connected() signal power_connected()
signal power_disconnected() signal power_disconnected()
var wired = false var socket = false
var working = false
var wired setget , get_wired
export var power_usage = 0.0
export var power_source = 0.0
const DEBUG = true const DEBUG = true
func get_wired() -> bool:
return socket != null
func _physics_process(_delta):
if wired:
pass

View file

@ -1,5 +1,7 @@
tool tool
class_name ElectricSocket
extends Area2D extends Area2D
enum Direction { LEFT, RIGHT, UP, DOWN } enum Direction { LEFT, RIGHT, UP, DOWN }
@ -18,7 +20,7 @@ export(Flow) var flow = Flow.SINK setget set_flow
onready var socket = $socket onready var socket = $socket
var network: PowerNetwork = null var network = null
func _ready(): func _ready():
socket.material = socket.material.duplicate() socket.material = socket.material.duplicate()
@ -28,7 +30,7 @@ func _ready():
func add_connection(node: Node): func add_connection(node: Node):
var manager = node.get_node("PowerManager") as PowerManager var manager = node.get_node("PowerManager") as PowerManager
manager.wired = true manager.socket = self
func set_direction(dir): func set_direction(dir):
direction = dir direction = dir
@ -41,20 +43,15 @@ func set_flow(val):
func refresh_sprite(): func refresh_sprite():
if socket == null: if socket == null:
return return
var rot = 0
match direction: match direction:
Direction.DOWN: Direction.DOWN:
socket.region_rect.position = Vector2(0, 0) socket.region_rect.position = Vector2(0, 0)
rot = 0
Direction.UP: Direction.UP:
socket.region_rect.position = Vector2(32, 0) socket.region_rect.position = Vector2(32, 0)
rot = PI
Direction.LEFT: Direction.LEFT:
socket.region_rect.position = Vector2(32, 32) socket.region_rect.position = Vector2(32, 32)
rot = PI/2
Direction.RIGHT: Direction.RIGHT:
socket.region_rect.position = Vector2(0, 32) socket.region_rect.position = Vector2(0, 32)
rot = -PI/2
match flow: match flow:
Flow.SOURCE: Flow.SOURCE:
socket.material.set_shader_param("cable_color", source_color) socket.material.set_shader_param("cable_color", source_color)

View file

@ -14,7 +14,7 @@ func _ready():
if not Engine.editor_hint: if not Engine.editor_hint:
activationRange.visible = true activationRange.visible = true
func _physics_process(delta): func _physics_process(_delta):
if $PowerManager.wired: if $PowerManager.wired:
if $PowerManager.DEBUG: if $PowerManager.DEBUG:
update() update()
@ -48,7 +48,7 @@ func _force_close_ui():
$UIAnimation.play("fadeout") $UIAnimation.play("fadeout")
func _draw(): func _draw():
if $PowerManager.DEBUG: if $PowerManager.DEBUG and $PowerManager.wired:
var charge_px = int(current_charge / MAX_CHARGE * 32) var charge_px = int(current_charge / MAX_CHARGE * 32)
var charge_color = Color.greenyellow var charge_color = Color.greenyellow
if charge_px < 20: if charge_px < 20:

View file

@ -2,5 +2,5 @@ extends Control
onready var scene = $"/root/scene" as GameInstance onready var scene = $"/root/scene" as GameInstance
func _physics_process(delta): func _physics_process(_delta):
pass pass

View file

@ -14,7 +14,7 @@ func _ready():
network.add_node(self) network.add_node(self)
$"/root/scene/systems".add_child(network, true) $"/root/scene/systems".add_child(network, true)
func _physics_process(delta): func _physics_process(_delta):
if PowerNetwork.DEBUG: if PowerNetwork.DEBUG:
update() update()

View file

@ -5,6 +5,7 @@ class_name PowerNetwork
const DEBUG = true const DEBUG = true
var nodes = [] var nodes = []
var sockets = []
var debugColor = Color.cyan var debugColor = Color.cyan
@ -14,6 +15,18 @@ func _ready():
func add_node(node): func add_node(node):
nodes.append(node) nodes.append(node)
if node is ElectricSocket:
sockets.append(node)
func remove_node(node):
var node_idx = nodes.find(node)
if node_idx >= 0:
nodes.remove(node_idx)
var sock_idx = sockets.find(node)
if sock_idx >= 0:
sockets.remove(sock_idx)
# Do other splitting here
node.network = null
func join(network): func join(network):
for node in network.nodes: for node in network.nodes:
@ -23,4 +36,11 @@ func join(network):
network.queue_free() network.queue_free()
func _physics_process(_delta): func _physics_process(_delta):
pass # Recalculate power availability and usage
var total_source = 0
var total_usage = 0
for socket in sockets:
for connection in socket.connections:
var manager = connection as PowerManager
total_source += manager.power_source
total_usage += manager.power_usage

View file

@ -84,17 +84,14 @@ __meta__ = {
[node name="Engine" parent="engines" instance=ExtResource( 10 )] [node name="Engine" parent="engines" instance=ExtResource( 10 )]
position = Vector2( -320, 321 ) position = Vector2( -320, 321 )
direction = 0 direction = 0
strength = 1.0
[node name="Engine2" parent="engines" instance=ExtResource( 10 )] [node name="Engine2" parent="engines" instance=ExtResource( 10 )]
position = Vector2( -320, 208 ) position = Vector2( -320, 208 )
direction = 0 direction = 0
strength = 1.0
[node name="Engine3" parent="engines" instance=ExtResource( 10 )] [node name="Engine3" parent="engines" instance=ExtResource( 10 )]
position = Vector2( -320, 96 ) position = Vector2( -320, 96 )
direction = 0 direction = 0
strength = 1.0
[node name="objects" type="Node2D" parent="."] [node name="objects" type="Node2D" parent="."]
__meta__ = { __meta__ = {
@ -103,32 +100,25 @@ __meta__ = {
[node name="Computer" parent="objects" instance=ExtResource( 6 )] [node name="Computer" parent="objects" instance=ExtResource( 6 )]
position = Vector2( -64, 128 ) position = Vector2( -64, 128 )
direction = 3
computer_type = 0
[node name="Computer2" parent="objects" instance=ExtResource( 6 )] [node name="Computer2" parent="objects" instance=ExtResource( 6 )]
position = Vector2( -32, 128 ) position = Vector2( -32, 128 )
direction = 3
computer_type = 1 computer_type = 1
[node name="Computer3" parent="objects" instance=ExtResource( 6 )] [node name="Computer3" parent="objects" instance=ExtResource( 6 )]
position = Vector2( 0, 128 ) position = Vector2( 0, 128 )
direction = 3
computer_type = 2 computer_type = 2
[node name="Computer4" parent="objects" instance=ExtResource( 6 )] [node name="Computer4" parent="objects" instance=ExtResource( 6 )]
position = Vector2( 32, 128 ) position = Vector2( 32, 128 )
direction = 3
computer_type = 3 computer_type = 3
[node name="Computer5" parent="objects" instance=ExtResource( 6 )] [node name="Computer5" parent="objects" instance=ExtResource( 6 )]
position = Vector2( -96, 128 ) position = Vector2( -96, 128 )
direction = 3
computer_type = 4 computer_type = 4
[node name="Computer6" parent="objects" instance=ExtResource( 6 )] [node name="Computer6" parent="objects" instance=ExtResource( 6 )]
position = Vector2( -128, 128 ) position = Vector2( -128, 128 )
direction = 3
computer_type = 5 computer_type = 5
[node name="StaticBody2D2" parent="objects" instance=ExtResource( 12 )] [node name="StaticBody2D2" parent="objects" instance=ExtResource( 12 )]
@ -139,7 +129,6 @@ position = Vector2( 128, 256 )
[node name="Computer7" parent="objects" instance=ExtResource( 6 )] [node name="Computer7" parent="objects" instance=ExtResource( 6 )]
position = Vector2( 64, 128 ) position = Vector2( 64, 128 )
direction = 3
computer_type = 6 computer_type = 6
[node name="Scanner" parent="objects" instance=ExtResource( 8 )] [node name="Scanner" parent="objects" instance=ExtResource( 8 )]
@ -147,10 +136,12 @@ position = Vector2( 288, 320 )
[node name="ElectricSocket" parent="objects" instance=ExtResource( 13 )] [node name="ElectricSocket" parent="objects" instance=ExtResource( 13 )]
position = Vector2( 128, 288 ) position = Vector2( 128, 288 )
connectionPaths = [ NodePath("../SMES1") ]
[node name="ElectricSocket2" parent="objects" instance=ExtResource( 13 )] [node name="ElectricSocket2" parent="objects" instance=ExtResource( 13 )]
position = Vector2( 256, 320 ) position = Vector2( 256, 320 )
direction = 1 direction = 1
connectionPaths = [ NodePath("../Scanner") ]
flow = 1 flow = 1
[node name="lights" type="Node2D" parent="."] [node name="lights" type="Node2D" parent="."]

View file

@ -14,6 +14,11 @@ _global_script_classes=[ {
"language": "GDScript", "language": "GDScript",
"path": "res://Actors/Components/ActivationRange.gd" "path": "res://Actors/Components/ActivationRange.gd"
}, { }, {
"base": "Area2D",
"class": "ElectricSocket",
"language": "GDScript",
"path": "res://Actors/Objects/ElectricSocket/ElectricSocket.gd"
}, {
"base": "Node", "base": "Node",
"class": "GameInstance", "class": "GameInstance",
"language": "GDScript", "language": "GDScript",
@ -96,6 +101,7 @@ _global_script_classes=[ {
} ] } ]
_global_script_class_icons={ _global_script_class_icons={
"ActivationRange": "", "ActivationRange": "",
"ElectricSocket": "",
"GameInstance": "", "GameInstance": "",
"GameMap": "", "GameMap": "",
"GameObjectComputer": "", "GameObjectComputer": "",