semi-idle-arpg/scripts/AbilityPlayer.gd

80 lines
2.2 KiB
GDScript3
Raw Normal View History

2024-10-18 09:11:15 -06:00
# base player ability class
2024-10-15 08:37:50 -06:00
class_name AbilityPlayer
extends AbilityBase
var middleX
var playerPos
@export var base_cooldown := 0.0
func _ready() -> void:
2024-10-18 09:11:15 -06:00
middleX = get_viewport_rect().size.x / 2
2024-10-15 08:37:50 -06:00
func _process(_delta: float) -> void:
2024-10-18 09:11:15 -06:00
playerPos = Vector2(middleX, get_viewport_rect().size.y * (0.75))
$IdleCooldown.wait_time = auto_speed
func _get_enemies() -> Array:
var enemies := get_tree().get_nodes_in_group("enemies")
return enemies
func _get_num_enemies() -> int:
var enemies := get_tree().get_nodes_in_group("enemies")
return enemies.size()
func _get_distsq_to_player(enemy: Node) -> float:
return enemy.position.distance_squared_to(playerPos)
func _get_angle_to_player(enemy: Node) -> float:
return enemy.position.angle_to_point(playerPos)
func _get_target(enemies) -> Node:
# var numEnemies := _get_num_enemies()
var enemyDists
var closestEnemy
# get list of distances by mapping distance to each node in enemies
enemyDists = enemies.map(_get_distsq_to_player)
2024-10-15 08:37:50 -06:00
# get closest enemy by finding first index with min dist
if enemyDists.min() <= self.attack_range**2:
closestEnemy = enemies[enemyDists.find(enemyDists.min())]
return closestEnemy
func _get_targets(enemies) -> Array:
var enemyDists
var enemiesInRange := []
# get list of distances by mapping distance to each node in enemies
enemyDists = enemies.map(_get_distsq_to_player)
# append enemies in range to enemiesInRange
for i in range(enemies.size()):
if enemyDists[i] <= self.attack_range**2:
enemiesInRange.append(enemies[i])
return enemiesInRange
func _deal_idle_damage(enemiesList) -> void:
match target_type:
TargTypes.SINGLE:
var target = _get_target(enemiesList)
target.damageTaken += attack_damage
_:
var targets = _get_targets(enemiesList)
for target in targets:
target.damageTaken += attack_damage
func _on_idle_cooldown_timeout() -> void:
var enemiesList = _get_enemies()
if not enemiesList.is_empty() and self.visible:
_deal_idle_damage(enemiesList)