update
parent
49dc9f4a4d
commit
5dd03bb1dd
54
src/main.rs
54
src/main.rs
|
@ -1,4 +1,6 @@
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use std::ops::Add;
|
||||||
|
use std::rc::Rc;
|
||||||
use console_engine::{pixel, KeyCode};
|
use console_engine::{pixel, KeyCode};
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use crate::load_yaml::load_yaml_folder;
|
use crate::load_yaml::load_yaml_folder;
|
||||||
|
@ -11,6 +13,7 @@ pub struct Level {
|
||||||
pub walls: Vec<i32>,
|
pub walls: Vec<i32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, PartialEq)]
|
||||||
pub struct Vec2 {
|
pub struct Vec2 {
|
||||||
pub x: i32,
|
pub x: i32,
|
||||||
pub y: i32,
|
pub y: i32,
|
||||||
|
@ -22,12 +25,26 @@ impl Vec2 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Add for Vec2 {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn add(self, other: Self) -> Self {
|
||||||
|
Self {
|
||||||
|
x: self.x + other.x,
|
||||||
|
y: self.y + other.y,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// initializes a screen of 20x10 characters with a target of 3 frames per second
|
// initializes a screen of 20x10 characters with a target of 3 frames per second
|
||||||
// coordinates will range from [0,0] to [19,19]
|
// coordinates will range from [0,0] to [19,19]
|
||||||
let mut engine = console_engine::ConsoleEngine::init(51, 32, 50).unwrap();
|
let mut engine = console_engine::ConsoleEngine::init(51, 32, 50).unwrap();
|
||||||
let mut path: Vec<Level> = vec![];
|
let mut path: Vec<Level> = vec![];
|
||||||
|
|
||||||
|
let mut current_walls: Vec<Vec2> = vec![];
|
||||||
|
let mut current_doors: Vec<Vec2> = vec![];
|
||||||
|
|
||||||
let mut player_location: Vec2 = Vec2::new(25, 16);
|
let mut player_location: Vec2 = Vec2::new(25, 16);
|
||||||
let mut player_direction: Vec2 = Vec2::new(0, 0);
|
let mut player_direction: Vec2 = Vec2::new(0, 0);
|
||||||
|
|
||||||
|
@ -77,7 +94,10 @@ fn main() {
|
||||||
|
|
||||||
player_location = Vec2::new(player_location.x + player_direction.x, player_location.y + player_direction.y);
|
player_location = Vec2::new(player_location.x + player_direction.x, player_location.y + player_direction.y);
|
||||||
|
|
||||||
engine.rect(0, 1, 50, 31, pixel::pxl('█')); // draw a line of '#' from [0,0] to [19,9]
|
current_walls = play_area_to_walls(Vec2::new(50, 31));
|
||||||
|
for walls in ¤t_walls {
|
||||||
|
engine.set_pxl(walls.x, walls.y, pixel::pxl('█'));
|
||||||
|
}
|
||||||
|
|
||||||
let current_level = &path[path.len() - 1];
|
let current_level = &path[path.len() - 1];
|
||||||
if current_level.doors[0] == true {
|
if current_level.doors[0] == true {
|
||||||
|
@ -95,7 +115,7 @@ fn main() {
|
||||||
|
|
||||||
engine.set_pxl(player_location.x, player_location.y, pixel::pxl('@'));
|
engine.set_pxl(player_location.x, player_location.y, pixel::pxl('@'));
|
||||||
|
|
||||||
engine.print(0, 0, format!("Result: {}", path.len()).as_str()); // prints some value at [0,1]
|
engine.print(0, 0, format!("Path length: {}", path.len()).as_str()); // prints some value at [0,1]
|
||||||
|
|
||||||
if engine.is_key_pressed(KeyCode::Char('q')) {
|
if engine.is_key_pressed(KeyCode::Char('q')) {
|
||||||
break;
|
break;
|
||||||
|
@ -115,9 +135,37 @@ fn main() {
|
||||||
player_direction.x = -1;
|
player_direction.x = -1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
for walls in ¤t_walls {
|
||||||
|
if player_location + player_direction == *walls {
|
||||||
|
player_direction = Vec2::new(0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for doors in ¤t_doors {
|
||||||
|
if player_location + player_direction == *doors {
|
||||||
|
load_next_level();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
engine.draw();
|
engine.draw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn play_area_to_walls(area_end: Vec2) -> Vec<Vec2> {
|
||||||
|
let mut return_value: Vec<Vec2> = vec![];
|
||||||
|
|
||||||
|
for x in 0..=area_end.x {
|
||||||
|
return_value.push(Vec2::new(x, 1));
|
||||||
|
return_value.push(Vec2::new(x, area_end.y));
|
||||||
|
}
|
||||||
|
for y in 1..=area_end.y {
|
||||||
|
return_value.push(Vec2::new(0, y));
|
||||||
|
return_value.push(Vec2::new(area_end.x, y));
|
||||||
|
}
|
||||||
|
|
||||||
|
return return_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_next_level() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue