~jpgleeson/RustyDagger

dda2373a91bc24bb04e6aa9e61ff48998dc6fe16 — jpgleeson 3 months ago 8ae1807 master
Field of View and Fog of War added.
1 files changed, 62 insertions(+), 14 deletions(-)

M src/main.rs
M src/main.rs => src/main.rs +62 -14
@@ 2,6 2,7 @@ use tcod::colors::*;
use tcod::console::*;
use tcod::input::Key;
use tcod::input::KeyCode::*;
use tcod::map::{FovAlgorithm, Map as FovMap};
use rand::Rng;
use std::cmp;



@@ 15,16 16,23 @@ const FPSCAP: i32 = 30;

const COLOUR_DARK_WALL:Color = Color {r:20, g: 20, b: 20};
const COLOUR_DARK_GROUND:Color = Color{r:80, g: 80, b: 80};
const COLOUR_LIGHT_WALL:Color = Color {r: 120, g: 120, b: 120};
const COLOUR_LIGHT_GROUND:Color = Color{r:180, g: 180, b: 180};

const MAXROOMSIZE: i32 = 10;
const MINROOMSIZE: i32 = 4;
const MAXNUMBERROOMS: i32 = 48;
const MAXCAVESTEPS:i32 = 2500;

const FOV_ALGORITHM: FovAlgorithm = FovAlgorithm::Basic;
const FOV_LIGHT_WALLS: bool = true;
const TORCH_RADIUS: i32 = 12;

struct Tcod
{
    root:Root,
    con:Offscreen,
    fov: FovMap,
}

#[derive(Clone, Copy, Debug)]


@@ 32,18 40,19 @@ struct Tile
{
    blocked: bool,
    blockSight: bool,
    explored: bool,
}

impl Tile
{
    pub fn empty() -> Self
    {
        Tile{ blocked: false, blockSight: false,}
        Tile{ blocked: false, blockSight: false, explored: false,}
    }

    pub fn wall() -> Self
    {
        Tile{ blocked: true, blockSight: true,}
        Tile{ blocked: true, blockSight: true, explored: false,}
    }
}



@@ 206,10 215,17 @@ fn buildCaveMap(player: &mut Object) -> Map
    let mut x = startX;
    let mut y = startY;

    for _ in 0..MAXCAVESTEPS
    let npcStep:i32 = rand::thread_rng().gen_range(0, MAXCAVESTEPS);

    for a in 0..MAXCAVESTEPS
    {
        map[x as usize][y as usize] = Tile::empty();

        if a == npcStep
        {
            let NPC:Object = Object::new(x, y, '@', BLUE);
        }

        let nextDirection = rand::thread_rng().gen_range(0, 4);
        if (nextDirection == 0 && x < (MAPWIDTH-1))
        {


@@ 303,7 319,7 @@ fn main() {
    let root:Root = Root::initializer().font("arial10x10.png", FontLayout::Tcod).font_type(FontType::Greyscale).size(SCREENWIDTH, SCREENHEIGHT).title("Rusty Dagger").init();
    let con = Offscreen::new(MAPWIDTH, MAPHEIGHT);

    let mut tcod = Tcod {root, con};
    let mut tcod = Tcod {root, con: Offscreen::new(MAPWIDTH, MAPHEIGHT), fov: FovMap::new(MAPWIDTH, MAPHEIGHT),};

    tcod::system::set_fps(FPSCAP);



@@ 313,7 329,7 @@ fn main() {

    let mut objects = [player, NPC];

    let game;
    let mut game;

    if (rand::random())
    {


@@ 324,14 340,28 @@ fn main() {
        game = Game{map: buildCaveMap(&mut objects[0]),};
    }

    for y in 0..MAPHEIGHT
    {
        for x in 0..MAPWIDTH
        {
            tcod.fov.set(x, y, !game.map[x as usize][y as usize].blockSight, !game.map[x as usize][y as usize].blocked,);
        }
    }

    let mut lastPlayerPosition = (-1, -1);

    while(!tcod.root.window_closed())
    {
        tcod.con.clear();
        render(&mut tcod, &game, &objects);
        let fov_recompute = lastPlayerPosition != (objects[0].x, objects[0].y);
        render(&mut tcod, &mut game, &objects, fov_recompute);
        tcod.root.flush();

        let player = &mut objects[0];
        let exit = handleInput(&mut tcod, &game, player);

        lastPlayerPosition = (player.x, player.y);

        let exit = handleInput(&mut tcod, &mut game, player);
        
        if (exit)
        {


@@ 356,28 386,46 @@ fn handleInput(tcod: &mut Tcod, game: &Game, player: &mut Object) -> bool
    false
}

fn render(tcod: &mut Tcod, game: &Game, objects: &[Object])
fn render(tcod: &mut Tcod, game: &mut Game, objects: &[Object], fovRecompute: bool)
{
    if (fovRecompute)
    {
        let player = &objects[0];
        tcod.fov.compute_fov(player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGORITHM);
    }

    for y in 0..MAPHEIGHT
    {
        for x in 0..MAPWIDTH
        {
            let visible = tcod.fov.is_in_fov(x, y);
            let wall = game.map[x as usize][y as usize].blockSight;

            if (wall)
            let colour = match(visible, wall)
            {
                (false, true) => COLOUR_DARK_WALL,
                (false, false) => COLOUR_LIGHT_WALL,
                (true, true) => COLOUR_DARK_GROUND,
                (true, false) => COLOUR_LIGHT_GROUND,
            };

            let explored = &mut game.map[x as usize][y as usize].explored;
            if visible
            {
                tcod.con.set_char_background(x, y, COLOUR_DARK_WALL, BackgroundFlag::Set);
                *explored = true;
            }
            else
            if *explored
            {
                tcod.con.set_char_background(x, y, COLOUR_DARK_GROUND, BackgroundFlag::Set);
                tcod.con.set_char_background(x, y, colour, BackgroundFlag::Set);
            }
        }
    }

    for Object in objects
    {
        Object.draw(&mut tcod.con)
        if (tcod.fov.is_in_fov(Object.x, Object.y))
        {
            Object.draw(&mut tcod.con)
        }
    }

    blit(&tcod.con, (0, 0), (MAPWIDTH, MAPHEIGHT), &mut tcod.root, (0, 0), 1.0, 1.0);