Bastien Sevajol 3 years ago
parent
commit
9945ee0390
13 changed files with 586 additions and 14 deletions
  1. 86 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 75 0
      resources/map1.tmx
  4. BIN
      resources/terrain.png
  5. 39 0
      resources/terrain.tsx
  6. BIN
      resources/terrain.xcf
  7. 9 0
      resources/ui.tsx
  8. 0 2
      src/config.rs
  9. 1 0
      src/main.rs
  10. 176 0
      src/map/mod.rs
  11. 109 0
      src/map/tile.rs
  12. 1 1
      src/scene/item.rs
  13. 89 11
      src/scene/main.rs

+ 86 - 0
Cargo.lock View File

@@ -97,6 +97,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
97 97
 checksum = "a4521f3e3d031370679b3b140beb36dfe4801b09ac77e30c61941f97df3ef28b"
98 98
 
99 99
 [[package]]
100
+name = "base64"
101
+version = "0.10.1"
102
+source = "registry+https://github.com/rust-lang/crates.io-index"
103
+checksum = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e"
104
+dependencies = [
105
+ "byteorder",
106
+]
107
+
108
+[[package]]
100 109
 name = "bindgen"
101 110
 version = "0.56.0"
102 111
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1054,6 +1063,15 @@ dependencies = [
1054 1063
 ]
1055 1064
 
1056 1065
 [[package]]
1066
+name = "itertools"
1067
+version = "0.9.0"
1068
+source = "registry+https://github.com/rust-lang/crates.io-index"
1069
+checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b"
1070
+dependencies = [
1071
+ "either",
1072
+]
1073
+
1074
+[[package]]
1057 1075
 name = "itoa"
1058 1076
 version = "0.4.7"
1059 1077
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1149,6 +1167,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
1149 1167
 checksum = "9385f66bf6105b241aa65a61cb923ef20efc665cb9f9bb50ac2f0c4b7f378d41"
1150 1168
 
1151 1169
 [[package]]
1170
+name = "libflate"
1171
+version = "0.1.27"
1172
+source = "registry+https://github.com/rust-lang/crates.io-index"
1173
+checksum = "d9135df43b1f5d0e333385cb6e7897ecd1a43d7d11b91ac003f4d2c2d2401fdd"
1174
+dependencies = [
1175
+ "adler32",
1176
+ "crc32fast",
1177
+ "rle-decode-fast",
1178
+ "take_mut",
1179
+]
1180
+
1181
+[[package]]
1152 1182
 name = "libloading"
1153 1183
 version = "0.6.7"
1154 1184
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1694,6 +1724,7 @@ version = "0.1.0"
1694 1724
 dependencies = [
1695 1725
  "ggez",
1696 1726
  "glam",
1727
+ "tiled",
1697 1728
 ]
1698 1729
 
1699 1730
 [[package]]
@@ -1999,6 +2030,12 @@ dependencies = [
1999 2030
 ]
2000 2031
 
2001 2032
 [[package]]
2033
+name = "rle-decode-fast"
2034
+version = "1.0.1"
2035
+source = "registry+https://github.com/rust-lang/crates.io-index"
2036
+checksum = "cabe4fa914dec5870285fa7f71f602645da47c486e68486d2b4ceb4a343e90ac"
2037
+
2038
+[[package]]
2002 2039
 name = "rodio"
2003 2040
 version = "0.13.1"
2004 2041
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2319,6 +2356,12 @@ dependencies = [
2319 2356
 ]
2320 2357
 
2321 2358
 [[package]]
2359
+name = "take_mut"
2360
+version = "0.2.2"
2361
+source = "registry+https://github.com/rust-lang/crates.io-index"
2362
+checksum = "f764005d11ee5f36500a149ace24e00e3da98b0158b3e2d53a7495660d3f4d60"
2363
+
2364
+[[package]]
2322 2365
 name = "tempfile"
2323 2366
 version = "3.2.0"
2324 2367
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2364,6 +2407,18 @@ dependencies = [
2364 2407
 ]
