From dc7e53c58db83ace15426ea015bfc6c9421347b1 Mon Sep 17 00:00:00 2001 From: Christian Nieves Date: Sun, 8 Dec 2024 02:34:26 -0600 Subject: [PATCH] refactor to plugin --- src/snake/main.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/snake/main.rs b/src/snake/main.rs index 95ec0bc..7749638 100644 --- a/src/snake/main.rs +++ b/src/snake/main.rs @@ -30,6 +30,9 @@ impl Size { } } +#[derive(Resource)] +struct MovementTimer(Timer); + fn snake_movement_system( input: Res>, @@ -100,8 +103,21 @@ fn setup_camera(mut commands: Commands) { commands.spawn((Camera2d {},)); } +pub struct SnakePlugin; +impl Plugin for SnakePlugin { + fn build(&self, app: &mut App) { + app.add_systems(Startup, (setup_camera, setup_player)); + app.add_systems(Update, snake_movement_system); + app.add_systems(PostUpdate, (scale_translation, position_translation)); + + app.insert_resource(MovementTimer(Timer::from_seconds( + 1.0, + TimerMode::Repeating, + ))); + } +} + fn main() { - let systems = snake_movement_system; App::new() .add_plugins( DefaultPlugins.set(WindowPlugin { @@ -118,8 +134,6 @@ fn main() { }), ) .add_plugins(WorldInspectorPlugin::new()) - .add_systems(Startup, (setup_camera, setup_player)) - .add_systems(Update, systems) - .add_systems(PostUpdate, (scale_translation, position_translation)) + .add_plugins(SnakePlugin) .run(); }