7 Hour Roguelike, spring 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.3 KiB

extern crate termion;
extern crate rogue_util;
extern crate rogue_engine;
extern crate rand;
pub mod gen;
pub mod ent;
use rogue_util::{coord::*, grid::path::Traversable};
use termion::{raw::IntoRawMode, input::TermRead};
use std::io::{self, Write};
use std::rc::Rc;
use std::cell::RefCell;
use rogue_engine::world::{self, World, entity::{EntRef, ent_ref}};
fn main() -> io::Result<()>
{
let stdin = io::stdin();
let mut stdout = io::stdout().into_raw_mode().unwrap();
let w = Rc::new(RefCell::new(World::new(0, stdin.keys())));
let newents: Rc<RefCell<Vec<EntRef>>> = Rc::new(RefCell::new(Vec::new()));
let borrowed_newents = Rc::clone(&newents);
w.borrow_mut().gen_cb.borrow_mut().push(Box::new(move |_, _, g| {
for pt in g.rect().iter() {
if g.get(pt).unwrap().can_pass() {
if rand::random::<f32>() < 0.005 {
borrowed_newents.borrow_mut().push(ent_ref(ent::Prowler::new(pt)));
break;
}
}
}
}));
let ppos: V2i = {
let wg = w.borrow_mut();
let mut rg = wg.region_mut();
let mut ps: Option<V2i> = None;
for pt in &R2i::origin_dim(V2i(0, 0), rg.grid_size()) {
let tile = rg.get_or_create(pt);
if tile.can_pass() {
ps = Some(pt);
break;
}
}
ps.unwrap()
};
w.borrow_mut().player_mut().set_pos(ppos);
let (width, height) = termion::terminal_size()?;
let mut rs = world::render::RenderState::new(V2i(width as isize, height as isize));
let mut died = false;
loop {
w.borrow().render(&mut rs, &mut stdout)?;
print!("{}HP: {}",
termion::cursor::Goto(1, height),
w.borrow().player().hp(),
);
stdout.flush()?;
if let Some(()) = w.borrow_mut().step() { break; }
if w.borrow().player().is_dead() { died = true; break; }
for eref in newents.borrow_mut().drain(..) {
w.borrow_mut().add_entity(eref);
}
}
print!("{}{}",
termion::clear::All,
termion::cursor::Goto(1, 1),
);
std::mem::drop(stdout);
if died { println!("you died!"); }
println!("insert finalization here");
io::stdout().flush()?;
Ok(())
}