From 63d2a59042fb9d90a586ad85d8364de7c38aea8e Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Wed, 6 Nov 2024 22:35:04 +0100 Subject: next commit --- Towers/Components/AttackComponent.gd | 29 ++++++++++++----------------- Towers/Components/AttackComponent.tscn | 19 +++++++++++++++++++ Towers/Components/BurnComponent.gd | 5 ----- Towers/Components/BurnComponent.tscn | 12 ++++++++++++ Towers/Components/FrostComponent.gd | 5 ----- Towers/Components/FrostComponent.tscn | 12 ++++++++++++ Towers/Components/RangeComponent.gd | 33 +++++++++++---------------------- Towers/Components/RangeComponent.tscn | 22 ++++++++++++++++++++++ Towers/Components/TowerComponent.gd | 27 ++++++++++++++++----------- Towers/Components/TowerComponent.tscn | 14 ++++++++++++++ 10 files changed, 118 insertions(+), 60 deletions(-) create mode 100644 Towers/Components/AttackComponent.tscn create mode 100644 Towers/Components/BurnComponent.tscn create mode 100644 Towers/Components/FrostComponent.tscn create mode 100644 Towers/Components/RangeComponent.tscn create mode 100644 Towers/Components/TowerComponent.tscn (limited to 'Towers/Components') 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 -- cgit v1.2.3