2365 2408
 
2366 2409
 [[package]]
2410
+name = "tiled"
2411
+version = "0.9.5"
2412
+source = "registry+https://github.com/rust-lang/crates.io-index"
2413
+checksum = "8c9b76189425fed476c9cb0e2a97adb0c6c9d370bf69a57cb951814e77a5fe63"
2414
+dependencies = [
2415
+ "base64",
2416
+ "libflate",
2417
+ "xml-rs",
2418
+ "zstd",
2419
+]
2420
+
2421
+[[package]]
2367 2422
 name = "tinyvec"
2368 2423
 version = "1.2.0"
2369 2424
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2759,3 +2814,34 @@ dependencies = [
2759 2814
  "crc32fast",
2760 2815
  "thiserror",
2761 2816
 ]
2817
+
2818
+[[package]]
2819
+name = "zstd"
2820
+version = "0.5.4+zstd.1.4.7"
2821
+source = "registry+https://github.com/rust-lang/crates.io-index"
2822
+checksum = "69996ebdb1ba8b1517f61387a883857818a66c8a295f487b1ffd8fd9d2c82910"
2823
+dependencies = [
2824
+ "zstd-safe",
2825
+]
2826
+
2827
+[[package]]
2828
+name = "zstd-safe"
2829
+version = "2.0.6+zstd.1.4.7"
2830
+source = "registry+https://github.com/rust-lang/crates.io-index"
2831
+checksum = "98aa931fb69ecee256d44589d19754e61851ae4769bf963b385119b1cc37a49e"
2832
+dependencies = [
2833
+ "libc",
2834
+ "zstd-sys",
2835
+]
2836
+
2837
+[[package]]
2838
+name = "zstd-sys"
2839
+version = "1.4.18+zstd.1.4.7"
2840
+source = "registry+https://github.com/rust-lang/crates.io-index"
2841
+checksum = "a1e6e8778706838f43f771d80d37787cb2fe06dafe89dd3aebaf6721b9eaec81"
2842
+dependencies = [
2843
+ "cc",
2844
+ "glob",
2845
+ "itertools",
2846
+ "libc",
2847
+]

+ 1 - 0
Cargo.toml View File

@@ -7,3 +7,4 @@ edition = "2018"
7 7
 [dependencies]
8 8
 ggez = { git = "https://github.com/ggez/ggez", tag = "0.6.0-rc0" }
9 9
 glam = "0.12.0"
10
+tiled = "0.9.5"

+ 75 - 0
resources/map1.tmx View File

@@ -0,0 +1,75 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<map version="1.5" tiledversion="1.6.0" orientation="orthogonal" renderorder="right-down" width="112" height="64" tilewidth="5" tileheight="5" infinite="0" nextlayerid="3" nextobjectid="1">
3
+ <tileset firstgid="1" source="terrain.tsx"/>
4
+ <imagelayer id="2" name="background">
5
+  <image source="map1bg.png" width="560" height="320"/>
6
+ </imagelayer>
7
+ <layer id="1" name="terrain" width="112" height="64">
8
+  <data encoding="csv">
9
+1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4,4,4,4,4,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4,4,4,4,4,4,4,4,4,4,5,4,4,5,4,4,4,
10
+1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4,4,4,4,4,4,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4,4,4,4,4,4,4,4,4,4,5,4,4,5,4,4,4,
11
+1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4,4,4,4,4,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,7,7,7,7,7,7,7,7,7,7,7,4,4,4,4,4,4,4,4,4,4,5,4,4,5,4,4,4,
12
+1,1,1,1,1,1,1,1,1,4,4,1,1,1,1,1,4,4,4,4,4,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,6,6,6,6,6,6,6,6,6,7,4,4,4,4,4,4,4,4,4,4,4,4,4,5,4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,4,4,4,4,4,4,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4,4,1,1,1,1,4,4,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,2,4,4,
72
+1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4,7,7,7,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4,4,4,4,4,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,4,4
73
+</data>
74
+ </layer>
75
+</map>

