summaryrefslogtreecommitdiff
path: root/Game
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2024-10-12 23:09:56 +0200
committerDaniel Weipert <git@mail.dweipert.de>2024-10-12 23:09:56 +0200
commit0697f674ec33f8381ba68cf064732ee40e6e584f (patch)
tree89334f288356390d193fa327bb3895fa366985b5 /Game
parent647e2e1812c00e29337a61560644f7aa91d0eb0f (diff)
next commit
Diffstat (limited to 'Game')
-rw-r--r--Game/Client.gd31
-rw-r--r--Game/Network.gd47
-rw-r--r--Game/Player.gd7
-rw-r--r--Game/Selection/multi_select_area.gd2
-rw-r--r--Game/States/Build/BuilderElement.gd16
-rw-r--r--Game/States/Build/StateBuild.gd10
-rw-r--r--Game/States/Build/build_grid.gd14
7 files changed, 50 insertions, 77 deletions
diff --git a/Game/Client.gd b/Game/Client.gd
index 5051dc6..db33a26 100644
--- a/Game/Client.gd
+++ b/Game/Client.gd
@@ -8,7 +8,7 @@ var state: State :
state = value
stage_state_changed.emit(value)
-var stage: Stage
+var current_stage: Stage
var player: Player:
get():
@@ -19,34 +19,25 @@ func _ready():
pass
-func initialize_stage(current_stage: Stage):
- stage = current_stage
+func initialize_stage(stage: Stage):
+ current_stage = stage
-func place_tower(tower: Tower, position: Vector2):
+func place_tower(tower: Tower):
var network_id = multiplayer.get_unique_id()
tower.owner_id = network_id
- #tower.set_multiplayer_authority(network_id)
tower.name = "Tower@" + str(network_id) + "@" + str(Time.get_ticks_usec())
- stage.place_tower(tower, position)
- Network.place_tower.rpc(inst_to_dict(tower.to_network_data()))
- #Network.place_tower.rpc(Network.to_rpc_object(tower), position)
-
- player.towers[position] = tower
- player.score += 1
- Network.update_player.rpc({"score": player.score})
+ current_stage.place_tower.rpc(inst_to_dict(tower.to_network_data()))
func remove_tower(tower: Tower):
if tower.owner_id == multiplayer.get_unique_id():
destroy_tower(tower)
- player.score -= 1
- Network.update_player.rpc({"score": player.score})
func destroy_tower(tower: Tower):
- stage.destroy_tower(tower)
+ current_stage.destroy_tower(tower)
Network.destroy_tower.rpc(tower.global_position)
player.towers.erase(tower.global_position)
@@ -67,10 +58,9 @@ func update_tower(path: NodePath, data: Tower.NetworkData):
Network.update_tower.rpc(path, inst_to_dict(data))
-func spawn_unit(unit: Unit, spawn: Spawn):
+func spawn_unit(unit: Unit, spawn: Spawn, overwrite_target: PathNode = null):
var network_id = multiplayer.get_unique_id()
unit.owner_id = network_id
- #unit.set_multiplayer_authority(network_id)
unit.name = "Unit@" + str(network_id) + "@" + str(Time.get_ticks_usec())
unit.global_position = spawn.spawn_position
@@ -78,12 +68,11 @@ func spawn_unit(unit: Unit, spawn: Spawn):
unit.hp = randi_range(50, 150) #20000b
unit.speed = randi_range(100, 150)
- stage.spawn_unit(unit)
+ if overwrite_target:
+ unit.target = overwrite_target
#var data := UnitCreationData.fromUnit(unit)
- var data := unit.to_network_data()
- Network.spawn_unit.rpc(inst_to_dict(data))
- #Network.spawn_unit.rpc(Network.to_rpc_object(unit), Network.to_rpc_object(spawn))
+ current_stage.spawn_unit.rpc(inst_to_dict(unit.to_network_data()))
func array_intersect(first, second):
diff --git a/Game/Network.gd b/Game/Network.gd
index 1fabeaf..558c8fa 100644
--- a/Game/Network.gd
+++ b/Game/Network.gd
@@ -2,6 +2,7 @@ extends Node
signal players_changed
+signal player_joined(player: Player)
func _ready():
@@ -70,6 +71,9 @@ func get_player(id: int) -> Player:
return %Players.get_node(str(id))
+func get_players() -> Array[Node]:
+ return %Players.get_children()
+
func get_ordered_player_ids() -> Array:
var keys = %Players.get_children().map(func(item: Player):
return item.id
@@ -91,42 +95,31 @@ func add_to_players(remote_data: Dictionary):
%Players.add_child(player)
+ player_joined.emit(player)
players_changed.emit()
-@rpc("any_peer")
-func update_player(remote_data: Dictionary):
- var id = multiplayer.get_remote_sender_id()
+@rpc("any_peer", "call_local")
+func update_player(id: int, remote_data: Dictionary):
var player = get_player(id)
- player.score = remote_data.score
+ for property in remote_data:
+ if typeof(player[property]) == TYPE_INT:
+ player[property] += remote_data[property]
+ else:
+ player[property] = remote_data[property]
players_changed.emit()
@rpc("any_peer")
-#func place_tower(remote_tower: Dictionary, position: Vector2):
-func place_tower(remote_data: Dictionary):
- print(remote_data)
- var data: Tower.NetworkData = dict_to_inst(remote_data)
- var remote_player_id = multiplayer.get_remote_sender_id()
-
- var tower = Tower.from_network_data(data)
- tower.owner_id = remote_player_id
-
- Network.get_player(remote_player_id).towers[tower.global_position] = tower
-
- Client.stage.place_tower(tower, tower.global_position)
-
-
-@rpc("any_peer")
#func destroy_tower(remote_tower: Dictionary):
func destroy_tower(position: Vector2):
var owner_id = multiplayer.get_remote_sender_id()
var player = get_player(owner_id)
var tower = player.towers.get(position)
- Client.stage.destroy_tower(tower)
+ Client.current_stage.destroy_tower(tower)
@rpc("any_peer")
@@ -139,20 +132,6 @@ func update_tower(remote_tower_node_path: NodePath, remote_data: Dictionary):
@rpc("any_peer")
-func spawn_unit(remote_data: Dictionary):#, _remote_spawn: Dictionary):
- #print(remote_data)
- var data: Unit.NetworkData = dict_to_inst(remote_data)
- var unit := Unit.from_network_data(data)
- #var spawn = from_rpc_object(remote_spawn, "res://Stages/Paths/Spawn.tscn")
-
- var remote_id = multiplayer.get_remote_sender_id()
- unit.owner_id = remote_id
- #unit.set_multiplayer_authority(remote_id)
-
- Client.stage.spawn_unit(unit)
-
-
-@rpc("any_peer")
func remove_unit(remote_unit_node_path):
var unit = get_tree().current_scene.get_node_or_null(remote_unit_node_path)
if unit:
diff --git a/Game/Player.gd b/Game/Player.gd
index dc81e0a..d0fbff5 100644
--- a/Game/Player.gd
+++ b/Game/Player.gd
@@ -2,15 +2,8 @@ class_name Player
extends Node
-signal score_changed
-
var username: String
-var score: int :
- set(value):
- score = value
- score_changed.emit()
-
var id: int:
set(value):
id = value
diff --git a/Game/Selection/multi_select_area.gd b/Game/Selection/multi_select_area.gd
index 9b2e8ce..c3cc902 100644
--- a/Game/Selection/multi_select_area.gd
+++ b/Game/Selection/multi_select_area.gd
@@ -5,7 +5,7 @@ signal select(nodes: Array)
func _ready() -> void:
- var camera_rect = Client.stage.get_node("Camera").get_rect()
+ var camera_rect = Client.current_stage.get_node("Camera").get_rect()
global_position = camera_rect.position + camera_rect.size / 2
diff --git a/Game/States/Build/BuilderElement.gd b/Game/States/Build/BuilderElement.gd
index c698f07..eb5ae12 100644
--- a/Game/States/Build/BuilderElement.gd
+++ b/Game/States/Build/BuilderElement.gd
@@ -25,7 +25,7 @@ func _ready():
var element_collision_shape = instance.get_node("CollisionShape2D").duplicate() as CollisionShape2D
collision_shape = element_collision_shape.shape
- var tile_size = Client.stage.map.tile_set.tile_size
+ var tile_size = Client.current_stage.map.tile_set.tile_size
var snapped_size = collision_shape.size.snapped(tile_size)
for x in (snapped_size.x / tile_size.x):
for y in (snapped_size.y / tile_size.y):
@@ -39,9 +39,15 @@ func _ready():
collision_visibility_area.set_collision_mask_value(1, true)
collision_visibility_area.set_collision_mask_value(2, true)
collision_visibility_area.set_collision_mask_value(3, true)
+ collision_visibility_area.set_collision_mask_value(4, true)
+ collision_visibility_area.set_collision_mask_value(8, true)
collision_visibility_area.colliding_color = Color(1, 0, 0, 0.5)
collision_visibility_area.not_colliding_color = Color(0, 1, 0, 0.25)
+ if Client.current_stage.has_method("get_builder_collision_masks"):
+ for idx in Client.current_stage.get_builder_collision_masks():
+ collision_visibility_area.set_collision_mask_value(idx, true)
+
var shape = RectangleShape2D.new()
shape.size = Vector2(tile_size - Vector2i(1, 1))
var cshape = CollisionShape2D.new()
@@ -56,7 +62,7 @@ func _ready():
func _draw():
draw_circle(
- Client.stage.map.tile_set.tile_size,
+ Client.current_stage.map.tile_set.tile_size,
8 + instance.attack_range,
Color(1, 1, 1, 0.75),
false,
@@ -66,7 +72,7 @@ func _draw():
func _process(_delta):
previous_position = global_position
- global_position = get_global_mouse_position().snapped(Client.stage.map.tile_set.tile_size) - Vector2(16,16)
+ global_position = get_global_mouse_position().snapped(Client.current_stage.map.tile_set.tile_size) - Vector2(16,16)
if global_position != previous_position:
queue_redraw()
@@ -82,6 +88,10 @@ func can_build():
if area.get_overlapping_bodies().size() > 0:
return false
+
+
+ if Client.current_stage.has_method("can_build"):
+ return Client.current_stage.can_build()
return true
diff --git a/Game/States/Build/StateBuild.gd b/Game/States/Build/StateBuild.gd
index f16ee3d..dee5084 100644
--- a/Game/States/Build/StateBuild.gd
+++ b/Game/States/Build/StateBuild.gd
@@ -18,13 +18,15 @@ func _state_input(event: InputEvent):
if current_builder_element and current_builder_element.can_build():
var placed_tower = current_builder_element.element.instantiate() as Tower
+
+ placed_tower.global_position = current_builder_element.global_position
placed_tower.attack_range = [
- Client.stage.map.tile_set.tile_size.x * 2,
- Client.stage.map.tile_set.tile_size.x * 3,
- Client.stage.map.tile_set.tile_size.x * 4,
+ Client.current_stage.map.tile_set.tile_size.x * 2,
+ Client.current_stage.map.tile_set.tile_size.x * 3,
+ Client.current_stage.map.tile_set.tile_size.x * 4,
].pick_random()
- Client.place_tower(placed_tower, current_builder_element.global_position)
+ Client.place_tower(placed_tower)
if not Input.is_action_pressed("builder_tower_place_keep"):
current_builder_element.queue_free()
diff --git a/Game/States/Build/build_grid.gd b/Game/States/Build/build_grid.gd
index 6fd93aa..766c79b 100644
--- a/Game/States/Build/build_grid.gd
+++ b/Game/States/Build/build_grid.gd
@@ -2,20 +2,20 @@ extends Node2D
func _draw():
- var area = Client.stage.map.get_used_rect()
- var cell_size = Client.stage.map.tile_set.tile_size
- var offset = Client.stage.map.position
+ var area = Client.current_stage.map.get_used_rect()
+ var cell_size = Client.current_stage.map.tile_set.tile_size
+ var offset = area.position
for row in range(0, area.size.y + 1):
draw_line(
- Vector2(offset.x, offset.y + row * cell_size.y),
- Vector2(offset.x + area.size.x * cell_size.x, offset.y + row * cell_size.y),
+ Vector2(offset.x * cell_size.x, offset.y + row * cell_size.y),
+ Vector2(offset.x * cell_size.x + area.size.x * cell_size.x, offset.y + row * cell_size.y),
Color(1, 1, 1, 0.25),
1.0
)
for column in range(0, area.size.x + 1):
draw_line(
- Vector2(offset.x + column * cell_size.x, offset.y),
- Vector2(offset.x + column * cell_size.x, offset.y + area.size.y * cell_size.y),
+ Vector2(offset.x * cell_size.x + column * cell_size.x, offset.y),
+ Vector2(offset.x * cell_size.x + column * cell_size.x, offset.y + area.size.y * cell_size.y),
Color(1, 1, 1, 0.25),
1.0
)