Skip to content

Instantly share code, notes, and snippets.

@MarkJr94
Created June 28, 2013 21:11
Show Gist options
  • Save MarkJr94/5888147 to your computer and use it in GitHub Desktop.
Save MarkJr94/5888147 to your computer and use it in GitHub Desktop.
// mummy.rs
use pc::PC;
use common::{Character};
#[deriving(ToStr)]
pub struct Mummy {
hp: uint,
attack: uint,
x: int,
y: int
}
impl Character for Mummy {
pub fn x(&self) -> int {
self.x
}
pub fn y(&self) -> int {
self.y
}
pub fn set_x(&mut self, new_x: int) {
self.x = new_x;
}
pub fn set_y(&mut self, new_y:int) {
self.y = new_y;
}
pub fn translate(&mut self, dx: int, dy: int) {
self.x += dx;
self.y += dy;
}
pub fn new(x: int, y: int) -> Mummy {
Mummy {hp: 5u, attack: 3u, x: x, y: y }
}
pub fn attack(&mut self, player: &mut Character) -> uint {
let op_hp = player.hp_mut();
*op_hp -= self.attack;
self.attack
}
pub fn name(&self) -> ~str {
~"Mummy"
}
pub fn rest(&mut self) { }
pub fn as_str(&self) -> ~str {
self.to_str()
}
pub fn hp(&self) -> uint {
self.hp
}
pub fn hp_mut<'r>(&'r mut self) -> &'r mut uint {
&'r mut self.hp
}
pub fn stamina(&self) -> uint {
100
}
}
// Compiler error
monster/mummy.rs:39:21: 39:27 error: cannot borrow immutable argument as mutable
monster/mummy.rs:39 let op_hp = player.hp_mut();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment