From a02f17eadeb8952bf2a844a3459a65159a37a003 Mon Sep 17 00:00:00 2001 From: Federico Borello <156438142+fborello-lambda@users.noreply.github.com> Date: Thu, 18 Jul 2024 21:57:29 -0300 Subject: [PATCH] refactor: encapsulate funcionalities (#5) * wip: create plugins * refactor: separate logic into files * fix: structure --- src/algae.rs | 56 ++++++++++++++++++++ src/components.rs | 16 ++++++ src/fish.rs | 82 ++++++++++++++++++++++++++++++ src/main.rs | 127 +++++----------------------------------------- src/resources.rs | 21 ++++++++ 5 files changed, 189 insertions(+), 113 deletions(-) create mode 100644 src/algae.rs create mode 100644 src/components.rs create mode 100644 src/fish.rs create mode 100644 src/resources.rs diff --git a/src/algae.rs b/src/algae.rs new file mode 100644 index 0000000..b4e6cdc --- /dev/null +++ b/src/algae.rs @@ -0,0 +1,56 @@ +use crate::components::*; +use crate::resources::*; +use bevy::prelude::*; +use rand::*; + +#[derive(Component)] +pub struct Algae; +#[derive(Bundle)] +pub struct AlgaeBundle { + pub(crate) position: Position, + pub(crate) sprite: SpriteBundle, + pub(crate) algae_component: Algae, +} + +pub struct AlgaePlugin; +impl Plugin for AlgaePlugin { + fn build(&self, app: &mut App) { + app.add_systems(Startup, algae_setup) + .add_systems(Update, algae_update); + } +} + +fn algae_setup(mut commands: Commands, asset_server: Res) { + for _ in 0..10 { + let start_pos = Vec2::new( + rand::thread_rng().gen_range(-300.0..300.0), + rand::thread_rng().gen_range(-300.0..300.0), + ); + commands.spawn(AlgaeBundle { + algae_component: Algae, + position: Position { v2: start_pos }, + sprite: SpriteBundle { + texture: asset_server.load("embedded://fish-forage/algae.png"), + transform: Transform::from_scale(Vec3::splat(1.)), + ..default() + }, + }); + } +} + +fn algae_update( + time: Res