diff options
author | Daniel Weipert <git@mail.dweipert.de> | 2024-11-03 14:10:07 +0100 |
---|---|---|
committer | Daniel Weipert <git@mail.dweipert.de> | 2024-11-03 14:10:07 +0100 |
commit | 84365b6f901a9c0251a9f724a8d9f81a3787e97d (patch) | |
tree | 401e2bf99e6573d1d069f5044e7448067cdfb384 /Towers/Tower.gd | |
parent | a43499efa6c228cea3633f9661f3ff09127bd82d (diff) |
next commit
Diffstat (limited to 'Towers/Tower.gd')
-rw-r--r-- | Towers/Tower.gd | 155 |
1 files changed, 27 insertions, 128 deletions
diff --git a/Towers/Tower.gd b/Towers/Tower.gd index 8acdf38..2634966 100644 --- a/Towers/Tower.gd +++ b/Towers/Tower.gd @@ -5,6 +5,8 @@ extends StaticBody2D signal selected signal deselected +signal selected_secondary + static var selected_towers: Array[Tower] static var hovered_tower: Tower @@ -52,12 +54,18 @@ signal selection_group_id_changed(previous_id: String) # rpc owner id @export var owner_id = 1 -signal attack_value_changed(value: int) -@export var attack_range: int = 32 +@export var attack_range: int = 32: + set(value): + attack_range = value + $Range/CollisionShape2D.shape.radius = attack_range + @export var attack_power: int = 1 -@export var attack_speed: int = 1 -var speed_base: float = 2.0 +var attack_speed_base: float = 2.0 +@export var attack_speed: int = 1: + set(value): + attack_speed = value + %ShootCooldown.wait_time = attack_speed_base / attack_speed signal components_changed @export var components: Array[TowerComponent] = [] @@ -68,14 +76,7 @@ func _init(): func _ready(): - $Range/CollisionShape2D.shape.radius = attack_range - $ShootCooldown.wait_time = speed_base / attack_speed - - %Data.text = "Range: %s - Power: %s - Speed: %s" % [ - attack_range, attack_power, attack_speed - ] - - %HUD.visible = false + $AnimatedSprite2D.play() redraw_components() components_changed.connect(func(): @@ -101,9 +102,9 @@ func _draw(): modulate = Color(1.5, 1.5, 1.5) if is_highlighted: - var sprite_size = $Sprite2D.get_rect().size + var sprite_size = $AnimatedSprite2D.sprite_frames.get_frame_texture("default", 0).get_size() * $AnimatedSprite2D.scale draw_rect( - Rect2($Sprite2D.position, sprite_size), + Rect2($AnimatedSprite2D.position, sprite_size), Color(1, 1, 1, 1.0), false, 2.0 / get_viewport().get_camera_2d().zoom.x @@ -128,6 +129,9 @@ func _on_input_event(_viewport: Node, event: InputEvent, _shape_idx: int): if owner_id != multiplayer.get_unique_id(): return + if event.is_action_pressed("select_secondary"): + selected_secondary.emit() + if Client.state is StateBuild: if event.is_action_pressed("builder_cancel"): Client.remove_tower(self) @@ -146,7 +150,7 @@ func _on_selectable_area_hover_enter() -> void: func _on_selectable_area_hover_exit() -> void: is_hovered = false -func _on_selectable_area_select(event: InputEvent) -> void: +func _on_selectable_area_select_primary(event: InputEvent) -> void: # disable remote select for now if owner_id != multiplayer.get_unique_id(): return @@ -178,14 +182,10 @@ func remove_component(component_name: String): components_changed.emit() func redraw_components(): - for node in %Components.get_children(): - node.modulate = Color(1.0, 1.0, 1.0, 0.5) - for idx in range(components.size()): var component = components[idx] var sprite = $ComponentsAnchor.get_node(NodePath(component.name)) sprite.position.y = (idx + 1) * -16 - %Components.get_node(component.name).modulate = Color(1.0, 1.0, 1.0, 1.0) func is_melee_range(): @@ -209,13 +209,6 @@ func shoot(): target.set_hp(target.hp - 1) -func get_hud(): - var data = %HUD.get_child(0).duplicate() - data.name = name - - return data - - func get_region(): var collision_shape := $CollisionShape2D var shape = $CollisionShape2D.shape as RectangleShape2D @@ -235,6 +228,7 @@ func get_group_id() -> String: for component in components: string += component.name + string += str(component.level) return string.md5_text() @@ -243,102 +237,6 @@ func _on_tree_exiting() -> void: is_selected = false -func _on_h_box_container_gui_input(event: InputEvent) -> void: # TODO: money cost - # TODO: upgradeable - if event.is_action_pressed("select"): - get_viewport().set_input_as_handled() - - var hud: HUD = Client.current_stage.get_node("HUD") - var towers = hud.selection_groups[hud.selected_group].duplicate() - for tower: Tower in towers: - var found = false - for idx in range(tower.components.size()): - var component = tower.components[idx] - if component.name == "Frost": - found = true - tower.remove_component(component.name) - break - - if not found: - tower.add_component(preload("res://Towers/Components/FrostComponent.gd").new()) - - Client.update_tower(tower.get_path(), tower.to_network_data()) - - -func _on_h_box_container_2_gui_input(event: InputEvent) -> void: - if event.is_action_pressed("select"): - get_viewport().set_input_as_handled() - - var hud: HUD = Client.current_stage.get_node("HUD") - var towers = hud.selection_groups[hud.selected_group].duplicate() - for tower: Tower in towers: - var found = false - for idx in range(tower.components.size()): - var component = tower.components[idx] - if component.name == "Burn": - found = true - tower.remove_component(component.name) - break - - if not found: - tower.add_component(preload("res://Towers/Components/BurnComponent.gd").new()) - - Client.update_tower(tower.get_path(), tower.to_network_data()) - - -func _on_button_1_pressed() -> void: - var hud: HUD = Client.current_stage.get_node("HUD") - var towers = hud.selection_groups[hud.selected_group].duplicate() - - if Client.player.money < (attack_range / 8.0) * 10 * towers.size(): - return - - for tower: Tower in towers: - tower.attack_range += 8 - tower.attack_value_changed.emit((attack_range / 8.0) - 1) - tower.get_node("%Data").text = "Range: %s - Power: %s - Speed: %s" % [ - tower.attack_range, tower.attack_power, tower.attack_speed - ] - tower.get_node("%Range/CollisionShape2D").shape.radius = attack_range - tower.selection_group_id = tower.get_group_id() - Client.update_tower(tower.get_path(), tower.to_network_data()) - - -func _on_button_2_pressed() -> void: - var hud: HUD = Client.current_stage.get_node("HUD") - var towers = hud.selection_groups[hud.selected_group].duplicate() - - if Client.player.money < attack_power * 10 * towers.size(): - return - - for tower: Tower in towers: - tower.attack_power += 1 - tower.attack_value_changed.emit(attack_power - 1) - tower.get_node("%Data").text = "Range: %s - Power: %s - Speed: %s" % [ - tower.attack_range, tower.attack_power, tower.attack_speed - ] - tower.selection_group_id = tower.get_group_id() - Client.update_tower(tower.get_path(), tower.to_network_data()) - - -func _on_button_3_pressed() -> void: - var hud: HUD = Client.current_stage.get_node("HUD") - var towers = hud.selection_groups[hud.selected_group].duplicate() - - if Client.player.money < attack_speed * 10 * towers.size(): - return - - for tower: Tower in towers: - tower.attack_speed += 1 - tower.attack_value_changed.emit(attack_speed - 1) - tower.get_node("%Data").text = "Range: %s - Power: %s - Speed: %s" % [ - tower.attack_range, tower.attack_power, tower.attack_speed - ] - tower.get_node("%ShootCooldown").wait_time = (speed_base / attack_speed) - tower.selection_group_id = tower.get_group_id() - Client.update_tower(tower.get_path(), tower.to_network_data()) - - class NetworkData extends Resource: var name: String @@ -364,10 +262,10 @@ func to_network_data() -> NetworkData: data.attack_power = attack_power data.attack_speed = attack_speed data.components = components.map(func(item: TowerComponent): - return item.name + return inst_to_dict(item.to_network_data()) ) - data.sprite_modulate = $Sprite2D.modulate + data.sprite_modulate = $AnimatedSprite2D.modulate data.components_anchor_modulate = $ComponentsAnchor.modulate # IMPROVEMENT: check against last update and only set changed values @@ -378,10 +276,11 @@ func update_with_network_data(data: NetworkData): for component in components.duplicate(): remove_component(component.name) - for component_name in data.components: - add_component( - load("res://Towers/Components/" + component_name + "Component.gd").new() - ) + for component_data in data.components: + var component_network_data: TowerComponent.NetworkData = dict_to_inst(component_data) + var component = load("res://Towers/Components/" + component_network_data.name + "Component.gd").new() + component.level = component_network_data.level + add_component(component) static func from_network_data(data: NetworkData) -> Tower: var tower: Tower = preload("res://Towers/Tower.tscn").instantiate() |