-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.rs
435 lines (400 loc) · 14.2 KB
/
mod.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#[cfg(feature = "tilemap_json")]
pub mod pyxeledit;
#[cfg(feature = "tilemap_xml")]
pub mod tiled;
pub mod tile_animation;
use std::collections::HashMap;
use log::{info,debug,error};
use tetra::graphics::{Texture, Color, Rectangle, Drawable, DrawParams};
use tetra::Context;
use tetra::math::Vec2;
#[cfg(feature = "tilemap_json")]
use crate::tilemap::pyxeledit::PyxelTilemap;
#[cfg(feature = "tilemap_xml")]
use crate::tilemap::tiled::TiledTilemap;
use crate::TetraVec2;
use crate::utils::vecgrid::VecGrid;
#[allow(dead_code)]
impl Tilemap{
/// create empty tilemap
/// use add_tiles_from_map or set_tiles to fill
pub fn new(clip: Rectangle, tile_width: i64, tile_height: i64, width: usize, height: usize) -> Tilemap{
info!("create empty tilemap");
Tilemap{
width,
height,
viewport: DEFAULT_RECTANGLE,
tile_height,
tile_width,
layers: vec![Layer {tiles: VecGrid::new(width, height),..Layer::default()}],
tile_rectangles: get_tile_rectangles(clip, tile_width, tile_height),
layer_to_draw: DEFAULT_LAYER_TO_DRAW,
}
}
#[cfg(feature = "tilemap_json")]
pub fn from_pyxeledit(clip: Rectangle, data: &str) -> Tilemap{
let pyxeltilemap = PyxelTilemap::new(data);
transform_pyxeltilemap(clip, pyxeltilemap)
}
#[cfg(feature = "tilemap_xml")]
pub fn from_tiled(clip: Rectangle, data: &str) -> Tilemap{
let tiledtilemap = TiledTilemap::new(data);
transform_tiledtilemap(clip, tiledtilemap)
}
pub fn color(&mut self, color: Color) ->&Tilemap{
if self.layer_to_draw == -1{
for mut l in self.layers.iter_mut(){
l.color = color;
}
self
}else{
match self.layers.get_mut(self.layer_to_draw as usize){ //layer vec id not = layer.number
None => self,
Some(mut l) => {
l.color = color;
self
},
}
}
}
/// just a map with tile ids
/// neither rotation nor flipping
pub fn set_tiles_from_map(&mut self, layer: usize, list: &[Vec<u32>]){
let tiles = self.create_tiles_from_map(list);
match self.layers.get_mut(layer){
None => self.add_layer(tiles),
Some(layer) => layer.tiles = tiles,
}
}
pub fn viewport(&mut self, rectangle: Rectangle) -> &Tilemap{
self.viewport = rectangle;
self
}
pub fn get_all_position_from_id(&self,layer: usize, id: u32) -> Vec<TetraVec2>{
let mut positions = Vec::new();
if let Some(layer) = self.layers.get(layer) {
let tiles = &*layer.tiles.get_data();
for (i,t) in tiles.iter().enumerate(){
if t.is_some() && t.as_ref().unwrap().id == id{
let x = (i % self.width) * self.tile_width as usize;
let y = ((i / self.width)+1) * self.tile_height as usize;
positions.push(TetraVec2::new((x+1) as f32,(y-1) as f32));
}
}
};
positions
}
pub fn replace_all_tileid(&mut self, layer: usize, old_id: u32, new_id: Option<u32>){
if let Some(layer) = self.layers.get_mut(layer) {
for x in 0..self.height {
for y in 0..self.width {
if let Some(tile) = layer.tiles.get_mut(x, y) {
if tile.id == old_id {
if let Some(id) = new_id {
tile.id = id;
} else {
layer.tiles.delete(x, y);
}
}
}
}
}
}else{
error!("layer{} not found!", layer);
}
}
pub fn set_tileid_at(&mut self, layer: usize, new_id: u32, position: TetraVec2){
let x = (position.x as i64 / self.tile_width) as usize;
let y = (position.y as i64 / self.tile_height) as usize;
if let Some(layer) = self.layers.get_mut(layer){
match layer.tiles.get_mut(x, y){
None => layer.tiles.set(Tile{
id: new_id,
x: x as i64,
y: y as i64,
position_x: (x as i64 * self.tile_width) as f32,
position_y: (y as i64 * self.tile_height) as f32,
..Tile::default()
}, x, y),
Some(tile) => tile.id = new_id,
};
}else{
error!("layer{} not found!", layer);
}
}
pub fn visibility(&mut self, layer: usize, visibility: bool){
if let Some(mut l) = self.layers.get_mut(layer) {
l.visibility = visibility
}else{
error!("layer{} not found!", layer);
}
}
pub fn get_layer_id(&self, name: &str) ->usize{
for (i, layer) in self.layers.iter().enumerate(){
if layer.name.eq(name){
return i;
}
}
99
}
pub fn get_layer_name(&self, layer: usize) ->&str{
if let Some(layer) = self.layers.get(layer as usize){
&layer.name
}else{
error!("layer{} not found!", layer);
""
}
}
pub fn get_id_at_position(&self, layer: usize, position: TetraVec2) -> Option<u32>{
let x = position.x as i64 / self.tile_width;
let y = position.y as i64 / self.tile_height;
self.get_id_at(layer, x as usize, y as usize)
}
pub fn get_id_at(&self, layer_nr: usize, x: usize,y: usize) -> Option<u32>{
match self.layers.get(layer_nr) {
None => {
debug!("no layer!");
None
},
Some(layer) => {
match layer.tiles.get(x,y){
None => {
debug!("layer[{}] {}, no tile at {},{}!",layer_nr, layer.name, x,y);
None
},
Some(tile) => Some(tile.id)
}
},
}
}
fn is_inside_viewport(&self, position: TetraVec2) -> bool{
!(position.x < self.viewport.x ||
position.y < self.viewport.y ||
position.x > self.viewport.x + self.viewport.width ||
position.y > self.viewport.y + self.viewport.height
)
}
pub fn get_clip_from_id(&self, id: u32) -> Rectangle{
self.tile_rectangles[&id]
}
pub fn get_frames_from_ids(&self, ids: &[u32]) -> Vec<Rectangle>{
let mut frames = Vec::with_capacity(ids.len());
for id in ids{
frames.push(self.tile_rectangles[&(id)]);
}
frames
}
fn create_tiles_from_map(&mut self, list: &[Vec<u32>])->VecGrid<Tile>{
let mut tiles = VecGrid::new(list.len(), list[0].len());
for (x,row) in list.iter().enumerate() {
for (y,id) in row.iter().enumerate(){
tiles.set(Tile {
id: *id,
x: x as i64,
y: y as i64,
position_x: (x as i64 * self.tile_width) as f32,
position_y: (y as i64 * self.tile_height) as f32,
..Tile::default()
},x,y);
};
}
tiles
}
fn add_layer(&mut self, tiles: VecGrid<Tile>){
let layer = Layer{
tiles,
..Layer::default()
};
self.layers.push(layer);
}
pub fn draw_layer<P>(&mut self, ctx: &mut Context,texture: &Texture, params: P, layer_to_draw: usize)
where
P: Into<DrawParams>,
{
let params = params.into();
self.draw(ctx, texture, params,Some(layer_to_draw));
}
pub fn draw<P>(&mut self, ctx: &mut Context,texture: &Texture, params: P, layer_to_draw: Option<usize>)
where
P: Into<DrawParams>,
{
let params = params.into();
for (i, layer) in self.layers.iter().enumerate() {
if layer.visibility && layer_to_draw.is_none() || layer_to_draw.is_some() && i == layer_to_draw.unwrap(){
for tile in layer.tiles.get_data().iter().filter(|t| t.is_some()) {
match tile {
None => (),
Some(tile) => {
let tmp_pos = Vec2::new(params.position.x + tile.position_x, params.position.y + tile.position_y);
if self.is_inside_viewport(tmp_pos) || draw_everything(&self.viewport) {
texture.draw(ctx, DrawParams::new()
.position(tmp_pos)
.clip(self.tile_rectangles[&tile.id])
.rotation(tile.rotation)
.scale(tile.scale)
.color(layer.color)
);
}
}
}
}
}
}
}
}
#[allow(dead_code)]
pub struct Tilemap {
width: usize,
height: usize,
viewport: Rectangle,
tile_height: i64,
tile_width: i64,
layers: Vec<Layer>,
tile_rectangles: HashMap<u32, Rectangle>,
layer_to_draw: i64,
}
pub struct Layer {
tiles: VecGrid<Tile>,
name: String,
visibility: bool,
color: Color,
}
#[allow(dead_code)]
pub struct Tile {
id: u32,
x: i64,
y: i64,
position_x: f32,
position_y: f32,
rotation: f32,
scale: TetraVec2,
}
fn get_tile_rectangles(clip: Rectangle, tile_width: i64, tile_height: i64) ->HashMap<u32, Rectangle>{
let mut id = 0;
let x = i64::from(clip.height as i32) / tile_width;
let y = i64::from(clip.width as i32) / tile_height;
let mut tile_rectangles: HashMap<u32, Rectangle> = HashMap::with_capacity((x * y) as usize);
for i in 0..x{
for j in 0..y{
let rec = Rectangle::new(clip.x +(j*tile_width) as f32,clip.y +(i*tile_height) as f32, tile_width as f32, tile_height as f32); //switch x and y axis
tile_rectangles.insert(id,rec);
id +=1;
}
}
tile_rectangles
}
#[cfg(feature = "tilemap_json")]
fn transform_pyxeltilemap(clip: Rectangle, pyxeltilemap: PyxelTilemap) ->Tilemap{
Tilemap{
width: pyxeltilemap.tileswide as usize,
height: pyxeltilemap.tileshigh as usize,
viewport: DEFAULT_RECTANGLE,
tile_height: pyxeltilemap.tile_height,
tile_width: pyxeltilemap.tile_width,
layers: transform_pyxellayer(&pyxeltilemap.layers, pyxeltilemap.tileswide as usize, pyxeltilemap.tileshigh as usize),
tile_rectangles: get_tile_rectangles(clip, pyxeltilemap.tile_width, pyxeltilemap.tile_height),
layer_to_draw: DEFAULT_LAYER_TO_DRAW,
}
}
#[cfg(feature = "tilemap_json")]
fn transform_pyxellayer(pyxellayers: &[pyxeledit::Layers], width: usize, height: usize) ->Vec<Layer>{
let mut layers: Vec<Layer> = Vec::new();
for pyxellayer in pyxellayers.iter().rev(){
let l = Layer{
tiles: transform_pyxeltile(&pyxellayer.tiles, width, height),
name: pyxellayer.name.clone(),
..Layer::default()
};
layers.push(l);
}
layers
}
#[cfg(feature = "tilemap_json")]
fn transform_pyxeltile(pyxeltiles: &[pyxeledit::Tile], width: usize, height: usize) -> VecGrid<Tile>{
let mut vecgrid: VecGrid<Tile> = VecGrid::new(width, height);
for t in pyxeltiles.iter(){
let tile = Tile{
id: t.id as u32,
x: t.x,
y: t.y,
position_x: t.position_x,
position_y: t.position_y,
rotation: t.rotation,
scale: Vec2::new(t.scale.0,t.scale.1),
};
vecgrid.set(tile, t.x as usize, t.y as usize);
};
vecgrid
}
#[cfg(feature = "tilemap_xml")]
fn transform_tiledtilemap(clip: Rectangle, tiledtilemap: TiledTilemap) ->Tilemap{
Tilemap{
width: tiledtilemap.tilewidth,
height: tiledtilemap.tileheight,
viewport: DEFAULT_RECTANGLE,
tile_height: tiledtilemap.tile_height,
tile_width: tiledtilemap.tile_width,
layers: transform_tiledlayer(&tiledtilemap.layers,tiledtilemap.tilewidth,tiledtilemap.tileheight),
tile_rectangles: get_tile_rectangles(clip, tiledtilemap.tile_width, tiledtilemap.tile_height),
layer_to_draw: DEFAULT_LAYER_TO_DRAW,
}
}
#[cfg(feature = "tilemap_xml")]
fn transform_tiledlayer(tiledlayers: &[tiled::Layer], width: usize, height: usize) ->Vec<Layer>{
let mut layers: Vec<Layer> = Vec::new();
for tiledlayer in tiledlayers.iter(){
let l = Layer{
tiles: transform_tiledtile(&tiledlayer.tiles, width, height),
name: tiledlayer.name.clone(),
..Layer::default()
};
layers.push(l);
}
layers
}
#[cfg(feature = "tilemap_xml")]
fn transform_tiledtile(tiledtiles: &[tiled::Tile], width: usize, height: usize) -> VecGrid<Tile>{
let mut vecgrid: VecGrid<Tile> = VecGrid::new(width, height);
for t in tiledtiles.iter(){
let tile = Tile{
id: t.id,
x: t.x,
y: t.y,
position_x: t.position_x,
position_y: t.position_y,
rotation: t.rotation,
scale: Vec2::new(t.scale.0,t.scale.1),
};
vecgrid.set(tile, t.x as usize, t.y as usize);
};
vecgrid
}
fn draw_everything(rectangle: &Rectangle) -> bool{
let rectangle_to_compare = DEFAULT_RECTANGLE;
rectangle_to_compare.eq(rectangle)
}
impl Default for Layer {
fn default() -> Layer {
Layer{
tiles: VecGrid::new(1,1),
name: "".to_string(),
visibility: true,
color: Color::rgb(1.0, 1.0, 1.0),
}
}
}
impl Default for Tile {
fn default() -> Tile {
Tile{
id: 0,
x: 0,
y: 0,
position_x: 0.0,
position_y: 0.0,
rotation: 0.0,
scale: Vec2::new(1.0, 1.0),
}
}
}
const DEFAULT_RECTANGLE: Rectangle = Rectangle{ x: 0.0, y: 0.0, width: 0.0, height: 0.0 };
const DEFAULT_LAYER_TO_DRAW: i64 = -1;