BIN
resources/terrain.png View File


+ 39 - 0
resources/terrain.tsx View File

@@ -0,0 +1,39 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<tileset version="1.5" tiledversion="1.6.0" name="terrain" tilewidth="5" tileheight="5" tilecount="2000" columns="100">
3
+ <image source="terrain.png" width="500" height="100"/>
4
+ <tile id="0">
5
+  <properties>
6
+   <property name="ID" value="ShortGrass"/>
7
+  </properties>
8
+ </tile>
9
+ <tile id="1">
10
+  <properties>
11
+   <property name="ID" value="MiddleGrass"/>
12
+  </properties>
13
+ </tile>
14
+ <tile id="2">
15
+  <properties>
16
+   <property name="ID" value="HighGrass"/>
17
+  </properties>
18
+ </tile>
19
+ <tile id="3">
20
+  <properties>
21
+   <property name="ID" value="Dirt"/>
22
+  </properties>
23
+ </tile>
24
+ <tile id="4">
25
+  <properties>
26
+   <property name="ID" value="Mud"/>
27
+  </properties>
28
+ </tile>
29
+ <tile id="5">
30
+  <properties>
31
+   <property name="ID" value="Concrete"/>
32
+  </properties>
33
+ </tile>
34
+ <tile id="6">
35
+  <properties>
36
+   <property name="ID" value="BrickWall"/>
37
+  </properties>
38
+ </tile>
39
+</tileset>

BIN
resources/terrain.xcf View File


+ 9 - 0
resources/ui.tsx View File

@@ -0,0 +1,9 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<tileset version="1.5" tiledversion="1.6.0" name="ui" tilewidth="5" tileheight="5" tilecount="19200" columns="160">
3
+ <image source="ui.png" width="800" height="600"/>
4
+ <tile id="0">
5
+  <properties>
6
+   <property name="name" value="toto"/>
7
+  </properties>
8
+ </tile>
9
+</tileset>

+ 0 - 2
src/config.rs View File

@@ -32,8 +32,6 @@ pub const DEFAULT_SELECTED_SQUARE_SIDE: f32 = 14.0;
32 32
 pub const DEFAULT_SELECTED_SQUARE_SIDE_HALF: f32 = DEFAULT_SELECTED_SQUARE_SIDE / 2.0;
33 33
 //
34 34
 pub const SCENE_ITEMS_CHANGE_ERR_MSG: &str = "scene_items content change !";
35
-//
36
-pub const DEBUG: bool = true;
37 35
 // Distance from move target point to consider reached
38 36
 pub const MOVE_TO_REACHED_WHEN_DISTANCE_INFERIOR_AT: f32 = 3.0;
39 37
 // Velocity of move vector

+ 1 - 0
src/main.rs View File

@@ -8,6 +8,7 @@ use scene::main::MainState;
8 8
 
9 9
 mod behavior;
10 10
 mod config;
11
+mod map;
11 12
 mod physics;
12 13
 mod scene;
13 14
 mod ui;

+ 176 - 0
src/map/mod.rs View File

