player: add protected and suspended members

`suspended' is now for suspending a player's movement input keys;
`protected' is for preventing a player's death except for special cases
such as when health goes below 1 or the player drops out of the world.

To kill a player without checking `protected', use _die().
This commit is contained in:
Jeremy Baxter 2026-01-24 12:43:28 +13:00
parent 6342b751f8
commit e549788fcd

View file

@ -10,6 +10,8 @@ const gear_slots = ["1", "2", "3"]
var starting_gear = preload("res://gears/gear.tscn") var starting_gear = preload("res://gears/gear.tscn")
var dead = false
var protected = false
var suspended = false var suspended = false
var direction = Vector3.ZERO var direction = Vector3.ZERO
var target_velocity = Vector3.ZERO var target_velocity = Vector3.ZERO
@ -45,6 +47,8 @@ func message(string):
# State functions # State functions
func harm(hp): func harm(hp):
if protected:
return
assert(hp >= 0) assert(hp >= 0)
_health -= hp _health -= hp
make_hud() make_hud()
@ -57,7 +61,8 @@ func heal(hp):
func health(): func health():
return _health return _health
func die(): func _die():
dead = true
suspended = true suspended = true
visible = false visible = false
$HUD.visible = false $HUD.visible = false
@ -72,12 +77,17 @@ func die():
$RespawnTimer.start() $RespawnTimer.start()
message("Le gone") message("Le gone")
func die():
if protected:
return
_die()
func respawn(): func respawn():
position = Vector3(spawn) position = Vector3(spawn)
$CameraGimbal.reset() $CameraGimbal.reset()
_health = 100 _health = 100
visible = true visible = true
suspended = false dead = false
# add starting gear # add starting gear
var gear = starting_gear.instantiate() var gear = starting_gear.instantiate()
@ -185,16 +195,17 @@ func do_movement(delta):
var mx = 0 var mx = 0
var mz = 0 var mz = 0
if Input.is_action_pressed("move_forward"): if not suspended:
mz -= 1 if Input.is_action_pressed("move_forward"):
if Input.is_action_pressed("move_back"): mz -= 1
mz += 1 if Input.is_action_pressed("move_back"):
if Input.is_action_pressed("move_left"): mz += 1
mx -= 1 if Input.is_action_pressed("move_left"):
if Input.is_action_pressed("move_right"): mx -= 1
mx += 1 if Input.is_action_pressed("move_right"):
if Input.is_action_pressed("jump") and is_on_floor(): mx += 1
target_velocity.y = jump_power if Input.is_action_pressed("jump") and is_on_floor():
target_velocity.y = jump_power
if !(mx == 0 and mz == 0): if !(mx == 0 and mz == 0):
move_player(mx, mz) move_player(mx, mz)
@ -227,12 +238,12 @@ func _ready():
respawn() respawn()
func _physics_process(delta): func _physics_process(delta):
if health() < 1 or position.y <= -1000: if dead:
die()
if suspended:
return return
if health() < 1 or position.y <= -1000:
_die()
# Backpack keys # Backpack keys
do_backpack_keys() do_backpack_keys()
# Movement # Movement