summaryrefslogtreecommitdiff
path: root/Towers/Tower.gd
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2024-11-06 21:00:56 +0100
committerDaniel Weipert <git@mail.dweipert.de>2024-11-06 21:00:56 +0100
commitf20b3a48a14f34def41d823fabb476981ef7055e (patch)
tree35608bd22ff3ec8d9210747df39186d212aa6ab7 /Towers/Tower.gd
parent6f17d6c1d458275e8744d57a7f433cc3eb5434d7 (diff)
next commit
Diffstat (limited to 'Towers/Tower.gd')
-rw-r--r--Towers/Tower.gd154
1 files changed, 84 insertions, 70 deletions
diff --git a/Towers/Tower.gd b/Towers/Tower.gd
index 187cd93..1d856f3 100644
--- a/Towers/Tower.gd
+++ b/Towers/Tower.gd
@@ -40,7 +40,7 @@ var is_highlighted := false:
is_highlighted = value
queue_redraw()
-var mobs_in_range: Array = []
+#var mobs_in_range: Array = []
#var selection_area: Area2D
@@ -54,22 +54,22 @@ 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_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
+#@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
+#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] = []
+@export var components: Dictionary
func _init():
@@ -79,6 +79,9 @@ func _init():
func _ready():
$AnimatedSprite2D.play()
+ for component in [RangeTowerComponent.new(), AttackTowerComponent.new()]:
+ add_component(component)
+
redraw_components()
components_changed.connect(func():
selection_group_id = get_group_id()
@@ -92,7 +95,7 @@ func _draw():
if Client.state is StateDefault:
draw_circle(
Vector2(Client.current_stage.map.tile_set.tile_size) / scale,
- 8 + attack_range,
+ 8 + components.get(TowerComponent.ComponentType.Range).range,
Color(1, 1, 1, 0.5),
false,
1.0
@@ -112,11 +115,15 @@ func _draw():
)
-func _process(_delta: float) -> void:
+func _process(delta: float) -> void:
if multiplayer.is_server():
- if $ShootCooldown.is_stopped() and not mobs_in_range.is_empty():
- shoot()
- $ShootCooldown.start()
+ 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()
@@ -139,11 +146,11 @@ 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_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:
@@ -168,49 +175,56 @@ func _on_selectable_area_select_primary(event: InputEvent) -> void:
func add_component(component: TowerComponent):
- components.append(component)
+ components[component.type] = component
+ component.on_add(self)
+
var sprite = component.sprite
- $ComponentsAnchor.add_child(sprite)
+ %ComponentsAnchor.add_child(sprite)
redraw_components()
+
components_changed.emit()
-func remove_component(component_name: String):
- for component in components:
- if component.name == component_name:
- components.erase(component)
- $ComponentsAnchor.remove_child($ComponentsAnchor.get_node(NodePath(component.name)))
- break
+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():
- for idx in range(components.size()):
- var component = components[idx]
- var sprite = $ComponentsAnchor.get_node(NodePath(component.name))
+ var keys = components.keys()
+ for idx in range(keys.size()):
+ var key = keys[idx]
+ var component: TowerComponent = components[key]
+ var sprite = %ComponentsAnchor.get_node(NodePath(component.id))
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 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()
@@ -227,12 +241,12 @@ func get_region():
func get_group_id() -> String:
var string := ""
- string += str(attack_range)
- string += str(attack_power)
- string += str(attack_speed)
+ #string += str(attack_range)
+ #string += str(attack_power)
+ #string += str(attack_speed)
- for component in components:
- string += component.name
+ for component: TowerComponent in components.values():
+ string += component.id
string += str(component.level)
return string.md5_text()
@@ -263,15 +277,15 @@ 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.map(func(item: TowerComponent):
+ #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())
)
data.sprite_modulate = $AnimatedSprite2D.modulate
- data.components_anchor_modulate = $ComponentsAnchor.modulate
+ data.components_anchor_modulate = %ComponentsAnchor.modulate
# IMPROVEMENT: check against last update and only set changed values
@@ -279,7 +293,7 @@ func to_network_data() -> NetworkData:
func update_with_network_data(data: NetworkData):
for component in components.duplicate():
- remove_component(component.name)
+ remove_component(component.id)
for component_data in data.components:
var component_network_data: TowerComponent.NetworkData = dict_to_inst(component_data)
@@ -294,11 +308,11 @@ 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.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
+ tower.get_node("%ComponentsAnchor").modulate = data.components_anchor_modulate
return tower