game: add weapon active ability

This commit is contained in:
2025-08-21 05:52:40 +02:00
parent 8f02850a73
commit a0d121fadc
7 changed files with 49 additions and 11 deletions

View File

@@ -1,9 +1,13 @@
class_name Player
extends CharacterBody2D
const WEAPON_SWORD = preload("res://scenes/weapons/weapon_sword.tscn")
@export var camera: Camera2D
@export var main_ui: MainUI
var weapon: WeaponBase
@onready var sprite_2d: Sprite2D = $Sprite2D
var player_stats: PlayerStats = PlayerStats.new()
@@ -12,11 +16,18 @@ var dead: bool = false
var death_anim_done: bool = false
var god_mode: bool = false
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("active"):
weapon.do_active()
func _ready() -> void:
camera.position = global_position
main_ui.player_ui.update_hp()
GlobalConst.sig_debug_god_mode.connect(toggle_god_mode)
if not weapon:
weapon = WEAPON_SWORD.instantiate()
add_child(weapon)
func _physics_process(delta: float) -> void:

View File

@@ -1,8 +1,7 @@
[gd_scene load_steps=6 format=3 uid="uid://ca2so8fm3q8fe"]
[gd_scene load_steps=5 format=3 uid="uid://ca2so8fm3q8fe"]
[ext_resource type="Texture2D" uid="uid://5x5wimok8uw2" path="res://assets/sprites/roguelikeChar_transparent.png" id="1_3vyb7"]
[ext_resource type="Script" uid="uid://cvqaxckx4num3" path="res://scenes/player.gd" id="1_g2els"]
[ext_resource type="PackedScene" uid="uid://dfikvj27k01tu" path="res://scenes/weapons/weapon_sword.tscn" id="3_qhqgy"]
[sub_resource type="CircleShape2D" id="CircleShape2D_3vyb7"]
radius = 8.0
@@ -30,6 +29,4 @@ monitorable = false
[node name="CollisionShape2D" type="CollisionShape2D" parent="PickupArea"]
shape = SubResource("CircleShape2D_qhqgy")
[node name="WeaponSword" parent="." instance=ExtResource("3_qhqgy")]
[connection signal="area_entered" from="PickupArea" to="." method="_on_pickup_area_area_entered"]

View File

@@ -7,6 +7,7 @@ extends Node2D
@export var attack_duration: float
@export var attack_range: float
@onready var active_cd_timer: Timer = $ActiveCDTimer
func _on_attack_cd_timer_timeout() -> void:
do_attack()
@@ -15,6 +16,15 @@ func _on_attack_cd_timer_timeout() -> void:
func do_attack() -> void:
push_error("%s does not implement do_attack" % self)
func do_active() -> void:
if not active_cd_timer.is_stopped():
return
active_cd_timer.start()
_do_active()
func _do_active() -> void:
push_error("%s does not implement do_active" % self)
func find_target_in_radius() -> EnemyBase:
var space_state: PhysicsDirectSpaceState2D = get_world_2d().direct_space_state

View File

@@ -8,4 +8,7 @@ script = ExtResource("1_v4xn6")
[node name="AttackCDTimer" type="Timer" parent="."]
autostart = true
[node name="ActiveCDTimer" type="Timer" parent="."]
one_shot = true
[connection signal="timeout" from="AttackCDTimer" to="." method="_on_attack_cd_timer_timeout"]

View File

@@ -23,20 +23,32 @@ func do_attack() -> void:
return
var projectile = WEAPON_SWORD_PROJECTILE.instantiate()
projectile.damage = attack_damage
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):
func deal_damage(enemy: EnemyBase, damage_mult: float):
var crit_chance = _player.player_stats.get_final("crit_chance", _player.modifiers)
var damage_dealt = attack_damage
var damage_dealt = attack_damage * damage_mult
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)
func _on_projectile_hit(projectile: WeaponSwordProjectile, enemy: EnemyBase, damage_mult: float):
deal_damage(enemy, damage_mult)

View File

@@ -4,7 +4,7 @@ extends Node2D
@export var speed: float = 500.0
@export var range: float = 200.0
@export var target: Node2D
@export var damage: float
@export var damage_mult: float = 1.0
@export var on_hit_sig: Signal
var _direction: Vector2
@@ -36,5 +36,5 @@ func _physics_process(delta: float) -> void:
func _on_area_2d_body_entered(body: Node2D) -> void:
if body in _already_hit:
return
on_hit_sig.emit(self, body)
on_hit_sig.emit(self, body, damage_mult)
_already_hit.append(body)