Skip to content

Commit

Permalink
Merge pull request #9 from Earthmark/earthmark-dev
Browse files Browse the repository at this point in the history
Added player, and victory text.
  • Loading branch information
Earthmark authored Apr 16, 2022
2 parents c0a0b07 + d3002d5 commit d5726d9
Show file tree
Hide file tree
Showing 8 changed files with 465 additions and 372 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name = "nothing-moves"
version = "0.1.0"

[dependencies]
bevy = "0.6"
bevy = "0.7"
rand = "0.8"

# Enable only a small amount of optimization in debug mode
Expand Down
54 changes: 34 additions & 20 deletions src/level/input.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,54 @@
use bevy::prelude::{Input, KeyCode, Query, Res};
use bevy::prelude::*;

use super::maze_level::*;
use super::maze_level::{Axis, Direction};

pub fn level_navigation<const DIMS: usize>(
mut query: Query<&mut MazeLevel<DIMS>>,
pub fn level_navigation(
level: Option<ResMut<MazeLevel>>,
keys: Res<Input<KeyCode>>,
mut position_event: EventWriter<PositionChanged>,
mut axis_event: EventWriter<AxisChanged>,
) {
if keys.just_pressed(KeyCode::Q) {
for mut level in query.iter_mut() {
if let Some(mut level) = level {
if keys.just_pressed(KeyCode::Q) {
level.shift_axis(Axis::X, Direction::Negative);
axis_event.send(AxisChanged { axis: level.axis() });
}
}
if keys.just_pressed(KeyCode::E) {
for mut level in query.iter_mut() {
if keys.just_pressed(KeyCode::E) {
level.shift_axis(Axis::X, Direction::Positive);
axis_event.send(AxisChanged { axis: level.axis() });
}
}
if keys.just_pressed(KeyCode::W) {
for mut level in query.iter_mut() {
if keys.just_pressed(KeyCode::Z) {
level.shift_axis(Axis::Y, Direction::Negative);
axis_event.send(AxisChanged { axis: level.axis() });
}
if keys.just_pressed(KeyCode::X) {
level.shift_axis(Axis::Y, Direction::Positive);
axis_event.send(AxisChanged { axis: level.axis() });
}
if keys.just_pressed(KeyCode::W) {
level.move_pos(Axis::X, Direction::Positive);
position_event.send(PositionChanged {
position: level.pos(),
});
}
}
if keys.just_pressed(KeyCode::S) {
for mut level in query.iter_mut() {
if keys.just_pressed(KeyCode::S) {
level.move_pos(Axis::X, Direction::Negative);
position_event.send(PositionChanged {
position: level.pos(),
});
}
}
if keys.just_pressed(KeyCode::D) {
for mut level in query.iter_mut() {
if keys.just_pressed(KeyCode::D) {
level.move_pos(Axis::Y, Direction::Positive);
position_event.send(PositionChanged {
position: level.pos(),
});
}
}
if keys.just_pressed(KeyCode::A) {
for mut level in query.iter_mut() {
if keys.just_pressed(KeyCode::A) {
level.move_pos(Axis::Y, Direction::Negative);
position_event.send(PositionChanged {
position: level.pos(),
});
}
}
}
102 changes: 83 additions & 19 deletions src/level/loader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use crate::AppState;
use bevy::prelude::*;
use rand::prelude::*;

use super::MazeLevel;
use super::{
maze_level::{AxisChanged, PositionChanged},
MazeLevel,
};

#[derive(Clone, Debug)]
pub struct LoadLevel {
Expand Down Expand Up @@ -34,27 +38,87 @@ impl Default for LoadLevel {
}
}

pub fn level_load_system(mut commands: Commands, mut events: EventReader<LoadLevel>) {
pub fn level_load_system(
mut c: Commands,
mut events: EventReader<LoadLevel>,
mut app_state: ResMut<State<AppState>>,
) {
for level_loader in events.iter() {
let mut rng = match level_loader.rng_source {
RngSource::Seeded(seed) => StdRng::seed_from_u64(seed),
};
match level_loader.dimensions {
DimensionLength::Two(lengths) => {
commands.spawn().insert(MazeLevel::new(&lengths, &mut rng))
}
DimensionLength::Three(lengths) => {
commands.spawn().insert(MazeLevel::new(&lengths, &mut rng))
}
DimensionLength::Four(lengths) => {
commands.spawn().insert(MazeLevel::new(&lengths, &mut rng))
}
DimensionLength::Five(lengths) => {
commands.spawn().insert(MazeLevel::new(&lengths, &mut rng))
}
DimensionLength::Six(lengths) => {
commands.spawn().insert(MazeLevel::new(&lengths, &mut rng))
}
};
c.insert_resource(match level_loader.dimensions {
DimensionLength::Two(lengths) => MazeLevel::new(&lengths, &mut rng),
DimensionLength::Three(lengths) => MazeLevel::new(&lengths, &mut rng),
DimensionLength::Four(lengths) => MazeLevel::new(&lengths, &mut rng),
DimensionLength::Five(lengths) => MazeLevel::new(&lengths, &mut rng),
DimensionLength::Six(lengths) => MazeLevel::new(&lengths, &mut rng),
});
app_state.push(AppState::InMaze).unwrap();
}
}

pub fn initial_events_on_load(
maze: Res<MazeLevel>,
mut position_changed: EventWriter<PositionChanged>,
mut axis_changed: EventWriter<AxisChanged>,
) {
position_changed.send(PositionChanged {
position: maze.pos(),
});
axis_changed.send(AxisChanged { axis: maze.axis() });
}

pub fn load_maze_assets(
mut c: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
c.insert_resource(MazeAssets {
joint: meshes.add(Mesh::from(shape::Box::new(0.2, 1.0, 0.2))),
wall: meshes.add(Mesh::from(shape::Box::new(0.1, 0.6, 1.0))),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
});
}

pub fn spawn_player(
mut c: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
c.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Capsule {
radius: 0.3,
..default()
})),
material: materials.add(Color::rgb(0.5, 0.5, 0.8).into()),
..Default::default()
});
}

#[derive(Component)]
pub struct MazeAssets {
joint: Handle<Mesh>,
wall: Handle<Mesh>,
material: Handle<StandardMaterial>,
}

impl MazeAssets {
pub fn wall(&self, transform: Transform) -> PbrBundle {
PbrBundle {
mesh: self.wall.clone(),
material: self.material.clone(),
transform,
..Default::default()
}
}

pub fn joint(&self, transform: Transform) -> PbrBundle {
PbrBundle {
mesh: self.joint.clone(),
material: self.material.clone(),
transform,
..Default::default()
}
}
}
Loading

0 comments on commit d5726d9

Please sign in to comment.