diff options
Diffstat (limited to 'Towers')
-rw-r--r-- | Towers/Components/AttackComponent.gd | 29 | ||||
-rw-r--r-- | Towers/Components/AttackComponent.tscn | 19 | ||||
-rw-r--r-- | Towers/Components/BurnComponent.gd | 5 | ||||
-rw-r--r-- | Towers/Components/BurnComponent.tscn | 12 | ||||
-rw-r--r-- | Towers/Components/FrostComponent.gd | 5 | ||||
-rw-r--r-- | Towers/Components/FrostComponent.tscn | 12 | ||||
-rw-r--r-- | Towers/Components/RangeComponent.gd | 33 | ||||
-rw-r--r-- | Towers/Components/RangeComponent.tscn | 22 | ||||
-rw-r--r-- | Towers/Components/TowerComponent.gd | 27 | ||||
-rw-r--r-- | Towers/Components/TowerComponent.tscn | 14 | ||||
-rw-r--r-- | Towers/Tower.gd | 101 | ||||
-rw-r--r-- | Towers/Tower.tscn | 24 |
12 files changed, 129 insertions, 174 deletions
diff --git a/Towers/Components/AttackComponent.gd b/Towers/Components/AttackComponent.gd index c7660cf..0b09ac1 100644 --- a/Towers/Components/AttackComponent.gd +++ b/Towers/Components/AttackComponent.gd @@ -4,32 +4,33 @@ extends TowerComponent var power := 1 -var speed_base := 1.0 +var speed_base := 2.0 var speed := 1 -var shoot_cooldown := Timer.new() -var shoot_sound := AudioStreamPlayer2D.new() +@onready var shoot_cooldown := $ShootCooldown +@onready var shoot_sound := $ShootSound -@export var range_component: RangeTowerComponent: +var range_component: RangeTowerComponent: get(): return current_tower.components[ComponentType.Range] -func _init() -> void: - type = ComponentType.Attack - set_sprite(preload("res://Towers/Components/Assets/attack-component.png")) - +func _ready() -> void: update_power() - update_speed() level_changed.connect(update_power) + + update_speed() level_changed.connect(update_speed) func update_power(): - power = ceil(level / 2.0) + if level <= 2: + power = level + else: + power = 1 + max(1, level - ceil(level / 2.0)) func update_speed(): - speed = level - ceil(level / 2.0) + speed = max(1, ceil(level / 2.0)) shoot_cooldown.wait_time = speed_base / speed func update_range(): @@ -39,12 +40,6 @@ func update_range(): func on_add(tower: Tower): super.on_add(tower) - shoot_cooldown.one_shot = true - tower.add_child(shoot_cooldown) - - shoot_sound.stream = preload("res://Towers/Assets/shoot.ogg") - tower.add_child(shoot_sound) - update_range() range_component.range_changed.connect(update_range) diff --git a/Towers/Components/AttackComponent.tscn b/Towers/Components/AttackComponent.tscn new file mode 100644 index 0000000..2cad794 --- /dev/null +++ b/Towers/Components/AttackComponent.tscn @@ -0,0 +1,19 @@ +[gd_scene load_steps=5 format=3 uid="uid://4nd1npteqvh3"] + +[ext_resource type="PackedScene" uid="uid://cly4agfoqhq2q" path="res://Towers/Components/TowerComponent.tscn" id="1_j2cqy"] +[ext_resource type="Script" path="res://Towers/Components/AttackComponent.gd" id="2_dbyck"] +[ext_resource type="Texture2D" uid="uid://gbknvb38euuq" path="res://Towers/Components/Assets/attack-component.png" id="2_vr0du"] +[ext_resource type="AudioStream" uid="uid://cpkwvp5llf7x6" path="res://Towers/Assets/shoot.ogg" id="4_ytht8"] + +[node name="AttackTowerComponent" instance=ExtResource("1_j2cqy")] +script = ExtResource("2_dbyck") +type = 1 + +[node name="Sprite" parent="." index="0"] +texture = ExtResource("2_vr0du") + +[node name="ShootCooldown" type="Timer" parent="." index="1"] +one_shot = true + +[node name="ShootSound" type="AudioStreamPlayer2D" parent="." index="2"] +stream = ExtResource("4_ytht8") diff --git a/Towers/Components/BurnComponent.gd b/Towers/Components/BurnComponent.gd index d708d4f..ddac88b 100644 --- a/Towers/Components/BurnComponent.gd +++ b/Towers/Components/BurnComponent.gd @@ -1,11 +1,6 @@ extends TowerComponent -func _init() -> void: - type = ComponentType.Burn - set_sprite(preload("res://Towers/Components/Assets/burn-component.png")) - - func on_shoot(target: Unit): var effect = preload("res://Effects/BurnEffect.tscn").instantiate() effect.level = level diff --git a/Towers/Components/BurnComponent.tscn b/Towers/Components/BurnComponent.tscn new file mode 100644 index 0000000..285cfa0 --- /dev/null +++ b/Towers/Components/BurnComponent.tscn @@ -0,0 +1,12 @@ +[gd_scene load_steps=4 format=3 uid="uid://c0xtty8n0lwpr"] + +[ext_resource type="PackedScene" uid="uid://cly4agfoqhq2q" path="res://Towers/Components/TowerComponent.tscn" id="1_56lm4"] +[ext_resource type="Script" path="res://Towers/Components/BurnComponent.gd" id="2_nevgj"] +[ext_resource type="Texture2D" uid="uid://2djpswd6sgng" path="res://Towers/Components/Assets/burn-component.png" id="3_o6seh"] + +[node name="TowerComponent" instance=ExtResource("1_56lm4")] +script = ExtResource("2_nevgj") +type = 4 + +[node name="Sprite" parent="." index="0"] +texture = ExtResource("3_o6seh") diff --git a/Towers/Components/FrostComponent.gd b/Towers/Components/FrostComponent.gd index 2443b70..4d2b04c 100644 --- a/Towers/Components/FrostComponent.gd +++ b/Towers/Components/FrostComponent.gd @@ -1,11 +1,6 @@ extends TowerComponent -func _init() -> void: - type = ComponentType.Frost - set_sprite(preload("res://Towers/Components/Assets/frost-component.png")) - - func on_shoot(target: Unit): var effect = preload("res://Effects/SlowEffect.tscn").instantiate() effect.level = level diff --git a/Towers/Components/FrostComponent.tscn b/Towers/Components/FrostComponent.tscn new file mode 100644 index 0000000..976d3e8 --- /dev/null +++ b/Towers/Components/FrostComponent.tscn @@ -0,0 +1,12 @@ +[gd_scene load_steps=4 format=3 uid="uid://b1opnngyr3de4"] + +[ext_resource type="PackedScene" uid="uid://cly4agfoqhq2q" path="res://Towers/Components/TowerComponent.tscn" id="1_8hes5"] +[ext_resource type="Script" path="res://Towers/Components/FrostComponent.gd" id="2_bsb0f"] +[ext_resource type="Texture2D" uid="uid://ba3dmlce1wv2p" path="res://Towers/Components/Assets/frost-component.png" id="3_qwp1n"] + +[node name="FrostTowerComponent" instance=ExtResource("1_8hes5")] +script = ExtResource("2_bsb0f") +type = 3 + +[node name="Sprite" parent="." index="0"] +texture = ExtResource("3_qwp1n") diff --git a/Towers/Components/RangeComponent.gd b/Towers/Components/RangeComponent.gd index a2f7ae7..74a6eeb 100644 --- a/Towers/Components/RangeComponent.gd +++ b/Towers/Components/RangeComponent.gd @@ -9,17 +9,14 @@ var range := 0: range = value range_changed.emit() -var area := Area2D.new() -var collision_shape := CollisionShape2D.new() -var shape := CircleShape2D.new() +@onready var area: Area2D = $Range +@onready var collision_shape := $Range/CollisionShape2D +@onready var shape: CircleShape2D = collision_shape.shape var units_in_range: Array[Unit] -func _init() -> void: - type = ComponentType.Range - set_sprite(preload("res://Towers/Components/Assets/range-component.png")) - +func _ready() -> void: update_range() level_changed.connect(update_range) @@ -34,19 +31,11 @@ func update_range(): func on_add(tower: Tower): super.on_add(tower) - area.set_collision_layer_value(1, false) - area.set_collision_mask_value(1, true) - - area.body_entered.connect(func(body: Node2D): - units_in_range.append(body) - ) - area.body_exited.connect(func(body: Node2D): - units_in_range.erase(body) - ) - area.position = tower.get_node("CenterAnchor").position - - collision_shape.shape = shape - area.add_child(collision_shape) - - tower.add_child(area) + + +func _on_range_body_entered(body: Node2D) -> void: + units_in_range.append(body) + +func _on_range_body_exited(body: Node2D) -> void: + units_in_range.erase(body) diff --git a/Towers/Components/RangeComponent.tscn b/Towers/Components/RangeComponent.tscn new file mode 100644 index 0000000..1047529 --- /dev/null +++ b/Towers/Components/RangeComponent.tscn @@ -0,0 +1,22 @@ +[gd_scene load_steps=5 format=3 uid="uid://b0bdw5n6p5k8u"] + +[ext_resource type="PackedScene" uid="uid://cly4agfoqhq2q" path="res://Towers/Components/TowerComponent.tscn" id="1_738xf"] +[ext_resource type="Script" path="res://Towers/Components/RangeComponent.gd" id="2_4fk2a"] +[ext_resource type="Texture2D" uid="uid://dx07y4scyi5a1" path="res://Towers/Components/Assets/range-component.png" id="3_7nqma"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_q388v"] + +[node name="RangeTowerComponent" instance=ExtResource("1_738xf")] +script = ExtResource("2_4fk2a") + +[node name="Sprite" parent="." index="0"] +texture = ExtResource("3_7nqma") + +[node name="Range" type="Area2D" parent="." index="1"] +collision_layer = 0 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Range" index="0"] +shape = SubResource("CircleShape2D_q388v") + +[connection signal="body_entered" from="Range" to="." method="_on_range_body_entered"] +[connection signal="body_exited" from="Range" to="." method="_on_range_body_exited"] diff --git a/Towers/Components/TowerComponent.gd b/Towers/Components/TowerComponent.gd index 7ca766a..30b1c44 100644 --- a/Towers/Components/TowerComponent.gd +++ b/Towers/Components/TowerComponent.gd @@ -1,8 +1,10 @@ class_name TowerComponent -extends Node +extends Node2D -var current_tower: Tower +var current_tower: Tower: + get(): + return get_parent() enum ComponentType { Range, @@ -13,8 +15,15 @@ enum ComponentType { Poison, } -var type: ComponentType -var sprite: TextureRect = TextureRect.new() +@export var type: ComponentType +var sprite: TextureRect: + get(): + @warning_ignore("shadowed_variable") + var sprite := $Sprite.duplicate() + sprite.name = get_type_name() + + return sprite + var id: String: get(): @@ -28,12 +37,8 @@ var level: int = 1: func set_sprite(texture: Texture2D): + sprite = get_node("Sprite") sprite.texture = texture - - sprite.texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST - #sprite.centered = false - sprite.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED - #sprite.scale = Vector2(2, 2) sprite.name = get_type_name() @@ -57,8 +62,8 @@ func to_network_data() -> NetworkData: return data -func update_with_network_data(_data: NetworkData): - pass +func update_with_network_data(data: NetworkData): + level = data.level static func from_network_data(data: NetworkData) -> TowerComponent: var component = preload("res://Towers/Components/TowerComponent.gd").new() diff --git a/Towers/Components/TowerComponent.tscn b/Towers/Components/TowerComponent.tscn new file mode 100644 index 0000000..6b1d7b2 --- /dev/null +++ b/Towers/Components/TowerComponent.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=2 format=3 uid="uid://cly4agfoqhq2q"] + +[ext_resource type="Script" path="res://Towers/Components/TowerComponent.gd" id="1_jb46t"] + +[node name="TowerComponent" type="Node2D"] +visible = false +script = ExtResource("1_jb46t") + +[node name="Sprite" type="TextureRect" parent="."] +texture_filter = 1 +offset_right = 40.0 +offset_bottom = 40.0 +stretch_mode = 5 +metadata/_edit_use_anchors_ = true 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 diff --git a/Towers/Tower.tscn b/Towers/Tower.tscn index 0bd4579..839917c 100644 --- a/Towers/Tower.tscn +++ b/Towers/Tower.tscn @@ -1,10 +1,9 @@ -[gd_scene load_steps=16 format=3 uid="uid://by1x56w21o165"] +[gd_scene load_steps=14 format=3 uid="uid://by1x56w21o165"] [ext_resource type="Script" path="res://Towers/Tower.gd" id="1_axo1d"] [ext_resource type="Texture2D" uid="uid://b1b18rd0tqbar" path="res://core_outdoor.png" id="1_mrep8"] [ext_resource type="PackedScene" uid="uid://cqktpc8c7ecn3" path="res://Game/Selection/SelectableArea.tscn" id="3_57d5u"] [ext_resource type="Texture2D" uid="uid://dujcs2ls3u6tj" path="res://Towers/Assets/spritesheet.png" id="3_brkps"] -[ext_resource type="AudioStream" uid="uid://cpkwvp5llf7x6" path="res://Towers/Assets/shoot.ogg" id="5_i05ow"] [sub_resource type="AtlasTexture" id="AtlasTexture_1ubf7"] atlas = ExtResource("3_brkps") @@ -59,8 +58,6 @@ animations = [{ [sub_resource type="RectangleShape2D" id="RectangleShape2D_atm5x"] size = Vector2(31, 31) -[sub_resource type="CircleShape2D" id="CircleShape2D_qa8kt"] - [sub_resource type="RectangleShape2D" id="RectangleShape2D_312i7"] size = Vector2(32, 32) @@ -91,19 +88,6 @@ centered = false position = Vector2(16, 16) shape = SubResource("RectangleShape2D_atm5x") -[node name="Range" type="Area2D" parent="."] -unique_name_in_owner = true -visible = false -collision_layer = 0 - -[node name="CollisionShape2D" type="CollisionShape2D" parent="Range"] -position = Vector2(16, 16) -shape = SubResource("CircleShape2D_qa8kt") - -[node name="ShootCooldown" type="Timer" parent="."] -unique_name_in_owner = true -one_shot = true - [node name="SelectableArea" parent="." instance=ExtResource("3_57d5u")] [node name="CollisionShape2D" type="CollisionShape2D" parent="SelectableArea"] @@ -125,14 +109,8 @@ position = Vector2(16, 29) [node name="CenterAnchor" type="Marker2D" parent="."] position = Vector2(16, 16) -[node name="SoundShoot" type="AudioStreamPlayer2D" parent="."] -visible = false -stream = ExtResource("5_i05ow") - [connection signal="input_event" from="." to="." method="_on_input_event"] [connection signal="tree_exiting" from="." to="." method="_on_tree_exiting"] -[connection signal="body_entered" from="Range" to="." method="_on_range_body_entered"] -[connection signal="body_exited" from="Range" to="." method="_on_range_body_exited"] [connection signal="hover_enter" from="SelectableArea" to="." method="_on_selectable_area_hover_enter"] [connection signal="hover_exit" from="SelectableArea" to="." method="_on_selectable_area_hover_exit"] [connection signal="select_primary" from="SelectableArea" to="." method="_on_selectable_area_select_primary"] |