CyberTower/scripts/player.gd

64 lines
1.9 KiB
GDScript3
Raw Normal View History

2024-02-16 16:55:13 +00:00
class_name Player
2024-02-16 12:35:30 +00:00
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
2024-02-17 04:04:43 +00:00
@export var weapon_bullet_damage = 4.0
@export var weapon_bullet_knockback = 4
2024-02-16 12:35:30 +00:00
@export var weapon_bullet_speed = 3200
@export var weapon_bullet_scene: PackedScene
@export var weapon_bullet_parent: Node2D
2024-02-17 04:04:43 +00:00
@export var fire_cooldown_duration = 0.35
2024-02-16 12:35:30 +00:00
@export var fire_cooldown_timer: Timer
2024-02-16 15:35:39 +00:00
@export var statistics: Statistics
2024-02-16 12:35:30 +00:00
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
2024-02-17 04:04:43 +00:00
bullet.damage = weapon_bullet_damage
bullet.knockback = weapon_bullet_knockback
2024-02-16 12:35:30 +00:00
2024-02-16 15:35:39 +00:00
statistics.bullet_shoot += 1
2024-02-16 12:35:30 +00:00
fire_cooldown_timer.start(fire_cooldown_duration)
func _on_dash_cooled_down():
dash_cooldown_timer.stop()
func _on_fire_cooled_down():
fire_cooldown_timer.stop()
func _ready():
var screen_size = get_viewport_rect().size
position = Vector2(screen_size.x / 2, screen_size.y / 2)
func _physics_process(delta):
deal_move(delta)
deal_weapon_shoot()
move_and_slide()