Browse Source

Respawn when the player falls off the map

Aaron Franke 5 years ago
parent
commit
7ea3d4de9c
2 changed files with 24 additions and 2 deletions
  1. 15 2
      player/player.gd
  2. 9 0
      player/player.tscn

+ 15 - 2
player/player.gd

@@ -22,6 +22,7 @@ var velocity = Vector3()
 var aiming = false
 var camera_x_rot = 0.0
 
+onready var initial_position = transform.origin
 onready var gravity = ProjectSettings.get_setting("physics/3d/default_gravity") * ProjectSettings.get_setting("physics/3d/default_gravity_vector")
 
 onready var animation_tree = $AnimationTree
@@ -41,6 +42,18 @@ func _ready():
 	orientation.origin = Vector3()
 
 
+func _process(_delta):
+	# Fade out to black if falling out of the map. -17 is lower than
+	# the lowest valid position on the map (which is a bit under -16).
+	# At 15 units below -17 (so -32), the screen turns fully black.
+	if transform.origin.y < -17:
+		$ColorRect.modulate.a = min((-17 - transform.origin.y) / 15, 1)
+		# If we're below -40, respawn (teleport to the initial position).
+		if transform.origin.y < -40:
+			$ColorRect.modulate.a = 0
+			transform.origin = initial_position
+
+
 func _physics_process(delta):
 	var camera_move = Vector2(Input.get_action_strength("view_right") - Input.get_action_strength("view_left"),
 								Input.get_action_strength("view_up") - Input.get_action_strength("view_down"))
@@ -175,8 +188,8 @@ func _input(event):
 
 
 func rotate_camera(move):
-	$CameraBase.rotate_y(-move.x)
-	$CameraBase.orthonormalize() # After relative transforms, camera needs to be renormalized.
+	camera_base.rotate_y(-move.x)
+	camera_base.orthonormalize() # After relative transforms, camera needs to be renormalized.
 	camera_x_rot += move.y
 	camera_x_rot = clamp(camera_x_rot, deg2rad(CAMERA_X_ROT_MIN), deg2rad(CAMERA_X_ROT_MAX))
 	camera_rot.rotation.x = camera_x_rot

+ 9 - 0
player/player.tscn

@@ -286,6 +286,15 @@ __meta__ = {
 anims/far = SubResource( 25 )
 anims/shoot = SubResource( 26 )
 
+[node name="ColorRect" type="ColorRect" parent="."]
+modulate = Color( 1, 1, 1, 0 )
+anchor_right = 1.0
+anchor_bottom = 1.0
+color = Color( 0, 0, 0, 1 )
+__meta__ = {
+"_edit_use_anchors_": false
+}
+
 [node name="Crosshair" type="TextureRect" parent="."]
 self_modulate = Color( 1, 1, 1, 0 )
 anchor_left = 0.5