From 0db41de73fb65bdbe2e412f79704cff1874c7312 Mon Sep 17 00:00:00 2001 From: Luis Ferro Date: Mon, 11 Nov 2019 20:02:55 +0100 Subject: Add char generation and positioning in first room of the dungeon --- src/character.rs | 41 ++++++++++++++++++++++++++++++++++++----- src/main.rs | 14 ++++++++++++++ src/world.rs | 15 ++++++++++++++- 3 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/character.rs b/src/character.rs index 04b31a8..76f4414 100644 --- a/src/character.rs +++ b/src/character.rs @@ -1,5 +1,8 @@ use std::cmp; +use crate::world::Point; + +#[derive(Default)] pub struct Character { pub name: String, pub class: String, @@ -8,7 +11,8 @@ pub struct Character { attack: i32, dodge: i32, luck: i32, - xp: i32 + xp: i32, + location: Point } pub trait Player { @@ -18,9 +22,12 @@ pub trait Player { health: i32, attack: i32, dodge: i32, - luck: i32 + luck: i32, + location: Point ) -> Character; + fn place(&mut self, location: Point); + fn select(&self, player_name: String, player_luck: i32) -> Self; fn damage(&mut self, damage_amount: i32); @@ -34,8 +41,16 @@ pub trait Player { fn info(&self) -> String; fn stats(&self) -> String; + + fn walk(&mut self, dir: Direction); } +pub enum Direction { + North, + South, + East, + West +} impl Player for Character { fn new( @@ -44,7 +59,8 @@ impl Player for Character { health: i32, attack: i32, dodge: i32, - luck: i32 + luck: i32, + location: Point ) -> Character { Character { name: name, @@ -55,9 +71,14 @@ impl Player for Character { dodge: dodge, luck: luck, xp: 0, + location: location } } + fn place(&mut self, location: Point) { + self.location = location; + } + fn select(&self, player_name: String, player_luck: i32) -> Self { Self::new( player_name, @@ -65,10 +86,20 @@ impl Player for Character { self.health, self.attack, self.dodge, - self.luck + player_luck + self.luck + player_luck, + (0,0) ) } + fn walk(&mut self, dir: Direction) { + match dir { + Direction::North => { (); }, + Direction::South => { (); }, + Direction::East => { (); }, + Direction::West => { (); } + } + } + fn damage(&mut self, damage_amount: i32) { self.health = cmp::max(0, self.health - damage_amount); self.xp += 2; @@ -109,7 +140,7 @@ mod tests { use super::*; fn test_attack() { - let player = Character::new("".to_string(), "Rogue".to_string(), 1, 4, 1, 4); + let player = Character::new("".to_string(), "Rogue".to_string(), 1, 4, 1, 4, (0,0)); assert_eq!(player.attack(), 6); } diff --git a/src/main.rs b/src/main.rs index 7da68ed..523ef13 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ mod computer; mod world; use character::Player; +use character::Character; use computer::Enemy; use pancurses::{Window, initscr, endwin}; use rand::Rng; @@ -57,6 +58,19 @@ fn main() { let mut world = World::new(80, 24); world.generate(); + let start_location = world.get_start_location(); + + let mut character: Character = Character::new( + "Kshar".to_string(), + "Warror".to_string(), + 30, + 10, + 10, + 20, + start_location + ); + character.place(start_location); + debug_world(&world); // let window = initscr(); diff --git a/src/world.rs b/src/world.rs index 69f3897..d55a9e5 100644 --- a/src/world.rs +++ b/src/world.rs @@ -1,6 +1,6 @@ use rand::Rng; -type Point = (usize, usize); +pub type Point = (usize, usize); #[derive(Clone)] pub enum TileType { @@ -193,7 +193,13 @@ pub trait GameWorld { fn generate(&mut self); +<<<<<<< Updated upstream fn to_tilegrid(&self) -> Result; +======= + fn to_tilegrid(&self) -> TileGrid; + + fn get_start_location(&self) -> Point; +>>>>>>> Stashed changes } fn hor_dist(point1: Point, point2: Point) -> f32 { @@ -331,6 +337,13 @@ impl GameWorld for World { Ok(grid) } + + fn get_start_location(&self) -> Point { + if self.rooms.len() > 0 { + return self.rooms[0].center; + } + return (0,0) + } } #[cfg(test)] -- cgit v1.2.3