diff options
author | Guillaume Pasquet <dev@etenil.net> | 2019-07-05 21:03:55 +0100 |
---|---|---|
committer | Guillaume Pasquet <dev@etenil.net> | 2019-07-05 21:03:55 +0100 |
commit | 7abfab104ffa052d885eb7d333f0569dc61ab74c (patch) | |
tree | 6ee0a9d1743de49cbcb36b2fdc6e6e9024013f33 /src |
Start generating game world
Diffstat (limited to 'src')
-rw-r--r-- | src/character.rs | 116 | ||||
-rw-r--r-- | src/computer.rs | 36 | ||||
-rw-r--r-- | src/main.rs | 70 | ||||
-rw-r--r-- | src/world.rs | 79 |
4 files changed, 301 insertions, 0 deletions
diff --git a/src/character.rs b/src/character.rs new file mode 100644 index 0000000..04b31a8 --- /dev/null +++ b/src/character.rs @@ -0,0 +1,116 @@ +use std::cmp;
+
+pub struct Character {
+ pub name: String,
+ pub class: String,
+ pub health: i32,
+ max_health: i32,
+ attack: i32,
+ dodge: i32,
+ luck: i32,
+ xp: i32
+}
+
+pub trait Player {
+ fn new(
+ name: String,
+ class: String,
+ health: i32,
+ attack: i32,
+ dodge: i32,
+ luck: i32
+ ) -> Character;
+
+ fn select(&self, player_name: String, player_luck: i32) -> 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;
+}
+
+
+impl Player for Character {
+ fn new(
+ name: String,
+ class: String,
+ health: i32,
+ attack: i32,
+ dodge: i32,
+ luck: i32
+ ) -> Character {
+ Character {
+ name: name,
+ class: class,
+ max_health: health,
+ health: health,
+ attack: attack,
+ dodge: dodge,
+ luck: luck,
+ xp: 0,
+ }
+ }
+
+ fn select(&self, player_name: String, player_luck: i32) -> Self {
+ Self::new(
+ player_name,
+ self.class.to_string(),
+ self.health,
+ self.attack,
+ self.dodge,
+ self.luck + player_luck
+ )
+ }
+
+ fn damage(&mut self, damage_amount: i32) {
+ self.health = cmp::max(0, self.health - damage_amount);
+ self.xp += 2;
+ }
+
+ fn heal(&mut self, heal_amount: i32) {
+ if (self.health) <= self.max_health {
+ self.health = cmp::min(self.health + heal_amount, self.max_health);
+ self.xp += 1;
+ }
+ }
+
+ fn attack(&self) -> i32 {
+ self.xp + self.attack + self.luck / 2
+ }
+
+ fn dodge(&self) -> i32 {
+ 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: {}",
+ self.class, self.health, self.attack, self.dodge, self.luck, self.xp
+ )
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn test_attack() {
+ let player = Character::new("".to_string(), "Rogue".to_string(), 1, 4, 1, 4);
+
+ assert_eq!(player.attack(), 6);
+ }
+}
diff --git a/src/computer.rs b/src/computer.rs new file mode 100644 index 0000000..a8b9b3e --- /dev/null +++ b/src/computer.rs @@ -0,0 +1,36 @@ +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/main.rs b/src/main.rs new file mode 100644 index 0000000..65ada01 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,70 @@ +extern crate rand; +extern crate pancurses; + +#[macro_use] +extern crate text_io; + +mod character; +mod computer; +mod world; + +use character::Player; +use computer::Enemy; +use pancurses::{initscr, endwin}; +use rand::Rng; +use std::io; + +fn main() { + let window = initscr(); + window.printw("Hello Rust"); + window.refresh(); + window.mv(2, 2); + window.printw("toto"); + window.refresh(); + window.getch(); + endwin(); +} + // println!( + // "=== Welcome to RRL {} the {}! ===\n", + // env!("CARGO_PKG_DESCRIPTION"), env!("CARGO_PKG_VERSION") + // ); + + // let characters: [character::Character; 5] = [ + // character::Character::new("".to_string(), "Cleric".to_string(), 7, 5, 6, 7), + // character::Character::new("".to_string(), "Warrior".to_string(), 10, 5, 5, 5), + // character::Character::new("".to_string(), "Hunter".to_string(), 5, 7, 7, 6), + // character::Character::new("".to_string(), "Wizard".to_string(), 3, 10, 5, 7), + // character::Character::new("".to_string(), "Thief".to_string(), 4, 5, 6, 10), + // ]; + + // let _luck_amount = rand::thread_rng().gen_range(2, 6); + + // println!("You enter the Ephemeral Plane of Creation..."); + // println!("Please enter your name."); + + // let mut input_text = String::new(); + + // io::stdin() + // .read_line(&mut input_text) + // .expect("Failed to read line"); + // let _character_name = input_text.trim(); + + // println!("Please select your character type:"); + // for (i, elem) in characters.iter().enumerate() { + // print!("\n{}. {}\n\n", i + 1, elem.info()); + // } + + // let mut character_index: usize = 100; + // while character_index > characters.len() { + // character_index = read!(); + // } + + // let mut player = characters[character_index].select(_character_name.to_string(), _luck_amount); + + // play(&mut player); +// } + +fn play(player: &mut character::Character) { + println!("=== Welcome to RRL {} the {}! ===\n", player.name, player.class); + println!("Your unique stats: {}", player.stats()); +} diff --git a/src/world.rs b/src/world.rs new file mode 100644 index 0000000..fe89c3d --- /dev/null +++ b/src/world.rs @@ -0,0 +1,79 @@ +pub enum BlockType {
+ Nothing,
+ Wall,
+ Floor,
+}
+
+pub struct World {
+ size: usize,
+ world: Vec<Vec<BlockType>>
+}
+
+pub trait GameWorld<'a> {
+ fn new(size: usize) -> Self;
+
+ fn generate(&mut self);
+
+ fn get_world(&'a self) -> &'a Vec<Vec<BlockType>>;
+
+ fn get_item(&'a self, x: usize, y: usize) -> &'a BlockType;
+}
+
+impl World {
+ fn make_corridor(&mut self, start: (usize, usize), end: (usize, usize)) {
+ for x in start.0..end.0 {
+ for y in start.1..end.1 {
+ self.world[x - 1][y] = BlockType::Wall;
+ self.world[x][y] = BlockType::Floor;
+ self.world[x + 1][y] = BlockType::Wall;
+ }
+ }
+ }
+}
+
+impl<'a> GameWorld<'a> for World {
+ fn new(size: usize) -> World {
+ World {
+ size: size,
+ world: Vec::with_capacity(size)
+ }
+ }
+
+ fn generate(&mut self) {
+ for _ in 0..self.size {
+ let mut subvec = Vec::with_capacity(self.size);
+ for _ in 0..self.size {
+ subvec.push(BlockType::Nothing);
+ }
+ self.world.push(subvec);
+ }
+
+ self.make_corridor((1, 10), (1, 13))
+ }
+
+ fn get_world(&'a self) -> &'a Vec<Vec<BlockType>> {
+ &self.world
+ }
+
+ fn get_item(&'a self, x: usize, y: usize) -> &'a BlockType {
+ &self.world[x][y]
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_generates_world() {
+ let mut world = World::new(128);
+ world.generate();
+
+ assert_eq!(world.get_world().len(), 128);
+ assert_eq!(world.get_world()[0].len(), 128);
+ match world.get_world()[0][0] {
+ BlockType::Nothing => assert!(true),
+ _ => assert!(false)
+ }
+ }
+}
|