8000 - Removed PausedState and implemented pause directly in main_game.rs. by chrispetkau · Pull Request #93 · amethyst/evoli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Oct 25, 2021. It is now read-only.

- Removed PausedState and implemented pause directly in main_game.rs. #93

Merged
merged 3 commits into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 100 additions & 100 deletions resources/prefabs/ui/main_game.ron
Original file line number Diff line number Diff line change
@@ -1,100 +1,100 @@
#![enable(implicit_some)]
Container(
transform: (
id: "main game",
anchor: Middle,
stretch: XY( x_margin: 0.0, y_margin: 0.0, keep_aspect_ratio: true),
width: 1280.0,
height: 960.0,
),
children: [
Button(
transform: (
id: "menu button",
x: -355.0,
y: 20.0,
width: 80.0,
height: 36.0,
anchor: BottomRight,
mouse_reactive: true,
),
button: (
text: "Menu",
font: File("assets/fonts/OpenSans-Regular.ttf", ("TTF", ())),
font_size: 24.0,
normal_image: SolidColor(0.4, 0.4, 0.4, 1.),
hover_image: SolidColor(0.5, 0.5, 0.5, 1.),
press_image: SolidColor(0.2, 0.2, 0.2, 1.),
normal_text_color: (0.2, 0.2, 0.2, 1.0),
hover_text_color: (0.7, 0.7, 0.7, 1.0),
press_text_color: (1.0, 1.0, 1.0, 1.0),
)
),
Button(
transform: (
id: "pause button",
x: -265.0,
y: 20.0,
width: 80.0,
height: 36.0,
anchor: BottomRight,
mouse_reactive: true,
),
button: (
text: "Pause",
font: File("assets/fonts/OpenSans-Regular.ttf", ("TTF", ())),
font_size: 24.0,
normal_image: SolidColor(0.4, 0.4, 0.4, 1.),
hover_image: SolidColor(0.5, 0.5, 0.5, 1.),
press_image: SolidColor(0.2, 0.2, 0.2, 1.),
normal_text_color: (0.2, 0.2, 0.2, 1.0),
hover_text_color: (0.7, 0.7, 0.7, 1.0),
press_text_color: (1.0, 1.0, 1.0, 1.0),
)
),
Button(
transform: (
id: "slow down button",
x: -165.0,
y: 20.0,
width: 100.0,
height: 36.0,
anchor: BottomRight,
mouse_reactive: true,
),
button: (
text: "Slow Down",
font: File("assets/fonts/OpenSans-Regular.ttf", ("TTF", ())),
font_size: 24.0,
normal_image: SolidColor(0.4, 0.4, 0.4, 1.),
hover_image: SolidColor(0.5, 0.5, 0.5, 1.),
press_image: SolidColor(0.2, 0.2, 0.2, 1.),
normal_text_color: (0.2, 0.2, 0.2, 1.0),
hover_text_color: (0.7, 0.7, 0.7, 1.0),
press_text_color: (1.0, 1.0, 1.0, 1.0),
)
),
Button(
transform: (
id: "speed up button",
x: -55.0,
y: 20.0,
width: 100.0,
height: 36.0,
anchor: BottomRight,
mouse_reactive: true,
),
button: (
text: "Speed Up",
font: File("assets/fonts/OpenSans-Regular.ttf", ("TTF", ())),
font_size: 24.0,
normal_image: SolidColor(0.4, 0.4, 0.4, 1.),
hover_image: SolidColor(0.5, 0.5, 0.5, 1.),
press_image: SolidColor(0.2, 0.2, 0.2, 1.),
normal_text_color: (0.2, 0.2, 0.2, 1.0),
hover_text_color: (0.7, 0.7, 0.7, 1.0),
press_text_color: (1.0, 1.0, 1.0, 1.0),
)
),
]
)
#![enable(implicit_some)]
Container(
transform: (
id: "main game",
anchor: Middle,
stretch: XY( x_margin: 0.0, y_margin: 0.0, keep_aspect_ratio: true),
width: 1280.0,
height: 960.0,
),
children: [
Button(
transform: (
id: "menu button",
x: -355.0,
y: 20.0,
width: 80.0,
height: 36.0,
anchor: BottomRight,
mouse_reactive: true,
),
button: (
text: "Menu",
font: File("assets/fonts/OpenSans-Regular.ttf", ("TTF", ())),
font_size: 24.0,
normal_image: SolidColor(0.4, 0.4, 0.4, 1.),
hover_image: SolidColor(0.5, 0.5, 0.5, 1.),
press_image: SolidColor(0.2, 0.2, 0.2, 1.),
normal_text_color: (0.2, 0.2, 0.2, 1.0),
hover_text_color: (0.7, 0.7, 0.7, 1.0),
press_text_color: (1.0, 1.0, 1.0, 1.0),
)
),
Button(
transform: (
id: "pause button",
x: -265.0,
y: 20.0,
width: 80.0,
height: 36.0,
anchor: BottomRight,
mouse_reactive: true,
),
button: (
text: "Pause",
font: File("assets/fonts/OpenSans-Regular.ttf", ("TTF", ())),
font_size: 24.0,
normal_image: SolidColor(0.4, 0.4, 0.4, 1.),
hover_image: SolidColor(0.5, 0.5, 0.5, 1.),
press_image: SolidColor(0.2, 0.2, 0.2, 1.),
normal_text_color: (0.2, 0.2, 0.2, 1.0),
hover_text_color: (0.7, 0.7, 0.7, 1.0),
press_text_color: (1.0, 1.0, 1.0, 1.0),
)
),
Button(
transform: (
id: "slow down button",
x: -165.0,
y: 20.0,
width: 100.0,
height: 36.0,
anchor: BottomRight,
mouse_reactive: true,
),
button: (
text: "Slow Down",
font: File("assets/fonts/OpenSans-Regular.ttf", ("TTF", ())),
font_size: 24.0,
normal_image: SolidColor(0.4, 0.4, 0.4, 1.),
hover_image: SolidColor(0.5, 0.5, 0.5, 1.),
press_image: SolidColor(0.2, 0.2, 0.2, 1.),
normal_text_color: (0.2, 0.2, 0.2, 1.0),
hover_text_color: (0.7, 0.7, 0.7, 1.0),
press_text_color: (1.0, 1.0, 1.0, 1.0),
)
),
Button(
transform: (
id: "speed up button",
x: -55.0,
y: 20.0,
width: 100.0,
height: 36.0,
anchor: BottomRight,
mouse_reactive: true,
),
button: (
text: "Speed Up",
font: File("assets/fonts/OpenSans-Regular.ttf", ("TTF", ())),
font_size: 24.0,
normal_image: SolidColor(0.4, 0.4, 0.4, 1.),
hover_image: SolidColor(0.5, 0.5, 0.5, 1.),
press_image: SolidColor(0.2, 0.2, 0.2, 1.),
normal_text_color: (0.2, 0.2, 0.2, 1.0),
hover_text_color: (0.7, 0.7, 0.7, 1.0),
press_text_color: (1.0, 1.0, 1.0, 1.0),
)
),
]
)
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[macro_use]
extern crate log;

