diff --git a/src/projectile.rs b/src/projectile.rs index c68fc94..295895f 100644 --- a/src/projectile.rs +++ b/src/projectile.rs @@ -3,23 +3,30 @@ use bevy::prelude::*; use crate::{ collision::{Collided, Collider}, damagable::Damagable, + ships::Factions, }; #[derive(Component)] pub struct Projectile { damage: u32, + faction: Factions, } impl Projectile { - pub fn new(damage: u32) -> Projectile { - Projectile { damage } + pub fn new(damage: u32, faction: Factions) -> Projectile { + Projectile { damage, faction } } } -pub fn spawn_projectile(commands: &mut Commands, sprite: Handle, damage: u32) { +pub fn spawn_projectile( + commands: &mut Commands, + sprite: Handle, + damage: u32, + faction: Factions, +) { commands .spawn(( - Projectile::new(damage), + Projectile::new(damage, faction), Sprite::from(sprite), Collider::new(8.), Transform::from_xyz(0., 0., 0.), @@ -30,14 +37,17 @@ pub fn observe_collision( collision: On, mut commands: Commands, projectile_query: Query<&Projectile>, - mut collision_query: Query<&mut Damagable>, + mut collision_query: Query<(&mut Damagable, &Factions)>, ) { let Ok(projectile) = projectile_query.get(collision.entity) else { return; }; - let Ok(mut collided) = collision_query.get_mut(collision.with) else { + let Ok((mut collided, faction)) = collision_query.get_mut(collision.with) else { return; }; + if projectile.faction.can_damage(faction) { + return; + } collided.damage(projectile.damage); commands.get_entity(collision.entity).unwrap().despawn(); diff --git a/src/ships/enemy.rs b/src/ships/enemy.rs index 141abf2..320a702 100644 --- a/src/ships/enemy.rs +++ b/src/ships/enemy.rs @@ -1,6 +1,6 @@ use bevy::prelude::*; -use crate::{collision::Collider, damagable::Damagable, movable::Movable}; +use crate::{collision::Collider, damagable::Damagable, movable::Movable, ships::Factions}; #[derive(Component)] pub struct Enemy; @@ -13,5 +13,6 @@ pub fn spawn_enemy(commands: &mut Commands, sprite: Handle, at: Vec2) { Sprite::from(sprite), Transform::from_xyz(at.x, at.y, 0.), Damagable::new(10), + Factions::EnemyFaction, )); } diff --git a/src/ships/mod.rs b/src/ships/mod.rs index a34077c..d54bfd7 100644 --- a/src/ships/mod.rs +++ b/src/ships/mod.rs @@ -5,6 +5,35 @@ use crate::projectile::spawn_projectile; pub mod enemy; pub mod player; +#[derive(Component)] +pub enum Factions { + PlayerFaction, + EnemyFaction, + NeutralFaction, +} + +impl Factions { + pub fn can_damage(&self, other: &Factions) -> bool { + match self { + Factions::PlayerFaction => match other { + Factions::PlayerFaction => false, + Factions::EnemyFaction => true, + Factions::NeutralFaction => false, + }, + Factions::EnemyFaction => match other { + Factions::PlayerFaction => true, + Factions::EnemyFaction => false, + Factions::NeutralFaction => false, + }, + Factions::NeutralFaction => match other { + Factions::EnemyFaction => false, + Factions::NeutralFaction => false, + Factions::PlayerFaction => false, + }, + } + } +} + pub struct ShipsPlugin; impl Plugin for ShipsPlugin { @@ -22,7 +51,7 @@ fn startup(mut commands: Commands, asset_server: Res) { commands.spawn(Camera2d); - player::spawn_player(&mut commands, player_sprite, Vec2::new(0., 0.)); - enemy::spawn_enemy(&mut commands, enemy_sprite.clone(), Vec2::new(100., 100.)); - spawn_projectile(&mut commands, projectile, 1); + player::spawn_player(&mut commands, player_sprite, Vec2::new(100., 100.)); + enemy::spawn_enemy(&mut commands, enemy_sprite.clone(), Vec2::new(0., 0.)); + spawn_projectile(&mut commands, projectile, 1, Factions::PlayerFaction); } diff --git a/src/ships/player.rs b/src/ships/player.rs index 89c5ab1..bc20a9e 100644 --- a/src/ships/player.rs +++ b/src/ships/player.rs @@ -1,4 +1,4 @@ -use crate::{collision::Collider, damagable::Damagable, movable::Movable}; +use crate::{collision::Collider, damagable::Damagable, movable::Movable, ships::Factions}; use bevy::prelude::*; #[derive(Component)] @@ -14,6 +14,7 @@ pub fn spawn_player(commands: &mut Commands, sprite: Handle, at: Vec2) { Sprite::from(sprite), Transform::from_xyz(at.x, at.y, 0.), Damagable::new(10), + Factions::PlayerFaction, )); }