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
use amethyst::{
assets::{Handle, Loader, ProgressCounter},
ecs::{World, WorldExt},
renderer::{
ImageFormat,
sprite::{Sprite, TextureCoordinates},
SpriteSheet,
SpriteSheetFormat,
},
ui::FontHandle,
};
use serde::{Deserialize, Serialize};
pub struct AssetTracker {
progress_counter: ProgressCounter,
}
impl AssetTracker {
pub fn new(progress_counter: ProgressCounter) -> AssetTracker {
AssetTracker {
progress_counter,
}
}
pub fn get_progress_counter(&self) -> &ProgressCounter {
&self.progress_counter
}
pub fn get_progress_counter_mut(&mut self) -> &mut ProgressCounter {
&mut self.progress_counter
}
}
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum Direction {
Up,
Down,
Left,
Right,
}
pub struct CommonResources {
pub font: FontHandle,
pub text_box: Handle<SpriteSheet>,
pub black: Handle<SpriteSheet>,
}
pub fn load_sprite_sheet(
world: &World,
image_name: &str,
ron_name: &str,
progress_counter: &mut ProgressCounter,
) -> Handle<SpriteSheet> {
let loader = world.read_resource::<Loader>();
let texture_handle = loader.load(
image_name,
ImageFormat::default(),
&mut *progress_counter,
&world.read_resource(),
);
loader.load(
ron_name,
SpriteSheetFormat(texture_handle),
&mut *progress_counter,
&world.read_resource()
)
}
pub fn load_full_texture_sprite_sheet(
world: &World,
image_name: &str,
image_size: &(u32, u32),
progress_counter: &mut ProgressCounter,
) -> Handle<SpriteSheet> {
let loader = world.read_resource::<Loader>();
let texture = loader.load(
image_name,
ImageFormat::default(),
&mut *progress_counter,
&world.read_resource(),
);
let sprite_sheet = SpriteSheet {
texture,
sprites: vec![
Sprite {
width: image_size.0 as f32,
height: image_size.1 as f32,
offsets: [0., 0.],
tex_coords: TextureCoordinates {
left: 0.,
right: 1.,
bottom: 1.,
top: 0.,
}
}
]
};
loader.load_from_data(
sprite_sheet,
&mut *progress_counter,
&world.read_resource()
)
}
pub fn get_direction_offset<T>(direction: &Direction) -> (T, T)
where
T: From<i8>
{
let (x, y) = match direction {
Direction::Up => (0, 1),
Direction::Down => (0, -1),
Direction::Left => (-1, 0),
Direction::Right => (1, 0),
};
(x.into(), y.into())
}
pub fn get_character_sprite_index_from_direction(direction: &Direction) -> usize {
match direction {
Direction::Up => 0,
Direction::Down => 3,
Direction::Left => 6,
Direction::Right => 9,
}
}