slight reorg
This commit is contained in:
parent
92c8c288bc
commit
a703f6ab85
5 changed files with 139 additions and 105 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -1312,6 +1312,7 @@ dependencies = [
|
|||
"bevy",
|
||||
"enumset",
|
||||
"rand",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
|||
|
|
@ -7,3 +7,4 @@ edition = "2024"
|
|||
bevy = "0.17.3"
|
||||
enumset = "1.1.10"
|
||||
rand = "0.9.2"
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
|
|
|
|||
1
src/gameplay/mod.rs
Normal file
1
src/gameplay/mod.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
pub mod navi;
|
||||
120
src/gameplay/navi.rs
Normal file
120
src/gameplay/navi.rs
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
use bevy::{post_process::bloom::Bloom, prelude::*, sprite::Anchor};
|
||||
use rand::Rng;
|
||||
|
||||
use crate::{
|
||||
GameState, PauseState,
|
||||
utils::input::{GameInputPlugin, InputButton, InputPressStatus},
|
||||
};
|
||||
|
||||
const PLAYER_SPEED: f32 = 100.;
|
||||
|
||||
pub struct NaviGameplayPlugin;
|
||||
|
||||
impl Plugin for NaviGameplayPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, (setup_scene, spawn_bus, spawn_enemy))
|
||||
.add_systems(
|
||||
Update,
|
||||
(
|
||||
move_player
|
||||
.run_if(in_state(GameState::Navi).and(in_state(PauseState::Running))),
|
||||
follow_player,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
#[derive(Component)]
|
||||
pub struct PlayerVWBus;
|
||||
|
||||
pub fn setup_scene(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||
) {
|
||||
commands.spawn((
|
||||
Mesh2d(meshes.add(Rectangle::new(1000., 70.))),
|
||||
MeshMaterial2d(materials.add(Color::srgb(0.2, 0.2, 0.3))),
|
||||
));
|
||||
|
||||
commands.spawn((Camera2d, Bloom::NATURAL));
|
||||
}
|
||||
|
||||
pub fn spawn_bus(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
let texture = asset_server.load("bus/basicbus.png");
|
||||
commands.spawn((
|
||||
PlayerVWBus,
|
||||
Sprite {
|
||||
image: texture,
|
||||
custom_size: Some(Vec2::new(192., 133.)),
|
||||
..default()
|
||||
},
|
||||
Transform::from_xyz(2., 0., 3.),
|
||||
Anchor::CENTER,
|
||||
));
|
||||
}
|
||||
|
||||
pub fn spawn_enemy(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||
) {
|
||||
let mut rng = rand::rng();
|
||||
for _ in 0..10 {
|
||||
commands.spawn((
|
||||
Mesh2d(meshes.add(Circle::new(10.))),
|
||||
MeshMaterial2d(materials.add(Color::srgb(1.1, 0.2, 0.3))),
|
||||
Transform::from_xyz(
|
||||
rng.random_range(-600..600) as f32,
|
||||
rng.random_range(-600..600) as f32,
|
||||
1.,
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn move_player(
|
||||
mut player: Single<&mut Transform, With<PlayerVWBus>>,
|
||||
mut player_sprite: Single<&mut Sprite, With<PlayerVWBus>>,
|
||||
time: Res<Time>,
|
||||
buttons_pressed: Res<InputPressStatus>,
|
||||
) {
|
||||
let mut direction = Vec2::ZERO;
|
||||
|
||||
if buttons_pressed.contains(InputButton::Up) {
|
||||
direction.y += 1.;
|
||||
}
|
||||
|
||||
if buttons_pressed.contains(InputButton::Down) {
|
||||
direction.y -= 1.;
|
||||
}
|
||||
|
||||
if buttons_pressed.contains(InputButton::Left) {
|
||||
direction.x -= 1.;
|
||||
}
|
||||
|
||||
if buttons_pressed.contains(InputButton::Right) {
|
||||
direction.x += 1.;
|
||||
}
|
||||
|
||||
let move_delta = direction.normalize_or_zero() * PLAYER_SPEED * time.delta_secs();
|
||||
player.translation += move_delta.extend(0.);
|
||||
|
||||
if direction.x > 0. {
|
||||
player_sprite.flip_x = true;
|
||||
} else if direction.x < 0. {
|
||||
player_sprite.flip_x = false;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn follow_player(
|
||||
mut camera: Single<&mut Transform, (With<Camera2d>, Without<PlayerVWBus>)>,
|
||||
player: Single<&Transform, (With<PlayerVWBus>, Without<Camera2d>)>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let Vec3 { x, y, .. } = player.translation;
|
||||
let direction = Vec3::new(x, y, camera.translation.z);
|
||||
|
||||
camera
|
||||
.translation
|
||||
.smooth_nudge(&direction, 10., time.delta_secs());
|
||||
}
|
||||
121
src/main.rs
121
src/main.rs
|
|
@ -1,125 +1,36 @@
|
|||
use bevy::{post_process::bloom::Bloom, prelude::*, sprite::Anchor};
|
||||
use rand::Rng;
|
||||
|
||||
use crate::utils::input::{GameInputPlugin, InputButton, InputPressStatus};
|
||||
use crate::{
|
||||
gameplay::navi::NaviGameplayPlugin,
|
||||
utils::input::{GameInputPlugin, InputButton, InputPressStatus},
|
||||
};
|
||||
|
||||
mod gameplay;
|
||||
mod utils;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct PlayerVWBus;
|
||||
|
||||
#[derive(States, Default, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum GameState {
|
||||
#[default]
|
||||
Navi,
|
||||
Pause,
|
||||
Combat,
|
||||
MainMenu,
|
||||
}
|
||||
|
||||
const PLAYER_SPEED: f32 = 100.;
|
||||
#[derive(States, Default, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum PauseState {
|
||||
#[default]
|
||||
Running,
|
||||
Paused,
|
||||
AnimatingIntoPause,
|
||||
AnimatingOutOfPause,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
App::new()
|
||||
.add_plugins((DefaultPlugins, GameInputPlugin))
|
||||
.add_plugins((DefaultPlugins, GameInputPlugin, NaviGameplayPlugin))
|
||||
.init_state::<GameState>()
|
||||
.add_systems(Startup, (setup_scene, spawn_bus, spawn_enemy))
|
||||
.add_systems(
|
||||
Update,
|
||||
(move_player.run_if(in_state(GameState::Navi)), follow_player),
|
||||
)
|
||||
.init_state::<PauseState>()
|
||||
.run();
|
||||
}
|
||||
|
||||
fn setup_scene(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||
) {
|
||||
commands.spawn((
|
||||
Mesh2d(meshes.add(Rectangle::new(1000., 70.))),
|
||||
MeshMaterial2d(materials.add(Color::srgb(0.2, 0.2, 0.3))),
|
||||
));
|
||||
|
||||
commands.spawn((Camera2d, Bloom::NATURAL));
|
||||
}
|
||||
|
||||
fn spawn_bus(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
let texture = asset_server.load("bus/basicbus.png");
|
||||
commands.spawn((
|
||||
PlayerVWBus,
|
||||
Sprite {
|
||||
image: texture,
|
||||
custom_size: Some(Vec2::new(192., 133.)),
|
||||
..default()
|
||||
},
|
||||
Transform::from_xyz(2., 0., 3.),
|
||||
Anchor::CENTER,
|
||||
));
|
||||
}
|
||||
|
||||
fn spawn_enemy(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||
) {
|
||||
let mut rng = rand::rng();
|
||||
for _ in 0..10 {
|
||||
commands.spawn((
|
||||
Mesh2d(meshes.add(Circle::new(10.))),
|
||||
MeshMaterial2d(materials.add(Color::srgb(1.1, 0.2, 0.3))),
|
||||
Transform::from_xyz(
|
||||
rng.random_range(-600..600) as f32,
|
||||
rng.random_range(-600..600) as f32,
|
||||
1.,
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
fn move_player(
|
||||
mut player: Single<&mut Transform, With<PlayerVWBus>>,
|
||||
mut player_sprite: Single<&mut Sprite, With<PlayerVWBus>>,
|
||||
time: Res<Time>,
|
||||
buttons_pressed: Res<InputPressStatus>,
|
||||
) {
|
||||
let mut direction = Vec2::ZERO;
|
||||
|
||||
if buttons_pressed.contains(InputButton::Up) {
|
||||
direction.y += 1.;
|
||||
}
|
||||
|
||||
if buttons_pressed.contains(InputButton::Down) {
|
||||
direction.y -= 1.;
|
||||
}
|
||||
|
||||
if buttons_pressed.contains(InputButton::Left) {
|
||||
direction.x -= 1.;
|
||||
}
|
||||
|
||||
if buttons_pressed.contains(InputButton::Right) {
|
||||
direction.x += 1.;
|
||||
}
|
||||
|
||||
let move_delta = direction.normalize_or_zero() * PLAYER_SPEED * time.delta_secs();
|
||||
player.translation += move_delta.extend(0.);
|
||||
|
||||
if direction.x > 0. {
|
||||
player_sprite.flip_x = true;
|
||||
} else if direction.x < 0. {
|
||||
player_sprite.flip_x = false;
|
||||
}
|
||||
}
|
||||
|
||||
fn follow_player(
|
||||
mut camera: Single<&mut Transform, (With<Camera2d>, Without<PlayerVWBus>)>,
|
||||
player: Single<&Transform, (With<PlayerVWBus>, Without<Camera2d>)>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let Vec3 { x, y, .. } = player.translation;
|
||||
let direction = Vec3::new(x, y, camera.translation.z);
|
||||
|
||||
camera
|
||||
.translation
|
||||
.smooth_nudge(&direction, 10., time.delta_secs());
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue