Menus, hurray!
This commit is contained in:
parent
9d5b554f7e
commit
8c26fa7eb8
7 changed files with 148 additions and 126 deletions
|
@ -23,16 +23,7 @@ pub struct Fonts {
|
|||
pub iosevkalytemin: Handle<Font>,
|
||||
}
|
||||
|
||||
pub struct AssetPlugin;
|
||||
|
||||
impl Plugin for AssetPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.init_resource::<AssetLoader>()
|
||||
.add_systems(Startup, load_assets);
|
||||
}
|
||||
}
|
||||
|
||||
fn load_assets(
|
||||
pub fn startup(
|
||||
mut assets: ResMut<AssetLoader>,
|
||||
asset_server: Res<AssetServer>,
|
||||
mut texture_atlases: ResMut<Assets<TextureAtlasLayout>>,
|
||||
|
|
|
@ -1,29 +1,24 @@
|
|||
use bevy::prelude::*;
|
||||
use bevy::render::camera::Camera as BevyCamera;
|
||||
use bevy::render::camera::Camera;
|
||||
|
||||
pub struct CameraPlugin;
|
||||
|
||||
#[derive(Component, Debug)]
|
||||
pub struct CameraTarget;
|
||||
|
||||
impl Plugin for CameraPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, spawn)
|
||||
.add_systems(PostUpdate, follow);
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn(mut commands: Commands) {
|
||||
pub fn startup(mut commands: Commands) {
|
||||
let mut bundle = (Camera2dBundle::default(), IsDefaultUiCamera);
|
||||
bundle.0.projection.scale = 0.5;
|
||||
commands.spawn(bundle);
|
||||
}
|
||||
|
||||
fn follow(
|
||||
player: Query<&Transform, With<CameraTarget>>,
|
||||
mut camera: Query<&mut Transform, (With<BevyCamera>, Without<CameraTarget>)>,
|
||||
#[derive(Component, Debug)]
|
||||
pub struct Watched;
|
||||
|
||||
pub fn update(
|
||||
watched: Query<&Transform, With<Watched>>,
|
||||
mut camera: Query<&mut Transform, (With<Camera>, Without<Watched>)>,
|
||||
) {
|
||||
if let Ok(player) = player.get_single() {
|
||||
camera.single_mut().translation = player.translation
|
||||
if let Ok(mut camera) = camera.get_single_mut() {
|
||||
if let Ok(watched) = watched.get_single() {
|
||||
camera.translation.x = watched.translation.x
|
||||
} else {
|
||||
camera.translation = Vec3::ZERO;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
114
src/main.rs
114
src/main.rs
|
@ -1,5 +1,5 @@
|
|||
use bevy::audio::{AudioPlugin, SpatialScale};
|
||||
use bevy::prelude::*;
|
||||
use bevy::prelude::{default, *};
|
||||
|
||||
mod assets;
|
||||
mod camera;
|
||||
|
@ -8,50 +8,82 @@ mod movement;
|
|||
mod player;
|
||||
mod statue;
|
||||
|
||||
#[derive(States, Default, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum Game {
|
||||
#[default]
|
||||
Running,
|
||||
Paused,
|
||||
}
|
||||
|
||||
#[derive(States, Default, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum View {
|
||||
// #[default]
|
||||
// LoadingScreen,
|
||||
#[default]
|
||||
MainMenu,
|
||||
InGame,
|
||||
}
|
||||
|
||||
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
struct MainMenuSet;
|
||||
|
||||
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
struct InGameSet;
|
||||
|
||||
fn main() {
|
||||
let mut app = App::new();
|
||||
|
||||
app.add_plugins(
|
||||
DefaultPlugins
|
||||
.set(WindowPlugin {
|
||||
primary_window: Some(Window {
|
||||
title: "Kodo Tag".into(),
|
||||
mode: bevy::window::WindowMode::BorderlessFullscreen,
|
||||
// resolution: (640. * 2., 360. * 2.).into(),
|
||||
..default()
|
||||
}),
|
||||
app.init_resource::<assets::AssetLoader>();
|
||||
|
||||
app.insert_state(View::default());
|
||||
app.insert_state(Game::default());
|
||||
|
||||
app.add_plugins((DefaultPlugins
|
||||
.set(WindowPlugin {
|
||||
primary_window: Some(Window {
|
||||
title: "Kodo Tag".into(),
|
||||
mode: bevy::window::WindowMode::BorderlessFullscreen,
|
||||
// resolution: (640. * 2., 360. * 2.).into(),
|
||||
..default()
|
||||
})
|
||||
.set(AudioPlugin {
|
||||
default_spatial_scale: SpatialScale::new_2d(1.),
|
||||
global_volume: GlobalVolume::new(1.),
|
||||
..default()
|
||||
})
|
||||
.set(ImagePlugin::default_nearest()),
|
||||
)
|
||||
.add_plugins((
|
||||
statue::Statue,
|
||||
camera::CameraPlugin,
|
||||
assets::AssetPlugin,
|
||||
player::Player,
|
||||
main_menu::MainMenu,
|
||||
movement::Movement,
|
||||
))
|
||||
.add_systems(Update, exit_on_escape)
|
||||
.insert_resource(ClearColor(Color::rgb(0.1, 0., 0.3)))
|
||||
.insert_resource(AmbientLight {
|
||||
color: Color::rgb(1., 1., 1.),
|
||||
brightness: 1.,
|
||||
});
|
||||
}),
|
||||
..default()
|
||||
})
|
||||
.set(AudioPlugin {
|
||||
default_spatial_scale: SpatialScale::new_2d(1.),
|
||||
global_volume: GlobalVolume::new(1.),
|
||||
..default()
|
||||
})
|
||||
.set(ImagePlugin::default_nearest()),))
|
||||
.add_systems(OnEnter(View::MainMenu), main_menu::startup)
|
||||
.add_systems(OnExit(View::MainMenu), main_menu::exit)
|
||||
.add_systems(OnEnter(View::InGame), (player::startup, statue::startup))
|
||||
.add_systems(OnExit(View::InGame), (player::exit, statue::exit))
|
||||
.add_systems(Startup, (assets::startup, camera::startup))
|
||||
.add_systems(
|
||||
Update,
|
||||
(
|
||||
(
|
||||
player::sprite_select,
|
||||
player::controls,
|
||||
movement::update_velocity_by_heading,
|
||||
movement::resolve_velocity.after(movement::update_velocity_by_heading),
|
||||
movement::ysort.after(movement::update_velocity_by_heading),
|
||||
camera::update
|
||||
.after(player::controls)
|
||||
.after(movement::resolve_velocity),
|
||||
)
|
||||
.in_set(InGameSet)
|
||||
.run_if(in_state(View::InGame)),
|
||||
(main_menu::update)
|
||||
.in_set(MainMenuSet)
|
||||
.run_if(in_state(View::MainMenu)),
|
||||
),
|
||||
)
|
||||
.insert_resource(ClearColor(Color::rgb(0.1, 0.1, 0.1)))
|
||||
.insert_resource(AmbientLight {
|
||||
color: Color::rgb(1., 1., 1.),
|
||||
brightness: 1.,
|
||||
});
|
||||
|
||||
app.run()
|
||||
}
|
||||
|
||||
fn exit_on_escape(
|
||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||
mut app_exit_events: ResMut<Events<bevy::app::AppExit>>,
|
||||
) {
|
||||
if keyboard_input.pressed(KeyCode::Escape) {
|
||||
app_exit_events.send(bevy::app::AppExit);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,23 +5,20 @@ use crate::assets::AssetLoader;
|
|||
#[derive(Component, Debug)]
|
||||
pub struct MainMenu;
|
||||
|
||||
impl Plugin for MainMenu {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(PostStartup, setup_main_menu);
|
||||
}
|
||||
}
|
||||
|
||||
fn setup_main_menu(mut commands: Commands, assets: Res<AssetLoader>) {
|
||||
pub fn startup(mut commands: Commands, assets: Res<AssetLoader>) {
|
||||
commands
|
||||
.spawn(NodeBundle {
|
||||
style: Style {
|
||||
width: Val::Percent(100.0),
|
||||
height: Val::Percent(100.0),
|
||||
justify_content: JustifyContent::SpaceBetween,
|
||||
.spawn((
|
||||
MainMenu,
|
||||
NodeBundle {
|
||||
style: Style {
|
||||
width: Val::Percent(100.0),
|
||||
height: Val::Percent(100.0),
|
||||
justify_content: JustifyContent::SpaceBetween,
|
||||
..default()
|
||||
},
|
||||
..default()
|
||||
},
|
||||
..default()
|
||||
})
|
||||
))
|
||||
.with_children(|parent| {
|
||||
// text
|
||||
parent.spawn((
|
||||
|
@ -59,3 +56,21 @@ fn setup_main_menu(mut commands: Commands, assets: Res<AssetLoader>) {
|
|||
});
|
||||
});
|
||||
}
|
||||
|
||||
pub fn exit(mut commands: Commands, q: Query<Entity, With<MainMenu>>) {
|
||||
for id in q.iter() {
|
||||
commands.entity(id).despawn_recursive();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(
|
||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||
mut next_state: ResMut<NextState<crate::View>>,
|
||||
mut app_exit_events: ResMut<Events<bevy::app::AppExit>>,
|
||||
) {
|
||||
if keyboard_input.just_pressed(KeyCode::Escape) {
|
||||
app_exit_events.send(bevy::app::AppExit);
|
||||
} else if keyboard_input.pressed(KeyCode::Enter) {
|
||||
next_state.set(crate::View::InGame)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
struct SystemSet;
|
||||
|
||||
#[derive(Component, Deref, DerefMut, Debug, Default)]
|
||||
pub struct Velocity(pub Vec2);
|
||||
|
||||
|
@ -9,19 +12,19 @@ pub struct Heading(pub Vec2);
|
|||
#[derive(Component, Deref, DerefMut, Debug, Default)]
|
||||
pub struct Speed(pub f32);
|
||||
|
||||
fn update_position(mut query: Query<(&Velocity, &mut Transform)>, time: Res<Time>) {
|
||||
pub fn resolve_velocity(mut query: Query<(&Velocity, &mut Transform)>, time: Res<Time>) {
|
||||
for (velocity, mut transform) in query.iter_mut() {
|
||||
transform.translation += (velocity.0 * time.delta_seconds()).extend(0.);
|
||||
}
|
||||
}
|
||||
|
||||
fn mover_movement(mut query: Query<(&mut Velocity, &Speed, &Heading)>) {
|
||||
pub fn update_velocity_by_heading(mut query: Query<(&mut Velocity, &Speed, &Heading)>) {
|
||||
if let Ok((mut velocity, speed, heading)) = query.get_single_mut() {
|
||||
**velocity = heading.normalize_or_zero() * (**speed);
|
||||
}
|
||||
}
|
||||
|
||||
fn ysort(mut q: Query<&mut Transform>) {
|
||||
pub fn ysort(mut q: Query<&mut Transform>) {
|
||||
q.iter_mut()
|
||||
.for_each(|mut tf| tf.translation.z = -tf.translation.y)
|
||||
}
|
||||
|
@ -32,19 +35,3 @@ pub struct Mover {
|
|||
pub heading: Heading,
|
||||
pub speed: Speed,
|
||||
}
|
||||
|
||||
pub struct Movement;
|
||||
|
||||
impl Plugin for Movement {
|
||||
fn build(&self, app: &mut App) {
|
||||
// TODO: these systems probably needs ordering?
|
||||
app.add_systems(
|
||||
Update,
|
||||
(
|
||||
update_position,
|
||||
mover_movement.after(update_position),
|
||||
ysort.after(mover_movement),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ use bevy::sprite::MaterialMesh2dBundle;
|
|||
use bevy::sprite::Mesh2dHandle;
|
||||
use bevy::{prelude::*, window::PrimaryWindow};
|
||||
|
||||
use crate::camera::CameraTarget;
|
||||
use crate::camera::Watched;
|
||||
use crate::{
|
||||
assets::AssetLoader,
|
||||
movement::{Heading, Mover, Speed, Velocity},
|
||||
|
@ -17,14 +17,7 @@ pub struct Player;
|
|||
#[derive(Component, Debug)]
|
||||
pub struct Crosshair;
|
||||
|
||||
impl Plugin for Player {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(PostStartup, spawn_player)
|
||||
.add_systems(Update, (sprite_select, controls.after(sprite_select)));
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_player(
|
||||
pub fn startup(
|
||||
mut commands: Commands,
|
||||
assets: Res<AssetLoader>,
|
||||
asset_server: Res<AssetServer>,
|
||||
|
@ -37,7 +30,7 @@ fn spawn_player(
|
|||
commands
|
||||
.spawn((
|
||||
Player,
|
||||
CameraTarget,
|
||||
Watched,
|
||||
SpriteSheetBundle {
|
||||
texture,
|
||||
atlas: assets.images.player.clone().into(),
|
||||
|
@ -95,7 +88,7 @@ fn spawn_player(
|
|||
});
|
||||
}
|
||||
|
||||
fn controls(
|
||||
pub fn controls(
|
||||
mut commands: Commands,
|
||||
mut player_q: Query<(&mut Sprite, &mut TextureAtlas, &mut Heading, Entity), With<Player>>,
|
||||
asset_server: Res<AssetServer>,
|
||||
|
@ -104,6 +97,7 @@ fn controls(
|
|||
assets: Res<AssetLoader>,
|
||||
win_q: Query<&Window, With<PrimaryWindow>>,
|
||||
cam_q: Query<(&Camera, &GlobalTransform), With<Camera>>,
|
||||
mut next_state: ResMut<NextState<crate::View>>,
|
||||
mut crosshair_q: Query<&mut Transform, With<Crosshair>>,
|
||||
) {
|
||||
let (mut sprite, mut texture, mut heading, player_entity) = player_q.single_mut();
|
||||
|
@ -153,6 +147,7 @@ fn controls(
|
|||
}
|
||||
for key in input.get_just_pressed() {
|
||||
match key {
|
||||
KeyCode::Escape => next_state.set(crate::View::MainMenu),
|
||||
KeyCode::Space => {
|
||||
let child = commands
|
||||
.spawn(AudioSourceBundle {
|
||||
|
@ -192,13 +187,15 @@ fn controls(
|
|||
}
|
||||
}
|
||||
|
||||
fn sprite_select(
|
||||
pub fn sprite_select(
|
||||
mut q: Query<(&Handle<TextureAtlasLayout>, &mut TextureAtlas), With<Player>>,
|
||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||
texture_atlases: Res<Assets<TextureAtlasLayout>>,
|
||||
) {
|
||||
if let Ok((texture, mut sprite)) = q.get_single_mut() {
|
||||
if keyboard_input.just_pressed(KeyCode::KeyN) {
|
||||
if keyboard_input.just_pressed(KeyCode::KeyN) {
|
||||
info!("pressed N");
|
||||
if let Ok((texture, mut sprite)) = q.get_single_mut() {
|
||||
info!("next tex: {}", sprite.index);
|
||||
let t = texture_atlases
|
||||
.get(texture)
|
||||
.expect("could not load player texture");
|
||||
|
@ -210,3 +207,9 @@ fn sprite_select(
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn exit(mut commands: Commands, q: Query<Entity, With<Player>>) {
|
||||
for id in q.iter() {
|
||||
commands.entity(id).despawn_recursive();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,17 +1,10 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
use crate::assets::AssetLoader;
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Component, Debug)]
|
||||
pub struct Statue;
|
||||
|
||||
impl Plugin for Statue {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(PostStartup, spawn_statue);
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_statue(mut commands: Commands, assets: Res<AssetLoader>, asset_server: Res<AssetServer>) {
|
||||
pub fn startup(mut commands: Commands, assets: Res<AssetLoader>, asset_server: Res<AssetServer>) {
|
||||
let texture = asset_server.load("img/Props.png");
|
||||
commands.spawn((
|
||||
Statue,
|
||||
|
@ -38,3 +31,9 @@ fn spawn_statue(mut commands: Commands, assets: Res<AssetLoader>, asset_server:
|
|||
},
|
||||
));
|
||||
}
|
||||
|
||||
pub fn exit(mut commands: Commands, q: Query<Entity, With<Statue>>) {
|
||||
for id in q.iter() {
|
||||
commands.entity(id).despawn_recursive();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue