diff options
Diffstat (limited to 'Towers/Tower.gd')
-rw-r--r-- | Towers/Tower.gd | 101 |
1 files changed, 10 insertions, 91 deletions
diff --git a/Towers/Tower.gd b/Towers/Tower.gd index 1d856f3..34e12ae 100644 --- a/Towers/Tower.gd +++ b/Towers/Tower.gd @@ -40,10 +40,6 @@ var is_highlighted := false: is_highlighted = value queue_redraw() -#var mobs_in_range: Array = [] - -#var selection_area: Area2D - signal selection_group_id_changed(previous_id: String) @onready var selection_group_id := get_group_id(): set(value): @@ -54,20 +50,6 @@ signal selection_group_id_changed(previous_id: String) # rpc owner id @export var owner_id = 1 -#@export var attack_range: int = 32: - #set(value): - #attack_range = value - #$Range/CollisionShape2D.shape.radius = attack_range - #$SoundShoot.max_distance = attack_range * Client.current_stage.map.tile_set.tile_size.x - -#@export var attack_power: int = 1 - -#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: Dictionary @@ -79,7 +61,7 @@ func _init(): func _ready(): $AnimatedSprite2D.play() - for component in [RangeTowerComponent.new(), AttackTowerComponent.new()]: + for component in [preload("res://Towers/Components/RangeComponent.tscn").instantiate(), preload("res://Towers/Components/AttackComponent.tscn").instantiate()]: add_component(component) redraw_components() @@ -120,17 +102,6 @@ func _process(delta: float) -> void: for component in components.values(): if component.has_method("process"): component.process(delta) - - #if $ShootCooldown.is_stopped() and not mobs_in_range.is_empty(): - #shoot() - #$ShootCooldown.start() - - #if selection_area and is_instance_valid(selection_area): - #var bodies = selection_area.get_overlapping_bodies() - #if bodies.size() > 0: - #selection_area.queue_free() - #for body in bodies: - #Client.select_tower(body) func _on_input_event(_viewport: Node, event: InputEvent, _shape_idx: int): @@ -146,13 +117,6 @@ func _on_input_event(_viewport: Node, event: InputEvent, _shape_idx: int): Client.remove_tower(self) -#func _on_range_body_entered(body: Node2D) -> void: - #mobs_in_range.append(body) -# -#func _on_range_body_exited(body: Node2D) -> void: - #mobs_in_range.erase(body) - - func _on_selectable_area_hover_enter() -> void: is_hovered = true @@ -176,24 +140,15 @@ func _on_selectable_area_select_primary(event: InputEvent) -> void: func add_component(component: TowerComponent): components[component.type] = component + add_child(component) component.on_add(self) - var sprite = component.sprite + var sprite = component.sprite.duplicate() %ComponentsAnchor.add_child(sprite) redraw_components() components_changed.emit() -func remove_component(component: TowerComponent): - components.erase(component.type) - %ComponentsAnchor.remove_child(%ComponentsAnchor.get_node(NodePath(component.id))) - #for component in components: - #if component.name == component_name: - #components.erase(component) - #$ComponentsAnchor.remove_child($ComponentsAnchor.get_node(NodePath(component.name))) - #break - redraw_components() - components_changed.emit() func redraw_components(): var keys = components.keys() @@ -204,30 +159,6 @@ func redraw_components(): sprite.position.y = (idx + 1) * -16 -#func is_melee_range(): - #return attack_range <= (Client.current_stage.map.tile_set.tile_size.x * 2) - - -#func shoot(): - #var target = mobs_in_range[0] as Unit - # - #for component in components: - #if component.has_method("on_shoot"): - #component.on_shoot(target) - # - #shoot_fx.rpc() - # - #if is_melee_range(): - #target.set_hp(target.hp - 1) - #else: # TODO - #target.set_hp(target.hp - 1) -# -#@rpc("authority", "call_local") -#func shoot_fx(): - #$SoundShoot.play() - - - func get_region(): var collision_shape := $CollisionShape2D var shape = $CollisionShape2D.shape as RectangleShape2D @@ -241,10 +172,6 @@ func get_region(): func get_group_id() -> String: var string := "" - #string += str(attack_range) - #string += str(attack_power) - #string += str(attack_speed) - for component: TowerComponent in components.values(): string += component.id string += str(component.level) @@ -277,11 +204,11 @@ func to_network_data() -> NetworkData: data.owner_id = owner_id data.position = global_position - #data.attack_range = attack_range - #data.attack_power = attack_power - #data.attack_speed = attack_speed data.components = components.values().map(func(item: TowerComponent): - return inst_to_dict(item.to_network_data()) + var component_data = inst_to_dict(item.to_network_data()) + component_data["path"] = item.get_path() + + return component_data ) data.sprite_modulate = $AnimatedSprite2D.modulate @@ -292,14 +219,10 @@ func to_network_data() -> NetworkData: return data func update_with_network_data(data: NetworkData): - for component in components.duplicate(): - remove_component(component.id) - 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) + var component = get_node(component_data.path) as TowerComponent + component.update_with_network_data(dict_to_inst(component_data)) + redraw_components() static func from_network_data(data: NetworkData) -> Tower: var tower: Tower = preload("res://Towers/Tower.tscn").instantiate() @@ -308,10 +231,6 @@ static func from_network_data(data: NetworkData) -> Tower: tower.owner_id = data.owner_id tower.global_position = data.position - #tower.attack_range = data.attack_range - #tower.attack_power = data.attack_power - #tower.attack_speed = data.attack_speed - tower.get_node("AnimatedSprite2D").modulate = data.sprite_modulate tower.get_node("%ComponentsAnchor").modulate = data.components_anchor_modulate |