Skip to content

Commit

Permalink
♻️ round minion and player + balance
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienjuif committed Nov 10, 2023
1 parent 06f5005 commit afaa0a1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/castles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl CastleBundle {
minion_spawn_timer: Timer::from_seconds(3., TimerMode::Repeating),
minion_spawn_timer_q: Timer::from_seconds(0.2, TimerMode::Repeating),
},
health: Health::new(300.)
health: Health::new(1000.)
.with_health_bar_position(Vec3::new(0.0, 50.0, 0.0))
.with_health_bar_size(Vec2::new(size.x, 5.)),
rewards: Rewards { gold: 500. },
Expand Down
44 changes: 29 additions & 15 deletions src/minions.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use crate::{common::*, health::Health, teams::Team};
use bevy::{
prelude::*,
sprite::{Sprite, SpriteBundle},
sprite::MaterialMesh2dBundle,
time::{Time, Timer},
utils::{default, HashMap},
};
use bevy_rapier2d::prelude::*;

const MINION_SCALE: f32 = 100.;
const MINION_SCALE: f32 = 190.;
const DESTROY_MINIONS_AFTER_SECS: f32 = 120.;
const DECAY_VALUE_PER_SEC: f32 = 10.;
const REWARDS_GOLD: f32 = 1.;
const MINION_TOUCH_DAMAGE: f32 = 80.;

pub struct MinionsPlugin;

Expand All @@ -32,7 +33,8 @@ impl Plugin for MinionsPlugin {
#[derive(Bundle)]
pub struct MinionBundle {
minion: Minion,
sprite: SpriteBundle,
mesh: MaterialMesh2dBundle<ColorMaterial>,
// sprite: SpriteBundle,
health: Health,
rewards: Rewards,
team: Team,
Expand All @@ -44,17 +46,29 @@ pub struct MinionBundle {
}

impl MinionBundle {
pub fn new(translation: Vec3, team: Team) -> Self {
pub fn new(
meshes: &mut ResMut<Assets<Mesh>>,
materials: &mut ResMut<Assets<ColorMaterial>>,
translation: Vec3,
team: Team,
) -> Self {
MinionBundle {
sprite: SpriteBundle {
sprite: Sprite {
color: team.color,
custom_size: Some(Vec2::new(10.0, 10.0)),
..default()
},
mesh: MaterialMesh2dBundle {
mesh: meshes.add(shape::Circle::new(6.).into()).into(),
material: materials.add(ColorMaterial::from(team.color)),
transform: Transform::from_translation(translation),
..default()
},
// TODO: add sprite sheet later
// sprite: SpriteBundle {
// sprite: Sprite {
// color: team.color,
// custom_size: Some(Vec2::new(10.0, 10.0)),
// ..default()
// },
// transform: Transform::from_translation(translation),
// ..default()
// },
minion: Minion {
// to avoid leaks
// maybe a better option on top of that is to leach health every seconds on minions and make them die!
Expand All @@ -70,7 +84,7 @@ impl MinionBundle {
team,
// physics
body: RigidBody::Dynamic,
collider: Collider::cuboid(5.0, 5.),
collider: Collider::ball(6.),
events: ActiveEvents::COLLISION_EVENTS,
}
}
Expand Down Expand Up @@ -187,8 +201,8 @@ fn check_collisions_minions(
}

// hurt each other (TODO: maybe they are hurting each other twice if the couple is twice in the events (a, b) and (b, a))
health_a.hit(1.);
health_b.hit(1.);
health_a.hit(MINION_TOUCH_DAMAGE);
health_b.hit(MINION_TOUCH_DAMAGE);

continue;
}
Expand All @@ -204,14 +218,14 @@ fn check_collisions_minions(
continue;
}

health.hit(1.);
health.hit(MINION_TOUCH_DAMAGE);
} else if let Ok((team, mut health)) = query_hit_entities.get_mut(*e2) {
// if they are from the same team, do nothing special
if minion_team.id == team.id {
continue;
}

health.hit(1.);
health.hit(MINION_TOUCH_DAMAGE);
}
}
}
Expand Down
35 changes: 25 additions & 10 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::common::*;
use crate::health::Health;
use crate::racks::{RackBundle, RACK_GOLD_VALUE};
use crate::teams::{Team, Teams};
use bevy::sprite::MaterialMesh2dBundle;
use bevy::{
input::gamepad::GamepadButtonChangedEvent,
prelude::*,
Expand Down Expand Up @@ -86,24 +87,38 @@ impl Plugin for LocalPlayerPlugin {
}
}

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

let team = teams.get_expect("a".into());

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),
MaterialMesh2dBundle {
mesh: meshes.add(shape::Circle::new(30.).into()).into(),
material: materials.add(ColorMaterial::from(team.color)),
transform: Transform::from_translation(Vec3::new(-150., 0., 0.)),
..default()
},
// TODO:: add sprite sheet later
// 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.),
Collider::ball(30.),
ActiveEvents::COLLISION_EVENTS,
LocalPlayer {},
Player {
Expand All @@ -116,7 +131,7 @@ fn setup(mut commands: Commands, teams: Res<Teams>) {
.with_health_bar_position(Vec3::new(0.0, 40.0, 0.1))
.with_health_bar_size(Vec2::new(50.0, 5.0)),
Name("local_player".to_string()),
teams.get_expect("a".into()),
team,
))
.with_children(|parent| {
parent.spawn((
Expand Down
6 changes: 5 additions & 1 deletion src/racks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl RackBundle {
minion_spawn_timer: Timer::from_seconds(3., TimerMode::Repeating),
minion_spawn_timer_q: Timer::from_seconds(0.2, TimerMode::Repeating),
},
health: Health::new(80.)
health: Health::new(220.)
.with_health_bar_position(Vec3::new(0.0, 20.0, 0.0))
.with_health_bar_size(Vec2::new(size.x, 5.)),
rewards: Rewards { gold: 100. },
Expand Down Expand Up @@ -100,6 +100,8 @@ fn spawn_minions(
mut commands: Commands,
time: Res<Time>,
mut query: Query<(&mut Rack, &Collider, &Transform, &Team)>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let mut rng: rand::rngs::ThreadRng = rand::thread_rng();

Expand Down Expand Up @@ -134,6 +136,8 @@ fn spawn_minions(
}

commands.spawn(MinionBundle::new(
&mut meshes,
&mut materials,
Vec3::new(
transform.translation.x + offset_x,
transform.translation.y + offset_y,
Expand Down

0 comments on commit afaa0a1

Please sign in to comment.