Ask before the death screen's Title Screen button leaves the world
This commit is contained in:
@@ -63,13 +63,24 @@ impl Confirm {
|
||||
atlas: &HudAtlas,
|
||||
cursor: (f32, f32),
|
||||
labels: [&Text; 2],
|
||||
) {
|
||||
self.draw_active(batch, atlas, cursor, labels, true);
|
||||
}
|
||||
/// `draw` with the buttons greyed out while `setDelay` runs (`active` false).
|
||||
pub fn draw_active(
|
||||
&self,
|
||||
batch: &mut HudBatch,
|
||||
atlas: &HudAtlas,
|
||||
cursor: (f32, f32),
|
||||
labels: [&Text; 2],
|
||||
active: bool,
|
||||
) {
|
||||
for (line, x, y) in &self.lines {
|
||||
batch.text_styled(atlas, line, *x, *y, u32::MAX, true);
|
||||
}
|
||||
for ((bx, by), label) in self.buttons.into_iter().zip(labels) {
|
||||
let hover = ui::hovered(cursor, bx, by, 150.0, 20.0);
|
||||
ui::button(batch, atlas, bx, by, 150.0, 20.0, label, true, hover);
|
||||
let hover = active && ui::hovered(cursor, bx, by, 150.0, 20.0);
|
||||
ui::button(batch, atlas, bx, by, 150.0, 20.0, label, active, hover);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4281,6 +4281,7 @@ impl Game<'_> {
|
||||
self.screen = Screen::Dead;
|
||||
self.ui.died_at = Some(Instant::now());
|
||||
self.ui.death_text = None;
|
||||
self.ui.death_confirm = None;
|
||||
self.capture(false);
|
||||
}
|
||||
self.previous_position = feet;
|
||||
@@ -11860,6 +11861,20 @@ impl ApplicationHandler for Game<'_> {
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
// ConfirmScreen.keyPressed: Escape answers no, once its delay is over.
|
||||
WindowEvent::KeyboardInput { event, .. }
|
||||
if self.screen == Screen::Dead && self.ui.death_confirm.is_some() =>
|
||||
{
|
||||
if event.state == ElementState::Pressed
|
||||
&& event.physical_key == PhysicalKey::Code(KeyCode::Escape)
|
||||
&& self
|
||||
.ui
|
||||
.death_confirm
|
||||
.is_some_and(|t| t.elapsed().as_millis() >= 1000)
|
||||
{
|
||||
self.death_confirm_answer(false)?;
|
||||
}
|
||||
}
|
||||
// ConfirmScreen.keyPressed: Escape answers no.
|
||||
WindowEvent::KeyboardInput { event, .. } if self.server_packs.prompt.is_some() => {
|
||||
if event.state == ElementState::Pressed
|
||||
|
||||
@@ -102,6 +102,9 @@ pub struct UiState {
|
||||
/// Death screen cause and when the player died (buttons wait 20 ticks).
|
||||
pub death_text: Option<Text>,
|
||||
pub died_at: Option<std::time::Instant>,
|
||||
/// `DeathScreen.TitleConfirmScreen` and when it opened (its buttons wait
|
||||
/// 20 ticks, `setDelay`).
|
||||
pub death_confirm: Option<std::time::Instant>,
|
||||
/// `LivingEntity.deathTime` of the window's player: ticks since it died.
|
||||
pub death_time: u32,
|
||||
}
|
||||
@@ -886,6 +889,7 @@ impl Game<'_> {
|
||||
let cause = self.ui.death_cause.take();
|
||||
self.ui.died_at = Some(std::time::Instant::now());
|
||||
self.ui.death_text = None;
|
||||
self.ui.death_confirm = None;
|
||||
if !self.ui.rules.bool("show_death_messages") {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -2185,6 +2185,7 @@ impl Game<'_> {
|
||||
if p.health <= 0.0 && self.screen != Screen::Dead && self.player.is_some() {
|
||||
self.screen = Screen::Dead;
|
||||
self.ui.death_time = 0;
|
||||
self.ui.death_confirm = None;
|
||||
self.capture(false);
|
||||
}
|
||||
}
|
||||
@@ -2202,6 +2203,7 @@ impl Game<'_> {
|
||||
if me && self.screen != Screen::Dead && self.player.is_some() {
|
||||
self.screen = Screen::Dead;
|
||||
self.ui.death_time = 0;
|
||||
self.ui.death_confirm = None;
|
||||
self.capture(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ use super::{Game, Screen};
|
||||
use horizon_client::{
|
||||
Result,
|
||||
command::args::GameMode,
|
||||
confirm_screen::Confirm,
|
||||
hud::{HudBatch, rgba},
|
||||
text::{self, Text},
|
||||
ui,
|
||||
@@ -84,6 +85,14 @@ fn pause_icons(w: f32, h: f32) -> [(f32, f32, &'static str); 4] {
|
||||
]
|
||||
}
|
||||
/// DeathScreen.init: a hardcore player can only spectate.
|
||||
/// `DeathScreen.TitleConfirmScreen`: its title and the yes and no labels.
|
||||
fn death_confirm_texts() -> (Text, [Text; 2]) {
|
||||
let t = |key: &str| Text::translate(key, &[]);
|
||||
(
|
||||
t("deathScreen.quit.confirm"),
|
||||
[t("deathScreen.titleScreen"), t("deathScreen.respawn")],
|
||||
)
|
||||
}
|
||||
fn death_buttons(w: f32, h: f32, hardcore: bool) -> [(f32, f32, &'static str, u8); 2] {
|
||||
let respawn = if hardcore {
|
||||
"deathScreen.spectate"
|
||||
@@ -231,6 +240,20 @@ impl Game<'_> {
|
||||
),
|
||||
);
|
||||
}
|
||||
// DeathScreen.handleExitToTitleScreen: the TitleConfirmScreen takes the
|
||||
// screen over, on the same background.
|
||||
if let Some(opened) = self.ui.death_confirm {
|
||||
let (title, labels) = death_confirm_texts();
|
||||
let size = self.gui_size();
|
||||
Confirm::new(&atlas.font, size, &title, &Text::literal("")).draw_active(
|
||||
batch,
|
||||
atlas,
|
||||
self.cursor,
|
||||
[&labels[0], &labels[1]],
|
||||
opened.elapsed().as_millis() >= 1000,
|
||||
);
|
||||
return;
|
||||
}
|
||||
let title = if self.level.hardcore {
|
||||
"deathScreen.title.hardcore"
|
||||
} else {
|
||||
@@ -294,6 +317,23 @@ impl Game<'_> {
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
if let Some(opened) = self.ui.death_confirm {
|
||||
// ConfirmScreen.setDelay: both buttons wait 20 ticks.
|
||||
if opened.elapsed().as_millis() < 1000 {
|
||||
return Ok(());
|
||||
}
|
||||
let (title, _) = death_confirm_texts();
|
||||
let confirm = Confirm::new(
|
||||
&self.gui.atlas.font,
|
||||
self.gui_size(),
|
||||
&title,
|
||||
&Text::literal(""),
|
||||
);
|
||||
if let Some(yes) = confirm.answer_at(self.cursor) {
|
||||
self.death_confirm_answer(yes)?;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
let size = self
|
||||
.renderer
|
||||
.as_ref()
|
||||
@@ -303,6 +343,27 @@ impl Game<'_> {
|
||||
.into_iter()
|
||||
.find(|d| ui::hovered(self.cursor, d.0, d.1, 200.0, 20.0))
|
||||
.map(|d| d.3);
|
||||
// handleExitToTitleScreen: outside hardcore, Title Screen asks first.
|
||||
if hit == Some(1) && !self.level.hardcore {
|
||||
self.ui.death_confirm = Some(std::time::Instant::now());
|
||||
self.audio.play_button_click();
|
||||
return Ok(());
|
||||
}
|
||||
self.death_action(hit)
|
||||
}
|
||||
/// The confirm screen's answer: yes leaves the world, no respawns
|
||||
/// (`DeathScreen.handleExitToTitleScreen`'s callback).
|
||||
pub(super) fn death_confirm_answer(&mut self, yes: bool) -> Result<()> {
|
||||
self.ui.death_confirm = None;
|
||||
if yes {
|
||||
self.audio.play_button_click();
|
||||
self.close_world()
|
||||
} else {
|
||||
self.death_action(Some(0))
|
||||
}
|
||||
}
|
||||
/// A death screen button: 0 respawns (or spectates in hardcore), 1 leaves.
|
||||
fn death_action(&mut self, hit: Option<u8>) -> Result<()> {
|
||||
if hit == Some(0) && self.remote.is_none() {
|
||||
let keep = self.ui.rules.bool("keep_inventory") || self.spectator();
|
||||
self.experience.respawn(keep);
|
||||
|
||||
Reference in New Issue
Block a user