From d3d47b0cb378220fae47070839bc3a79d787def5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20L=C3=B8vbr=C3=B8tte=20Olsen?= Date: Thu, 9 May 2019 01:16:43 +0200 Subject: [PATCH] add another player --- game/mod.rs | 7 +++---- game/player.rs | 15 +++++++++++++-- main.rs | 7 ++++--- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/game/mod.rs b/game/mod.rs index b6d3daa..28a1e92 100644 --- a/game/mod.rs +++ b/game/mod.rs @@ -1,4 +1,4 @@ -mod player; +pub mod player; mod star; mod carrier; @@ -14,9 +14,8 @@ pub struct Game { } impl Game { - pub fn addPlayer(&mut self, name: String) { - let mut player: Player = Player::default(); - player.name = name; + pub fn addPlayer(&mut self, name: String, color: Option<(u8,u8,u8)>, race: Option) { + let player = Player::new(name, color, race); self.players.push(player); } } \ No newline at end of file diff --git a/game/player.rs b/game/player.rs index 10ea7c2..796f3ac 100644 --- a/game/player.rs +++ b/game/player.rs @@ -1,5 +1,5 @@ #[derive(Debug)] -enum Race { +pub enum Race { Griffin, Catte } @@ -36,7 +36,7 @@ struct Research { #[derive(Debug)] pub struct Player { - pub name: String, + name: String, color: (u8, u8, u8), race: Race, research: Research @@ -51,4 +51,15 @@ impl Default for Player { research: Research::default() } } +} + +impl Player { + pub fn new(name: String, color: Option<(u8, u8, u8)>, race: Option) -> Player { + Player { + name: name, + color: color.unwrap_or(Player::default().color), + race: race.unwrap_or(Player::default().race), + research: Research::default() + } + } } \ No newline at end of file diff --git a/main.rs b/main.rs index 935d2d5..955e00c 100644 --- a/main.rs +++ b/main.rs @@ -24,7 +24,8 @@ fn world() -> &'static str { fn test() -> String { let mut game: Game = Game::default(); - game.addPlayer("Daniel".to_string()); - - format!("{:?}", game) + game.addPlayer("Daniel".to_string(), Option::Some((255, 0, 0)), Option::Some(game::player::Race::Catte)); + game.addPlayer("Torpus".to_string(), Option::Some((0, 255, 0)), Option::Some(game::player::Race::Griffin)); + + format!("{:#?}", game) } \ No newline at end of file