diff options
Diffstat (limited to 'Game')
-rw-r--r-- | Game/Client.gd | 31 | ||||
-rw-r--r-- | Game/Network.gd | 51 | ||||
-rw-r--r-- | Game/Player.gd | 2 | ||||
-rw-r--r-- | Game/Selection/selectable_area.gd | 12 | ||||
-rw-r--r-- | Game/Selection/selection_rectangle.gd | 15 | ||||
-rw-r--r-- | Game/States/Build/BuilderElement.gd | 11 | ||||
-rw-r--r-- | Game/States/Build/StateBuild.gd | 22 | ||||
-rw-r--r-- | Game/States/Default/StateDefault.gd | 10 | ||||
-rw-r--r-- | Game/States/StateManager.gd | 4 |
9 files changed, 110 insertions, 48 deletions
diff --git a/Game/Client.gd b/Game/Client.gd index 3d9cf34..dbaf69e 100644 --- a/Game/Client.gd +++ b/Game/Client.gd @@ -9,11 +9,11 @@ var state: State : stage_state_changed.emit(value) var stage: Stage -var player: Player +var player: Player = Player.new() func _ready(): - player = Player.new() + #player = Player.new() player.id = multiplayer.get_unique_id() @@ -29,11 +29,12 @@ func initialize_stage(current_stage: Stage): func place_tower(tower: Tower, position: Vector2): var network_id = multiplayer.get_unique_id() tower.owner_id = network_id - tower.set_multiplayer_authority(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(Network.to_rpc_object(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 @@ -48,8 +49,10 @@ func remove_tower(tower: Tower): func destroy_tower(tower: Tower): - stage.destroy_tower(tower) - Network.destroy_tower.rpc(Network.to_rpc_object(tower)) + if multiplayer.is_server(): + stage.destroy_tower(tower) + else: + Network.destroy_tower.rpc_id(1, tower.global_position) player.towers.erase(tower.global_position) update_player() @@ -66,19 +69,27 @@ func deselect_tower(): tower.is_selected = false +func update_tower(tower: Tower, data: Dictionary): + Network.update_tower.rpc(tower.get_path(), data) + + func spawn_unit(unit: Unit, spawn: Spawn): var network_id = multiplayer.get_unique_id() unit.owner_id = network_id - unit.set_multiplayer_authority(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 - unit.target = spawn.next_node + unit.target = spawn.next_node.path_position unit.hp = randi_range(50, 150) #20000b unit.speed = randi_range(100, 150) - stage.spawn_unit(unit, spawn) - Network.spawn_unit.rpc(Network.to_rpc_object(unit), Network.to_rpc_object(spawn)) + stage.spawn_unit(unit) + + #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)) func array_intersect(first, second): diff --git a/Game/Network.gd b/Game/Network.gd index 9349245..48558ce 100644 --- a/Game/Network.gd +++ b/Game/Network.gd @@ -156,35 +156,55 @@ func get_nested_node_and_property(node: Node, property: String) -> Dictionary: @rpc("any_peer") -func place_tower(remote_tower: Dictionary, position: Vector2): +#func place_tower(remote_tower: Dictionary, position: Vector2): +func place_tower(remote_data: Dictionary): + var data: Tower.NetworkData = dict_to_inst(remote_data) var remote_player_id = multiplayer.get_remote_sender_id() - var tower = from_rpc_object(remote_tower, "res://Towers/Tower.tscn") + var tower = Tower.from_network_data(data) tower.owner_id = remote_player_id players[remote_player_id].towers[tower.global_position] = tower - Client.stage.place_tower(tower, position) + Client.stage.place_tower(tower, tower.global_position) @rpc("any_peer") -func destroy_tower(remote_tower: Dictionary): - var player = players[remote_tower.owner_id] as Player - var tower = player.towers.get(remote_tower.global_position) +#func destroy_tower(remote_tower: Dictionary): +func destroy_tower(position: Vector2): + var owner_id = multiplayer.get_remote_sender_id() + var player = players[owner_id] as Player + var tower = player.towers.get(position) Client.stage.destroy_tower(tower) @rpc("any_peer") -func spawn_unit(remote_unit: Dictionary, remote_spawn: Dictionary): - var unit = from_rpc_object(remote_unit, "res://Units/Unit.tscn") - var spawn = from_rpc_object(remote_spawn, "res://Stages/Paths/Spawn.tscn") +func update_tower(remote_tower_node_path, data): + var tower: Tower = get_tree().current_scene.get_node_or_null(remote_tower_node_path) + if tower: + if "components" in data: + for c in tower.components.duplicate(): + tower.remove_component(c.name) + for c in data.components: + tower.add_component( + load("res://Towers/Components/" + c + "Component.gd").new() + ) + + +@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) + #unit.set_multiplayer_authority(remote_id) - Client.stage.spawn_unit(unit, spawn) + Client.stage.spawn_unit(unit) + @rpc("any_peer") func remove_unit(remote_unit_node_path): @@ -192,13 +212,20 @@ func remove_unit(remote_unit_node_path): if unit: unit.queue_free() + @rpc("any_peer") func update_unit(remote_unit_node_path, data): var unit: Unit = get_tree().current_scene.get_node_or_null(remote_unit_node_path) if unit: + if "hp" in data: + unit.hp = data.hp if "position" in data: unit.position = data.position - unit.hp = data.hp if "sprite" in data: unit.get_node("Sprite2D").self_modulate = data.sprite.self_modulate + if "current_path" in data: + unit.current_path = data.current_path + unit.line.points = PackedVector2Array(data.current_path) + if "current_path_idx" in data: + unit.current_path_idx = data.current_path_idx diff --git a/Game/Player.gd b/Game/Player.gd index 43856d5..dce19db 100644 --- a/Game/Player.gd +++ b/Game/Player.gd @@ -5,6 +5,7 @@ extends Resource signal score_changed var id := 1 +var name := "" var towers: Dictionary : set(value): @@ -32,5 +33,6 @@ func get_color(): func get_rpc_properties() -> Dictionary: return { "id": null, + "name": null, "score": null, } diff --git a/Game/Selection/selectable_area.gd b/Game/Selection/selectable_area.gd index 59d4be7..b3336f1 100644 --- a/Game/Selection/selectable_area.gd +++ b/Game/Selection/selectable_area.gd @@ -6,12 +6,12 @@ signal hover_exit signal select(event: InputEvent) -func _on_area_entered(_area: Area2D) -> void: - hover_enter.emit() - - -func _on_area_exited(_area: Area2D) -> void: - hover_exit.emit() +#func _on_area_entered(_area: Area2D) -> void: + #hover_enter.emit() +# +# +#func _on_area_exited(_area: Area2D) -> void: + #hover_exit.emit() func _on_mouse_entered() -> void: diff --git a/Game/Selection/selection_rectangle.gd b/Game/Selection/selection_rectangle.gd index a57f25b..5e7d1c1 100644 --- a/Game/Selection/selection_rectangle.gd +++ b/Game/Selection/selection_rectangle.gd @@ -14,7 +14,9 @@ var size: Vector2 = Vector2.ZERO : var is_active: bool : get(): - return abs(size) > Vector2(1, 1) + return abs(size) > Vector2(1, 1) and Client.state is StateDefault + +@onready var camera: Camera2D = get_tree().get_first_node_in_group("camera") func _process(_delta: float) -> void: @@ -30,7 +32,8 @@ func _process(_delta: float) -> void: if is_active and Input.is_action_just_released("select"): for area in get_overlapping_areas(): if "is_selected" in area.get_parent(): - area.get_parent().is_selected = true + if area.get_parent().owner_id == multiplayer.get_unique_id(): + area.get_parent().is_selected = true size = Vector2.ZERO queue_redraw() @@ -40,12 +43,14 @@ func _draw(): if is_active: var rect = Rect2(Vector2.ZERO, size) draw_rect(rect, color_background) - draw_rect(rect, color_border, false, 2.0) + draw_rect(rect, color_border, false, 1.25 / max(camera.zoom.x, camera.zoom.y)) func _on_area_entered(area: Area2D) -> void: - area.get_parent().is_hovered = true + if area.get_parent().owner_id == multiplayer.get_unique_id(): + area.get_parent().is_hovered = true func _on_area_exited(area: Area2D) -> void: - area.get_parent().is_hovered = false + if area.get_parent().owner_id == multiplayer.get_unique_id(): + area.get_parent().is_hovered = false diff --git a/Game/States/Build/BuilderElement.gd b/Game/States/Build/BuilderElement.gd index bb10b06..c698f07 100644 --- a/Game/States/Build/BuilderElement.gd +++ b/Game/States/Build/BuilderElement.gd @@ -2,7 +2,8 @@ class_name BuilderElement extends Node2D -@export var element: Node2D +@export var element: Resource +var instance: Tower var collision_shape: Shape2D @@ -12,14 +13,16 @@ var collision_areas: Array[Area2D] = [] func _ready(): - var element_sprite: Sprite2D = element.get_node("Sprite2D") + instance = element.instantiate() + + var element_sprite: Sprite2D = instance.get_node("Sprite2D") $Sprite2D.texture = element_sprite.texture $Sprite2D.region_enabled = element_sprite.region_enabled $Sprite2D.region_rect = element_sprite.region_rect $Sprite2D.scale = element_sprite.scale $Sprite2D.position = element_sprite.position - var element_collision_shape = element.get_node("CollisionShape2D").duplicate() as CollisionShape2D + 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 @@ -54,7 +57,7 @@ func _ready(): func _draw(): draw_circle( Client.stage.map.tile_set.tile_size, - 8 + element.attack_range, + 8 + instance.attack_range, Color(1, 1, 1, 0.75), false, 1.0 diff --git a/Game/States/Build/StateBuild.gd b/Game/States/Build/StateBuild.gd index d2fb39d..f16ee3d 100644 --- a/Game/States/Build/StateBuild.gd +++ b/Game/States/Build/StateBuild.gd @@ -17,14 +17,14 @@ func _state_input(event: InputEvent): if event.is_action_pressed("builder_tower_select"): if current_builder_element and current_builder_element.can_build(): - var placed_tower = current_builder_element.element.duplicate() as Tower - Client.place_tower(placed_tower, current_builder_element.global_position) + var placed_tower = current_builder_element.element.instantiate() as Tower + 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, + ].pick_random() - placed_tower.selected.connect(func(): - Client.stage.hud.tower.text = "Range: %s - Power: %s - Speed: %s" % [ - placed_tower.attack_range, placed_tower.attack_power, placed_tower.attack_speed - ] - ) + Client.place_tower(placed_tower, current_builder_element.global_position) if not Input.is_action_pressed("builder_tower_place_keep"): current_builder_element.queue_free() @@ -38,3 +38,11 @@ func _state_input(event: InputEvent): current_builder_element = null set_state("StateDefault") + + if event.is_action_pressed("builder_cancel") and current_builder_element: + current_builder_element.queue_free() + current_builder_element = null + + +func _state_unhandled_input(_event: InputEvent) -> void: + pass diff --git a/Game/States/Default/StateDefault.gd b/Game/States/Default/StateDefault.gd index 1d2dd9f..b7b630c 100644 --- a/Game/States/Default/StateDefault.gd +++ b/Game/States/Default/StateDefault.gd @@ -6,6 +6,12 @@ func _state_input(event: InputEvent) -> void: if event.is_action_pressed("build_mode_start"): set_state("StateBuild") + if event.is_action_pressed("select"): + if Unit.selected_unit: + Unit.selected_unit.is_selected = false + + +func _state_unhandled_input(event: InputEvent) -> void: # deselect if event.is_action_pressed("select"): # if not multi selecting @@ -13,7 +19,3 @@ func _state_input(event: InputEvent) -> void: if Tower.selected_towers: for tower in Tower.selected_towers.duplicate(): tower.is_selected = false - - if event.is_action_pressed("select"): - if Unit.selected_unit: - Unit.selected_unit.is_selected = false diff --git a/Game/States/StateManager.gd b/Game/States/StateManager.gd index e62cdb4..5a176e2 100644 --- a/Game/States/StateManager.gd +++ b/Game/States/StateManager.gd @@ -10,6 +10,10 @@ func _input(event: InputEvent) -> void: Client.state._state_input(event) +func _unhandled_input(event: InputEvent) -> void: + Client.state._state_unhandled_input(event) + + func set_state(state: Variant): if state is not State: state = get_node(state) |