use amethyst::assets::PrefabLoaderSystem;
use amethyst::{
assets::Processor,
Expand Down
69 changes: 53 additions & 16 deletions src/states/main_game.rs
9E19
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use amethyst;

use amethyst::{
core::math::{Rotation3, Vector3},
core::math::{clamp, Rotation3, Vector3},
core::{transform::Transform, ArcThreadPool, Time},
ecs::*,
input::InputEvent,
Expand All @@ -24,18 +24,23 @@ use crate::{
debug::DebugConfig, prefabs::UiPrefabRegistry, spatial_grid::SpatialGrid,
world_bounds::WorldBounds,
},
states::{menu::MenuState, paused::PausedState},
states::menu::MenuState,
systems::*,
};
use rand::{thread_rng, Rng};
use std::f32::consts::PI;

const TIME_SCALE_FACTOR: f32 = 2.0;
const TIME_SCALE_RANGE: (f32, f32) = (1.0 / 4.0, 1.0 * 4.0);

pub struct MainGameState {
dispatcher: Dispatcher<'static, 'static>,
debug_dispatcher: Dispatcher<'static, 'static>,
ui_dispatcher: Dispatcher<'static, 'static>,
ui: Option<Entity>,
camera: Option<Entity>,
paused: bool,
desired_time_scale: f32,
}

impl MainGameState {
Expand Down Expand Up @@ -197,8 +202,6 @@ impl MainGameState {
&[],
)
.build(),
// The ui dispatcher will also run when this game state is paused. This is necessary so that
// the user can interact with the UI even if the game is in the `Paused` game state.
ui_dispatcher: DispatcherBuilder::new()
.with(
main_game_ui::MainGameUiSystem::default(),
Expand All @@ -208,25 +211,46 @@ impl MainGameState {
.build(),
ui: None,
camera: None,
paused: false,
desired_time_scale: 1.0,
}
}

fn handle_action(&self, action: &str, world: &mut World) -> SimpleTrans {
// push desired_time_scale into effect
fn update_time_scale(&self, world: &mut World) {
world
.write_resource::<Time>()
.set_time_scale(if self.paused {
0.0
} else {
self.desired_time_scale
});
}

fn handle_action(&mut self, action: &str, world: &mut World) -> SimpleTrans {
if action == "ToggleDebug" {
let mut debug_config = world.write_resource::<DebugConfig>();
debug_config.visible = !debug_config.visible;
Trans::None
} else if action == main_game_ui::PAUSE_BUTTON.action {
Trans::Push(Box::new(PausedState::default()))
self.paused = !self.paused;
self.update_time_scale(world);
Trans::None
} else if action == main_game_ui::SPEED_UP_BUTTON.action {
let mut time_resource = world.write_resource::<Time>();
let current_time_scale = time_resource.time_scale();
time_resource.set_time_scale(2.0 * current_time_scale);
self.desired_time_scale = clamp(
self.desired_time_scale * TIME_SCALE_FACTOR,
TIME_SCALE_RANGE.0,
TIME_SCALE_RANGE.1,
);
self.update_time_scale(world);
Trans::None
} else if action == main_game_ui::SLOW_DOWN_BUTTON.action {
let mut time_resource = world.write_resource::<Time>();
let current_time_scale = time_resource.time_scale();
time_resource.set_time_scale(0.5 * current_time_scale);
self.desired_time_scale = clamp(
self.desired_time_scale / TIME_SCALE_FACTOR,
TIME_SCALE_RANGE.0,
TIME_SCALE_RANGE.1,
);
self.update_time_scale(world);
Trans::None
} else if action == main_game_ui::MENU_BUTTON.action {
Trans::Switch(Box::new(MenuState::default()))
Expand All @@ -252,6 +276,8 @@ impl SimpleState for MainGameState {
}

fn on_start(&mut self, data: StateData<GameData>) {
info!("start main game");

self.dispatcher.setup(&mut data.world.res);
self.debug_dispatcher.setup(&mut data.world.res);
self.ui_dispatcher.setup(&mut data.world.res);
Expand Down Expand Up @@ -362,9 +388,15 @@ impl SimpleState for MainGameState {
.with(transform)
.build(),
);

// initialize time scale
self.paused = false;
data.world.write_resource::<Time>().set_time_scale(1.0);
}

fn on_stop(&mut self, data: StateData<GameData>) {
info!("stop main game");

if let Some(ui) = self.ui {
if data.world.delete_entity(ui).is_ok() {
self.ui = None;
Expand All @@ -386,6 +418,12 @@ impl SimpleState for MainGameState {
{
organisms.push(entity);
}
data.world
.delete_entities(&organisms)
.expect("failed to delete all organisms");

// fix up time scale before we leave this state
data.world.write_resource::<Time>().set_time_scale(1.0);
}

fn update(&mut self, data: &mut StateData<GameData>) -> SimpleTrans {
Expand All @@ -401,10 +439,9 @@ impl SimpleState for MainGameState {
}

data.data.update(&data.world);
Trans::None
}

fn shadow_update(&mut self, data: StateData<GameData>) {
self.ui_dispatcher.dispatch(&mut data.world.res);
self.ui_dispatcher.dispatch(&data.world.res);

Trans::None
}
}
1 change: 0 additions & 1 deletion src/states/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod loading;
pub mod main_game;
pub mod menu;
pub mod paused;

//use amethyst::{
//core::EventReader,
Expand Down
31 changes: 0 additions & 31 deletions src/states/paused.rs

This file was deleted.

Loading
0