1
- use crate :: utils:: { Point2D , min_of_f32_vec, max_of_f32_vec } ;
1
+ use crate :: utils:: { max_of_f32_vec , min_of_f32_vec, Point2D } ;
2
2
use amethyst:: core:: ecs:: { Component , DenseVecStorage } ;
3
- use geo:: { Polygon , LineString } ;
4
3
use geo:: intersects:: Intersects ;
4
+ use geo:: { LineString , Polygon } ;
5
5
use std:: cmp:: Ordering ;
6
6
7
7
pub struct ButtonPlatform ;
@@ -42,24 +42,52 @@ impl Colliders {
42
42
pub fn from_vec ( colliders : Vec < Collider > ) -> Colliders {
43
43
let ( min_x, min_y, max_x, max_y) = {
44
44
(
45
- colliders. iter ( ) . map ( |col| min_of_f32_vec ( [ col. a . x , col. b . x , col. c . x , col. d . x ] ) ) . min_by ( |a, b| a. partial_cmp ( b) . unwrap_or ( Ordering :: Equal ) ) . unwrap ( ) ,
46
- colliders. iter ( ) . map ( |col| min_of_f32_vec ( [ col. a . y , col. b . y , col. c . y , col. d . y ] ) ) . min_by ( |a, b| a. partial_cmp ( b) . unwrap_or ( Ordering :: Equal ) ) . unwrap ( ) ,
47
- colliders. iter ( ) . map ( |col| max_of_f32_vec ( [ col. a . x , col. b . x , col. c . x , col. d . x ] ) ) . max_by ( |a, b| a. partial_cmp ( b) . unwrap_or ( Ordering :: Equal ) ) . unwrap ( ) ,
48
- colliders. iter ( ) . map ( |col| max_of_f32_vec ( [ col. a . y , col. b . y , col. c . y , col. d . y ] ) ) . max_by ( |a, b| a. partial_cmp ( b) . unwrap_or ( Ordering :: Equal ) ) . unwrap ( )
45
+ colliders
46
+ . iter ( )
47
+ . map ( |col| min_of_f32_vec ( [ col. a . x , col. b . x , col. c . x , col. d . x ] ) )
48
+ . min_by ( |a, b| a. partial_cmp ( b) . unwrap_or ( Ordering :: Equal ) )
49
+ . unwrap ( ) ,
50
+ colliders
51
+ . iter ( )
52
+ . map ( |col| min_of_f32_vec ( [ col. a . y , col. b . y , col. c . y , col. d . y ] ) )
53
+ . min_by ( |a, b| a. partial_cmp ( b) . unwrap_or ( Ordering :: Equal ) )
54
+ . unwrap ( ) ,
55
+ colliders
56
+ . iter ( )
57
+ . map ( |col| max_of_f32_vec ( [ col. a . x , col. b . x , col. c . x , col. d . x ] ) )
58
+ . max_by ( |a, b| a. partial_cmp ( b) . unwrap_or ( Ordering :: Equal ) )
59
+ . unwrap ( ) ,
60
+ colliders
61
+ . iter ( )
62
+ . map ( |col| max_of_f32_vec ( [ col. a . y , col. b . y , col. c . y , col. d . y ] ) )
63
+ . max_by ( |a, b| a. partial_cmp ( b) . unwrap_or ( Ordering :: Equal ) )
64
+ . unwrap ( ) ,
49
65
)
50
66
} ;
51
- Colliders { polygons : colliders. iter ( ) . map ( |collider| collider. to_polygon ( ) ) . collect ( ) , colliders, min_x, min_y, max_x, max_y }
67
+ Colliders {
68
+ polygons : colliders
69
+ . iter ( )
70
+ . map ( |collider| collider. to_polygon ( ) )
71
+ . collect ( ) ,
72
+ colliders,
73
+ min_x,
74
+ min_y,
75
+ max_x,
76
+ max_y,
77
+ }
52
78
}
53
79
pub fn from_points ( a : Point2D , b : Point2D , c : Point2D , d : Point2D ) -> Colliders {
54
80
Colliders {
55
- polygons : vec ! [
56
- Polygon :: new(
57
- LineString :: from(
58
- vec![ ( a. x, a. y) , ( b. x, b. y) , ( c. x, c. y) , ( d. x, d. y) , ( a. x, a. y) ]
59
- ) ,
60
- vec![ ] ,
61
- )
62
- ] ,
81
+ polygons : vec ! [ Polygon :: new(
82
+ LineString :: from( vec![
83
+ ( a. x, a. y) ,
84
+ ( b. x, b. y) ,
85
+ ( c. x, c. y) ,
86
+ ( d. x, d. y) ,
87
+ ( a. x, a. y) ,
88
+ ] ) ,
89
+ vec![ ] ,
90
+ ) ] ,
63
91
colliders : Vec :: new ( ) ,
64
92
min_x : min_of_f32_vec ( [ a. x , b. x , c. x , d. x ] ) ,
65
93
min_y : min_of_f32_vec ( [ a. y , b. y , c. y , d. y ] ) ,
@@ -71,7 +99,9 @@ impl Colliders {
71
99
pub fn polygons ( & self ) -> & Vec < Polygon < f32 > > {
72
100
& self . polygons
73
101
}
74
- pub fn colliders ( & self ) -> & Vec < Collider > { & self . colliders }
102
+ pub fn colliders ( & self ) -> & Vec < Collider > {
103
+ & self . colliders
104
+ }
75
105
pub fn to_owned_polygons ( & self ) -> Vec < Polygon < f32 > > {
76
106
self . polygons . clone ( )
77
107
}
@@ -92,18 +122,31 @@ pub struct Collider {
92
122
impl Collider {
93
123
pub fn new ( starting_point : Point2D , width : f32 , height : f32 ) -> Self {
94
124
Collider {
95
- b : Point2D { x : starting_point. x + width, y : starting_point. y } ,
96
- d : Point2D { x : starting_point. x , y : starting_point. y + height } ,
97
- c : Point2D { x : starting_point. x + width, y : starting_point. y + height } ,
125
+ b : Point2D {
126
+ x : starting_point. x + width,
127
+ y : starting_point. y ,
128
+ } ,
129
+ d : Point2D {
130
+ x : starting_point. x ,
131
+ y : starting_point. y + height,
132
+ } ,
133
+ c : Point2D {
134
+ x : starting_point. x + width,
135
+ y : starting_point. y + height,
136
+ } ,
98
137
a : starting_point,
99
138
}
100
139
}
101
140
102
141
pub fn to_polygon ( & self ) -> Polygon < f32 > {
103
142
Polygon :: new (
104
- LineString :: from (
105
- vec ! [ ( self . a. x, self . a. y) , ( self . b. x, self . b. y) , ( self . c. x, self . c. y) , ( self . d. x, self . d. y) , ( self . a. x, self . a. y) ]
106
- ) ,
143
+ LineString :: from ( vec ! [
144
+ ( self . a. x, self . a. y) ,
145
+ ( self . b. x, self . b. y) ,
146
+ ( self . c. x, self . c. y) ,
147
+ ( self . d. x, self . d. y) ,
148
+ ( self . a. x, self . a. y) ,
149
+ ] ) ,
107
150
vec ! [ ] ,
108
151
)
109
152
}
@@ -116,7 +159,10 @@ impl Collider {
116
159
}
117
160
}
118
161
119
- pub fn are_colliding ( ship_polygon : & Vec < Polygon < f32 > > , struct_polygons : & Vec < Polygon < f32 > > ) -> bool {
162
+ pub fn are_colliding (
163
+ ship_polygon : & Vec < Polygon < f32 > > ,
164
+ struct_polygons : & Vec < Polygon < f32 > > ,
165
+ ) -> bool {
120
166
for polygon in ship_polygon. iter ( ) {
121
167
for struct_polygon in struct_polygons. iter ( ) {
122
168
if polygon. intersects ( struct_polygon) {
@@ -128,15 +174,33 @@ pub fn are_colliding(ship_polygon: &Vec<Polygon<f32>>, struct_polygons: &Vec<Pol
128
174
}
129
175
130
176
pub fn compute_is_eligible_for_collision ( col1 : & Colliders , col2 : & Colliders ) -> bool {
131
- !( col1. min_x < col2. min_x && col1. max_x < col2. min_x && col1. min_x < col2. max_x && col1. max_x < col2. max_x )
132
- && !( col1. min_x > col2. min_x && col1. max_x > col2. min_x && col1. min_x > col2. max_x && col1. max_x > col2. max_x )
133
- && !( col1. min_y < col2. min_y && col1. max_y < col2. min_y && col1. min_y < col2. max_y && col1. max_y < col2. max_y )
134
- && !( col1. min_y > col2. min_y && col1. max_y > col2. min_y && col1. min_y > col2. max_y && col1. max_y > col2. max_y )
177
+ !( col1. min_x < col2. min_x
178
+ && col1. max_x < col2. min_x
179
+ && col1. min_x < col2. max_x
180
+ && col1. max_x < col2. max_x )
181
+ && !( col1. min_x > col2. min_x
182
+ && col1. max_x > col2. min_x
183
+ && col1. min_x > col2. max_x
184
+ && col1. max_x > col2. max_x )
185
+ && !( col1. min_y < col2. min_y
186
+ && col1. max_y < col2. min_y
187
+ && col1. min_y < col2. max_y
188
+ && col1. max_y < col2. max_y )
189
+ && !( col1. min_y > col2. min_y
190
+ && col1. max_y > col2. min_y
191
+ && col1. min_y > col2. max_y
192
+ && col1. max_y > col2. max_y )
135
193
}
136
194
137
- pub fn compute_ship_is_eligible_for_collision ( col1 : & Colliders , min_x : f32 , max_x : f32 , min_y : f32 , max_y : f32 ) -> bool {
195
+ pub fn compute_ship_is_eligible_for_collision (
196
+ col1 : & Colliders ,
197
+ min_x : f32 ,
198
+ max_x : f32 ,
199
+ min_y : f32 ,
200
+ max_y : f32 ,
201
+ ) -> bool {
138
202
!( col1. min_x < min_x && col1. max_x < min_x && col1. min_x < max_x && col1. max_x < max_x)
139
203
&& !( col1. min_x > min_x && col1. max_x > min_x && col1. min_x > max_x && col1. max_x > max_x)
140
204
&& !( col1. min_y < min_y && col1. max_y < min_y && col1. min_y < max_y && col1. max_y < max_y)
141
205
&& !( col1. min_y > min_y && col1. max_y > min_y && col1. min_y > max_y && col1. max_y > max_y)
142
- }
206
+ }
0 commit comments