Skip to content

Commit

Permalink
Merge pull request #59 from sinproject-iwasaki/58-make-blocks-fall-us…
Browse files Browse the repository at this point in the history
…ing-game-timer

Make blocks fall using GameTimer #58
  • Loading branch information
sinproject-iwasaki authored Mar 10, 2024
2 parents d3a5eb7 + 8eb7a19 commit 8575925
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 75 deletions.
43 changes: 21 additions & 22 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,38 @@ use bevy::prelude::*;

use crate::{block_pattern::BlockPatterns, sprite};

#[derive(Resource)]
pub struct EventTriggerState {
event_timer: Timer,
}
// #[derive(Resource)]
// pub struct EventTriggerState {
// event_timer: Timer,
// }

impl Default for EventTriggerState {
fn default() -> Self {
Self {
event_timer: Timer::from_seconds(1.0, TimerMode::Repeating),
}
}
}
// impl Default for EventTriggerState {
// fn default() -> Self {
// Self {
// event_timer: Timer::from_seconds(1.0, TimerMode::Repeating),
// }
// }
// }

#[derive(Event, Default)]
pub struct NewBlockEvent;

pub fn event_trigger(
time: Res<Time>,
mut state: ResMut<EventTriggerState>,
mut new_block_events: EventWriter<NewBlockEvent>,
) {
if state.event_timer.tick(time.delta()).finished() {
new_block_events.send_default();
}
}
// pub fn event_trigger(
// time: Res<Time>,
// mut state: ResMut<EventTriggerState>,
// mut new_block_events: EventWriter<NewBlockEvent>,
// ) {
// if state.event_timer.tick(time.delta()).finished() {
// new_block_events.send_default();
// }
// }

