Skip to content

Commit

Permalink
added end game screen and trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDIMAS committed Apr 18, 2021
1 parent b395b71 commit a450544
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 1 deletion.
Binary file modified data/levels/lab.rgs
Binary file not shown.
104 changes: 104 additions & 0 deletions src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,107 @@ impl DeathScreen {
ui.node(self.root).visibility()
}
}

pub struct FinalScreen {
root: Handle<UiNode>,
exit_to_menu: Handle<UiNode>,
exit_game: Handle<UiNode>,
sender: Sender<Message>,
}

impl FinalScreen {
pub fn new(ui: &mut Gui, font: SharedFont, sender: Sender<Message>) -> Self {
let exit_to_menu;
let exit_game;
let root = BorderBuilder::new(
WidgetBuilder::new()
.with_visibility(false)
.with_width(ui.screen_size().x)
.with_height(ui.screen_size().y)
.with_background(Brush::Solid(Color::opaque(40, 40, 40)))
.with_child(
GridBuilder::new(
WidgetBuilder::new()
.with_child(
TextBuilder::new(
WidgetBuilder::new()
.with_foreground(Brush::Solid(Color::opaque(255, 255, 255)))
.on_row(0)
.on_column(0)
.with_horizontal_alignment(HorizontalAlignment::Center)
.with_vertical_alignment(VerticalAlignment::Bottom),
)
.with_text("Thanks for playing demo version of the game!")
.with_font(font.clone())
.build(&mut ui.build_ctx()),
)
.with_child(
StackPanelBuilder::new(
WidgetBuilder::new()
.with_vertical_alignment(VerticalAlignment::Top)
.on_row(1)
.on_column(0)
.with_child({
exit_to_menu = ButtonBuilder::new(
WidgetBuilder::new()
.with_width(300.0)
.with_margin(Thickness::uniform(2.0)),
)
.with_text("Exit To Menu")
.with_font(font.clone())
.build(&mut ui.build_ctx());
exit_to_menu
})
.with_child({
exit_game = ButtonBuilder::new(
WidgetBuilder::new()
.with_margin(Thickness::uniform(2.0))
.with_width(300.0),
)
.with_text("Exit Game")
.with_font(font)
.build(&mut ui.build_ctx());
exit_game
}),
)
.build(&mut ui.build_ctx()),
),
)
.add_row(Row::stretch())
.add_row(Row::stretch())
.add_column(Column::stretch())
.build(&mut ui.build_ctx()),
),
)
.build(&mut ui.build_ctx());

Self {
root,
exit_to_menu,
exit_game,
sender,
}
}

pub fn handle_ui_message(&mut self, message: &GuiMessage) {
if let UiMessageData::Button(ButtonMessage::Click) = message.data() {
if message.destination() == self.exit_to_menu {
self.sender.send(Message::ToggleMainMenu).unwrap();
} else if message.destination() == self.exit_game {
self.sender.send(Message::QuitGame).unwrap();
}
}
}

pub fn set_visible(&self, ui: &Gui, state: bool) {
ui.send_message(WidgetMessage::visibility(
self.root,
MessageDirection::ToWidget,
state,
));
}

pub fn is_visible(&self, ui: &Gui) -> bool {
ui.node(self.root).visibility()
}
}
1 change: 1 addition & 0 deletions src/level/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ pub async fn analyze(
.add(Turret::new(handle, scene, ShootMode::Consecutive, Hostility::All).await);
}
"NextLevelTrigger" => triggers.add(Trigger::new(handle, TriggerKind::NextLevel)),
"EndGameTrigger" => triggers.add(Trigger::new(handle, TriggerKind::EndGame)),
"ZombieWithGun" => spawn_points.push(SpawnPoint {
position: node.global_position(),
rotation: **node.local_transform().rotation(),
Expand Down
4 changes: 4 additions & 0 deletions src/level/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::sync::mpsc::Sender;

pub enum TriggerKind {
NextLevel,
EndGame,
}

impl Default for TriggerKind {
Expand All @@ -33,12 +34,14 @@ impl TriggerKind {
fn id(&self) -> u32 {
match self {
TriggerKind::NextLevel => 0,
TriggerKind::EndGame => 1,
}
}

fn from_id(id: u32) -> Result<Self, String> {
match id {
0 => Ok(Self::NextLevel),
1 => Ok(Self::EndGame),
_ => Err(format!("Invalid trigger id {}!", id)),
}
}
Expand Down Expand Up @@ -101,6 +104,7 @@ impl TriggerContainer {
if actor_position.metric_distance(&position) < 1.0 {
match trigger.kind {
TriggerKind::NextLevel => sender.send(Message::LoadNextLevel).unwrap(),
TriggerKind::EndGame => sender.send(Message::EndGame).unwrap(),
}
}
}
Expand Down
32 changes: 31 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub mod weapon;

use crate::config::SoundConfig;
use crate::gui::journal::JournalDisplay;
use crate::gui::FinalScreen;
use crate::level::lab::LabLevel;
use crate::level::LevelKind;
use crate::player::PlayerPersistentData;
Expand Down Expand Up @@ -172,6 +173,7 @@ pub struct Game {
load_context: Option<Arc<Mutex<LoadContext>>>,
loading_screen: LoadingScreen,
death_screen: DeathScreen,
final_screen: FinalScreen,
weapon_display: WeaponDisplay,
inventory_interface: InventoryInterface,
item_display: ItemDisplay,
Expand Down Expand Up @@ -359,6 +361,7 @@ impl Game {
&sound_config,
)),
death_screen: DeathScreen::new(&mut engine.user_interface, font.clone(), tx.clone()),
final_screen: FinalScreen::new(&mut engine.user_interface, font.clone(), tx.clone()),
control_scheme,
debug_text: Handle::NONE,
weapon_display: WeaponDisplay::new(font, engine.resource_manager.clone()),
Expand Down Expand Up @@ -417,6 +420,21 @@ impl Game {
}
WindowEvent::Resized(new_size) => {
game.engine.renderer.set_frame_size(new_size.into());

game.engine
.user_interface
.send_message(WidgetMessage::width(
game.loading_screen.root,
MessageDirection::ToWidget,
new_size.width as f32,
));
game.engine
.user_interface
.send_message(WidgetMessage::height(
game.loading_screen.root,
MessageDirection::ToWidget,
new_size.height as f32,
));
}
_ => (),
},
Expand All @@ -442,6 +460,7 @@ impl Game {
);

self.death_screen.handle_ui_message(message);
self.final_screen.handle_ui_message(message);

if matches!(message.data(), UiMessageData::Button(ButtonMessage::Click))
|| (matches!(
Expand Down Expand Up @@ -548,6 +567,8 @@ impl Game {
self.set_menu_visible(false);
self.death_screen
.set_visible(&self.engine.user_interface, false);
self.final_screen
.set_visible(&self.engine.user_interface, false);

// Set control scheme for player.
if let Some(level) = &mut self.level {
Expand Down Expand Up @@ -641,6 +662,7 @@ impl Game {
pub fn is_any_menu_visible(&self) -> bool {
self.menu.is_visible(&self.engine.user_interface)
|| self.death_screen.is_visible(&self.engine.user_interface)
|| self.final_screen.is_visible(&self.engine.user_interface)
}

pub fn update(&mut self, time: GameTime) {
Expand Down Expand Up @@ -709,7 +731,7 @@ impl Game {
while let Ok(message) = self.events_receiver.try_recv() {
match &message {
Message::StartNewGame => {
self.load_level(LevelKind::Lab, None);
self.load_level(LevelKind::Arrival, None);
}
Message::SaveGame => match self.save_game() {
Ok(_) => {
Expand Down Expand Up @@ -759,6 +781,12 @@ impl Game {
.set_visible(&self.engine.user_interface, true);
self.menu.sync_to_model(&mut self.engine, false);
}
Message::EndGame => {
self.destroy_level();
self.final_screen
.set_visible(&self.engine.user_interface, true);
self.menu.sync_to_model(&mut self.engine, false);
}
Message::SetMusicVolume(volume) => {
self.sound_config.music_volume = *volume;
// TODO: Apply to sound manager of level when it will handle music!
Expand Down Expand Up @@ -811,6 +839,8 @@ impl Game {
self.menu.set_visible(&mut self.engine, true);
self.death_screen
.set_visible(&self.engine.user_interface, false);
self.final_screen
.set_visible(&self.engine.user_interface, false);
}
Message::SyncInventory => {
if let Some(ref mut level) = self.level {
Expand Down
1 change: 1 addition & 0 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ pub enum Message {
LoadNextLevel,
ToggleMainMenu,
EndMatch,
EndGame,
SyncInventory,
SyncJournal,
ShowItemDisplay {
Expand Down

0 comments on commit a450544

Please sign in to comment.