-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.rs
175 lines (154 loc) · 5.26 KB
/
map.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
use crate::graph::{Coordinate, LinearGraph, Wall};
use crate::map_element::MapElement;
use mockall_double::double;
#[cfg(test)]
use mockall::automock;
#[double]
use crate::graph::GraphMethods;
#[derive(Default, Clone)]
pub struct Map {
pub width: i64,
pub height: i64,
}
#[cfg_attr(test, automock)]
impl Map {
fn validate_coordinate(&self, coordinate: &Coordinate) -> bool {
if coordinate.x < 0.0
|| coordinate.y < 0.0
|| coordinate.x > self.width as f64 - 1.0
|| coordinate.y > self.height as f64 - 1.0
{
return false;
}
return true;
}
fn get_wall(
&self,
position: &Coordinate, // has to return coordinates sorted in clockwise order
map_elements: &Vec<Box<dyn MapElement>>,
start_position: &Coordinate,
) -> Option<(Wall, LinearGraph)> {
for map_element in map_elements {
let wall = map_element.is_coordinate_in_object(position, start_position);
if wall != None {
return wall;
}
}
return None;
}
pub(crate) fn cast_ray(
&self,
position: &Coordinate,
ray: &LinearGraph,
map_elements: &Vec<Box<dyn MapElement>>,
) -> Option<(Wall, LinearGraph)> {
let start_position = position;
let mut last_position = position.clone();
let mut next_position: Coordinate;
loop {
next_position = GraphMethods::get_next(&ray, &last_position);
if !self.validate_coordinate(&next_position) {
return None;
}
let wall = self.get_wall(&next_position, map_elements, &start_position);
if wall != None {
return wall;
}
last_position = next_position;
}
}
}
#[cfg(test)]
mod tests {
#![allow(non_upper_case_globals)]
use super::*;
use crate::graph::MockGraphMethods;
use crate::map_element::{Color, MockMapElement, Point};
use mockall::*;
#[test]
fn cast_ray_complex() {
let mut seq = Sequence::new();
let map = Map {
width: 50,
height: 50,
};
lazy_static! {
static ref current_position: Coordinate = Coordinate { x: 30.0, y: 20.0 };
static ref next_position_1: Coordinate = Coordinate { x: 40.0, y: 30.5 };
static ref next_position_2: Coordinate = Coordinate { x: 45.0, y: 35.5 };
static ref ray: LinearGraph = LinearGraph::default();
}
let wall = Wall {
start_point: Point { x: 10, y: 15 },
end_point: Point { x: 20, y: 25 },
primary_object_color: Color::Blue,
};
let get_next_context = MockGraphMethods::get_next_context();
let mut map_element = Box::new(MockMapElement::new());
get_next_context
.expect()
.times(1)
.withf(|_ray, coordinate| *_ray == *ray && *coordinate == *current_position)
.return_const(next_position_1.clone())
.in_sequence(&mut seq);
map_element
.expect_is_coordinate_in_object()
.times(1)
.withf(|coordinate, _| *coordinate == *next_position_1)
.return_const(None)
.in_sequence(&mut seq);
get_next_context
.expect()
.times(1)
.withf(|_ray, coordinate| *_ray == *ray && *coordinate == *next_position_1)
.return_const(next_position_2.clone())
.in_sequence(&mut seq);
map_element
.expect_is_coordinate_in_object()
.times(1)
.withf(|coordinate, _| *coordinate == *next_position_2)
.returning(|_, _| {
Some((
Wall {
start_point: Point { x: 10, y: 15 },
end_point: Point { x: 20, y: 25 },
primary_object_color: Color::Blue,
},
LinearGraph::default(),
))
})
.in_sequence(&mut seq);
match map.cast_ray(¤t_position, &ray, &vec![map_element]) {
Some((wall_, _)) => assert_eq!(wall_, wall),
ret_wall @ _ => panic!(
"wrong value cast_ray_complex expected: {:?} received: {:?}",
wall, ret_wall
),
}
}
#[test]
fn cast_ray_out_of_map() {
let mut seq = Sequence::new();
let map = Map {
width: 50,
height: 50,
};
lazy_static! {
static ref ray: LinearGraph = LinearGraph::default();
}
static current_positon: Coordinate = Coordinate { x: 30.0, y: 20.0 };
let next_position_out_of_map = Coordinate { x: 55.0, y: 30.0 };
let get_next_context = MockGraphMethods::get_next_context();
let map_element = Box::new(MockMapElement::new());
get_next_context
.expect()
.times(1)
.withf(|_ray, coordinate| *_ray == *ray && *coordinate == current_positon)
.return_const(next_position_out_of_map)
.in_sequence(&mut seq);
assert_eq!(
map.cast_ray(¤t_positon, &ray, &vec![map_element]),
None
);
}
}