167 lines
4.5 KiB
GDScript
167 lines
4.5 KiB
GDScript
extends PanelContainer
|
|
|
|
@onready var _screen = $PanelMargin/PanelColumn/Screen
|
|
@onready var _close_button: Button = $PanelMargin/PanelColumn/CloseButton
|
|
|
|
var current_floor: int = EventBus.STARTING_FLOOR
|
|
var saved_count := 0
|
|
var doors_closing_flag := false
|
|
var _onboarded := false
|
|
|
|
var base_survivors := 2
|
|
var survivors_per_floor_increase := 1
|
|
var survivors_remaining := 0
|
|
|
|
var threshold := 2
|
|
|
|
var score := 0
|
|
var points_per_person := 100
|
|
|
|
const MOVING_ZONE_START_FLOOR := 7
|
|
const MOVING_ZONE_BASE_SPEED := 40.0
|
|
const MOVING_ZONE_SPEED_PER_FLOOR := 12.0
|
|
|
|
var robot_speed_top := 1.0
|
|
var robot_speed_per_floor := 0.15
|
|
var robot_delay_top := 8.0
|
|
var robot_delay_per_floor := 0.6
|
|
var robot_delay_min := 1.0
|
|
|
|
var people_in_elevator := 0
|
|
|
|
const PERFECT_STUN_DURATION := 0.25
|
|
const PRE_PULSE_DELAY := 3.5
|
|
|
|
func _ready():
|
|
_close_button.text = "CLOSE"
|
|
_close_button.pressed.connect(_on_block_pressed)
|
|
|
|
_screen.pulse_started.connect(_on_pulse_started)
|
|
_screen.pulse_blocked.connect(_on_pulse_blocked)
|
|
_screen.pulse_blocked_perfect.connect(_on_pulse_blocked_perfect)
|
|
_screen.doors_closing.connect(func(): _on_doors_closing(true))
|
|
|
|
EventBus.game_started.connect(_start_floor)
|
|
|
|
func _unhandled_input(event):
|
|
if event is InputEventKey and event.pressed and not event.echo:
|
|
if event.keycode == KEY_SPACE:
|
|
_on_block_pressed()
|
|
elif event.keycode == KEY_R:
|
|
get_tree().reload_current_scene()
|
|
|
|
func _start_floor():
|
|
$SfxOpen.play()
|
|
$SfxBell.play()
|
|
doors_closing_flag = false
|
|
people_in_elevator = 0
|
|
_close_button.text = "CLOSE"
|
|
EventBus.doors_opened.emit()
|
|
|
|
var floors_remaining = current_floor - 1
|
|
survivors_remaining = base_survivors + (floors_remaining * survivors_per_floor_increase)
|
|
|
|
EventBus.people_changed.emit(people_in_elevator, threshold)
|
|
EventBus.floor_changed.emit(current_floor)
|
|
EventBus.floor_started.emit(survivors_remaining)
|
|
|
|
var floors_descended = EventBus.STARTING_FLOOR - current_floor
|
|
var robot_speed = robot_speed_top + floors_descended * robot_speed_per_floor
|
|
var robot_delay = max(robot_delay_min, robot_delay_top - floors_descended * robot_delay_per_floor)
|
|
EventBus.robot_floor_started.emit(robot_delay, robot_speed)
|
|
|
|
_screen.start(current_floor)
|
|
|
|
if current_floor <= MOVING_ZONE_START_FLOOR:
|
|
var floors_below = MOVING_ZONE_START_FLOOR - current_floor
|
|
_screen.set_block_zone_movement(MOVING_ZONE_BASE_SPEED + floors_below * MOVING_ZONE_SPEED_PER_FLOOR)
|
|
|
|
if not _onboarded:
|
|
_onboarded = true
|
|
_screen.show_onboarding("BLOCK\nIN GREEN ZONE")
|
|
get_tree().create_timer(3.0).timeout.connect(
|
|
func():
|
|
if not is_instance_valid(_screen):
|
|
return
|
|
_screen.end_onboarding()
|
|
_screen.launch_pulse(),
|
|
CONNECT_ONE_SHOT
|
|
)
|
|
else:
|
|
get_tree().create_timer(PRE_PULSE_DELAY).timeout.connect(
|
|
func():
|
|
if not is_instance_valid(self) or doors_closing_flag:
|
|
return
|
|
_screen.launch_pulse(),
|
|
CONNECT_ONE_SHOT
|
|
)
|
|
|
|
func _on_block_pressed():
|
|
if doors_closing_flag:
|
|
return
|
|
if _screen.pulse_active:
|
|
_screen.attempt_block()
|
|
else:
|
|
_on_doors_closing(false)
|
|
|
|
func _on_pulse_started(duration: float):
|
|
_close_button.text = "BLOCK"
|
|
EventBus.pulse_started.emit(duration)
|
|
|
|
func _on_pulse_blocked_perfect():
|
|
EventBus.robot_stun_requested.emit(PERFECT_STUN_DURATION)
|
|
|
|
func _on_pulse_blocked():
|
|
_close_button.text = "CLOSE"
|
|
EventBus.pulse_blocked.emit()
|
|
survivors_remaining -= 1
|
|
people_in_elevator += 1
|
|
$SfxDing.play()
|
|
EventBus.people_changed.emit(people_in_elevator, threshold)
|
|
|
|
if survivors_remaining <= 0:
|
|
get_tree().create_timer(0.25).timeout.connect(_on_doors_closing, CONNECT_ONE_SHOT)
|
|
return
|
|
|
|
var gap = _screen.get_pulse_gap()
|
|
get_tree().create_timer(gap).timeout.connect(
|
|
func():
|
|
if not doors_closing_flag:
|
|
_screen.launch_pulse(),
|
|
CONNECT_ONE_SHOT
|
|
)
|
|
|
|
func _on_doors_closing(fast: bool = false):
|
|
if doors_closing_flag:
|
|
return
|
|
doors_closing_flag = true
|
|
EventBus.doors_closed.emit(fast)
|
|
|
|
if people_in_elevator < threshold:
|
|
_screen.show_loss("TOO FEW")
|
|
EventBus.game_lost.emit("TOO FEW")
|
|
return
|
|
|
|
var base_points = people_in_elevator * points_per_person
|
|
var bonus = max(0, people_in_elevator - threshold) * points_per_person
|
|
score += base_points + bonus
|
|
saved_count += people_in_elevator
|
|
|
|
EventBus.score_changed.emit(score)
|
|
EventBus.saved_changed.emit(saved_count)
|
|
|
|
current_floor -= 1
|
|
|
|
if current_floor <= 1:
|
|
_screen.show_win()
|
|
EventBus.floor_changed.emit(current_floor)
|
|
EventBus.game_won.emit()
|
|
return
|
|
|
|
$SfxClose.play()
|
|
_screen.show_countdown()
|
|
_screen.shrink_block_zone()
|
|
|
|
var tween = create_tween()
|
|
tween.tween_interval(3.0)
|
|
tween.tween_callback(_start_floor)
|