aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuis Ferro <luis.ferro@eggplant.io>2019-11-12 14:28:32 +0100
committerLuis Ferro <luis.ferro@eggplant.io>2019-11-12 14:28:32 +0100
commit857d7a8eccec2493163dbb1ae2f45a24a52f108c (patch)
tree5c6837e528b56d9f00dc637fdf1edb962e1b0987
parente7b227ed268e8570518f9d1452b5e7d0d0a0ee32 (diff)
parent8fa3fa881bc3b954e136295fe6cc7022737ae9db (diff)
Fix conflicts
-rw-r--r--src/computer.rs36
-rw-r--r--src/entities.rs (renamed from src/character.rs)101
-rw-r--r--src/main.rs24
-rw-r--r--src/tiling.rs52
-rw-r--r--src/world.rs73
5 files changed, 145 insertions, 141 deletions
diff --git a/src/computer.rs b/src/computer.rs
deleted file mode 100644
index a8b9b3e..0000000
--- a/src/computer.rs
+++ /dev/null
@@ -1,36 +0,0 @@
-pub struct Computer {
- level: i32,
- difficulty: i32,
-}
-
-pub trait Enemy {
- fn new(level: i32, difficulty: i32) -> Self;
-
- fn action(&self) -> (i32, i32);
-
- fn level_up(&mut self);
-
- fn stats(&self) -> String;
-}
-
-impl Enemy for Computer {
- fn new(level: i32, difficulty: i32) -> Computer {
- Computer {
- level: level,
- difficulty: difficulty
- }
- }
-
- fn action(&self) -> (i32, i32) {
- (self.level, self.difficulty)
- }
-
- fn level_up(&mut self) {
- self.level += 1;
- self.difficulty += 3;
- }
-
- fn stats(&self) -> String {
- format!("level: {} difficulty: {}", self.level, self.difficulty)
- }
-}
diff --git a/src/character.rs b/src/entities.rs
index a62c51f..c3ec262 100644
--- a/src/character.rs
+++ b/src/entities.rs
@@ -1,7 +1,14 @@
use std::cmp;
-use crate::world::Point;
+use crate::world::{Point, Direction};
+use crate::tiling::TileType;
+pub trait Entity {
+ fn info(&self) -> String;
+ fn place(&mut self, location: Point);
+}
+
+#[derive(Clone)]
pub struct Character {
pub name: String,
pub class: String,
@@ -13,11 +20,11 @@ pub struct Character {
dodge: i32,
luck: i32,
xp: i32,
+ tile_type: TileType
}
-pub trait Player {
+pub trait Enemy {
fn new(
- name: String,
class: String,
health: i32,
attack: i32,
@@ -25,32 +32,68 @@ pub trait Player {
luck: i32,
level: i32,
location: Point
- ) -> Character;
-
- fn place(&mut self, location: Point);
+ ) -> Self;
- fn select(&self, player_name: String, player_luck: i32) -> Self;
+ fn set_tile_type(&mut self, tile_type: TileType);
+}
+pub trait Player {
+ fn new(
+ name: String,
+ class: String,
+ health: i32,
+ attack: i32,
+ dodge: i32,
+ luck: i32,
+ location: Point
+ ) -> Self;
fn damage(&mut self, damage_amount: i32);
-
fn heal(&mut self, heal_amount: i32);
-
fn attack(&self) -> i32;
-
fn dodge(&self) -> i32;
-
- fn info(&self) -> String;
-
fn stats(&self) -> String;
-
fn walk(&mut self, dir: Direction);
}
-pub enum Direction {
- North,
- South,
- East,
- West
+impl Entity for Character {
+ fn place(&mut self, location: Point) {
+ self.location = location;
+ }
+
+ fn info(&self) -> String {
+ format!(
+ "{} \thp: {} attack: {} dodge: {} luck: {}",
+ self.class, self.health, self.attack, self.dodge, self.luck
+ )
+ }
+}
+
+impl Enemy for Character {
+ fn new(
+ class: String,
+ health: i32,
+ attack: i32,
+ dodge: i32,
+ luck: i32,
+ location: Point
+ ) -> Character {
+ Character {
+ name: class.clone(),
+ class: class.clone(),
+ max_health: health,
+ health,
+ attack,
+ dodge,
+ luck,
+ xp: 0,
+ location: location,
+ tile_type: TileType::Character
+ }
+ }
+
+ fn set_tile_type(&mut self, tile_type: TileType) {
+ self.tile_type = tile_type
+ }
}
impl Player for Character {
@@ -62,7 +105,8 @@ impl Player for Character {
dodge: i32,
luck: i32,
level: i32,
- location: Point
+ location: Point,
+ tile_type: TileType
) -> Character {
Character {
name: name,
@@ -74,7 +118,8 @@ impl Player for Character {
luck: luck,
xp: 0,
level: 0,
- location: location
+ location: location,
+ tile_type: TileType::Player
}
}
@@ -95,6 +140,11 @@ impl Player for Character {
)
}
+ location: location,
+ tile_type: TileType::Player
+ }
+ }
+
fn walk(&mut self, dir: Direction) {
match dir {
Direction::North => { (); },
@@ -124,13 +174,6 @@ impl Player for Character {
self.xp + self.dodge + self.luck / 2
}
- fn info(&self) -> String {
- format!(
- "{} \thp: {} attack: {} dodge: {} luck: {}",
- self.class, self.health, self.attack, self.dodge, self.luck
- )
- }
-
fn stats(&self) -> String {
format!(
"{} - hp: {} attack: {} dodge: {} luck: {} experience: {}",
@@ -144,7 +187,7 @@ mod tests {
use super::*;
fn test_attack() {
- let player = Character::new("".to_string(), "Rogue".to_string(), 1, 4, 1, 4, 0,(0,0));
+ let player: Character = Player::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 08e1b25..9f5e45b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,16 +4,14 @@ extern crate pancurses;
#[macro_use]
extern crate text_io;
-mod character;
-mod computer;
+mod entities;
mod world;
+mod tiling;
-use std::env;
-use character::Player;
-use character::Character;
-use computer::Enemy;
+use entities::{Character, Player, Entity};
use pancurses::{Window, initscr, endwin, Input, noecho};
-use world::{Dungeon, Level, Generable, TileType};
+use world::{Dungeon, Level, Generatable};
+use tiling::TileType;
fn tile_to_str(tile: &TileType) -> &str {
match tile {
@@ -42,18 +40,6 @@ fn render_level(window: &Window, level: &Level) {
}
}
-fn debug_level(level: Level) {
- let grid = level.to_tilegrid().unwrap();
-
- for line in grid.raw_data().iter() {
- for block in line.iter() {
- print!("{}", tile_to_str(block));
- }
- print!("\n");
- }
-}
-
-
fn main() {
let window = initscr();
let mut level = 0;
diff --git a/src/tiling.rs b/src/tiling.rs
new file mode 100644
index 0000000..139af24
--- /dev/null
+++ b/src/tiling.rs
@@ -0,0 +1,52 @@
+pub struct TileGrid {
+ grid: Vec<Vec<TileType>>
+}
+
+impl<'a> TileGrid {
+ pub fn new(xsize: usize, ysize: usize) -> TileGrid {
+ let mut grid = TileGrid {
+ grid: Vec::with_capacity(ysize)
+ };
+
+ for _ in 0..ysize {
+ let mut subvec = Vec::with_capacity(xsize);
+ for _ in 0..xsize {
+ subvec.push(TileType::Empty);
+ }
+ grid.grid.push(subvec);
+ }
+
+ return grid;
+ }
+
+ pub fn set_tile(&mut self, x: usize, y: usize, tile: TileType) {
+ self.grid[y][x] = tile;
+ }
+
+ /// Sets a tile if nothing lies underneath it.
+ pub fn set_empty_tile(&mut self, x: usize, y: usize, tile: TileType) {
+ self.set_tile(x, y, match self.grid[y][x] {
+ TileType::Empty => tile,
+ _ => self.grid[y][x].clone()
+ })
+ }
+
+ pub fn raw_data(&'a self) -> &'a Vec<Vec<TileType>> {
+ &self.grid
+ }
+}
+
+pub trait Tileable {
+ fn tile(&self, grid: &mut TileGrid) -> Result<(), String>;
+}
+
+#[derive(Clone)]
+pub enum TileType {
+ Empty,
+ Wall,
+ Floor,
+ StairsUp,
+ StairsDown,
+ Character,
+ Player
+}
diff --git a/src/world.rs b/src/world.rs
index 2fa72ae..abaffa1 100644
--- a/src/world.rs
+++ b/src/world.rs
@@ -1,26 +1,21 @@
use rand::Rng;
+use crate::entities::{Entity};
+use crate::tiling::{TileGrid, Tileable, TileType};
+
+pub enum Direction {
+ North,
+ South,
+ East,
+ West
+}
pub type Point = (usize, usize);
-#[derive(Clone)]
-pub enum TileType {
- Empty,
- Wall,
- Floor,
- StairsUp,
- StairsDown,
- Character,
-}
-
enum CorridorType {
Horizontal,
Vertical
}
-trait Tileable {
- fn tile(&self, grid: &mut TileGrid) -> Result<(), String>;
-}
-
const LEFT: (i8, i8) = (-1i8, 0);
const RIGHT: (i8, i8) = (1i8, 0);
const UP: (i8, i8) = (0, -1i8);
@@ -144,49 +139,12 @@ impl Tileable for Corridor {
}
}
-pub struct TileGrid {
- grid: Vec<Vec<TileType>>
-}
-
-impl<'a> TileGrid {
- pub fn new(xsize: usize, ysize: usize) -> TileGrid {
- let mut grid = TileGrid {
- grid: Vec::with_capacity(ysize)
- };
-
- for _ in 0..ysize {
- let mut subvec = Vec::with_capacity(xsize);
- for _ in 0..xsize {
- subvec.push(TileType::Empty);
- }
- grid.grid.push(subvec);
- }
-
- return grid;
- }
-
- fn set_tile(&mut self, x: usize, y: usize, tile: TileType) {
- self.grid[y][x] = tile;
- }
-
- /// Sets a tile if nothing lies underneath it.
- fn set_empty_tile(&mut self, x: usize, y: usize, tile: TileType) {
- self.set_tile(x, y, match self.grid[y][x] {
- TileType::Empty => tile,
- _ => self.grid[y][x].clone()
- })
- }
-
- pub fn raw_data(&'a self) -> &'a Vec<Vec<TileType>> {
- &self.grid
- }
-}
-
pub struct Level {
xsize: usize,
ysize: usize,
rooms: Vec<Room>,
corridors: Vec<Corridor>,
+ entities: Vec<Box<dyn Entity>>,
entrance: Point,
exit: Point
}
@@ -198,7 +156,7 @@ pub struct Dungeon {
pub levels: Vec<Level>
}
-pub trait Generable {
+pub trait Generatable {
fn generate(&mut self);
}
@@ -230,7 +188,7 @@ impl Dungeon {
}
}
-impl Generable for Dungeon {
+impl Generatable for Dungeon {
fn generate(&mut self) {
let mut level = Level::new(self.xsize, self.ysize, None);
level.generate();
@@ -254,8 +212,9 @@ impl Level {
Level {
xsize,
ysize,
- rooms: Vec::new(),
- corridors: Vec::new(),
+ rooms: vec![],
+ corridors: vec![],
+ entities: vec![],
entrance: match start {
Some(st) => st,
None => (0, 0)
@@ -357,7 +316,7 @@ impl Level {
}
}
-impl Generable for Level {
+impl Generatable for Level {
fn generate(&mut self) {
let mut rng = rand::thread_rng();
let room_number = rng.gen_range(3, 5);