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
use amethyst::core::{math::Vector3, Transform};
use crate::{
common::{Direction, get_direction_offset},
constants::{TILE_SIZE, UNIVERSAL_PLAYER_OFFSET_Y},
};
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
pub trait CoordinateSystem {
type CoordinateType;
fn x(&self) -> Self::CoordinateType;
fn y(&self) -> Self::CoordinateType;
}
pub struct WorldOffset(i32, i32);
impl WorldOffset {
pub fn new(x: i32, y: i32) -> WorldOffset {
WorldOffset(x, y)
}
pub fn invert(&self) -> WorldOffset {
WorldOffset::new(-self.0, -self.1)
}
}
#[derive(Clone, Default)]
pub struct WorldCoordinates(i32, i32);
impl WorldCoordinates {
pub fn new(x: i32, y: i32) -> WorldCoordinates {
WorldCoordinates(x, y)
}
pub fn origin() -> WorldCoordinates {
WorldCoordinates::new(0, 0)
}
pub fn to_transform(&self) -> Transform {
let mut transform = Transform::default();
transform.set_translation_xyz(self.x() as f32, self.y() as f32, 0.);
transform
}
pub fn offset_by_direction(&self, direction: &Direction) -> WorldCoordinates {
let tile_size: i32 = TILE_SIZE.into();
let (offset_x, offset_y) = get_direction_offset::<i32>(&direction);
WorldCoordinates::new(
self.x() + tile_size * offset_x,
self.y() + tile_size * offset_y,
)
}
pub fn with_offset(&self, offset: &WorldOffset) -> WorldCoordinates {
WorldCoordinates::new(
self.x() + offset.0,
self.y() + offset.1,
)
}
pub fn to_world_offset(&self) -> WorldOffset {
WorldOffset::new(self.x(), self.y())
}
pub fn corner(&self) -> WorldCoordinates {
let half_tile: i32 = (TILE_SIZE / 2).into();
WorldCoordinates::new(
self.x() - half_tile,
self.y() - half_tile,
)
}
}
impl CoordinateSystem for WorldCoordinates {
type CoordinateType = i32;
fn x(&self) -> i32 {
self.0
}
fn y(&self) -> i32 {
self.1
}
}
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct MapCoordinates(u32, u32);
impl MapCoordinates {
pub fn new(x: u32, y: u32) -> MapCoordinates {
MapCoordinates(x, y)
}
pub fn from_tuple(vector: &(u32, u32)) -> MapCoordinates {
MapCoordinates::new(vector.0, vector.1)
}
pub fn to_world_offset(&self) -> WorldOffset {
let tile_size: i32 = TILE_SIZE.into();
let half_tile = tile_size / 2;
WorldOffset::new(
i32::try_from(self.x()).unwrap() * tile_size + half_tile,
i32::try_from(self.y()).unwrap() * tile_size + half_tile,
)
}
}
impl CoordinateSystem for MapCoordinates {
type CoordinateType = u32;
fn x(&self) -> u32 {
self.0
}
fn y(&self) -> u32 {
self.1
}
}
#[derive(Clone, Debug)]
pub struct PlayerCoordinates(f32, f32);
impl PlayerCoordinates {
pub fn new(x: f32, y: f32) -> PlayerCoordinates {
PlayerCoordinates(x, y)
}
pub fn from_world_coordinates(coordinates: &WorldCoordinates) -> PlayerCoordinates {
PlayerCoordinates::new(
coordinates.x() as f32,
coordinates.y() as f32 + UNIVERSAL_PLAYER_OFFSET_Y,
)
}
pub fn to_world_coordinates(&self) -> WorldCoordinates {
WorldCoordinates::new(
self.x() as i32,
(self.y() - UNIVERSAL_PLAYER_OFFSET_Y) as i32,
)
}
pub fn from_transform(transform: &Transform) -> PlayerCoordinates {
PlayerCoordinates::new(
transform.translation().x,
transform.translation().y,
)
}
pub fn to_transform(&self) -> Transform {
let mut transform = Transform::default();
transform.set_translation_xyz(self.x(), self.y(), 0.);
transform.set_scale(Vector3::new(2., 2., 2.));
transform
}
pub fn offset_by_direction(&self, direction: &Direction) -> PlayerCoordinates {
let tile_size: f32 = TILE_SIZE.into();
let (offset_x, offset_y) = get_direction_offset::<f32>(&direction);
PlayerCoordinates::new(
self.x() + tile_size * offset_x,
self.y() + tile_size * offset_y,
)
}
pub fn get_direction_to(&self, target: &PlayerCoordinates) -> Option<Direction> {
let dx = self.x() - target.x();
let dy = self.y() - target.y();
let dx_is_0 = dx.abs() < std::f32::EPSILON;
let dy_is_0 = dy.abs() < std::f32::EPSILON;
if dx_is_0 == dy_is_0 {
None
} else if dx < 0. {
Some(Direction::Right)
} else if dx > 0. {
Some(Direction::Left)
} else if dy < 0. {
Some(Direction::Up)
} else {
Some(Direction::Down)
}
}
}
impl CoordinateSystem for PlayerCoordinates {
type CoordinateType = f32;
fn x(&self) -> f32 {
self.0
}
fn y(&self) -> f32 {
self.1
}
}