pub fn event_listener(
pub fn new_bock_event_listener(
mut commands: Commands,
mut new_block_events: EventReader<NewBlockEvent>,
mut block_patterns: Res<BlockPatterns>,
) {
for _ in new_block_events.read() {
println!("New block event received!");
sprite::spawn_block(&mut commands, &mut block_patterns)
}
}
30 changes: 30 additions & 0 deletions src/game_timer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use bevy::prelude::*;

#[derive(Resource)]
pub struct GameTimer {
timer: Timer,
}

impl GameTimer {
pub fn new() -> Self {
Self {
timer: Timer::from_seconds(1.0, TimerMode::Repeating),
}
}

pub fn game_timer(time: Res<Time>, mut game_timer: ResMut<Self>) {
if game_timer.timer.tick(time.delta()).finished() {
println!("Game timer ticked!");
}
}

pub fn timer(&self) -> &Timer {
&self.timer
}
}

impl Default for GameTimer {
fn default() -> Self {
Self::new()
}
}
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod color_resources;
mod config;
mod constants;
mod events;
mod game_timer;
mod input;
pub mod my_app;
mod position;
Expand All @@ -17,7 +18,9 @@ mod utils;
mod validator;
mod window;

use my_app::MyApp;

#[cfg_attr(coverage_nightly, coverage(off))]
pub fn run() {
my_app::create_app().run();
MyApp::new().run();
}
117 changes: 78 additions & 39 deletions src/my_app.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,93 @@
use bevy::app::PluginGroupBuilder;
use bevy::prelude::*;

use crate::block_pattern;
use crate::camera;
use crate::events;
use crate::events::NewBlockEvent;
use crate::game_timer::GameTimer;
use crate::input;
use crate::position::Position;
use crate::sprite;
use crate::text;
use crate::window;

fn init_window_plugin() -> WindowPlugin {
let window = window::init_window();
pub struct MyApp {
app: App,
}

impl MyApp {
fn init_window_plugin() -> WindowPlugin {
let window = window::init_window();

WindowPlugin {
primary_window: Some(window),
..default()
WindowPlugin {
primary_window: Some(window),
..default()
}
}
}

pub fn setup(app: &mut App) {
// let color_resources = ColorResources::generate_example();
let block_patterns = block_pattern::BlockPatterns::new();

app.insert_resource(block_patterns)
.add_event::<events::NewBlockEvent>()
.init_resource::<events::EventTriggerState>()
.add_systems(Startup, camera::spawn_camera)
.add_systems(Startup, text::spawn_initial_text)
// .insert_resource(color_resources)
// .add_systems(Startup, setup::setup)
.add_systems(Update, (events::event_trigger, events::event_listener))
.add_systems(Update, input::keyboard_input_system)
.add_systems(Update, input::check_esc_to_exit)
.add_systems(Update, sprite::position_transform);
// .add_systems(Update, sprite::change_color)
// .add_systems(Update, sprite::spawn_block);
}
fn get_plugins() -> PluginGroupBuilder {
DefaultPlugins.set(Self::init_window_plugin())
}

pub fn setup(mut new_block_events: EventWriter<events::NewBlockEvent>) {
new_block_events.send_default();
}

fn init(app: &mut App) {
// let color_resources = ColorResources::generate_example();
let block_patterns = block_pattern::BlockPatterns::new();

app.insert_resource(block_patterns)
.add_event::<NewBlockEvent>()
.init_resource::<GameTimer>()
// .init_resource::<events::EventTriggerState>()
.add_systems(Startup, camera::spawn_camera)
.add_systems(Startup, text::spawn_initial_text)
.add_systems(Startup, Self::setup)
// .insert_resource(color_resources)
// .add_systems(Startup, setup::setup)
// .add_systems(Update, (events::event_trigger, events::event_listener))
.add_systems(Update, GameTimer::game_timer)
.add_systems(Update, events::new_bock_event_listener)
.add_systems(Update, input::keyboard_input_system)
.add_systems(Update, input::check_esc_to_exit)
.add_systems(Update, sprite::position_transform)
.add_systems(Update, Position::block_fall);
// .add_systems(Update, sprite::change_color)
// .add_systems(Update, sprite::spawn_block);
}

#[cfg_attr(coverage_nightly, coverage(off))]
pub fn create_app() -> App {
let mut app = App::new();
let plugin = DefaultPlugins.set(init_window_plugin());
#[cfg_attr(coverage_nightly, coverage(off))]
pub fn new() -> Self {
let mut app = App::new();
let plugin = Self::get_plugins();

app.add_plugins(plugin);
setup(&mut app);
app.add_plugins(plugin);
Self::init(&mut app);

app
Self { app }
}

pub fn new_for_debug() -> Self {
let mut app = App::new();
let plugins = MinimalPlugins;
// let plugins = Self::get_plugins();
// let plugins = DefaultPlugins.build().disable::<WinitPlugin>();

app.add_plugins(plugins);
Self::init(&mut app);

Self { app }
}

pub fn app(&self) -> &App {
&self.app
}

pub fn run(&mut self) {
self.app.run();
}
}

#[cfg(test)]
Expand All @@ -55,19 +98,15 @@ mod tests {

#[test]
fn test_init_window_plugin() {
init_window_plugin();
MyApp::init_window_plugin();
}

#[test]
fn test_create_app() {
let mut app = App::new();
// let plugin = MinimalPlugins.set(init_window_plugin());
// plugin.build().disable::<WinitPlugin>()

app.add_plugins(MinimalPlugins);
setup(&mut app);
let my_app = MyApp::new_for_debug();

assert!(app
assert!(my_app
.app
.world
.contains_resource::<block_pattern::BlockPatterns>());
}
Expand Down
13 changes: 12 additions & 1 deletion src/position.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::prelude::*;

use crate::utils;
use crate::{game_timer::GameTimer, utils};

#[derive(Component)]
pub struct Position {
Expand All @@ -23,7 +23,18 @@ impl Position {

Vec3::new(translation_vec2.x, translation_vec2.y, 0.0)
}

pub fn block_fall(game_timer: ResMut<GameTimer>, mut position_query: Query<&mut Position>) {
if !game_timer.timer().finished() {
return;
}

for mut position in position_query.iter_mut() {
position.y -= 1;
}
}
}

#[cfg(test)]
mod tests {
use crate::utils::unit_size;
Expand Down
17 changes: 5 additions & 12 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
use bevy::prelude::*;
// use bevy::prelude::*;
use my_bevy_game::*;

fn init_game_app() -> App {
let mut app = App::new();

app.add_plugins(MinimalPlugins);
my_app::setup(&mut app);
// app.update();

app
}
use my_bevy_game::my_app::MyApp;

#[test]
fn test_app_creation() {
let app = init_game_app();
let my_app = MyApp::new_for_debug();

let block_patterns = app
let block_patterns = my_app
.app()
.world
.get_resource::<block_pattern::BlockPatterns>()
.unwrap();
Expand Down

0 comments on commit 8575925

Please sign in to comment.