This repository has been archived by the owner on Jan 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
234 lines (201 loc) · 6.93 KB
/
build.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
const LEVELS: &[&str] = &[
"1-1.json", "1-2.json", "1-3.json", "1-4.json", "1-5.json", "1-6.json", "1-7.json", "1-8.json",
"2-4.json", "2-2.json", "2-1.json", "2-3.json",
];
fn main() {
let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR environment variable must be specified");
tiled_export::export_tilemap(&out_dir).expect("Failed to export tilemap");
for &level in LEVELS {
tiled_export::export_level(&out_dir, level).expect("Failed to export level");
}
}
mod tiled_export {
use serde::Deserialize;
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufReader, BufWriter, Write};
const COLLISION_TILE: i32 = 1;
const KILL_TILE: i32 = 2;
const WIN_TILE: i32 = 4;
pub fn export_tilemap(out_dir: &str) -> std::io::Result<()> {
let filename = "map/tilemap.json";
println!("cargo:rerun-if-changed={}", filename);
let file = File::open(filename)?;
let reader = BufReader::new(file);
let tilemap: TiledTilemap = serde_json::from_reader(reader)?;
let output_file = File::create(format!("{}/tilemap.rs", out_dir))?;
let mut writer = BufWriter::new(output_file);
let tile_data: HashMap<_, _> = tilemap
.tiles
.iter()
.map(|tile| {
(
tile.id,
match tile.tile_type.as_str() {
"Collision" => COLLISION_TILE,
"Kill" => KILL_TILE,
"Win" => WIN_TILE,
_ => 0,
},
)
})
.collect();
let tile_info = (0..tilemap.tilecount)
.map(|id| *tile_data.get(&id).unwrap_or(&0))
.map(|tile_type| tile_type.to_string())
.collect::<Vec<String>>()
.join(", ");
writeln!(
&mut writer,
"pub const COLLISION_TILE: i32 = {};",
COLLISION_TILE
)?;
writeln!(&mut writer, "pub const KILL_TILE: i32 = {};", KILL_TILE)?;
writeln!(&mut writer, "pub const WIN_TILE: i32 = {};", WIN_TILE)?;
writeln!(
&mut writer,
"pub const TILE_DATA: &[u32] = &[{}];",
tile_info
)?;
Ok(())
}
pub fn export_level(out_dir: &str, level_file: &str) -> std::io::Result<()> {
let filename = format!("map/{}", level_file);
println!("cargo:rerun-if-changed={}", filename);
let file = File::open(filename)?;
let reader = BufReader::new(file);
let level: TiledLevel = serde_json::from_reader(reader)?;
let output_file = File::create(format!("{}/{}.rs", out_dir, level_file))?;
let mut writer = BufWriter::new(output_file);
let layer_1 = level.layers[0]
.data
.as_ref()
.expect("Expected first layer to be a tile layer")
.iter()
.map(|id| get_map_id(*id).to_string())
.collect::<Vec<_>>()
.join(", ");
let layer_2 = level.layers[1]
.data
.as_ref()
.expect("Expected second layer to be a tile layer")
.iter()
.map(|id| get_map_id(*id).to_string())
.collect::<Vec<_>>()
.join(", ");
writeln!(&mut writer, "const WIDTH: u32 = {};", level.width)?;
writeln!(&mut writer, "const HEIGHT: u32 = {};", level.height)?;
writeln!(&mut writer, "const TILEMAP: &[u16] = &[{}];", layer_1)?;
writeln!(&mut writer, "const BACKGROUND: &[u16] = &[{}];", layer_2)?;
let objects = level.layers[2]
.objects
.as_ref()
.expect("Expected third layer to be an object layer")
.iter()
.map(|object| (&object.object_type, (object.x, object.y)));
let mut snails = vec![];
let mut slimes = vec![];
let mut enemy_stops = vec![];
let mut player_start = None;
for (object_type, (x, y)) in objects {
match object_type.as_str() {
"Snail Spawn" => snails.push((x, y)),
"Slime Spawn" => slimes.push((x, y)),
"Player Start" => player_start = Some((x, y)),
"Enemy Stop" => enemy_stops.push((x, y)),
_ => panic!("Unknown object type {}", object_type),
}
}
let player_start = player_start.expect("Need a start place for the player");
let slimes_str = slimes
.iter()
.map(|slime| format!("({}, {})", slime.0, slime.1))
.collect::<Vec<_>>()
.join(", ");
let snails_str = snails
.iter()
.map(|slime| format!("({}, {})", slime.0, slime.1))
.collect::<Vec<_>>()
.join(", ");
let enemy_stop_str = enemy_stops
.iter()
.map(|enemy_stop| format!("({}, {})", enemy_stop.0, enemy_stop.1))
.collect::<Vec<_>>()
.join(", ");
writeln!(
&mut writer,
"const SNAILS: &[(i32, i32)] = &[{}];",
snails_str
)?;
writeln!(
&mut writer,
"const SLIMES: &[(i32, i32)] = &[{}];",
slimes_str
)?;
writeln!(
&mut writer,
"const ENEMY_STOPS: &[(i32, i32)] = &[{}];",
enemy_stop_str
)?;
writeln!(
&mut writer,
"const START_POS: (i32, i32) = ({}, {});",
player_start.0, player_start.1
)?;
writeln!(
&mut writer,
r#"
use crate::Level;
use agb::number::Vector2D;
pub const fn get_level() -> Level {{
Level {{
background: &TILEMAP,
foreground: &BACKGROUND,
dimensions: Vector2D {{x: WIDTH, y: HEIGHT}},
collision: &crate::map_tiles::tilemap::TILE_DATA,
enemy_stops: &ENEMY_STOPS,
slimes: &SLIMES,
snails: &SNAILS,
start_pos: START_POS,
}}
}}
"#
)?;
Ok(())
}
fn get_map_id(id: i32) -> i32 {
match id {
0 => 10,
i => i - 1,
}
}
#[derive(Deserialize)]
struct TiledLevel {
layers: Vec<TiledLayer>,
width: i32,
height: i32,
}
#[derive(Deserialize)]
struct TiledLayer {
data: Option<Vec<i32>>,
objects: Option<Vec<TiledObject>>,
}
#[derive(Deserialize)]
struct TiledObject {
#[serde(rename = "type")]
object_type: String,
x: i32,
y: i32,
}
#[derive(Deserialize)]
struct TiledTilemap {
tiles: Vec<TiledTile>,
tilecount: i32,
}
#[derive(Deserialize)]
struct TiledTile {
id: i32,
#[serde(rename = "type")]
tile_type: String,
}
}