Skip to content

Commit

Permalink
Merge pull request #7 from Earthmark/earthmark-dev
Browse files Browse the repository at this point in the history
Build fixes and such.
  • Loading branch information
Earthmark authored Mar 26, 2022
2 parents 3059ed6 + 4faa751 commit b69ef36
Show file tree
Hide file tree
Showing 13 changed files with 517 additions and 252 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,19 @@ jobs:
toolchain: stable
target: wasm32-unknown-unknown
override: true
- name: install wasm-bindgen-cli
- name: install trunk
run: |
cargo install wasm-bindgen-cli
cargo install trunk
- name: Build
run: |
cargo build --release --target wasm32-unknown-unknown
trunk build --release
- name: Prepare package
run: |
wasm-bindgen --no-typescript --out-name nothing_moves --out-dir wasm --target web target/wasm32-unknown-unknown/release/${{ env.binary }}.wasm
cp -r assets wasm/
cp -r assets dist/
- name: Package as a zip
uses: vimtor/action-zip@v1
with:
files: wasm
files: dist
dest: ${{ env.binary }}.zip

- name: Upload binaries to release
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ Cargo.lock
# Added by cargo

/target

/dist
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ version = "0.1.0"

[dependencies]
bevy = "0.6"
bevy_tweening = "0.3.2"
rand = "0.8"

# Enable only a small amount of optimization in debug mode
Expand Down
Empty file added index.html
Empty file.
5 changes: 3 additions & 2 deletions src/level/input.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use bevy::prelude::*;
use bevy::prelude::{Input, KeyCode, Query, Res};

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

pub fn level_navigation<const DIMS: usize>(
mut query: Query<&mut MazeLevel<DIMS>>,
Expand Down
38 changes: 17 additions & 21 deletions src/level/loader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy::prelude::*;
use rand::prelude::*;

use super::{maze_renderer::MazeAssets, MazeLevel};
use super::MazeLevel;

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

pub fn level_load_system(
mut commands: Commands,
mut events: EventReader<LoadLevel>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
assets: Res<AssetServer>,
) {
pub fn level_load_system(mut commands: Commands, mut events: EventReader<LoadLevel>) {
for level_loader in events.iter() {
let mut entity = commands.spawn();

entity
.insert(Transform::default())
.insert(GlobalTransform::default());

let mut rng = match level_loader.rng_source {
RngSource::Seeded(seed) => StdRng::seed_from_u64(seed),
};
match level_loader.dimensions {
DimensionLength::Two(lengths) => entity.insert(MazeLevel::new(&lengths, &mut rng)),
DimensionLength::Three(lengths) => entity.insert(MazeLevel::new(&lengths, &mut rng)),
DimensionLength::Four(lengths) => entity.insert(MazeLevel::new(&lengths, &mut rng)),
DimensionLength::Five(lengths) => entity.insert(MazeLevel::new(&lengths, &mut rng)),
DimensionLength::Six(lengths) => entity.insert(MazeLevel::new(&lengths, &mut rng)),
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))
}
};

entity.insert(MazeAssets::new(&mut meshes, &mut materials, &assets));
}
}
156 changes: 94 additions & 62 deletions src/level/maze_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,23 @@ use bevy::prelude::*;

#[derive(Component)]
pub struct MazeLevel<const DIMS: usize> {
position: [u8; DIMS],
maze: maze::Maze<DIMS>,
dim_x: usize,
dim_y: usize,
position: [u8; DIMS],
axis: [u8; 2],
}

#[derive(Clone, Debug)]
pub struct AxisChanged {
pub level: Entity,
pub old_axis: [u8; 2],
pub axis: [u8; 2],
}

#[derive(Clone, Debug)]
pub struct PositionChanged<const DIMS: usize> {
pub level: Entity,
pub old_position: [u8; DIMS],
pub position: [u8; DIMS],
}

#[derive(PartialEq, Eq, Clone, Copy)]
Expand All @@ -16,12 +29,25 @@ pub enum Axis {
}

impl Axis {
fn invert(&self) -> Axis {
pub fn invert(&self) -> Axis {
match self {
Axis::X => Axis::Y,
Axis::Y => Axis::X,
}
}

pub fn get<'a, T>(&self, v: &'a [T; 2]) -> &'a T {
match self {
Axis::X => &v[0],
Axis::Y => &v[1],
}
}
pub fn get_mut<'a, T>(&self, v: &'a mut [T; 2]) -> &'a mut T {
match self {
Axis::X => &mut v[0],
Axis::Y => &mut v[1],
}
}
}

#[derive(PartialEq, Eq, Clone, Copy)]
Expand All @@ -31,7 +57,7 @@ pub enum Direction {
}

