150 lines
4.6 KiB
GDScript
150 lines
4.6 KiB
GDScript
extends Area3D
|
|
class_name CombatArea
|
|
|
|
var out_of_bounds_players: Dictionary = {} # body -> { time_left: 5.0 }
|
|
var recovery_players: Dictionary = {} # body -> { time_safe: 0.0, time_left: float }
|
|
|
|
var _ui_layer: CanvasLayer = null
|
|
var _ui_label: Label = null
|
|
var _ui_overlay: ColorRect = null
|
|
var _is_local_player_out: bool = false
|
|
var _local_player_body: Node3D = null
|
|
|
|
func _ready() -> void:
|
|
collision_layer = 0
|
|
collision_mask = 4294967295 # All layers
|
|
body_exited.connect(_on_body_exited)
|
|
body_entered.connect(_on_body_entered)
|
|
set_process(false)
|
|
|
|
func _process(delta: float) -> void:
|
|
# Process players who are actively out of bounds
|
|
var to_kill: Array = []
|
|
for body in out_of_bounds_players.keys():
|
|
if "is_dead" in body and body.is_dead:
|
|
out_of_bounds_players.erase(body)
|
|
if body == _local_player_body:
|
|
_remove_ui()
|
|
continue
|
|
|
|
var data = out_of_bounds_players[body]
|
|
data.time_left -= delta
|
|
if data.time_left <= 0.0:
|
|
data.time_left = 0.0
|
|
to_kill.append(body)
|
|
|
|
if body == _local_player_body and _is_local_player_out and _ui_label and _ui_overlay:
|
|
_ui_label.text = "WARNING: RETURN TO COMBAT AREA\n%.1f" % data.time_left
|
|
_ui_overlay.color = Color(1.0, 0.0, 0.0, 0.8 * (1.0 - (data.time_left / 5.0)))
|
|
|
|
for body in to_kill:
|
|
_kill_player(body)
|
|
|
|
# Process players who returned but are recovering
|
|
var recovered: Array = []
|
|
for body in recovery_players.keys():
|
|
if "is_dead" in body and body.is_dead:
|
|
recovered.append(body)
|
|
continue
|
|
|
|
var data = recovery_players[body]
|
|
data.time_safe += delta
|
|
if data.time_safe >= 10.0:
|
|
recovered.append(body)
|
|
|
|
for body in recovered:
|
|
recovery_players.erase(body)
|
|
|
|
if out_of_bounds_players.is_empty() and recovery_players.is_empty():
|
|
set_process(false)
|
|
|
|
func _on_body_exited(body: Node3D) -> void:
|
|
if body is CharacterBody3D and body.has_method("take_damage") and str(body.name).is_valid_int():
|
|
if "is_dead" in body and body.is_dead:
|
|
return
|
|
if body.is_queued_for_deletion() or not is_inside_tree():
|
|
return
|
|
|
|
var start_time = 5.0
|
|
if recovery_players.has(body):
|
|
start_time = recovery_players[body].time_left
|
|
recovery_players.erase(body)
|
|
|
|
out_of_bounds_players[body] = { "time_left": start_time }
|
|
|
|
var is_local = false
|
|
if multiplayer.has_multiplayer_peer():
|
|
if body.get_multiplayer_authority() == multiplayer.get_unique_id():
|
|
is_local = true
|
|
|
|
if is_local:
|
|
_is_local_player_out = true
|
|
_local_player_body = body
|
|
_create_ui()
|
|
|
|
set_process(true)
|
|
|
|
func _on_body_entered(body: Node3D) -> void:
|
|
if out_of_bounds_players.has(body):
|
|
var time_left_val = out_of_bounds_players[body].time_left
|
|
out_of_bounds_players.erase(body)
|
|
|
|
if time_left_val < 5.0:
|
|
recovery_players[body] = { "time_left": time_left_val, "time_safe": 0.0 }
|
|
set_process(true)
|
|
|
|
if body == _local_player_body and _is_local_player_out:
|
|
_remove_ui()
|
|
_is_local_player_out = false
|
|
_local_player_body = null
|
|
|
|
func _create_ui() -> void:
|
|
if _ui_layer: return
|
|
|
|
_ui_layer = CanvasLayer.new()
|
|
_ui_layer.layer = 100 # Put it on top of everything
|
|
|
|
_ui_overlay = ColorRect.new()
|
|
_ui_overlay.set_anchors_preset(Control.PRESET_FULL_RECT)
|
|
_ui_overlay.color = Color(1.0, 0.0, 0.0, 0.0)
|
|
_ui_overlay.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
_ui_layer.add_child(_ui_overlay)
|
|
|
|
_ui_label = Label.new()
|
|
_ui_label.set_anchors_preset(Control.PRESET_CENTER)
|
|
_ui_label.grow_horizontal = Control.GROW_DIRECTION_BOTH
|
|
_ui_label.grow_vertical = Control.GROW_DIRECTION_BOTH
|
|
_ui_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
|
_ui_label.add_theme_font_size_override("font_size", 48)
|
|
_ui_label.add_theme_color_override("font_color", Color(1, 0.2, 0.2))
|
|
_ui_label.add_theme_color_override("font_outline_color", Color(0, 0, 0))
|
|
_ui_label.add_theme_constant_override("outline_size", 8)
|
|
var display_time = 5.0
|
|
if out_of_bounds_players.has(_local_player_body):
|
|
display_time = out_of_bounds_players[_local_player_body].time_left
|
|
_ui_label.text = "WARNING: RETURN TO COMBAT AREA\n%.1f" % display_time
|
|
|
|
_ui_layer.add_child(_ui_label)
|
|
|
|
if get_tree().current_scene:
|
|
get_tree().current_scene.call_deferred("add_child", _ui_layer)
|
|
|
|
func _remove_ui() -> void:
|
|
if _ui_layer:
|
|
_ui_layer.queue_free()
|
|
_ui_layer = null
|
|
_ui_label = null
|
|
_ui_overlay = null
|
|
|
|
func _kill_player(body: Node3D) -> void:
|
|
if body:
|
|
if multiplayer.is_server():
|
|
if body.has_method("take_damage"):
|
|
body.take_damage(9999, Vector3.ZERO, null, Vector3.ZERO)
|
|
|
|
out_of_bounds_players.erase(body)
|
|
if body == _local_player_body and _is_local_player_out:
|
|
_remove_ui()
|
|
_is_local_player_out = false
|
|
_local_player_body = null
|