forked from amethyst/bracket-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex06-astar-mouse.rs
247 lines (216 loc) · 6.99 KB
/
ex06-astar-mouse.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// This is based on example 3, but adds in highlighting visible tiles.
//
// Comments that duplicate previous examples have been removed for brevity.
//////////////////////////////////////////////////////////////
rltk::add_wasm_support!();
use rltk::{Algorithm2D, BaseMap, Console, DistanceAlg, GameState, Point, Rltk, RGB};
extern crate rand;
use crate::rand::Rng;
#[derive(PartialEq, Copy, Clone)]
enum TileType {
Wall,
Floor,
}
#[derive(PartialEq, Copy, Clone)]
enum Mode {
Waiting,
Moving,
}
struct State {
map: Vec<TileType>,
player_position: usize,
visible: Vec<bool>,
mode: Mode,
path: rltk::NavigationPath,
}
pub fn xy_idx(x: i32, y: i32) -> usize {
(y as usize * 80) + x as usize
}
pub fn idx_xy(idx: usize) -> (i32, i32) {
(idx as i32 % 80, idx as i32 / 80)
}
impl State {
pub fn new() -> State {
let mut state = State {
map: vec![TileType::Floor; 80 * 50],
player_position: xy_idx(40, 25),
visible: vec![false; 80 * 50],
mode: Mode::Waiting,
path: rltk::NavigationPath::new(),
};
for x in 0..80 {
state.map[xy_idx(x, 0)] = TileType::Wall;
state.map[xy_idx(x, 49)] = TileType::Wall;
}
for y in 0..50 {
state.map[xy_idx(0, y)] = TileType::Wall;
state.map[xy_idx(79, y)] = TileType::Wall;
}
let mut rng = rand::thread_rng();
for _ in 0..1400 {
let x = rng.gen_range(1, 79);
let y = rng.gen_range(1, 49);
let idx = xy_idx(x, y);
if state.player_position != idx {
state.map[idx] = TileType::Wall;
}
}
state
}
pub fn is_exit_valid(&self, x: i32, y: i32) -> bool {
if x < 1 || x > 79 || y < 1 || y > 49 {
return false;
}
let idx = (y * 80) + x;
self.map[idx as usize] == TileType::Floor
}
}
// Implement the game loop
impl GameState for State {
#[allow(non_snake_case)]
fn tick(&mut self, ctx: &mut Rltk) {
// Set all tiles to not visible
for v in &mut self.visible {
*v = false;
}
// Obtain the player's visible tile set, and apply it
let player_position = self.index_to_point2d(self.player_position as i32);
let fov = rltk::field_of_view(player_position, 8, self);
// Note that the steps above would generally not be run every frame!
for idx in &fov {
self.visible[xy_idx(idx.x, idx.y)] = true;
}
// Clear the screen
ctx.cls();
// Iterate the map array, incrementing coordinates as we go.
let mut y = 0;
let mut x = 0;
for (i, tile) in self.map.iter().enumerate() {
// Render a tile depending upon the tile type; now we check visibility as well!
let mut fg;
let mut glyph = ".";
match tile {
TileType::Floor => {
fg = RGB::from_f32(0.5, 0.5, 0.0);
}
TileType::Wall => {
fg = RGB::from_f32(0.0, 1.0, 0.0);
glyph = "#";
}
}
if !self.visible[i] {
fg = fg.to_greyscale();
}
ctx.print_color(x, y, fg, RGB::from_f32(0., 0., 0.), glyph);
// Move the coordinates
x += 1;
if x > 79 {
x = 0;
y += 1;
}
}
// Either render the proposed path or run along it
if self.mode == Mode::Waiting {
// Render a mouse cursor
let mouse_pos = ctx.mouse_pos();
let mouse_idx = self.point2d_to_index(Point::new(mouse_pos.0, mouse_pos.1));
ctx.print_color(
mouse_pos.0,
mouse_pos.1,
RGB::from_f32(0.0, 1.0, 1.0),
RGB::from_f32(0.0, 1.0, 1.0),
"X",
);
if self.map[mouse_idx as usize] != TileType::Wall {
let path = rltk::a_star_search(self.player_position as i32, mouse_idx as i32, self);
if path.success {
for loc in path.steps.iter().skip(1) {
let x = loc % 80;
let y = loc / 80;
ctx.print_color(
x,
y,
RGB::from_f32(1., 0., 0.),
RGB::from_f32(0., 0., 0.),
"*",
);
}
if ctx.left_click {
self.mode = Mode::Moving;
self.path = path.clone();
}
}
}
} else {
self.player_position = self.path.steps[0] as usize;
self.path.steps.remove(0);
if self.path.steps.is_empty() {
self.mode = Mode::Waiting;
}
}
// Render the player @ symbol
let ppos = idx_xy(self.player_position);
ctx.print_color(
ppos.0,
ppos.1,
RGB::from_f32(1.0, 1.0, 0.0),
RGB::from_f32(0., 0., 0.),
"@",
);
}
}
impl BaseMap for State {
fn is_opaque(&self, idx: i32) -> bool {
self.map[idx as usize] == TileType::Wall
}
fn get_available_exits(&self, idx: i32) -> Vec<(i32, f32)> {
let mut exits: Vec<(i32, f32)> = Vec::new();
let x = idx % 80;
let y = idx / 80;
// Cardinal directions
if self.is_exit_valid(x - 1, y) {
exits.push((idx - 1, 1.0))
};
if self.is_exit_valid(x + 1, y) {
exits.push((idx + 1, 1.0))
};
if self.is_exit_valid(x, y - 1) {
exits.push((idx - 80, 1.0))
};
if self.is_exit_valid(x, y + 1) {
exits.push((idx + 80, 1.0))
};
// Diagonals
if self.is_exit_valid(x - 1, y - 1) {
exits.push(((idx - 80) - 1, 1.4));
}
if self.is_exit_valid(x + 1, y - 1) {
exits.push(((idx - 80) + 1, 1.4));
}
if self.is_exit_valid(x - 1, y + 1) {
exits.push(((idx + 80) - 1, 1.4));
}
if self.is_exit_valid(x + 1, y + 1) {
exits.push(((idx + 80) + 1, 1.4));
}
exits
}
fn get_pathing_distance(&self, idx1: i32, idx2: i32) -> f32 {
let p1 = Point::new(idx1 % 80, idx1 / 80);
let p2 = Point::new(idx2 % 80, idx2 / 80);
DistanceAlg::Pythagoras.distance2d(p1, p2)
}
}
impl Algorithm2D for State {
fn point2d_to_index(&self, pt: Point) -> i32 {
xy_idx(pt.x, pt.y) as i32
}
fn index_to_point2d(&self, idx: i32) -> Point {
Point::new(idx % 80, idx / 80)
}
}
fn main() {
let context = Rltk::init_simple8x8(80, 50, "RLTK Example 05 - A Star and a Mouse", "resources");
let gs = State::new();
rltk::main_loop(context, gs);
}