-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_element.rs
137 lines (128 loc) · 4.7 KB
/
map_element.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
use crate::graph::{Coordinate, LinearGraph, Wall};
use crate::map_element::{Color, Point};
use mockall_double::double;
#[double]
use crate::graph::GraphMethods;
#[cfg(test)]
use mockall::automock;
#[cfg_attr(test, automock)]
pub trait MapElement: Send + Sync {
fn is_point_in_object(&self, point: &Point) -> bool;
fn color(&self) -> Color;
fn update(&mut self, _time_elapsed: f64) {}
fn on_position_update(&mut self, _coordinate: &Coordinate) {}
fn is_coordinate_in_object(
&self,
coordinate: &Coordinate,
start_position: &Coordinate,
) -> Option<(Wall, LinearGraph)> {
if coordinate.x.fract() == 0.0 {
if self.is_point_in_object(&Point {
x: coordinate.x as i64,
y: coordinate.y.floor() as i64,
}) {
return Some((
Wall {
start_point: Point {
x: coordinate.x as i64,
y: coordinate.y.floor() as i64,
},
end_point: Point {
x: coordinate.x as i64,
y: coordinate.y.ceil() as i64,
},
primary_object_color: self.color(),
},
GraphMethods::from_two_coordinates(
start_position,
Coordinate {
x: coordinate.x,
y: coordinate.y.ceil() + 0.0001,
},
),
));
}
if coordinate.x >= 1.0
&& self.is_point_in_object(&Point {
x: coordinate.x as i64 - 1,
y: coordinate.y.floor() as i64,
})
{
return Some((
Wall {
start_point: Point {
x: coordinate.x as i64,
y: coordinate.y.ceil() as i64,
},
end_point: Point {
x: coordinate.x as i64,
y: coordinate.y.floor() as i64,
},
primary_object_color: self.color(),
},
GraphMethods::from_two_coordinates(
start_position,
Coordinate {
x: coordinate.x,
y: coordinate.y.floor() - 0.0001,
},
),
));
}
} else if coordinate.y.fract() == 0.0 {
if self.is_point_in_object(&Point {
x: coordinate.x.floor() as i64,
y: coordinate.y as i64,
}) {
return Some((
Wall {
start_point: Point {
x: coordinate.x.ceil() as i64,
y: coordinate.y as i64,
},
end_point: Point {
x: coordinate.x.floor() as i64,
y: coordinate.y as i64,
},
primary_object_color: self.color(),
},
GraphMethods::from_two_coordinates(
start_position,
Coordinate {
x: coordinate.x.floor() - 0.0001,
y: coordinate.y,
},
),
));
}
if coordinate.y >= 1.0
&& self.is_point_in_object(&Point {
x: coordinate.x.floor() as i64,
y: coordinate.y as i64 - 1,
})
{
return Some((
Wall {
start_point: Point {
x: coordinate.x.floor() as i64,
y: coordinate.y as i64,
},
end_point: Point {
x: coordinate.x.ceil() as i64,
y: coordinate.y as i64,
},
primary_object_color: self.color(),
},
GraphMethods::from_two_coordinates(
start_position,
Coordinate {
x: coordinate.x.ceil() + 0.0001,
y: coordinate.y,
},
),
));
}
}
return None;
}
}