Skip to content

Commit 1c87c61

Browse files
committed
lifetimes!
1 parent da5c32e commit 1c87c61

File tree

4 files changed

+34
-32
lines changed

4 files changed

+34
-32
lines changed

src/actor.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,25 @@ use rendering::renderers::{Color};
77
use rendering::windows::Windows;
88
use movement::{AggroMovementComponent, RandomMovementComponent, UserMovementComponent, MovementComponent};
99

10-
pub struct Actor<'a> {
10+
pub struct Actor {
1111
pub position: Point,
1212
pub display_char: char,
13-
pub movement_component: Box<MovementComponent + 'a>,
13+
pub movement_component: Box<MovementComponent + 'static>,
1414
pub is_pc: bool,
1515
pub foreground: Color,
1616
pub background: Color,
1717
pub health: u8
1818
}
1919

20-
impl<'a> Actor<'a> {
21-
pub fn new(x: i32, y: i32, dc: char, mc: Box<MovementComponent + 'a>, is_pc: bool, foreground: Color, background: Color, health: u8) -> Actor<'a> {
20+
impl Clone for Actor {
21+
pub fn clone(&self) -> Actor {
22+
let mc = self.movement_component.box_clone();
23+
Actor::new(self.position.x, self.position.y, self.display_char, mc, self.is_pc, self.foreground, self.background, self.health)
24+
}
25+
}
26+
27+
impl Actor {
28+
pub fn new(x: i32, y: i32, dc: char, mc: Box<MovementComponent + 'static>, is_pc: bool, foreground: Color, background: Color, health: u8) -> Actor {
2229
Actor {
2330
position: Point { x: x, y: y },
2431
display_char: dc,
@@ -30,30 +37,25 @@ impl<'a> Actor<'a> {
3037
}
3138
}
3239

33-
pub fn clone(&'a self) -> Actor<'a> {
34-
let mc = self.movement_component.box_clone();
35-
Actor::new(self.position.x, self.position.y, self.display_char, mc, self.is_pc, self.foreground, self.background, self.health)
36-
}
37-
38-
pub fn dog(x: i32, y: i32, move_info: Rc<RefCell<MoveInfo>>) -> Actor<'a> {
40+
pub fn dog(x: i32, y: i32, move_info: Rc<RefCell<MoveInfo>>) -> Actor {
3941
let mc : Box<RandomMovementComponent> = box MovementComponent::new(move_info);
4042
Actor::new(x, y, 'd', mc, false, Color::White, Color::Black, 20u8)
4143
}
4244

43-
pub fn cat(x: i32, y: i32, move_info: Rc<RefCell<MoveInfo>>) -> Actor<'a> {
45+
pub fn cat(x: i32, y: i32, move_info: Rc<RefCell<MoveInfo>>) -> Actor {
4446
let mc : Box<RandomMovementComponent> = box MovementComponent::new(move_info);
4547
Actor::new(x, y, 'c', mc, false, Color::White, Color::Black, 20u8)
4648
}
4749

48-
pub fn heroine(move_info: Rc<RefCell<MoveInfo>>) -> Actor<'a> {
50+
pub fn heroine(move_info: Rc<RefCell<MoveInfo>>) -> Actor {
4951
let point = {
5052
move_info.borrow().deref().char_location
5153
};
5254
let mc : Box<UserMovementComponent> = box MovementComponent::new(move_info);
5355
Actor::new(point.x, point.y, '@', mc, true, Color::Blue, Color::Black, 20u8)
5456
}
5557

56-
pub fn kobold(x: i32, y: i32, move_info: Rc<RefCell<MoveInfo>>) -> Actor<'a> {
58+
pub fn kobold(x: i32, y: i32, move_info: Rc<RefCell<MoveInfo>>) -> Actor {
5759
let mc : Box<AggroMovementComponent> = box MovementComponent::new(move_info);
5860
Actor::new(x, y, 'k', mc, false, Color::Red, Color::Black, 20u8)
5961
}

src/game.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub struct Game<'a, 'b> {
5151
pub rendering_component: Box<RenderingComponent + 'a>,
5252
pub game_state: Box<GameState<'a> + 'a>,
5353
pub windows: Windows<'a>,
54-
pub maps: Maps<'b>
54+
pub maps: Maps
5555
}
5656

5757
impl<'a, 'b> Game<'a, 'b> {

src/game_states.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub trait GameState<'a> {
1515
fn enter(&self, &mut Windows) {}
1616
fn exit(&self) {}
1717

18-
fn update<'b>(&mut self, maps: &'b mut Maps<'b>, windows: &mut Windows, Rc<RefCell<MoveInfo>>);
18+
fn update(&mut self, maps: &mut Maps, windows: &mut Windows, Rc<RefCell<MoveInfo>>);
1919
fn render<'r>(&mut self, renderer: &mut Box<RenderingComponent>, maps: &mut Maps, windows: &'r mut Windows<'r>) {
2020
renderer.before_render_new_frame();
2121
let mut all_windows = windows.all_windows();
@@ -48,7 +48,7 @@ impl<'a> GameState<'a> for MovementGameState {
4848
windows.input.flush_buffer();
4949
}
5050

51-
fn update<'b>(&mut self, maps: &'b mut Maps<'b>, windows: &mut Windows, move_info: Rc<RefCell<MoveInfo>>) {
51+
fn update(&mut self, maps: &mut Maps, windows: &mut Windows, move_info: Rc<RefCell<MoveInfo>>) {
5252
let last_keypress = {
5353
move_info.borrow().deref().last_keypress
5454
};
@@ -102,7 +102,7 @@ impl<'a> GameState<'a> for AttackInputGameState<'a> {
102102
windows.input.buffer_message(msg.as_slice())
103103
}
104104

105-
fn update<'b>(&mut self, maps: &'b mut Maps<'b>, windows: &mut Windows, move_info: Rc<RefCell<MoveInfo>>) {
105+
fn update(&mut self, maps: &mut Maps, windows: &mut Windows, move_info: Rc<RefCell<MoveInfo>>) {
106106
let last_keypress = {
107107
move_info.borrow().deref().last_keypress
108108
};

src/map.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ use rendering::windows::Windows;
77
use rendering::renderers::RenderingComponent;
88
use game::MoveInfo;
99

10-
pub struct Maps<'a> {
11-
pub terrain: Box<Map<'a>>,
12-
pub enemies: Box<Map<'a>>,
13-
pub friends: Box<Map<'a>>,
14-
pub pcs: Box<Map<'a>>
10+
pub struct Maps {
11+
pub terrain: Box<Map>,
12+
pub enemies: Box<Map>,
13+
pub friends: Box<Map>,
14+
pub pcs: Box<Map>
1515
}
1616

17-
impl<'a> Maps<'a> {
18-
pub fn new(move_info: Rc<RefCell<MoveInfo>>) -> Maps<'a> {
17+
impl Maps {
18+
pub fn new(move_info: Rc<RefCell<MoveInfo>>) -> Maps {
1919
let terrain = box Map::new(move_info.clone());
2020
let enemies = box Map::new(move_info.clone());
2121
let friends = box Map::new(move_info.clone());
@@ -29,7 +29,7 @@ impl<'a> Maps<'a> {
2929
}
3030
}
3131

32-
pub fn update(&'a mut self, windows: &mut Windows) {
32+
pub fn update(&mut self, windows: &mut Windows) {
3333
self.pcs.update(windows);
3434
self.terrain.update(windows);
3535
self.friends.update(windows);
@@ -53,15 +53,15 @@ impl<'a> Maps<'a> {
5353
}
5454
}
5555

56-
pub struct Map<'a> {
57-
pub content: Vec<Vec<Vec<Box<Actor<'a>>>>>,
56+
pub struct Map {
57+
pub content: Vec<Vec<Vec<Box<Actor>>>>,
5858
pub size: Bound,
5959
pub move_info: Rc<RefCell<MoveInfo>>
6060
}
6161

6262

63-
impl<'a> Map<'a> {
64-
pub fn new(move_info: Rc<RefCell<MoveInfo>>) -> Map<'a> {
63+
impl Map {
64+
pub fn new(move_info: Rc<RefCell<MoveInfo>>) -> Map {
6565
let size = {
6666
move_info.borrow().deref().bounds
6767
};
@@ -73,7 +73,7 @@ impl<'a> Map<'a> {
7373
}
7474
}
7575

76-
pub fn init_contents(size: Bound) -> Vec<Vec<Vec<Box<Actor<'a>>>>> {
76+
pub fn init_contents(size: Bound) -> Vec<Vec<Vec<Box<Actor>>>> {
7777
let mut contents : Vec<Vec<Vec<Box<Actor>>>> = vec![];
7878
for _ in range(0, size.max.x) {
7979
let mut x_vec : Vec<Vec<Box<Actor>>> = vec![];
@@ -86,7 +86,7 @@ impl<'a> Map<'a> {
8686
return contents;
8787
}
8888

89-
pub fn push_actor(&mut self, point: Point, actor: Box<Actor<'a>>) {
89+
pub fn push_actor(&mut self, point: Point, actor: Box<Actor>) {
9090
self.content[point.x as uint][point.y as uint].push(actor);
9191
}
9292

@@ -100,7 +100,7 @@ impl<'a> Map<'a> {
100100
{ self.move_info.borrow_mut().deref_mut().char_location = actor.position };
101101
}
102102
let point = actor.position;
103-
let new_actor = box actor.clone();
103+
let new_actor = actor.clone();
104104
new_content[point.x as uint][point.y as uint].push(new_actor);
105105
}
106106
}

0 commit comments

Comments
 (0)