@@ -0,0 +1,176 @@
1
+pub mod tile;
2
+
3
+use crate::map::tile::Tile;
4
+use ggez::GameError;
5
+use ggez::GameResult;
6
+use std::collections::HashMap;
7
+use std::fs::File;
8
+use std::io::BufReader;
9
+use std::path::Path;
10
+use tiled::{
11
+    parse_with_path, Image as TiledImage, Image, Layer, LayerData, Map as TiledMap, Orientation,
12
+    PropertyValue, Tile as TiledTile, TiledError, Tileset,
13
+};
14
+
15
+fn get_tile_from_terrain_tileset_with_id(
16
+    terrain_tileset: &Tileset,
17
+    id: u32,
18
+    terrain_image_width: u32,
19
+    terrain_image_height: u32,
20
+) -> GameResult<Tile> {
21
+    for tile in terrain_tileset.tiles.iter() {
22
+        if tile.id == id - terrain_tileset.first_gid {
23
+            let str_id = match tile.properties.get("ID") {
24
+                None => {
25
+                    return GameResult::Err(GameError::ResourceLoadError(format!(
26
+                        "Tile {} have no ID property",
27
+                        id
28
+                    )))
29
+                }
30
+                Some(property_value) => match property_value {
31
+                    PropertyValue::StringValue(str_id) => str_id.clone(),
32
+                    _ => {
33
+                        return GameResult::Err(GameError::ResourceLoadError(format!(
34
+                            "Tile {} must have String ID property value",
35
+                            id
36
+                        )))
37
+                    }
38
+                },
39
+            };
40
+
41
+            let tile_width = terrain_tileset.tile_width;
42
+            let tile_height = terrain_tileset.tile_height;
43
+            let relative_tile_width = tile_width as f32 / terrain_image_width as f32;
44
+            let relative_tile_height = tile_height as f32 / terrain_image_height as f32;
45
+            let len_by_width = terrain_image_width / tile_width;
46
+            let tile_y = tile.id / len_by_width;
47
+            let tile_x = tile.id  - (tile_y * len_by_width);
48
+
49
+            return GameResult::Ok(Tile::from_str_id(
50
+                &str_id,
51
+                tile_width,
52
+                tile_height,
53
+                relative_tile_width,
54
+                relative_tile_height,
55
+                tile_x,
56
+                tile_y,
57
+            ));
58
+        }
59
+    }
60
+
61
+    return GameResult::Err(GameError::ResourceLoadError(format!(
62
+        "No tile with {} found",
63
+        id
64
+    )));
65
+}
66
+
67
+pub struct Map {
68
+    pub tiled_map: TiledMap,
69
+    pub background_image: TiledImage,
70
+    pub terrain_image: TiledImage,
71
+    pub tiles: HashMap<(u32, u32), Tile>,
72
+}
73
+
74
+impl Map {
75
+    pub fn new(map_file_path: &Path) -> GameResult<Self> {
76
+        let map_file = File::open(map_file_path)?;
77
+        let map_file_reader = BufReader::new(map_file);
78
+        let tiled_map = match parse_with_path(map_file_reader, map_file_path) {
79
+            Ok(map) => map,
80
+            Err(e) => {
81
+                return GameResult::Err(GameError::ResourceLoadError(format!(
82
+                    "Fail to parse map: {:?}",
83
+                    e
84
+                )))
85
+            }
86
+        };
87
+
88
+        if &tiled_map.orientation != &Orientation::Orthogonal {
89
+            return GameResult::Err(GameError::ResourceLoadError(
90
+                "Map must be orthogonal orientation".to_string(),
91
+            ));
92
+        }
93
+        // FIXME BS NOW: manage correctly error
94
+        let background_image = match &(tiled_map.image_layers.first().unwrap()).image.as_ref() {
95
+            None => {
96
+                return GameResult::Err(GameError::ResourceLoadError(
97
+                    "No image layer found in map ".to_string(),
98
+                ))
99
+            }
100
+            Some(image) => image.clone(),
101
+        };
102
+
103
+        let terrain_tileset: Tileset = match tiled_map
104
+            .tilesets
105
+            .clone()
106
+            .into_iter()
107
+            .filter(|t| t.name == "terrain")
108
+            .collect::<Vec<Tileset>>()
109
+            .first()
110
+        {
111
+            None => {
112
+                return GameResult::Err(GameError::ResourceLoadError(
113
+                    "No terrain tileset found in map ".to_string(),
114
+                ))
115
+            }
116
+            Some(tileset) => tileset.clone(),
117
+        };
118
+
119
+        let terrain_image = {
120
+            match terrain_tileset.images.first() {
121
+                None => {
122
+                    return GameResult::Err(GameError::ResourceLoadError(
123
+                        "No terrain image found in terrain tileset".to_string(),
124
+                    ))
125
+                }
126
+                Some(terrain_image) => terrain_image.clone(),
127
+            }
128
+        };
129
+
130
+        let terrain_layer: Layer = match tiled_map
131
+            .layers
132
+            .clone()
133
+            .into_iter()
134
+            .filter(|l| l.name == "terrain")
135
+            .collect::<Vec<Layer>>()
136
+            .first()
137
+        {
138
+            None => {
139
+                return GameResult::Err(GameError::ResourceLoadError(
140
+                    "No terrain layer found in map ".to_string(),
141
+                ))
142
+            }
143
+            Some(layer) => layer.clone(),
144
+        };
145
+
146
+        let mut tiles: HashMap<(u32, u32), Tile> = HashMap::new();
147
+
148
+        match terrain_layer.tiles {
149
+            LayerData::Finite(layer_tiles) => {
150
+                for (x, tiles_row) in layer_tiles.iter().enumerate() {
151
+                    for (y, layer_tile) in tiles_row.iter().enumerate() {
152
+                        let tile = get_tile_from_terrain_tileset_with_id(
153
+                            &terrain_tileset,
154
+                            layer_tile.gid,
155
+                            terrain_image.width as u32,
156
+                            terrain_image.height as u32,
157
+                        )?;
158
+                        tiles.insert((y as u32, x as u32), tile);
159
+                    }
160
+                }
161
+            }
162
+            LayerData::Infinite(_) => {
163
+                return GameResult::Err(GameError::ResourceLoadError(
164
+                    "Terrain layer must be finite".to_string(),
165
+                ))
166
+            }
167
+        }
168
+
169
+        GameResult::Ok(Map {
170
+            tiled_map: tiled_map.clone(),
171
+            background_image: background_image.clone(),
172
+            terrain_image,
173
+            tiles,
174
+        })
175
+    }
176
+}

