Created
January 6, 2013 14:59
-
-
Save supernullset/4467756 to your computer and use it in GitHub Desktop.
MonsterAttack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use core::rand::{Rng}; | |
struct Monster { | |
name: ~str, | |
health: HealthPoints, | |
mut speed: int, | |
mut charisma: int, | |
mut max_attack: int, | |
position: Position | |
} | |
struct HealthPoints { | |
mut points: int | |
} | |
struct Position { | |
area: GameBoard, | |
mut x_axis: int, | |
mut y_axis: int | |
} | |
struct GameBoard { | |
x_cells: int, | |
y_cells: int | |
} | |
trait Attack { | |
fn attack(&self, target: @Monster); | |
} | |
trait Health { | |
fn deduct(&self, val: int); | |
fn add(&self, val: int); | |
fn is_dead(&self) -> bool; | |
} | |
impl Monster: Attack { | |
fn attack(&self, target: @Monster) { | |
let dmg = Rng().gen_int_range(0,self.max_attack); | |
// io::println(fmt!("%s attacks %s for %d damage!!",self.name, target.name, dmg)); | |
target.deduct(dmg); | |
// io::println(fmt!("%s has %d life left!!",target.name, target.health.points)) | |
} | |
} | |
impl Monster: Health { | |
fn deduct(&self, val: int) { | |
let tmp = self.health.points; | |
self.health.points = tmp - val; | |
} | |
fn add(&self, val: int) { | |
let tmp = self.health.points; | |
self.health.points = tmp + val; | |
} | |
fn is_dead(&self) -> bool { | |
if self.health.points < 0 { | |
true | |
} | |
else { | |
false | |
} | |
} | |
} | |
impl GameBoard { | |
static fn new(max_x_cells: int, max_y_cells: int) -> GameBoard { | |
GameBoard { x_cells: max_x_cells, y_cells: max_y_cells } | |
} | |
} | |
impl Position { | |
static fn new(area: GameBoard, x_axis: int, y_axis: int) -> Position { | |
Position {area: area, x_axis: x_axis, y_axis: y_axis} | |
} | |
static fn random_p(level: GameBoard) -> Position { | |
let rand_x = Rng().gen_int_range(0,level.x_cells); | |
let rand_y = Rng().gen_int_range(0,level.y_cells); | |
Position::new(level, rand_x, rand_y) | |
} | |
fn inspect() -> ~str { | |
fmt!("(%d, %d)", self.x_axis, self.y_axis) | |
} | |
fn random_move_y() { | |
if Rng().gen_int() % 2 == 0 { | |
self.y_axis += 1; | |
if self.y_axis > self.area.y_cells { self.y_axis = self.area.y_cells } | |
} else { | |
self.y_axis -= 1; | |
if self.y_axis < 0 { self.y_axis = 0 } | |
} | |
} | |
} | |
impl Monster { | |
static fn new(level: GameBoard, name: ~str, life: int, speed: int, charisma: int, max_attack: int) -> Monster { | |
let p = Position::random_p(level); | |
let l = HealthPoints { points: life }; | |
Monster {name:name, health:l, speed:speed, charisma:charisma, max_attack:max_attack, position: p} | |
} | |
} | |
fn compare(p1: int, p2: int) -> bool { | |
if p1 == p2 { true } | |
else { false } | |
} | |
fn main() { | |
let level_one = GameBoard::new(1,10); | |
let p = @Monster::new(level_one, ~"Phil", 46, 35, 91, 10); | |
let v = @Monster::new(level_one, ~"Veggie Burger", 46, 35, 91, 2); | |
loop { | |
if p.is_dead() || v.is_dead() { break;} | |
p.attack(v); | |
if !v.is_dead() { v.attack(p); } | |
p.position.random_move_y(); | |
v.position.random_move_y(); | |
let s = compare(p.position.y_axis, v.position.y_axis); | |
io::println(fmt!("%b", s)); | |
// io::println(p.position.inspect()); | |
// io::println(v.position.inspect()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment