CLI_Game/src/main.rs

124 lines
3.7 KiB
Rust
Raw Normal View History

use std::path::Path;
use console_engine::{pixel, KeyCode};
use serde::{Serialize, Deserialize};
use crate::load_yaml::load_yaml_folder;
pub mod load_yaml;
#[derive(Clone, Serialize, Deserialize)]
pub struct Level {
pub doors: Vec<bool>,
pub walls: Vec<i32>,
}
pub struct Vec2 {
pub x: i32,
pub y: i32,
}
impl Vec2 {
pub const fn new(x: i32, y: i32) -> Self {
Self { x, y }
}
}
fn main() {
// initializes a screen of 20x10 characters with a target of 3 frames per second
// coordinates will range from [0,0] to [19,19]
let mut engine = console_engine::ConsoleEngine::init(51, 32, 50).unwrap();
let mut path: Vec<Level> = vec![];
let mut player_location: Vec2 = Vec2::new(25, 16);
let mut player_direction: Vec2 = Vec2::new(0, 0);
let default_level: String = "
doors: [
true, # North
true, # East
true, # South
true, # West
]
walls: [
]
".to_string();
let level_yaml: Vec<String> = load_yaml_folder(default_level.clone(), Path::new("./data/levels"));
let mut levels_north_loaded: Vec<Level> = vec![];
let mut levels_east_loaded: Vec<Level> = vec![];
let mut levels_south_loaded: Vec<Level> = vec![];
let mut levels_west_loaded: Vec<Level> = vec![];
for level_data in level_yaml {
let level: Level = serde_yaml::from_str::<Level>(&level_data).expect("Error reading level");
if level.doors[0] == true {
levels_north_loaded.push(level.clone());
}
if level.doors[1] == true {
levels_east_loaded.push(level.clone());
}
if level.doors[2] == true {
levels_south_loaded.push(level.clone());
}
if level.doors[3] == true {
levels_west_loaded.push(level.clone());
}
}
path.push(serde_yaml::from_str::<Level>(&default_level).expect("Error loading starting level"));
// main loop, be aware that you'll have to break it because ctrl+C is captured
loop {
engine.wait_frame(); // wait for next frame + capture inputs
engine.clear_screen(); // reset the screen
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]
let current_level = &path[path.len() - 1];
if current_level.doors[0] == true {
engine.line(24, 1, 26, 1, pixel::pxl('#'));
}
if current_level.doors[1] == true {
engine.line(50, 15, 50, 17, pixel::pxl('#'));
}
if current_level.doors[2] == true {
engine.line(24, 31, 26, 31, pixel::pxl('#'));
}
if current_level.doors[3] == true {
engine.line(0, 15, 0, 17, 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]
if engine.is_key_pressed(KeyCode::Char('q')) {
break;
}
if player_direction.x == 0 && player_direction.y == 0 {
if engine.is_key_pressed(KeyCode::Char('w')) {
player_direction.y = -1;
}
if engine.is_key_pressed(KeyCode::Char('d')) {
player_direction.x = 1;
}
if engine.is_key_pressed(KeyCode::Char('s')) {
player_direction.y = 1;
}
if engine.is_key_pressed(KeyCode::Char('a')) {
player_direction.x = -1;
}
} else {
}
engine.draw();
}
}