impl Direction {
fn shift_wrapped(&self, value: usize, limit: usize) -> usize {
fn shift_wrapped(&self, value: u8, limit: u8) -> u8 {
(match self {
Direction::Positive => value.checked_add(1).unwrap_or(0),
Direction::Negative => value.checked_sub(1).unwrap_or(limit - 2),
Expand All @@ -43,8 +69,7 @@ impl<const DIMS: usize> Default for MazeLevel<DIMS> {
fn default() -> Self {
Self {
maze: Default::default(),
dim_x: 0,
dim_y: 1,
axis: [0, 1],
position: [0; DIMS],
}
}
Expand All @@ -57,53 +82,55 @@ impl<const DIMS: usize> MazeLevel<DIMS> {
..Default::default()
}
}
}

fn axis(&self, axis: Axis) -> usize {
match axis {
Axis::X => self.dim_x,
Axis::Y => self.dim_y,
}
impl<const DIMS: usize> MazeView for MazeLevel<DIMS> {
fn dims(&self) -> &[u8] {
self.maze.lengths()
}

fn axis_mut(&mut self, axis: Axis) -> &mut usize {
match axis {
Axis::X => &mut self.dim_x,
Axis::Y => &mut self.dim_y,
}
fn axis(&self) -> [u8; 2] {
self.axis
}

pub fn shift_axis(&mut self, axis: Axis, dir: Direction) {
let target_axis = self.axis(axis);
let off_target_axis = self.axis(axis.invert());
fn shift_axis(&mut self, axis: Axis, dir: Direction) {
let target_axis = *axis.get(&self.axis());
let off_target_axis = *axis.invert().get(&self.axis());

let linear_current = if target_axis > off_target_axis {
target_axis - 1
} else {
target_axis
};

let new_off_axis = dir.shift_wrapped(linear_current, self.dims());
let new_off_axis = dir.shift_wrapped(linear_current, DIMS as u8);
let dest = if new_off_axis >= off_target_axis {
new_off_axis + 1
} else {
new_off_axis
};

*self.axis_mut(axis) = dest;
*axis.get_mut(&mut self.axis) = dest;
}

// assume dim_x and dim_y are both together.
#[inline]
pub fn pos_limit(&self, axis: Axis) -> u8 {
self.length_of_dim(self.axis(axis)).unwrap()
fn pos_limit(&self) -> [u8; 2] {
[
self.maze.lengths()[self.axis[0] as usize],
self.maze.lengths()[self.axis[1] as usize],
]
}

pub fn pos(&self, axis: Axis) -> u8 {
self.position[self.axis(axis)]
fn pos(&self) -> [u8; 2] {
[
self.position[self.axis[0] as usize],
self.position[self.axis[1] as usize],
]
}

pub fn move_pos(&mut self, axis: Axis, dir: Direction) {
let dim = self.axis(axis);
fn move_pos(&mut self, axis: Axis, dir: Direction) {
let dim = *axis.get(&self.axis) as usize;
let mut pos = self.position;
if dir == Direction::Negative {
if let Some(new_pos) = pos[dim].checked_sub(1) {
Expand All @@ -123,46 +150,51 @@ impl<const DIMS: usize> MazeLevel<DIMS> {
}
}

fn should_make_wall(&self, position: &[u8; DIMS], direction: usize) -> bool {
if let Some(walkable) = self.maze.can_move(position, direction) {
fn should_make_wall(&self, position: [u8; 2], axis: Axis) -> bool {
let mut cursor = self.position;
cursor[self.axis[0] as usize] = position[0];
cursor[self.axis[1] as usize] = position[1];
if let Some(walkable) = self.maze.can_move(&cursor, *axis.get(&self.axis) as usize) {
!walkable
} else {
false
}
}
}

fn dims(&self) -> usize {
DIMS
}
pub trait MazeView {
fn dims(&self) -> &[u8];

fn length_of_dim(&self, dim: usize) -> Option<u8> {
self.maze.lengths().get(dim).copied()
}
fn axis(&self) -> [u8; 2];
fn shift_axis(&mut self, axis: Axis, dir: Direction);

pub fn iter_walls(&self) -> impl std::iter::Iterator<Item = ([u8; 2], [u8; 2])> + '_ {
let length_x = self.pos_limit(Axis::X);
let length_y = self.pos_limit(Axis::Y);
let position = self.position;

(0..length_x)
.flat_map(move |x| (0..length_y).map(move |y| (x, y)))
.flat_map(move |(cursor_x, cursor_y)| {
let mut cursor = position;
cursor[self.dim_x] = cursor_x;
cursor[self.dim_y] = cursor_y;
[
if self.should_make_wall(&cursor, self.dim_x) {
Some(([cursor_x, cursor_y], [cursor_x + 1, cursor_y]))
} else {
None
},
if self.should_make_wall(&cursor, self.dim_y) {
Some(([cursor_x, cursor_y], [cursor_x, cursor_y + 1]))
} else {
None
},
]
})
.flatten()
}
fn pos_limit(&self) -> [u8; 2];
fn pos(&self) -> [u8; 2];
fn move_pos(&mut self, axis: Axis, dir: Direction);

fn should_make_wall(&self, position: [u8; 2], axis: Axis) -> bool;
}

pub fn iter_walls(
maze: &impl MazeView,
) -> impl std::iter::Iterator<Item = ([u8; 2], [u8; 2])> + '_ {
let [length_x, length_y] = maze.pos_limit();

(0..length_x)
.flat_map(move |x| (0..length_y).map(move |y| [x, y]))
.flat_map(move |cursor| {
[
if maze.should_make_wall(cursor, Axis::X) {
Some((cursor, [cursor[0] + 1, cursor[1]]))
} else {
None
},
if maze.should_make_wall(cursor, Axis::Y) {
Some((cursor, [cursor[0], cursor[1] + 1]))
} else {
None
},
]
})
.flatten()
}
Loading

0 comments on commit b69ef36

Please sign in to comment.