-
Notifications
You must be signed in to change notification settings - Fork 2
/
entity.rs
205 lines (173 loc) · 5.35 KB
/
entity.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use std::borrow::ref_eq;
use std::managed;
use std::option::{Option, None, Some};
use std::rand::RngUtil; // for gen_uint_range
use std::rand::task_rng;
use amulet::ll;
use amulet::ll::Style;
use geometry::Offset;
use geometry::Point;
use interface::Interface;
use world::World;
// -----------------------------------------------------------------------------
// Prototypes: templates for entities
// TODO should probably distinguish somehow between architecture, creatures,
// and flooring in a static way
// Architecture
pub static ROCKFACE: Prototype = Prototype{
display: ' ', style: Style{ is_bold: false, is_underline: false, fg_color: -1, bg_color: -1 },
passable: false,
unspeed: 0,
};
pub static WALL: Prototype = Prototype{
display: '▒', style: Style{ is_bold: false, is_underline: false, fg_color: 8, bg_color: -1 },
passable: false,
unspeed: 0,
};
pub static FLOOR: Prototype = Prototype{
display: '·', style: Style{ is_bold: false, is_underline: false, fg_color: 8, bg_color: -1 },
passable: true,
unspeed: 0,
};
pub static PASSAGE: Prototype = Prototype{
display: '░', style: Style{ is_bold: false, is_underline: false, fg_color: 8, bg_color: -1 },
passable: true,
unspeed: 0,
};
// Creatures
// TODO 'player' is not a species
pub static PLAYER: Prototype = Prototype{
display: '☻', style: Style{ is_bold: false, is_underline: false, fg_color: 4, bg_color: -1 },
passable: false,
unspeed: 48,
};
pub static ENEMY: Prototype = Prototype{
display: 'a', style: Style{ is_bold: true, is_underline: false, fg_color: 1, bg_color: -1 },
passable: true,
unspeed: 72,
};
// Objects
pub static SCROLL: Prototype = Prototype{
display: '?', style: Style{ is_bold: true, is_underline: false, fg_color: -1, bg_color: -1 },
passable: true,
unspeed: 0,
};
pub struct Prototype {
display: char,
style: ll::Style,
passable: bool,
/// Base amount of time that an action takes, in tics
unspeed: uint,
}
impl Prototype {
/// Create a new entity from a prototype.
pub fn make_entity(&'static self) -> @mut Entity {
return @mut Entity{
proto: self,
location: Nowhere,
contents: ~[],
// TODO this doesn't seem right. not all objects have health.
// component ahoy. but same with `contents`, honestly.
health: 5,
// TODO does this belong to "behavior"?
spent_subtics: 0,
};
}
}
// -----------------------------------------------------------------------------
// Entities: actual game objects
pub enum Location {
Nowhere,
OnFloor(Point),
InContainer,
}
pub struct Entity {
proto: &'static Prototype,
location: Location,
contents: ~[@mut Entity],
health: uint,
spent_subtics: uint,
}
impl Entity {
// PHYSICS
pub fn is_passable(&self) -> bool {
return self.proto.passable;
}
// BEHAVIOR
pub fn act(@mut self, world: &mut World, interface: &mut Interface) -> Option<~Action:'static> {
let player = world.map.player;
if managed::mut_ptr_eq(self, player) {
return Some(interface.next_action(world));
}
let me_point = match self.location {
OnFloor(pt) => pt,
_ => fail!(~"todo"),
};
let player_point = match player.location {
OnFloor(pt) => pt,
_ => fail!(~"todo"),
};
let distance = player_point - me_point;
// If the player is adjacent, attack!
if distance.is_adjacent() {
return Some(~AttackAction{ actor: self, target: player } as ~Action:'static);
}
// Otherwise, approach! Pick direction at random.
let which = task_rng().gen_uint_range(0, distance.taxicab_length());
if which < distance.x_mag() {
world.map.move_entity(self, distance.x_dir(), 0);
}
else {
world.map.move_entity(self, 0, distance.y_dir());
}
return None;
}
}
// Actions... oh boy.
pub trait Action {
fn execute(&self, world: &mut World, interface: &mut Interface);
}
/** `actor` strikes `target`. */
pub struct AttackAction {
actor: @mut Entity,
target: @mut Entity,
}
impl Action for AttackAction {
fn execute(&self, world: &mut World, interface: &mut Interface) {
if ref_eq(self.target.proto, &PLAYER) {
interface.message("it hits you!");
}
else if ref_eq(self.actor.proto, &PLAYER) {
interface.message("you hit it!");
}
self.target.health -= 1;
if self.target.health == 0 {
if ref_eq(self.target.proto, &PLAYER) {
interface.message("you die...");
interface.end();
}
else {
interface.message("it dies");
world.map.remove_entity(self.target);
}
}
}
}
/** `actor` moves by some amount. */
pub struct MoveAction {
actor: @mut Entity,
offset: Offset,
}
impl Action for MoveAction {
fn execute(&self, world: &mut World, _interface: &mut Interface) {
world.map.move_entity(self.actor, self.offset.dx, self.offset.dy);
}
}
/** `actor` does nothing. */
pub struct WaitAction {
actor: @mut Entity,
}
impl Action for WaitAction {
fn execute(&self, _world: &mut World, _interface: &mut Interface) {
}
}