CyberTower/scripts/player.gd
2024-02-17 12:15:49 +08:00

70 lines
1.9 KiB
GDScript

class_name Player
extends CharacterBody2D
@export var speed = 1200
@export var speed_multiplier = 20
@export var friction = 0.9
@export var dash_cooldown_duration = 1.0
@export var dash_cooldown_timer: Timer
@export var weapon_bullet_damage = 4.0
@export var weapon_bullet_knockback = 4
@export var weapon_bullet_speed = 3200
@export var weapon_bullet_scene: PackedScene
@export var weapon_bullet_parent: Node2D
@export var fire_cooldown_duration = 0.35
@export var fire_cooldown_timer: Timer
@export var statistics: Statistics
func deal_move(delta):
var input_direction = Input.get_vector("move_left", "move_right", "move_up", "move_down")
velocity = velocity.move_toward(input_direction * speed, speed * delta)
velocity = velocity * friction
var is_dash = Input.is_action_pressed("skill_dash")
if is_dash && dash_cooldown_timer.is_stopped():
velocity *= speed_multiplier
dash_cooldown_timer.start(dash_cooldown_duration)
func deal_weapon_shoot():
var is_shooting = Input.is_action_pressed("weapon_fire")
if is_shooting && fire_cooldown_timer.is_stopped():
var mouse_position = get_global_mouse_position()
var direction = (mouse_position - position).normalized()
var bullet = weapon_bullet_scene.instantiate()
bullet.rotation = direction.angle()
weapon_bullet_parent.add_child(bullet)
bullet.global_position = global_position
bullet.velocity = direction * weapon_bullet_speed
bullet.damage = weapon_bullet_damage
bullet.knockback = weapon_bullet_knockback
statistics.bullet_shoot += 1
fire_cooldown_timer.start(fire_cooldown_duration)
func move_to_center():
var screen_size = get_viewport_rect().size
position = Vector2(screen_size.x / 2, screen_size.y / 2)
func _on_dash_cooled_down():
dash_cooldown_timer.stop()
func _on_fire_cooled_down():
fire_cooldown_timer.stop()
func _ready():
move_to_center()
func _physics_process(delta):
deal_move(delta)
deal_weapon_shoot()
move_and_slide()
func _on_wave_finished():
move_to_center()