Files
homework/scripts/player_controller.gd
2026-10-07 19:35:15 +08:00

196 lines
4.8 KiB
GDScript

extends CharacterBody2D
@export_category("移动参数")
@export var double_press_interval := 0.3
@export var move_speed: float = 75.0
@export var acceleration: float = 600.0
@export var deceleration: float = 800.0
@export var jump_velocity: float = -190.0
@onready var sprite: Sprite2D = $Sprite2D
var flying : bool = false
var last_space_press_time := -1000
func _physics_process(delta: float) -> void:
if !flying:
apply_gravity(delta)
handle_jump()
else:
handle_vertical_movement(delta)
handle_horizontal_movement(delta)
update_sprite_direction()
move_and_slide()
func apply_gravity(delta: float) -> void:
if not is_on_floor():
velocity += get_gravity() * delta
func handle_vertical_movement(delta:float) -> void:
var direction := Input.get_axis("squat", "jump")
var target_speed := direction * jump_velocity
if direction != 0.0:
velocity.y = move_toward(
velocity.y,
target_speed,
acceleration * delta
)
else:
velocity.y = move_toward(
velocity.y,
0.0,
acceleration * delta
)
func handle_horizontal_movement(delta: float) -> void:
var direction := Input.get_axis("move_left", "move_right")
var target_speed := direction * move_speed
if direction != 0.0:
velocity.x = move_toward(
velocity.x,
target_speed,
acceleration * delta
)
else:
velocity.x = move_toward(
velocity.x,
0.0,
deceleration * delta
)
func handle_jump() -> void:
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = jump_velocity
func update_sprite_direction() -> void:
if velocity.x != 0.0:
sprite.flip_h = velocity.x < 0.0
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("fly") and !event.is_echo():
handle_space_pressed()
if event.is_action_pressed("dash") and not is_dashing:
start_dash()
func handle_space_pressed() -> void:
var current_time := Time.get_ticks_msec()
var elapsed_time := current_time - last_space_press_time
if elapsed_time <= double_press_interval * 1000.0:
flying = not flying
last_space_press_time = -1000
else:
last_space_press_time = current_time
signal died
signal health_changed(current: int, max_hp: int)
@export var max_hp: int = 3
@export var knockback_force: float = 150.0
var hp : int
func _ready() -> void:
hp = max_hp
health_changed.emit(hp, max_hp)
func take_damage():
hp -= 1
hp = max(hp, 0)
health_changed.emit(hp, max_hp)
apply_knockback()
sprite.modulate = Color(1, 0, 0) # 变红
await get_tree().create_timer(0.5).timeout # 等 0.5 秒
sprite.modulate = Color(1, 1, 1)
if hp <= 0:
died.emit()
queue_free()
func apply_knockback():
# 取当前水平运动方向,反方向给速度
var dir = sign(velocity.x) # -1 或 1
if dir == 0:
dir = -1 # 静止时默认往左击退
velocity.x = -dir * knockback_force
velocity.y = -100
func _on_spikes_01_body_entered(body: Node2D) -> void:
if body.has_method("take_damage"):
body.take_damage()
print("你受伤了,还剩",hp,"点血") # Replace with function body.
func _on_spikes_03_body_entered(body: Node2D) -> void:
if body.has_method("take_damage"):
body.take_damage()
print("你受伤了,还剩",hp,"点血") # Replace with function body.
func _on_spikes_02_body_entered(body: Node2D) -> void:
if body.has_method("take_damage"):
body.take_damage()
print("你受伤了,还剩",hp,"点血") # Replace with function body.
func _on_spikes_05_body_entered(body: Node2D) -> void:
if body.has_method("take_damage"):
body.take_damage()
print("你受伤了,还剩",hp,"点血")
func _on_spikes_04_body_entered(body: Node2D) -> void:
if body.has_method("take_damage"):
body.take_damage()
print("你受伤了,还剩",hp,"点血")
signal coin_changed(total: int) # 金币变化时通知 UI
var coins: int = 0
func add_coin(amount: int = 1) -> void:
coins += amount
coin_changed.emit(coins)
print("金币:", coins)
@export var dash_speed: float = 600.0 # 闪现速度
@export var dash_time: float = 0.1 # 持续时间(秒)
var is_dashing := false
func start_dash():
is_dashing = true
# 朝当前面朝方向闪现
var dir = 1 if not sprite.flip_h else -1
velocity.x = dir * dash_speed
velocity.y = 0
await get_tree().create_timer(dash_time).timeout
velocity.x = 0
is_dashing = false
func _on_spikes_06_body_entered(body: Node2D) -> void:
if body.has_method("take_damage"):
body.take_damage()
print("你受伤了,还剩",hp,"点血")# Replace with function body.
func _on_spikes_07_body_entered(body: Node2D) -> void:
if body.has_method("take_damage"):
body.take_damage()
print("你受伤了,还剩",hp,"点血") # Replace with function body.
func _on_spikes_08_body_entered(body: Node2D) -> void:
if body.has_method("take_damage"):
body.take_damage()
print("你受伤了,还剩",hp,"点血") # Replace with function body.