+ 109 - 0
src/map/tile.rs View File

@@ -0,0 +1,109 @@
1
+pub enum TileId {
2
+    ShortGrass,
3
+    MiddleGrass,
4
+    HighGrass,
5
+    Dirt,
6
+    Mud,
7
+    Concrete,
8
+    BrickWall,
9
+}
10
+
11
+pub struct Tile {
12
+    pub id: TileId,
13
+    pub tile_width: u32,
14
+    pub tile_height: u32,
15
+    pub relative_tile_width: f32,
16
+    pub relative_tile_height: f32,
17
+    pub tile_x: u32,
18
+    pub tile_y: u32,
19
+    pub opacity: f32,
20
+}
21
+
22
+impl Tile {
23
+    pub fn from_str_id(
24
+        id: &str,
25
+        tile_width: u32,
26
+        tile_height: u32,
27
+        relative_tile_width: f32,
28
+        relative_tile_height: f32,
29
+        tile_x: u32,
30
+        tile_y: u32,
31
+    ) -> Self {
32
+        match id {
33
+            "ShortGrass" => Self {
34
+                id: TileId::ShortGrass,
35
+                opacity: 0.0,
36
+                tile_width,
37
+                tile_height,
38
+                relative_tile_width,
39
+                relative_tile_height,
40
+                tile_x,
41
+                tile_y,
42
+            },
43
+            "MiddleGrass" => Self {
44
+                id: TileId::MiddleGrass,
45
+                opacity: 0.1,
46
+                tile_width,
47
+                tile_height,
48
+                relative_tile_width,
49
+                relative_tile_height,
50
+                tile_x,
51
+                tile_y,
52
+            },
53
+            "HighGrass" => Self {
54
+                id: TileId::HighGrass,
55
+                opacity: 0.2,
56
+                tile_width,
57
+                tile_height,
58
+                relative_tile_width,
59
+                relative_tile_height,
60
+                tile_x,
61
+                tile_y,
62
+            },
63
+            "Dirt" => Self {
64
+                id: TileId::Dirt,
65
+                opacity: 0.0,
66
+                tile_width,
67
+                tile_height,
68
+                relative_tile_width,
69
+                relative_tile_height,
70
+                tile_x,
71
+                tile_y,
72
+            },
73
+            "Mud" => Self {
74
+                id: TileId::Mud,
75
+                opacity: 0.1,
76
+                tile_width,
77
+                tile_height,
78
+                relative_tile_width,
79
+                relative_tile_height,
80
+                tile_x,
81
+                tile_y,
82
+            },
83
+            "Concrete" => Self {
84
+                id: TileId::Concrete,
85
+                opacity: 0.0,
86
+                tile_width,
87
+                tile_height,
88
+                relative_tile_width,
89
+                relative_tile_height,
90
+                tile_x,
91
+                tile_y,
92
+            },
93
+            "BrickWall" => Self {
94
+                id: TileId::BrickWall,
95
+                opacity: 1.0,
96
+                tile_width,
97
+                tile_height,
98
+                relative_tile_width,
99
+                relative_tile_height,
100
+                tile_x,
101
+                tile_y,
102
+            },
103
+            &_ => {
104
+                // FIXME BS NOW: manage errors
105
+                panic!("Unknown tile id {}", id)
106
+            }
107
+        }
108
+    }
109
+}

