origin-shift/src/main.rs

149 lines
4.0 KiB
Rust

use std::cmp::PartialEq;
use raylib::prelude::*;
use rand::prelude::*;
#[derive(Debug, PartialEq, Clone)]
enum Direction {
Up,
Down,
Left,
Right,
NoWhere,
}
#[derive(Debug)]
struct Node {
direction: Direction,
}
#[derive(Debug)]
struct Maze {
maze: Vec<Vec<Node>>,
origin_node: (u32, u32),
size: (u32, u32),
}
fn generate_maze (size: (u32, u32)) -> Maze {
let mut maze: Vec<Vec<Node>> = vec![];
for _x in 0..size.0 {
let mut row = vec![];
for _y in 0..size.1 {
row.push(Node {
direction: Direction::NoWhere,
});
}
maze.push(row);
}
return Maze {
maze,
origin_node: (0, 0),
size,
};
}
fn iterate_maze (maze: &mut Maze) {
let mut rng: ThreadRng = rand::thread_rng();
let mut directions = vec![];
if maze.origin_node.0 != 0 {
directions.push(Direction::Left);
}
if maze.origin_node.1 != 0 {
directions.push(Direction::Down);
}
if maze.origin_node.0 as i32 != (maze.size.0 as i32) -1 {
directions.push(Direction::Right);
}
if maze.origin_node.1 as i32 != (maze.size.1 as i32) -1 {
directions.push(Direction::Up);
}
let direction = directions.choose(&mut rng).unwrap();
maze.maze[maze.origin_node.0 as usize][maze.origin_node.1 as usize].direction = direction.clone();
if *direction == Direction::Up {
maze.origin_node.1 += 1;
} else if *direction == Direction::Down {
maze.origin_node.1 -= 1;
} else if *direction == Direction::Right {
maze.origin_node.0 += 1;
} else {
maze.origin_node.0 -= 1;
}
maze.maze[maze.origin_node.0 as usize][maze.origin_node.1 as usize].direction = Direction::NoWhere;
}
fn empty_spots(maze: &mut Maze) -> bool {
let mut nodes_with_empty = 0;
for i in maze.maze.iter() {
for node in i.iter() {
if node.direction == Direction::NoWhere {
if nodes_with_empty == 1 {
return true;
}
nodes_with_empty += 1;
}
}
}
return false;
}
fn main() {
let mut maze = generate_maze((1000, 1000));
println!("{:?}", maze);
let spacing = 7;
let thickness = 5;
let mut iterations = 0;
while empty_spots(&mut maze) {
iterate_maze(&mut maze);
iterations += 1;
}
println!("{}", iterations);
println!("{}", empty_spots(&mut maze));
let (mut rl, thread) = raylib::init()
.size(1280, 720)
.title("Origin Shift")
.build();
while !rl.window_should_close() {
if rl.is_key_down(KeyboardKey::KEY_SPACE) {
iterate_maze(&mut maze);
}
let mut d = rl.begin_drawing(&thread);
d.clear_background(Color::BLACK);
for (x, row) in maze.maze.iter().enumerate() {
for (y, node) in row.iter().enumerate() {
d.draw_rectangle(x as i32 * spacing, y as i32 * spacing, thickness, thickness, Color::GREEN);
if node.direction == Direction::Up {
d.draw_rectangle(x as i32 * spacing, y as i32 * spacing + thickness, thickness, spacing, Color::GREEN);
}
if node.direction == Direction::Down {
d.draw_rectangle(x as i32 * spacing, y as i32 * spacing - (spacing - thickness), thickness, spacing - thickness, Color::GREEN);
}
if node.direction == Direction::Right {
d.draw_rectangle(x as i32 * spacing + thickness, y as i32 * spacing, spacing, thickness, Color::GREEN);
}
if node.direction == Direction::Left {
d.draw_rectangle(x as i32 * spacing - (spacing - thickness), y as i32 * spacing, spacing - thickness, thickness, Color::GREEN);
}
}
}
d.draw_rectangle(maze.origin_node.0 as i32 * spacing, maze.origin_node.1 as i32 * spacing, thickness, thickness, Color::BLUE);
d.draw_fps(0, 0);
}
}