summaryrefslogtreecommitdiff
path: root/Units/Unit.gd
diff options
context:
space:
mode:
Diffstat (limited to 'Units/Unit.gd')
-rw-r--r--Units/Unit.gd55
1 files changed, 44 insertions, 11 deletions
diff --git a/Units/Unit.gd b/Units/Unit.gd
index 6c3b254..8373a59 100644
--- a/Units/Unit.gd
+++ b/Units/Unit.gd
@@ -23,6 +23,11 @@ var is_selected = false :
var is_hovered = false :
set(value):
+ if value:
+ $Label.visible = true
+ else:
+ if not is_selected:
+ $Label.visible = false
is_hovered = value
queue_redraw()
@@ -39,9 +44,6 @@ var current_path_idx = 0 :
if line:
line.points = PackedVector2Array(current_path.slice(value))
-var previous_path: PackedVector2Array
-var previous_path_idx = 0
-
var previous_position: Vector2
var recent_closest_paths: Array[PackedVector2Array]
@@ -52,15 +54,15 @@ var roaming_mode = false
# rpc owner id
var owner_id = 1
-# unique shared id on the network on all clients
-var network_id
-
@export var base_speed: float = 100
@export var speed: float = base_speed
-@export var hp = 50
+@export var hp = 50 :
+ set = set_hp
@onready var line: Line2D = $UnitPathLine.duplicate()
+@onready var sprite = $Sprite2D
+
func _ready():
if not target:
@@ -89,6 +91,12 @@ func _ready():
func _physics_process(delta):
+ if get_multiplayer_authority() != multiplayer.get_unique_id():
+ Network.update_unit.rpc(get_path(), {
+ "hp": hp,
+ })
+ return
+
previous_position = global_position
if not current_path.is_empty():
@@ -113,6 +121,12 @@ func _physics_process(delta):
reset_path()
else:
stuck_position_accumulator = 0
+
+ Network.update_unit.rpc(get_path(), {
+ "position": global_position,
+ "hp": hp,
+ "sprite": {"self_modulate": $Sprite2D.self_modulate}
+ })
func _draw():
@@ -139,8 +153,12 @@ func _draw():
func _on_navigation_base_area_entered(area: Area2D):
+ if get_multiplayer_authority() != multiplayer.get_unique_id():
+ return
+
if area.is_in_group("goal"):
Client.player.score += 1
+ Client.update_player()
queue_free()
if area.is_in_group("path"):
var path_node = area.get_parent()
@@ -150,7 +168,7 @@ func _on_navigation_base_area_entered(area: Area2D):
func walk_along_path(path: PackedVector2Array, index: int, delta: float):
immediate_target = path[index]
- var displacement := (path[index]) - global_position
+ var displacement := immediate_target - global_position
var direction := displacement.normalized()
var distance := displacement.length()
@@ -161,7 +179,7 @@ func walk_along_path(path: PackedVector2Array, index: int, delta: float):
if effect.has_method("apply_physics"):
effect.apply_physics(delta)
# todo: changing velocity here doesn't work nicely
- # todo: because the velocity expects to each the point, so it stutters
+ # todo: because the velocity expects ??to each the point??, so it stutters
move_and_slide()
@@ -170,9 +188,11 @@ func set_hp(value):
# TODO: rpc on damage
hp = value
- %HPBar.set_value(value)
- $Label.text = str(hp)
+ if get_node("%HPBar"):
+ %HPBar.set_value(value)
+ if get_node("Label"):
+ $Label.text = str(hp)
if hp <= 0:
queue_free()
@@ -192,6 +212,9 @@ func reset_path():
current_path = get_grid_path()
if current_path.is_empty():
+ # TODO: nimm letzte route davor die noch ging, falls verfügbar
+ # TODO: schneide ab bis eins vor neue tower position. (=partial path)
+ # TODO: laufe bis dahin und checke dann end of partial path
current_path = get_grid_path(true)
recent_closest_paths.append(current_path)
@@ -224,6 +247,10 @@ func get_grid_path(partial = false):
func _on_selection_area_input_event(_viewport: Node, event: InputEvent, _shape_idx: int) -> void:
+ # disable remote select for now
+ if owner_id != multiplayer.get_unique_id():
+ return
+
if Client.state is StateDefault:
if event.is_action_pressed("select"):
if selected_unit:
@@ -243,14 +270,20 @@ func _on_selection_area_mouse_exited() -> void:
func _on_tree_exiting() -> void:
is_selected = false
line.queue_free()
+
+ #if get_multiplayer_authority() == multiplayer.get_unique_id():
+ Network.remove_unit.rpc(get_path())
func get_rpc_properties():
return {
+ "name": null,
"global_position": null,
"target": "res://Stages/Paths/PathNode.tscn",
"hp": null,
"speed": null,
"current_path": null,
"current_path_idx": null,
+ "sprite": "node://Sprite2D",
+ "owner_id": null,
}