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 committed by jeremy
parent 1e4aeddbb5
commit 5b7ab7811e

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()
@ -123,6 +133,8 @@ func use_backpack_slot(n):
return return
func equip(gear: Gear): func equip(gear: Gear):
if suspended:
return
var gear_name = gear.gear_name() var gear_name = gear.gear_name()
# do we have the gear equipped? # do we have the gear equipped?
if gear_name == $Pivot/Container/Gear.gear_name(): if gear_name == $Pivot/Container/Gear.gear_name():
@ -185,6 +197,7 @@ func do_movement(delta):
var mx = 0 var mx = 0
var mz = 0 var mz = 0
if not suspended:
if Input.is_action_pressed("move_forward"): if Input.is_action_pressed("move_forward"):
mz -= 1 mz -= 1
if Input.is_action_pressed("move_back"): if Input.is_action_pressed("move_back"):
@ -227,12 +240,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