|
| 1 | +extern crate dwemthys; |
| 2 | +extern crate tcod; |
| 3 | + |
| 4 | +use std::io; |
| 5 | +use std::rand::Rng; |
| 6 | + |
| 7 | +use dwemthys::monster::Monster; |
| 8 | +use dwemthys::player::Player; |
| 9 | +use dwemthys::vm::VM; |
| 10 | +use tcod::{Console, background_flag, key_code, Special}; |
| 11 | + |
| 12 | +struct Game { |
| 13 | + monsters: Vec<Monster>, |
| 14 | + player: Player, |
| 15 | + monster: Option<Monster>, |
| 16 | + player_weapon: int, |
| 17 | + over: bool |
| 18 | +} |
| 19 | + |
| 20 | +impl Game { |
| 21 | + fn new() -> Game { |
| 22 | + let monsters: Vec<Monster> = vec![ |
| 23 | + Monster::dragon(), |
| 24 | + Monster::cyclist(), |
| 25 | + Monster::deer(), |
| 26 | + Monster::ombudsman(), |
| 27 | + Monster::angel(), |
| 28 | + Monster::monkey() |
| 29 | + ]; |
| 30 | + let player = Player::rabbit(); |
| 31 | + |
| 32 | + Game { monsters: monsters, player: player, monster: None, player_weapon: -1, over: false } |
| 33 | + } |
| 34 | + |
| 35 | + fn introduce_foe(&mut self) { |
| 36 | + let should_get_new = match self.monster { |
| 37 | + Some(ref m) => m.life < 1, |
| 38 | + None => true |
| 39 | + }; |
| 40 | + if should_get_new { |
| 41 | + let opt : Option<Monster> = self.monsters.pop(); |
| 42 | + self.monster = opt.clone(); |
| 43 | + let name = match opt { |
| 44 | + Some(monster) => monster.name, |
| 45 | + None => { self.over = true; return } |
| 46 | + }; |
| 47 | + |
| 48 | + println!("[Get ready. {:s} has emerged.]", name) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + fn process_input(&mut self) { |
| 53 | + self.player_weapon = -1; |
| 54 | + println!("How would you like to attack? Boomerang, Hero's Sword, Lettuce or Bombs?"); |
| 55 | + println!("^ for Boomerang, / for Sword, % for Lettuce, * for Bombs or h for an |
| 56 | + explanation."); |
| 57 | + |
| 58 | + let mut reader = io::stdin(); |
| 59 | + let input = reader.read_line().ok().expect("Failed to read line."); |
| 60 | + let input_char: Option<String> = from_str(input.as_slice().trim()); |
| 61 | + |
| 62 | + let command = match input_char { |
| 63 | + Some(string) => string, |
| 64 | + None => fail!("fuck") |
| 65 | + }; |
| 66 | + let sword_dmg = match self.monster.clone() { |
| 67 | + Some(monster) => (4 + ((monster.life % 10) ^ 2)), |
| 68 | + None => fail!("Game over!") |
| 69 | + }; |
| 70 | + |
| 71 | + if command == "*".to_string() { |
| 72 | + if self.player.bombs > 0 { |
| 73 | + self.player_weapon = 86; |
| 74 | + self.player.bombs -= 1; |
| 75 | + } else { |
| 76 | + println!("You're out of bombs!"); |
| 77 | + } |
| 78 | + } else if command == "/".to_string() { |
| 79 | + self.player_weapon = sword_dmg; |
| 80 | + } else if command == "%".to_string() { |
| 81 | + self.player.lettuce(); |
| 82 | + self.player_weapon = 0; |
| 83 | + } else if command == "^".to_string() { |
| 84 | + self.player_weapon = 13; |
| 85 | + } else if command == "h".to_string() { |
| 86 | + println!("Your weapons: |
| 87 | +^ (Boomerang) has strength 13 |
| 88 | +/ (Hero's Sword) has variable strength based on your opponent ({:d}) |
| 89 | +% (Lettuce) Lettuce will build your strength and extra ruffage will fly in the face of your opponent!! |
| 90 | +* (Bomb) You only have {:d} left!", 1i, self.player.bombs); |
| 91 | + } else { |
| 92 | + println!("That didn't make any sense!"); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + fn display_intro(&self) { |
| 97 | + println!("A scalding SEETHING LAVA infiltrates the cacauphonous ENGORGED MINESHAFTS deep |
| 98 | +within the ageless canopy of the DWEMTHY FOREST... chalky and nocturnal screams from the |
| 99 | +belly of the RAVENOUS WILD STORKUPINE... who eats wet goslings RIGHT AFTER they've had a |
| 100 | +few graham crackers and a midday nap... amidst starved hippos TECHNICALLY ORPHANED but |
| 101 | +truthfully sheltered by umbrellas owned jointly by car dealership conglomerates... beneath |
| 102 | +uncapped vials of mildly pulpy BLUE ELIXIR... which shall remain... heretofore... |
| 103 | +UNDISTURBED... DWEMTHY!!!"); |
| 104 | + |
| 105 | + println!("You have six foes."); |
| 106 | + |
| 107 | + println!("These are the living, breathing monstrosities of Dwemthy's Array. I don't know |
| 108 | +how they got there. No one knows. Actually, I'm guessing the IntrepidDecomposedCyclist rode |
| 109 | +his ten-speed. But the others: NO ONE knows. |
| 110 | +
|
| 111 | +If it's really important for you to know, let's just say the others were born there. Can we |
| 112 | +move on?? |
| 113 | +
|
| 114 | +As Dwemthy's Array gets deeper, the challenge becomes more difficult."); |
| 115 | + |
| 116 | + println!("Fight the Array and the monsters will appear as you go. Godspeed and may you |
| 117 | +return with harrowing tales and nary an angel talon piercing through your shoulder. |
| 118 | +
|
| 119 | +Oh, and none of this \"I'm too young to die\" business. I'm sick of that crap. I'm not going |
| 120 | +to have you insulting our undead young people. They are our future. After our future is |
| 121 | +over, that is."); |
| 122 | + |
| 123 | + println!("\nPrepare for battle!\n\n=================\n\n") |
| 124 | + } |
| 125 | + |
| 126 | + fn update_game(&mut self) { |
| 127 | + let weapon = self.player_weapon; |
| 128 | + { |
| 129 | + let monster = match self.monster { |
| 130 | + Some(ref mut monster) => monster, |
| 131 | + None => { self.over = true; return } |
| 132 | + }; |
| 133 | + self.player.fight(monster, weapon); |
| 134 | + } |
| 135 | + if self.player.life < 1i { |
| 136 | + self.revive_rabbit(); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + fn revive_rabbit(&mut self) { |
| 141 | + println!("You have fallen in battle, but another foolish young rabbit rushes to take your |
| 142 | + place!"); |
| 143 | + self.player = Player::rabbit(); |
| 144 | + } |
| 145 | + |
| 146 | + fn render(&self) { |
| 147 | + println!("You have {:d} health!!", self.player.life); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +fn main() { |
| 152 | + let mut con = Console::init_root(80, 50, "libtcod Rust tutorial", false); |
| 153 | + let mut exit = false; |
| 154 | + while !(Console::window_closed() || exit) { |
| 155 | + con.clear(); |
| 156 | + con.put_char(40, 25, '@', background_flag::Set); |
| 157 | + con.flush(); |
| 158 | + let keypress = con.wait_for_keypress(true); |
| 159 | + match keypress.key { |
| 160 | + Special(key_code::Escape) => exit = true, |
| 161 | + _ => {} |
| 162 | + } |
| 163 | + } |
| 164 | + //let n : Option<u8> = from_str("05"); |
| 165 | + //let num : u8 = match n { |
| 166 | + //Some(nu) => nu, |
| 167 | + //None => fail!("couldn't parse") |
| 168 | + //}; |
| 169 | + //let array = [0x05, 0x10, 0x00, 0x05, 10u8, 0x01, num, 25u8]; |
| 170 | + //let mut vm = VM::new(); |
| 171 | + //vm.interpret(array); |
| 172 | + //let mut game = Game::new(); |
| 173 | + //game.display_intro(); |
| 174 | + //game.introduce_foe(); |
| 175 | + |
| 176 | + //loop { |
| 177 | + //&mut game.process_input(); |
| 178 | + //if game.player_weapon != -1 { |
| 179 | + //&mut game.update_game(); |
| 180 | + //&mut game.introduce_foe(); |
| 181 | + //game.render(); |
| 182 | + //} |
| 183 | + //if game.over { break; } |
| 184 | + //} |
| 185 | +} |
0 commit comments