58 lines
1.8 KiB
GDScript
58 lines
1.8 KiB
GDScript
class_name WeaponSword
|
|
extends WeaponBase
|
|
|
|
const WEAPON_SWORD_PROJECTILE = preload("res://scenes/weapons/weapon_sword_projectile.tscn")
|
|
|
|
@onready var targeting_range: Area2D = $TargetingRange
|
|
@onready var targeting_range_shape: CollisionShape2D = $TargetingRange/CollisionShape2D
|
|
|
|
signal projectile_hit(projectile: WeaponSwordProjectile, enemy: EnemyBase)
|
|
|
|
var _player: Player
|
|
|
|
|
|
func _ready() -> void:
|
|
targeting_range_shape.shape.radius = attack_range
|
|
projectile_hit.connect(_on_projectile_hit)
|
|
_player = get_tree().get_first_node_in_group(GlobalConst.GROUP_PLAYER)
|
|
|
|
|
|
func do_attack() -> void:
|
|
var target: EnemyBase = find_target_in_radius()
|
|
if not target:
|
|
return
|
|
|
|
var projectile = WEAPON_SWORD_PROJECTILE.instantiate()
|
|
projectile.target = target
|
|
projectile.on_hit_sig = projectile_hit
|
|
add_child(projectile)
|
|
|
|
|
|
func _do_active() -> void:
|
|
var radius = targeting_range_shape.shape.radius
|
|
var count = 15
|
|
for i in count:
|
|
var angle = TAU * float(i) / float(count)
|
|
var target_pos = Vector2(cos(angle), sin(angle)) * radius
|
|
var new_target := Marker2D.new()
|
|
new_target.global_position = global_position + target_pos
|
|
var projectile = WEAPON_SWORD_PROJECTILE.instantiate()
|
|
projectile.damage_mult = 3.0
|
|
projectile.target = new_target
|
|
projectile.on_hit_sig = projectile_hit
|
|
add_child(projectile)
|
|
|
|
|
|
func deal_damage(enemy: EnemyBase, damage_mult: float):
|
|
var weapon_crit = get_calculated("attack_crit_chance")
|
|
var player_crit = _player.player_stats.get_final("crit_chance", _player.modifiers)
|
|
var damage = get_calculated("attack_damage")
|
|
var is_crit = randf() >= 1 - weapon_crit + player_crit
|
|
if is_crit:
|
|
damage *= _player.player_stats.get_final("crit_multiplier", _player.modifiers)
|
|
enemy.take_damage(damage * damage_mult, is_crit)
|
|
|
|
|
|
func _on_projectile_hit(projectile: WeaponSwordProjectile, enemy: EnemyBase, damage_mult: float):
|
|
deal_damage(enemy, damage_mult)
|