+ 1 - 1
src/scene/item.rs View File

@@ -89,7 +89,7 @@ impl SceneItem {
89 89
     }
90 90
 
91 91
     pub fn tick_sprite(&mut self) {
92
-        let sprite_info =self.sprite_info();
92
+        let sprite_info = self.sprite_info();
93 93
         self.current_frame += sprite_info.tick_speed;
94 94
         // TODO: good way to have sprite info ? performant ?
95 95
         if self.current_frame as u16 >= sprite_info.tile_count {

+ 89 - 11
src/scene/main.rs View File

@@ -4,7 +4,7 @@ use std::f32::consts::FRAC_PI_2;
4 4
 
5 5
 use ggez::event::MouseButton;
6 6
 use ggez::graphics::{DrawMode, MeshBuilder, StrokeOptions};
7
-use ggez::input::keyboard::KeyCode;
7
+use ggez::input::keyboard::{KeyCode, pressed_keys};
8 8
 use ggez::timer::check_update_time;
9 9
 use ggez::{event, graphics, input, Context, GameResult};
10 10
 
@@ -12,11 +12,12 @@ use crate::behavior::animate::{digest_current_behavior, digest_current_order, di
12 12
 use crate::behavior::order::Order;
13 13
 use crate::behavior::ItemBehavior;
14 14
 use crate::config::{
15
-    ANIMATE_EACH, DEBUG, DEFAULT_SELECTED_SQUARE_SIDE, DEFAULT_SELECTED_SQUARE_SIDE_HALF,
15
+    ANIMATE_EACH, DEFAULT_SELECTED_SQUARE_SIDE, DEFAULT_SELECTED_SQUARE_SIDE_HALF,
16 16
     DISPLAY_OFFSET_BY, DISPLAY_OFFSET_BY_SPEED, MAX_FRAME_I, META_EACH, MOVE_FAST_VELOCITY,
17 17
     MOVE_HIDE_VELOCITY, MOVE_TO_REACHED_WHEN_DISTANCE_INFERIOR_AT, MOVE_VELOCITY, PHYSICS_EACH,
18 18
     SCENE_ITEMS_CHANGE_ERR_MSG, SPRITE_EACH, TARGET_FPS,
19 19
 };
20
+use crate::map::Map;
20 21
 use crate::physics::util::scene_point_from_window_point;
21 22
 use crate::physics::util::window_point_from_scene_point;
22 23
 use crate::physics::GridPosition;
@@ -29,16 +30,26 @@ use crate::ui::MenuItem;
29 30
 use crate::ui::{SceneItemPrepareOrder, UiComponent, UserEvent};
30 31
 use crate::util::velocity_for_behavior;
31 32
 use crate::{Offset, ScenePoint, WindowPoint};
33
+use std::fs::File;
34
+use std::path::Path;
35
+use std::time::Instant;
32 36
 
33 37
 pub struct MainState {
34 38
     // time
35 39
     frame_i: u32,
40
+    start: Instant,
41
+
42
+    // map
43
+    map: Map,
36 44
 
37 45
     // display
46
+    debug: bool,
47
+    debug_terrain: bool,
38 48
     display_offset: Offset,
39 49
     sprite_sheet_batch: graphics::spritebatch::SpriteBatch,
40 50
     map_batch: graphics::spritebatch::SpriteBatch,
41 51
     ui_batch: graphics::spritebatch::SpriteBatch,
52
+    terrain_batch: graphics::spritebatch::SpriteBatch,
42 53
 
43 54
     // scene items
44 55
     scene_items: Vec<SceneItem>,
@@ -48,6 +59,7 @@ pub struct MainState {
48 59
     physics_events: Vec<PhysicEvent>,
49 60
 
50 61
     // user interactions
62
+    last_key_consumed: HashMap<KeyCode, Instant>,
51 63
     left_click_down: Option<WindowPoint>,
52 64
     right_click_down: Option<WindowPoint>,
53 65
     current_cursor_position: WindowPoint,
@@ -57,14 +69,57 @@ pub struct MainState {
57 69
     scene_item_prepare_order: Option<SceneItemPrepareOrder>,
58 70
 }
59 71
 
72
+
73
+fn update_terrain_batch(mut terrain_batch: graphics::spritebatch::SpriteBatch,  map: &Map) -> graphics::spritebatch::SpriteBatch {
74
+    terrain_batch.clear();
75
+    for ((grid_x, grid_y), tile) in map.tiles.iter() {
76
+            // FIXME pre compute these data
77
+            let src_x = tile.tile_x as f32 * tile.relative_tile_width;
78
+            let src_y = tile.tile_y as f32 * tile.relative_tile_height;
79
+            let dest_x = *grid_x as f32 * tile.tile_width as f32;
80
+            let dest_y = *grid_y as f32 * tile.tile_height as f32;
81
+            terrain_batch.add(
82
+                graphics::DrawParam::new()
83
+                .src(graphics::Rect::new(
84
+                    src_x,
85
+                    src_y,
86
+                    tile.relative_tile_width,
87
+                    tile.relative_tile_height,
88
+                ))
89
+                .dest(ScenePoint::new(
90
+                    dest_x,
91
+                    dest_y,
92
+                ))
93
+            );
94
+        }
95
+
96
+    terrain_batch
97
+}
98
+
60 99
 impl MainState {
61 100
     pub fn new(ctx: &mut Context) -> GameResult<MainState> {
62
-        let sprite_sheet = graphics::Image::new(ctx, "/sprite_sheet.png").unwrap();
63
-        let sprite_sheet_batch = graphics::spritebatch::SpriteBatch::new(sprite_sheet);
64
-        let map = graphics::Image::new(ctx, "/map1bg.png").unwrap();
65
-        let map_batch = graphics::spritebatch::SpriteBatch::new(map);
66
-        let ui = graphics::Image::new(ctx, "/ui.png").unwrap();
67
-        let ui_batch = graphics::spritebatch::SpriteBatch::new(ui);
101
+        let map = Map::new(&Path::new("resources/map1.tmx"))?;
102
+
103
+        // FIXME manage error
104
+        let sprite_sheet_image = graphics::Image::new(ctx, "/sprite_sheet.png").unwrap();
105
+        let sprite_sheet_batch = graphics::spritebatch::SpriteBatch::new(sprite_sheet_image);
106
+
107
+        let map_image = graphics::Image::new(
108
+            ctx,
109
+            &Path::new(&format!("/{}", &map.background_image.source)),
110
+        )
111
+        .unwrap();
112
+        let map_batch = graphics::spritebatch::SpriteBatch::new(map_image);
113
+
114
+        // FIXME manage error
115
+        let ui_image = graphics::Image::new(ctx, "/ui.png").unwrap();
116
+        let ui_batch = graphics::spritebatch::SpriteBatch::new(ui_image);
117
+
118
+        // FIXME manage error
119
+        let terrain_image =
120
+            graphics::Image::new(ctx, format!("/{}", map.terrain_image.source)).unwrap();
121
+        let mut terrain_batch = graphics::spritebatch::SpriteBatch::new(terrain_image);
122
+        terrain_batch = update_terrain_batch(terrain_batch, &map);
68 123
 
69 124
         let mut scene_items = vec![];
70 125
         for x in 0..1 {
@@ -85,13 +140,19 @@ impl MainState {
85 140
 
86 141
         let mut main_state = MainState {
87 142
             frame_i: 0,
143
+            start: Instant::now(),
144
+            map,
145
+            debug: false,
146
+            debug_terrain: false,
88 147
             display_offset: Offset::new(0.0, 0.0),
89 148
             sprite_sheet_batch,
90 149
             map_batch,
91 150
             ui_batch,
151
+            terrain_batch,
92 152
             scene_items,
93 153
             scene_items_by_grid_position: HashMap::new(),
94 154
             physics_events: vec![],
155
+            last_key_consumed: HashMap::new(),
95 156
             left_click_down: None,
96 157
             right_click_down: None,
97 158
             current_cursor_position: WindowPoint::new(0.0, 0.0),
@@ -146,6 +207,19 @@ impl MainState {
146 207
             self.display_offset.y -= display_offset_by;
147 208
         }
148 209
 
210
+        if input::keyboard::is_key_pressed(ctx, KeyCode::F12) {
211
+            if  self.last_key_consumed.get(&KeyCode::F12).unwrap_or(&self.start).elapsed().as_millis() > 250 {
212
+                self.debug = !self.debug;
213
+                self.last_key_consumed.insert(KeyCode::F12, Instant::now());
214
+            }
215
+        }
216
+        if input::keyboard::is_key_pressed(ctx, KeyCode::F10) {
217
+            if  self.last_key_consumed.get(&KeyCode::F10).unwrap_or(&self.start).elapsed().as_millis() > 250 {
218
+                self.debug_terrain = !self.debug_terrain;
219
+                self.last_key_consumed.insert(KeyCode::F10, Instant::now());
220
+            }
221
+        }
222
+
149 223
         while let Some(user_event) = self.user_events.pop() {
150 224
             match user_event {
151 225
                 UserEvent::Click(window_click_point) => self.digest_click(window_click_point),
@@ -376,7 +450,7 @@ impl MainState {
376 450
         &self,
377 451
         mut mesh_builder: MeshBuilder,
378 452
     ) -> GameResult<MeshBuilder> {
379
-        if DEBUG {
453
+        if self.debug {
380 454
             // Draw circle on each scene item position
381 455
             for scene_item in self.scene_items.iter() {
382 456
                 mesh_builder.circle(
@@ -556,15 +630,19 @@ impl event::EventHandler for MainState {
556 630
         scene_mesh_builder = self.update_mesh_builder_with_selection_area(scene_mesh_builder)?;
557 631
         scene_mesh_builder = self.update_mesh_builder_with_prepare_order(scene_mesh_builder)?;
558 632
 
559
-        let scene_mesh = scene_mesh_builder.build(ctx)?;
560 633
         let window_draw_param = graphics::DrawParam::new().dest(window_point_from_scene_point(
561 634
             &ScenePoint::new(0.0, 0.0),
562 635
             &self.display_offset,
563 636
         ));
564 637
 
565 638
         graphics::draw(ctx, &self.map_batch, window_draw_param)?;
639
+        if self.debug_terrain {
640
+            graphics::draw(ctx, &self.terrain_batch, window_draw_param)?;
641
+        }
566 642
         graphics::draw(ctx, &self.sprite_sheet_batch, window_draw_param)?;
567
-        graphics::draw(ctx, &scene_mesh, window_draw_param)?;
643
+        if let Ok(scene_mesh) = scene_mesh_builder.build(ctx) {
644
+            graphics::draw(ctx, &scene_mesh, window_draw_param)?;
645
+        }
568 646
         graphics::draw(ctx, &self.ui_batch, window_draw_param)?;
569 647
 
570 648
         self.sprite_sheet_batch.clear();