summaryrefslogtreecommitdiff
path: root/Units
diff options
context:
space:
mode:
Diffstat (limited to 'Units')
-rw-r--r--Units/Unit.gd101
-rw-r--r--Units/Unit.tscn22
-rw-r--r--Units/UnitPathLine.tscn10
3 files changed, 101 insertions, 32 deletions
diff --git a/Units/Unit.gd b/Units/Unit.gd
index 8373a59..3b91d73 100644
--- a/Units/Unit.gd
+++ b/Units/Unit.gd
@@ -31,7 +31,7 @@ var is_hovered = false :
is_hovered = value
queue_redraw()
-var target: Node2D :
+var target: Vector2 :
set(value):
target = value
reset_path()
@@ -52,7 +52,7 @@ var stuck_position_accumulator = 0.0
var roaming_mode = false
# rpc owner id
-var owner_id = 1
+@export var owner_id = 1
@export var base_speed: float = 100
@export var speed: float = base_speed
@@ -61,12 +61,12 @@ var owner_id = 1
@onready var line: Line2D = $UnitPathLine.duplicate()
-@onready var sprite = $Sprite2D
+@onready var sprite: Sprite2D = $Sprite2D
func _ready():
if not target:
- target = Client.stage.get_node("%Goal")
+ target = Client.stage.get_node("%Goal").path_position
Client.stage.units.add_child(line)
@@ -91,10 +91,10 @@ func _ready():
func _physics_process(delta):
- if get_multiplayer_authority() != multiplayer.get_unique_id():
- Network.update_unit.rpc(get_path(), {
- "hp": hp,
- })
+ if not multiplayer.is_server():
+ #Network.update_unit.rpc(get_path(), {
+ #"hp": hp,
+ #})
return
previous_position = global_position
@@ -122,11 +122,13 @@ func _physics_process(delta):
else:
stuck_position_accumulator = 0
- Network.update_unit.rpc(get_path(), {
- "position": global_position,
- "hp": hp,
- "sprite": {"self_modulate": $Sprite2D.self_modulate}
- })
+ #Network.update_unit.rpc(get_path(), {
+ #"position": global_position,
+ #"hp": hp,
+ #"sprite": {"self_modulate": $Sprite2D.self_modulate},
+ #"current_path_idx": current_path_idx,
+ #"current_path": current_path,
+ #})
func _draw():
@@ -153,7 +155,7 @@ func _draw():
func _on_navigation_base_area_entered(area: Area2D):
- if get_multiplayer_authority() != multiplayer.get_unique_id():
+ if not multiplayer.is_server():
return
if area.is_in_group("goal"):
@@ -161,9 +163,9 @@ func _on_navigation_base_area_entered(area: Area2D):
Client.update_player()
queue_free()
if area.is_in_group("path"):
- var path_node = area.get_parent()
- if path_node.path_position == target.path_position:
- target = path_node.next_node
+ var path_node: PathNode = area.get_parent()
+ if path_node.path_position == target:
+ target = path_node.next_node.path_position
func walk_along_path(path: PackedVector2Array, index: int, delta: float):
@@ -206,6 +208,13 @@ func get_effects():
return effects
+func add_effect(effect: Effect):
+ var node = get_node_or_null(NodePath(effect.name)) as Effect
+ if node:
+ node.set_duration(node.duration)
+ else:
+ add_child(effect)
+
func reset_path():
roaming_mode = false
@@ -221,12 +230,12 @@ func reset_path():
# reached end of partial path
if current_path.size() == 1 and current_path[0] == global_position:
roaming_mode = true
- current_path = PackedVector2Array([target.path_position + Vector2(16,16)])
+ current_path = PackedVector2Array([target + Vector2(16,16)])
# iterating between one or more closest paths
elif recent_closest_paths.count(current_path) >= 2:
roaming_mode = true
- current_path = PackedVector2Array([target.path_position + Vector2(16,16)])
+ current_path = PackedVector2Array([target + Vector2(16,16)])
recent_closest_paths = []
else:
recent_closest_paths = []
@@ -241,7 +250,7 @@ func reset_path():
func get_grid_path(partial = false):
return Client.stage.path_grid.get_point_path(
Client.stage.map.local_to_map(global_position),
- Client.stage.map.local_to_map(target.path_position),
+ Client.stage.map.local_to_map(target),
partial
)
@@ -257,7 +266,6 @@ func _on_selection_area_input_event(_viewport: Node, event: InputEvent, _shape_i
selected_unit.is_selected = false
is_selected = true
$Label.text = str(hp)
- add_child(preload("res://Effects/SlowEffect.tscn").instantiate())
func _on_selection_area_mouse_entered() -> void:
@@ -277,13 +285,46 @@ func _on_tree_exiting() -> void:
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,
+ #"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,
}
+
+
+class NetworkData extends Resource:
+ var name: String
+ var position: Vector2
+ var target_position: Vector2
+ var hp: int
+ var speed: float
+ var texture_path: String
+
+func to_network_data() -> NetworkData:
+ var data = NetworkData.new()
+
+ data.name = name
+ data.position = global_position
+ data.target_position = target
+ data.hp = hp
+ data.speed = speed
+ data.texture_path = get_node("Sprite2D").texture.resource_path
+
+ return data
+
+static func from_network_data(data: NetworkData) -> Unit:
+ var unit: Unit = preload("res://Units/Unit.tscn").instantiate()
+
+ unit.name = data.name
+ unit.global_position = data.position
+ unit.target = data.target_position
+ unit.hp = data.hp
+ unit.speed = data.speed
+ unit.get_node("Sprite2D").texture = load(data.texture_path)
+
+ return unit
diff --git a/Units/Unit.tscn b/Units/Unit.tscn
index 879df08..7e6acbc 100644
--- a/Units/Unit.tscn
+++ b/Units/Unit.tscn
@@ -1,4 +1,4 @@
-[gd_scene load_steps=8 format=3 uid="uid://cslaufgh6ber3"]
+[gd_scene load_steps=9 format=3 uid="uid://cslaufgh6ber3"]
[ext_resource type="Script" path="res://Units/Unit.gd" id="1_bbcew"]
[ext_resource type="Texture2D" uid="uid://dsy7k2v5fhh6v" path="res://Assets/Mobs/angesnow-front.png" id="2_rxqq1"]
@@ -14,6 +14,23 @@ size = Vector2(8, 8)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_o5ax3"]
size = Vector2(16, 16)
+[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_0buot"]
+properties/0/path = NodePath(".:position")
+properties/0/spawn = true
+properties/0/replication_mode = 2
+properties/1/path = NodePath(".:hp")
+properties/1/spawn = true
+properties/1/replication_mode = 2
+properties/2/path = NodePath("Sprite2D:self_modulate")
+properties/2/spawn = true
+properties/2/replication_mode = 2
+properties/3/path = NodePath(".:owner_id")
+properties/3/spawn = true
+properties/3/replication_mode = 0
+properties/4/path = NodePath("Sprite2D:modulate")
+properties/4/spawn = true
+properties/4/replication_mode = 2
+
[node name="Unit" type="CharacterBody2D"]
y_sort_enabled = true
collision_mask = 4
@@ -65,6 +82,9 @@ width = 1.0
default_color = Color(1, 1, 1, 0.392157)
target_circle_radius = 4.0
+[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."]
+replication_config = SubResource("SceneReplicationConfig_0buot")
+
[connection signal="tree_exiting" from="." to="." method="_on_tree_exiting"]
[connection signal="area_entered" from="NavigationBase" to="." method="_on_navigation_base_area_entered"]
[connection signal="input_event" from="SelectionArea" to="." method="_on_selection_area_input_event"]
diff --git a/Units/UnitPathLine.tscn b/Units/UnitPathLine.tscn
index 62458c4..c0ac3ab 100644
--- a/Units/UnitPathLine.tscn
+++ b/Units/UnitPathLine.tscn
@@ -1,6 +1,14 @@
-[gd_scene load_steps=2 format=3 uid="uid://cifs0kcy5r0x2"]
+[gd_scene load_steps=3 format=3 uid="uid://cifs0kcy5r0x2"]
[ext_resource type="Script" path="res://Units/unit_path_line.gd" id="1_qbhs7"]
+[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_wfkop"]
+properties/0/path = NodePath(".:points")
+properties/0/spawn = true
+properties/0/replication_mode = 2
+
[node name="UnitPathLine" type="Line2D"]
script = ExtResource("1_qbhs7")
+
+[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="."]
+replication_config = SubResource("SceneReplicationConfig_wfkop")