diff --git a/src/common.rs b/src/common.rs index 93cdb99..d2a9936 100644 --- a/src/common.rs +++ b/src/common.rs @@ -16,9 +16,3 @@ pub struct Team { pub id: String, pub color: Color, } - -// TODO: move this in player crate -#[derive(Component)] -pub struct Player { - pub gold: u32, -} diff --git a/src/main.rs b/src/main.rs index 97dfd89..c2eebfd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,39 +1,23 @@ mod common; mod health_bar; mod minions; +mod player; mod racks; use bevy::{ - input::gamepad::GamepadButtonChangedEvent, log::{Level, LogPlugin}, prelude::*, - sprite::{Sprite, SpriteBundle}, - time::Time, DefaultPlugins, }; use bevy_rapier2d::prelude::*; -use common::*; -use health_bar::{Health, HealthBarBundle, HealthBarPlugin}; +use health_bar::HealthBarPlugin; use minions::MinionsPlugin; -use racks::{RacksPlugin, RACK_GOLD_VALUE}; - -const JOYSTICK_SCALE: f32 = 200.; -const DEFAULT_HAND_COLOR: Color = Color::rgb(0.8, 0.25, 0.24); - -// TODO: Move local player into a plugin - -#[derive(Component)] -struct LocalPlayer; - -#[derive(Component)] -struct Hand; +use player::LocalPlayerPlugin; +use racks::RacksPlugin; #[derive(Component)] struct Camera; -#[derive(Component)] -struct GoldUI; - fn main() { let mut app = App::new(); @@ -46,18 +30,10 @@ fn main() { MinionsPlugin, RacksPlugin, HealthBarPlugin, - )); - - init_physics(&mut app); - - app.add_systems(Startup, (setup, setup_ui)) - .add_systems(Update, (update_axes, update_button_values, update_ui)); - - app.run(); -} - -fn init_physics(app: &mut App) { - app.add_plugins(( + LocalPlayerPlugin, + )) + // --- physics --- + .add_plugins(( RapierPhysicsPlugin::::pixels_per_meter(100.), // RapierDebugRenderPlugin::default(), )) @@ -65,160 +41,13 @@ fn init_physics(app: &mut App) { .insert_resource(RapierConfiguration { gravity: Vec2::new(0.0, 0.0), ..default() - }); -} - -fn setup_ui(mut commands: Commands, asset_server: Res) { - commands.spawn(( - TextBundle::from_section( - "", - TextStyle { - font: asset_server.load("fonts/FiraSans-Regular.ttf"), - font_size: 30.0, - color: Color::WHITE, - }, - ) - .with_style(Style { - margin: UiRect::all(Val::Px(5.)), - ..default() - }), - // Because this is a distinct label widget and - // not button/list item text, this is necessary - // for accessibility to treat the text accordingly. - Label, - GoldUI, - )); + }) + // --- systems --- + .add_systems(Startup, setup) + .run(); } fn setup(mut commands: Commands) { // Camera commands.spawn((Camera2dBundle::default(), Camera {})); - - // Local player - let entity = commands - .spawn(( - SpriteBundle { - sprite: Sprite { - color: Color::rgb(0.25, 0.25, 0.75), - custom_size: Some(Vec2::new(50.0, 50.0)), - ..default() - }, - transform: Transform::from_xyz(0.0, 0.0, 0.0), - ..default() - }, - // RigidBody::KinematicVelocityBased, - RigidBody::Dynamic, - Collider::cuboid(25.0, 25.), - ActiveEvents::COLLISION_EVENTS, - LocalPlayer {}, - Player { gold: 20 }, - Health::new(100.), - Name("local_player".to_string()), - Team { - id: "a".to_string(), - color: Color::rgb(0.3, 0.3, 0.8), - }, - )) - .with_children(|parent| { - parent.spawn(( - SpriteBundle { - sprite: Sprite { - color: DEFAULT_HAND_COLOR, - custom_size: Some(Vec2::new(10.0, 10.0)), - ..default() - }, - transform: Transform::from_xyz(0.0, 22.0, 0.1), - ..default() - }, - Hand {}, - )); - }) - .id(); - - commands.spawn(HealthBarBundle::new( - entity, - Vec3::new(0.0, 40.0, 0.1), - Vec2::new(50.0, 5.0), - )); -} - -fn update_axes( - time: Res