Skip to content

Commit 057a128

Browse files
committed
Working towards Rust 1.0
1 parent 159f16e commit 057a128

File tree

14 files changed

+220
-131
lines changed

14 files changed

+220
-131
lines changed

Cargo.lock

Lines changed: 37 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
name = "dwemthys"
44
version = "0.0.1"
55
authors = ["jmcfarland"]
6-
build = "sh .build.sh"
7-
#build = "build.rs"
6+
#build = "sh .build.sh"
7+
build = "build.rs"
8+
9+
[dependencies]
10+
rand = "0.2.0"
811

912
[dependencies.tcod]
1013
git = "https://github.com/tomassedovic/tcod-rs.git"

build.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use std::io::Command;
2-
use std::os;
1+
use std::process::Command;
2+
use std::env;
33

44
fn main() {
55
println!("foo bar");
6-
let out_dir = os::getenv("OUT_DIR").unwrap();
6+
let out_dir = env::var("OUT_DIR").unwrap();
77
let libtcod_src_dir = "/Users/jmcfarland/src/libtcod";
88

99
let args = &[

src/actor.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
extern crate core;
2+
13
use std::cell::RefCell;
24
use std::rc::Rc;
35

@@ -7,6 +9,8 @@ use rendering::renderers::{Color};
79
use rendering::windows::Windows;
810
use movement::{AggroMovementComponent, RandomMovementComponent, UserMovementComponent, MovementComponent};
911

12+
use self::core::ops::Deref;
13+
1014
pub struct Actor {
1115
pub position: Point,
1216
pub display_char: char,
@@ -38,25 +42,25 @@ impl Actor {
3842
}
3943

4044
pub fn dog(x: i32, y: i32, move_info: Rc<RefCell<MoveInfo>>) -> Actor {
41-
let mc : Box<RandomMovementComponent> = box MovementComponent::new(move_info);
45+
let mc = Box::new(RandomMovementComponent::new(move_info));
4246
Actor::new(x, y, 'd', mc, false, Color::White, Color::Black, 20u8)
4347
}
4448

4549
pub fn cat(x: i32, y: i32, move_info: Rc<RefCell<MoveInfo>>) -> Actor {
46-
let mc : Box<RandomMovementComponent> = box MovementComponent::new(move_info);
50+
let mc = Box::new(RandomMovementComponent::new(move_info));
4751
Actor::new(x, y, 'c', mc, false, Color::White, Color::Black, 20u8)
4852
}
4953

5054
pub fn heroine(move_info: Rc<RefCell<MoveInfo>>) -> Actor {
5155
let point = {
5256
move_info.borrow().deref().char_location
5357
};
54-
let mc : Box<UserMovementComponent> = box MovementComponent::new(move_info);
58+
let mc = Box::new(UserMovementComponent::new(move_info));
5559
Actor::new(point.x, point.y, '@', mc, true, Color::Blue, Color::Black, 20u8)
5660
}
5761

5862
pub fn kobold(x: i32, y: i32, move_info: Rc<RefCell<MoveInfo>>) -> Actor {
59-
let mc : Box<AggroMovementComponent> = box MovementComponent::new(move_info);
63+
let mc = Box::new(AggroMovementComponent::new(move_info));
6064
Actor::new(x, y, 'k', mc, false, Color::Red, Color::Black, 20u8)
6165
}
6266

src/combat.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
extern crate rand;
2+
extern crate core;
3+
14
use std::num;
2-
use std::rand;
3-
use std::rand::distributions::{IndependentSample, Range};
5+
use self::rand::distributions::{IndependentSample, Range};
46

57
use actor::Actor;
8+
use self::core::ops::Deref;
69

710
pub trait Weapon {
8-
fn new() -> Self;
911
fn get_name(&self) -> String;
1012
fn deal_damage(&self, &Box<Actor>) -> u16;
1113
}
@@ -15,14 +17,16 @@ pub struct Boomerang {
1517
base_damage: u8
1618
}
1719

18-
impl Weapon for Boomerang {
19-
fn new() -> Boomerang {
20+
impl Boomerang {
21+
pub fn new() -> Boomerang {
2022
Boomerang {
2123
name: String::from_str("Little Boomerang"),
2224
base_damage: 13
2325
}
2426
}
27+
}
2528

29+
impl Weapon for Boomerang {
2630
fn get_name(&self) -> String {
2731
self.name.clone()
2832
}
@@ -42,19 +46,22 @@ pub struct Sword {
4246
base_damage: u8
4347
}
4448

45-
impl Weapon for Sword {
46-
fn new() -> Sword {
49+
impl Sword {
50+
pub fn new() -> Sword {
4751
Sword {
4852
name: String::from_str("Heroic Sworc"),
4953
base_damage: 4
5054
}
5155
}
56+
}
5257

58+
impl Weapon for Sword {
5359
fn get_name(&self) -> String { self.name.clone() }
5460

5561
fn deal_damage(&self, enemy: &Box<Actor>) -> u16 {
56-
let max = self.base_damage + num::pow(enemy.health % 10u8, 2);
57-
let mut rng = rand::task_rng();
62+
let x = enemy.health % 10u8;
63+
let max = self.base_damage + (x * x);
64+
let mut rng = rand::thread_rng();
5865
Range::new(0u8, max).ind_sample(&mut rng) as u16
5966
}
6067
}

src/game.rs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
extern crate tcod;
2+
extern crate core;
23

34
use std::cell::RefCell;
45
use std::rc::Rc;
@@ -30,6 +31,8 @@ use combat::{
3031
};
3132
use actor::Actor;
3233

34+
use self::core::ops::{Deref, DerefMut};
35+
3336
pub struct MoveInfo {
3437
pub last_keypress: Option<KeyboardInput>,
3538
pub char_location: Point,
@@ -64,12 +67,12 @@ impl Game {
6467
let message_bounds = Bound::new(0, 52, 99, 61);
6568
let map_bounds = Bound::new(0, 0, 78, 49);
6669

67-
let rc : Box<TcodRenderingComponent> = box RenderingComponent::new(total_bounds);
70+
let rc = Box::new(TcodRenderingComponent::new(total_bounds));
6871

69-
let sw : Box<TcodStatsWindowComponent> = box WindowComponent::new(stats_bounds);
70-
let iw : Box<TcodInputWindowComponent> = box WindowComponent::new(input_bounds);
71-
let mw : Box<TcodMessagesWindowComponent> = box WindowComponent::new(message_bounds);
72-
let maw : Box<TcodMapWindowComponent> = box WindowComponent::new(map_bounds);
72+
let sw = Box::new(TcodStatsWindowComponent::new(stats_bounds));
73+
let iw = Box::new(TcodInputWindowComponent::new(input_bounds));
74+
let mw = Box::new(TcodMessagesWindowComponent::new(message_bounds));
75+
let maw = Box::new(TcodMapWindowComponent::new(map_bounds));
7376

7477
let windows = Windows {
7578
input: iw,
@@ -78,19 +81,19 @@ impl Game {
7881
stats: sw
7982
};
8083

81-
let gs : Box<MovementGameState> = box GameState::new();
84+
let gs = Box::new(MovementGameState::new());
8285

8386
let move_info = Rc::new(RefCell::new(MoveInfo::new(map_bounds)));
8487
let mut maps = Maps::new(move_info.clone());
8588

86-
maps.friends.push_actor(Point::new(10, 10), box Actor::dog(10, 10, move_info.clone()));
87-
maps.friends.push_actor(Point::new(40, 25), box Actor::cat(40, 25, move_info.clone()));
88-
maps.enemies.push_actor(Point::new(20, 20), box Actor::kobold(20, 20, move_info.clone()));
89+
maps.friends.push_actor(Point::new(10, 10), Box::new(Actor::dog(10, 10, move_info.clone())));
90+
maps.friends.push_actor(Point::new(40, 25), Box::new(Actor::cat(40, 25, move_info.clone())));
91+
maps.enemies.push_actor(Point::new(20, 20), Box::new(Actor::kobold(20, 20, move_info.clone())));
8992

9093
let char_location = {
9194
move_info.borrow().deref().char_location
9295
};
93-
maps.pcs.push_actor(char_location, box Actor::heroine(move_info.clone()));
96+
maps.pcs.push_actor(char_location, Box::new(Actor::heroine(move_info.clone())));
9497

9598
Game {
9699
exit: false,
@@ -104,7 +107,8 @@ impl Game {
104107
}
105108

106109
pub fn render(&mut self) {
107-
self.game_state.render(&mut self.rendering_component, &mut self.maps, &mut self.windows);
110+
let ref mut render_component = self.rendering_component;
111+
self.game_state.render(render_component, &mut self.maps, &mut self.windows);
108112
}
109113

110114
pub fn update(&mut self) {
@@ -134,28 +138,28 @@ impl Game {
134138
Some(ks) => {
135139
match ks.key {
136140
Printable('/') => {
137-
let w : Box<Sword> = box Weapon::new();
138-
let is : Box<AttackInputGameState> = box GameState::new_with_weapon(w);
139-
self.game_state = is as Box<GameState>;
141+
let w = Box::new(Sword::new());
142+
let is = Box::new(AttackInputGameState::new_with_weapon(w));
143+
self.game_state = is;
140144
},
141145
Printable('^') => {
142-
let w : Box<Boomerang> = box Weapon::new();
143-
let is : Box<AttackInputGameState> = box GameState::new_with_weapon(w);
144-
self.game_state = is as Box<GameState>;
146+
let w = Box::new(Boomerang::new());
147+
let is = Box::new(AttackInputGameState::new_with_weapon(w));
148+
self.game_state = is;
145149
},
146150
Printable('*') => {
147-
let w : Box<Boomerang> = box Weapon::new();
148-
let is : Box<AttackInputGameState> = box GameState::new_with_weapon(w);
149-
self.game_state = is as Box<GameState>;
151+
let w = Box::new(Boomerang::new());
152+
let is = Box::new(AttackInputGameState::new_with_weapon(w));
153+
self.game_state = is;
150154
},
151155
Printable('%') => {
152-
let w : Box<Boomerang> = box Weapon::new();
153-
let is : Box<AttackInputGameState> = box GameState::new_with_weapon(w);
154-
self.game_state = is as Box<GameState>;
156+
let w = Box::new(Boomerang::new());
157+
let is = Box::new(AttackInputGameState::new_with_weapon(w));
158+
self.game_state = is;
155159
},
156160
_ => {
157-
let ms : Box<MovementGameState> = box GameState::new();
158-
self.game_state = ms as Box<GameState>;
161+
let ms = Box::new(MovementGameState::new());
162+
self.game_state = ms;
159163
}
160164
}
161165
},

0 commit comments

Comments
 (0)