Skip to content

Commit

Permalink
F5 to start playing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nertsal committed Aug 17, 2023
1 parent 9840a4a commit ec57a70
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/editor/handle_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl Editor {
geng::Key::Digit8 => self.handle_digit(8),
geng::Key::Digit9 => self.handle_digit(9),
geng::Key::Digit0 => self.handle_digit(0),
geng::Key::F5 => self.play_game(),
_ => {}
},
geng::Event::Wheel { delta } => {
Expand Down
21 changes: 20 additions & 1 deletion src/editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ enum State {
pub struct Editor {
geng: Geng,
assets: Rc<Assets>,
transition: Option<geng::state::Transition>,
config: EditorConfig,
util_render: UtilRender,
pixel_texture: ugli::Texture,
Expand Down Expand Up @@ -80,8 +81,9 @@ impl Editor {
geng_utils::texture::new_texture(geng.ugli(), vec2(1080 * 16 / 9, 1080));
ui_texture.set_filter(ugli::Filter::Nearest);

let model = Model::new(game_config, level.clone());
let model = Model::new(game_config, level.clone(), Time::ZERO);
Self {
transition: None,
util_render: UtilRender::new(&geng, &assets),
pixel_texture,
ui_texture,
Expand Down Expand Up @@ -121,6 +123,19 @@ impl Editor {
(pos / self.grid_size).map(Coord::round) * self.grid_size
}

/// Start playing the game from the current time.
fn play_game(&mut self) {
self.transition = Some(geng::state::Transition::Push(Box::new(
crate::game::Game::new(
&self.geng,
&self.assets,
self.model.config.clone(),
self.level.clone(),
self.current_beat * self.level.beat_time(),
),
)));
}

fn undo(&mut self) {
match &mut self.state {
State::Playing { .. } => {}
Expand Down Expand Up @@ -178,6 +193,10 @@ impl Editor {
}

impl geng::State for Editor {
fn transition(&mut self) -> Option<geng::state::Transition> {
self.transition.take()
}

fn update(&mut self, delta_time: f64) {
let delta_time = Time::new(delta_time as f32);
self.time += delta_time;
Expand Down
32 changes: 29 additions & 3 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,50 @@ use geng_utils::conversions::Vec2RealConversions;
#[allow(dead_code)]
pub struct Game {
geng: Geng,
transition: Option<geng::state::Transition>,
render: GameRender,
model: Model,
framebuffer_size: vec2<usize>,
/// Cursor position in screen space.
cursor_pos: vec2<f64>,
active_touch: Option<u64>,
music: geng::SoundEffect,
}

impl Game {
pub fn new(geng: &Geng, assets: &Rc<Assets>, rules: Config, level: Level) -> Self {
assets.music.play();
pub fn new(
geng: &Geng,
assets: &Rc<Assets>,
rules: Config,
level: Level,
start_time: Time,
) -> Self {
let mut music = assets.music.effect();
music.play_from(time::Duration::from_secs_f64(start_time.as_f32() as f64));
Self {
geng: geng.clone(),
transition: None,
render: GameRender::new(geng, assets),
model: Model::new(rules, level),
model: Model::new(rules, level, start_time),
framebuffer_size: vec2(1, 1),
cursor_pos: vec2::ZERO,
active_touch: None,
music,
}
}
}

impl Drop for Game {
fn drop(&mut self) {
self.music.stop()
}
}

impl geng::State for Game {
fn transition(&mut self) -> Option<geng::state::Transition> {
self.transition.take()
}

fn draw(&mut self, framebuffer: &mut ugli::Framebuffer) {
self.framebuffer_size = framebuffer.size();
ugli::clear(framebuffer, Some(crate::render::COLOR_DARK), None, None);
Expand All @@ -38,6 +59,11 @@ impl geng::State for Game {

fn handle_event(&mut self, event: geng::Event) {
match event {
geng::Event::KeyPress {
key: geng::Key::Escape,
} => {
self.transition = Some(geng::state::Transition::Pop);
}
geng::Event::CursorMove { position } => {
self.cursor_pos = position;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn main() {
geng.run_state(state).await;
} else {
// Game
let state = game::Game::new(&geng, &assets, config, level);
let state = game::Game::new(&geng, &assets, config, level, model::Time::ZERO);
geng.run_state(state).await;
}
});
Expand Down
6 changes: 4 additions & 2 deletions src/model/logic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::*;

impl Model {
/// Initialize the level by playing the events from the negative time.
pub fn init(&mut self) {
pub fn init(&mut self, target_time: Time) {
self.current_beat = self
.level
.events
Expand All @@ -14,9 +14,11 @@ impl Model {
.map(|x| x.as_f32().floor() as isize - 1)
.unwrap_or(0);

let target_beat = target_time / self.level.beat_time();

// Simulate at 60 fps
let delta_time = Time::new(1.0 / 60.0);
while self.current_beat < 0 {
while (self.current_beat as f32) + 1.0 - self.beat_timer.as_f32() < target_beat.as_f32() {
self.update(vec2::ZERO, delta_time);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub struct Model {
}

impl Model {
pub fn new(config: Config, level: Level) -> Self {
pub fn new(config: Config, level: Level, start_time: Time) -> Self {
let player_radius = config.player.radius;
let mut model = Self {
config,
Expand Down Expand Up @@ -75,7 +75,7 @@ impl Model {
telegraphs: vec![],
lights: vec![],
};
model.init();
model.init(start_time);
model
}
}

0 comments on commit ec57a70

Please sign in to comment.