Compare commits
2 commits
4bf62bfe39
...
385f1ba20f
| Author | SHA1 | Date | |
|---|---|---|---|
| 385f1ba20f | |||
| 526caf91c7 |
4 changed files with 52 additions and 11 deletions
|
|
@ -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<Image>, damage: u32) {
|
||||
pub fn spawn_projectile(
|
||||
commands: &mut Commands,
|
||||
sprite: Handle<Image>,
|
||||
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<Collided>,
|
||||
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();
|
||||
|
|
|
|||
|
|
@ -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<Image>, at: Vec2) {
|
|||
Sprite::from(sprite),
|
||||
Transform::from_xyz(at.x, at.y, 0.),
|
||||
Damagable::new(10),
|
||||
Factions::EnemyFaction,
|
||||
));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<AssetServer>) {
|
|||
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<Image>, at: Vec2) {
|
|||
Sprite::from(sprite),
|
||||
Transform::from_xyz(at.x, at.y, 0.),
|
||||
Damagable::new(10),
|
||||
Factions::PlayerFaction,
|
||||
));
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue