Skip to content

Commit

Permalink
♻️ teams as resource
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienjuif committed Nov 5, 2023
1 parent 7f48db4 commit 5a87f39
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 64 deletions.
6 changes: 0 additions & 6 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ pub struct Target {
#[derive(Component)]
pub struct Name(pub String);

#[derive(Component, Clone)]
pub struct Team {
pub id: String,
pub color: Color,
}

#[derive(Component)]
pub struct Rewards {
pub gold: f32,
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod health_bar;
mod minions;
mod player;
mod racks;
mod teams;

use bevy::{
log::{Level, LogPlugin},
Expand All @@ -14,6 +15,7 @@ use health_bar::HealthBarPlugin;
use minions::MinionsPlugin;
use player::LocalPlayerPlugin;
use racks::RacksPlugin;
use teams::TeamsPlugin;

#[derive(Component)]
struct Camera;
Expand All @@ -27,6 +29,7 @@ fn main() {
filter: "wgpu=error,bevy_render=warn,bevy_app=warn,bevy_ecs=warn,naga=warn,gilrs=warn"
.to_string(),
}),
TeamsPlugin,
MinionsPlugin,
RacksPlugin,
HealthBarPlugin,
Expand Down
1 change: 1 addition & 0 deletions src/minions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
common::*,
health_bar::{Health, HealthBarBundle},
teams::Team,
};
use bevy::{
prelude::*,
Expand Down
16 changes: 7 additions & 9 deletions src/player.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::common::*;
use crate::health_bar::{Health, HealthBarBundle};
use crate::racks::RACK_GOLD_VALUE;
use crate::racks::{RackBundle, RACK_GOLD_VALUE};
use crate::teams::{Team, Teams};
use bevy::{
input::gamepad::GamepadButtonChangedEvent,
prelude::*,
Expand Down Expand Up @@ -85,7 +86,7 @@ impl Plugin for LocalPlayerPlugin {
}
}

fn setup(mut commands: Commands) {
fn setup(mut commands: Commands, teams: Res<Teams>) {
let mut sword_cooldown = Timer::from_seconds(0.3, TimerMode::Once);
sword_cooldown.set_elapsed(sword_cooldown.duration());

Expand All @@ -100,8 +101,8 @@ fn setup(mut commands: Commands) {
transform: Transform::from_xyz(0.0, 0.0, 0.0),
..default()
},
// RigidBody::KinematicVelocityBased,
RigidBody::Dynamic,
RigidBody::KinematicVelocityBased,
// RigidBody::Dynamic,
Collider::cuboid(25.0, 25.),
ActiveEvents::COLLISION_EVENTS,
LocalPlayer {},
Expand All @@ -113,10 +114,7 @@ fn setup(mut commands: Commands) {
},
Health::new(100.),
Name("local_player".to_string()),
Team {
id: "a".to_string(),
color: Color::rgb(0.3, 0.3, 0.8),
},
teams.get_expect("a".into()),
))
.with_children(|parent| {
parent.spawn((
Expand Down Expand Up @@ -235,7 +233,7 @@ fn update_button_values(
&& button_event.value != 0.
&& player.gold >= RACK_GOLD_VALUE
{
crate::racks::spawn_rack(&mut commands, *transform, team.clone());
commands.spawn(RackBundle::new(team.clone(), *transform));
player.gold -= RACK_GOLD_VALUE;
}
}
Expand Down
91 changes: 42 additions & 49 deletions src/racks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy::{
time::{Time, Timer, TimerMode},
};

use crate::common::Team;
use crate::teams::{Team, Teams};

pub const RACK_GOLD_VALUE: f32 = 10.;

Expand All @@ -17,6 +17,37 @@ pub struct Rack {
minion_spawning: bool,
}

#[derive(Bundle)]
pub struct RackBundle {
pub sprite_bundle: SpriteBundle,
pub team: Team,
pub rack: Rack,
}

impl RackBundle {
pub fn new(team: Team, transform: Transform) -> Self {
RackBundle {
sprite_bundle: SpriteBundle {
sprite: Sprite {
color: team.color,
custom_size: Some(Vec2::new(20.0, 20.0)),
..default()
},
transform,
..default()
},
team,
rack: Rack {
minion_spawning: false,
minion_spawned_count: 0,
minion_spawn_count: 10,
minion_spawn_timer: Timer::from_seconds(3., TimerMode::Repeating),
minion_spawn_timer_q: Timer::from_seconds(0.2, TimerMode::Repeating),
},
}
}
}

pub struct RacksPlugin;

impl Plugin for RacksPlugin {
Expand All @@ -26,31 +57,19 @@ impl Plugin for RacksPlugin {
}
}

fn setup_rack(mut commands: Commands) {
spawn_rack(
&mut commands,
fn setup_rack(mut commands: Commands, teams: Res<Teams>) {
commands.spawn(RackBundle::new(
teams.get_expect("b".into()),
Transform::from_xyz(200.0, 200.0, 0.),
Team {
id: "b".to_string(),
color: Color::rgb(0.8, 0.3, 0.3),
},
);
spawn_rack(
&mut commands,
));
commands.spawn(RackBundle::new(
teams.get_expect("b".into()),
Transform::from_xyz(250.0, 150.0, 0.),
Team {
id: "b".to_string(),
color: Color::rgb(0.8, 0.3, 0.3),
},
);
spawn_rack(
&mut commands,
));
commands.spawn(RackBundle::new(
teams.get_expect("c".into()),
Transform::from_xyz(100.0, -100.0, 0.),
Team {
id: "c".to_string(),
color: Color::rgb(0.8, 0.8, 0.3),
},
);
));
}

fn spawn_minions(
Expand Down Expand Up @@ -86,29 +105,3 @@ fn spawn_minions(
}
}
}

pub fn spawn_rack(commands: &mut Commands, transform: Transform, team: Team) {
let id = commands
.spawn((
SpriteBundle {
sprite: Sprite {
color: team.color,
custom_size: Some(Vec2::new(20.0, 20.0)),
..default()
},
transform,
..default()
},
team,
Rack {
minion_spawning: false,
minion_spawned_count: 0,
minion_spawn_count: 10,
minion_spawn_timer: Timer::from_seconds(3., TimerMode::Repeating),
minion_spawn_timer_q: Timer::from_seconds(0.2, TimerMode::Repeating),
},
))
.id();

trace!("Spawning Rack: {:?}", id);
}
55 changes: 55 additions & 0 deletions src/teams.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use bevy::{prelude::*, utils::HashMap};

#[derive(Resource)]
pub struct Teams {
teams: HashMap<String, Team>,
}

impl Teams {
pub fn new() -> Self {
let mut teams = Teams {
teams: HashMap::new(),
};

teams.add(Team {
id: "a".to_string(),
color: Color::rgb(0.3, 0.3, 0.8),
});
teams.add(Team {
id: "b".to_string(),
color: Color::rgb(0.8, 0.3, 0.3),
});
teams.add(Team {
id: "c".to_string(),
color: Color::rgb(0.8, 0.8, 0.3),
});
teams
}

pub fn get(&self, id: String) -> Option<&Team> {
self.teams.get(&id)
}

pub fn get_expect(&self, id: String) -> Team {
self.get(id).expect("no team found").clone()
}

pub fn add(&mut self, team: Team) -> &mut Self {
self.teams.insert(team.id.clone(), team);
self
}
}

#[derive(Component, Clone)]
pub struct Team {
pub id: String,
pub color: Color,
}

pub struct TeamsPlugin;

impl Plugin for TeamsPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(Teams::new());
}
}

0 comments on commit 5a87f39

Please sign in to comment.