69 lines
2 KiB
Rust
69 lines
2 KiB
Rust
use bevy::prelude::*;
|
|
use rand::{prelude::*, rng};
|
|
|
|
use crate::{
|
|
FIRST_CORNER_X, FIRST_CORNER_Y, GameState, SECOND_CORNER_X, SECOND_CORNER_Y,
|
|
ships::enemy::spawn_enemy,
|
|
};
|
|
|
|
pub mod enemy;
|
|
pub mod gun;
|
|
pub mod player;
|
|
|
|
#[derive(Component, Copy, Clone, PartialEq, Eq)]
|
|
pub enum Factions {
|
|
PlayerFaction,
|
|
EnemyFaction,
|
|
}
|
|
|
|
impl Factions {
|
|
pub fn can_damage(&self, other: &Factions) -> bool {
|
|
match self {
|
|
Factions::PlayerFaction => match other {
|
|
Factions::PlayerFaction => false,
|
|
Factions::EnemyFaction => true,
|
|
},
|
|
Factions::EnemyFaction => match other {
|
|
Factions::PlayerFaction => true,
|
|
Factions::EnemyFaction => false,
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct ShipsPlugin;
|
|
|
|
impl Plugin for ShipsPlugin {
|
|
fn build(&self, app: &mut bevy::app::App) {
|
|
app.insert_resource(Time::<Fixed>::from_hz(60.))
|
|
.add_systems(OnEnter(GameState::Game), startup)
|
|
.add_systems(
|
|
FixedUpdate,
|
|
(
|
|
player::player_movement_system.run_if(in_state(GameState::Game)),
|
|
player::player_shooting_system.run_if(in_state(GameState::Game)),
|
|
gun::gun_shooting_system.run_if(in_state(GameState::Game)),
|
|
enemy::enemy_ai_system.run_if(in_state(GameState::Game)),
|
|
),
|
|
)
|
|
.add_observer(player::player_death_observer);
|
|
}
|
|
}
|
|
|
|
fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
let player_sprite: Handle<Image> = asset_server.load("player.png");
|
|
let enemy_sprite: Handle<Image> = asset_server.load("enemy.png");
|
|
|
|
player::spawn_player(&mut commands, player_sprite, Vec2::new(0., 0.));
|
|
|
|
let mut random = rng();
|
|
|
|
for _ in 0..50 {
|
|
let position = Vec2::new(
|
|
random.random_range(FIRST_CORNER_X..SECOND_CORNER_X),
|
|
random.random_range(FIRST_CORNER_Y..SECOND_CORNER_Y),
|
|
);
|
|
|
|
spawn_enemy(&mut commands, enemy_sprite.clone(), position);
|
|
}
|
|
}
|