game: add weapon base and sword
This commit is contained in:
42
scenes/weapons/weapon_sword.gd
Normal file
42
scenes/weapons/weapon_sword.gd
Normal file
@@ -0,0 +1,42 @@
|
||||
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.damage = attack_damage
|
||||
projectile.target = target
|
||||
projectile.on_hit_sig = projectile_hit
|
||||
add_child(projectile)
|
||||
|
||||
|
||||
func deal_damage(enemy: EnemyBase):
|
||||
var crit_chance = _player.player_stats.get_final("crit_chance", _player.modifiers)
|
||||
var damage_dealt = attack_damage
|
||||
var is_crit = randf() >= 1 - crit_chance
|
||||
if is_crit:
|
||||
damage_dealt *= _player.player_stats.get_final("crit_multiplier", _player.modifiers)
|
||||
enemy.take_damage(damage_dealt, is_crit)
|
||||
|
||||
|
||||
func _on_projectile_hit(projectile: WeaponSwordProjectile, enemy: EnemyBase):
|
||||
deal_damage(enemy)
|
Reference in New Issue
Block a user