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
use super::*;
use bevy::platform::hash::FixedHasher;
// use bevy_ecs_tilemap::prelude::*;
use std::hash::{BuildHasher, Hash, Hasher};
pub(crate) fn plugin(app: &mut App) {
#[cfg(feature = "scripting")]
lua::plugin(app);
}
impl super::Pico8<'_, '_> {
fn sprite_map(&self, map_index: Option<usize>) -> Result<&SpriteMap, Error> {
let index = map_index.unwrap_or(0);
self.pico8_asset()?
.maps
.get(index)
.ok_or(Error::NoSuch(format!("map index {index}").into()))
}
// fn sprite_map_mut(&mut self, map_index: Option<usize>) -> Result<&mut SpriteMap, Error> {
// let index = map_index.unwrap_or(0);
// self.pico8_asset_mut()?
// .maps
// .get_mut(index)
// .ok_or(Error::NoSuch(format!("map index {index}").into()))
// }
pub fn map(
&mut self,
map_pos: UVec2,
mut screen_start: Vec2,
size: UVec2,
mask: Option<u8>,
map_index: Option<usize>,
) -> Result<Entity, Error> {
trace!("map");
let map_index = map_index.unwrap_or(0);
screen_start = self.state.draw_state.apply_camera_delta(screen_start);
if cfg!(feature = "negate-y") {
screen_start.y = -screen_start.y;
}
let hash = {
let mut hasher = FixedHasher.build_hasher();
"map".hash(&mut hasher);
map_pos.hash(&mut hasher);
self.state.palette.hash(&mut hasher);
self.state.pal_map.hash(&mut hasher);
size.hash(&mut hasher);
mask.inspect(|m| m.hash(&mut hasher));
map_index.hash(&mut hasher);
hasher.finish()
};
let rect = URect {
min: map_pos,
max: map_pos + size,
};
self.state.draw_state.mark_drawn();
// See if there's already an entity available.
if let Some(id) = self.resurrect(hash, screen_start) {
// trace!("Resurrect map with hash {hash}");
return Ok(id);
}
// match self.sprite_map(map_index)?.clone() {
// SpriteMap::P8(map) => {
// trace!("Create map with hash {hash}");
// let map_size = TilemapSize::from(size);
let clearable = Clearable::new(self.defaults.time_to_live).with_hash(hash);
// let tile_storage = TileStorage::empty(map_size);
let gfx_material = self.gfx_material().clone();
let sprite_sheet = self
.pico8_asset()?
.sprite_sheets
.get(self.state.sprite_sheet_index)
.ok_or(Error::NoSuch("Sprite sheet for map".into()))?;
let map_entity = self
.commands
.spawn((
Name::new("map"),
Transform::from_translation(screen_start.extend(clearable.suggest_z())),
Visibility::Inherited,
clearable,
P8SpriteMap {
map_index,
sprite_sheet: sprite_sheet.clone(),
gfx_material,
rect,
mask,
},
))
.id();
Ok(map_entity)
// }
// #[cfg(feature = "level")]
// SpriteMap::Level(map) => Ok(map.map(screen_start, 0, &mut self.commands)),
// }
}
pub fn mget(
&self,
pos: Vec2,
map_index: Option<usize>,
_layer_index: Option<usize>,
) -> Result<usize, Error> {
let map: &SpriteMap = self.sprite_map(map_index)?;
match *map {
SpriteMap::P8(ref handle) => {
let map = self
.p8_maps
.get(handle)
.ok_or_else(|| Error::NoSuch("map for handle".into()))?;
let i = (pos.x as u32 + pos.y as u32 * MAP_COLUMNS) as usize;
Ok(*map.get(i).ok_or_else(|| {
Error::NoSuch(
format!(
"map index {i} with length {} {}",
map.len(),
if i > 0x1000 {
"; consider using the '--shared-data=map' argument."
} else {
""
}
)
.into(),
)
})? as usize)
}
#[cfg(feature = "level")]
SpriteMap::Level(ref map) => self.tiled.mget(map, pos, map_index, _layer_index).ok(),
}
}
pub fn mset(
&mut self,
pos: Vec2,
sprite_index: usize,
map_index: Option<usize>,
_layer_index: Option<usize>,
) -> Result<(), Error> {
let map = self.sprite_map(map_index)?.clone();
match map {
SpriteMap::P8(handle) => {
let map = self
.p8_maps
.get_mut(&handle)
.ok_or_else(|| Error::NoSuch("map for handle".into()))?;
map.get_mut((pos.x as u32 + pos.y as u32 * MAP_COLUMNS) as usize)
.map(|value| *value = sprite_index as u8)
.ok_or_else(|| Error::NoSuch("map entry".into()))
}
#[cfg(feature = "level")]
SpriteMap::Level(map) => {
todo!()
// self.tiled
// .mset(map, pos, sprite_index, map_index, layer_index)
}
}
}
}
#[cfg(feature = "scripting")]
mod lua {
use super::*;
use crate::{DropPolicy, N9Entity, pico8::lua::with_pico8};
use bevy_mod_scripting::bindings::{
ReflectReference,
function::{
into_ref::IntoScriptRef,
namespace::{GlobalNamespace, NamespaceBuilder},
script_function::FunctionCallContext,
},
};
pub(crate) fn plugin(app: &mut App) {
let world = app.world_mut();
NamespaceBuilder::<GlobalNamespace>::new_unregistered(world)
.register(
"mget",
|ctx: FunctionCallContext,
x: f32,
y: f32,
map_index: Option<usize>,
layer_index: Option<usize>| {
with_pico8(&ctx, move |pico8| {
pico8.mget(Vec2::new(x, y), map_index, layer_index)
})
},
)
.register(
"mset",
|ctx: FunctionCallContext,
x: f32,
y: f32,
v: usize,
map_index: Option<usize>,
layer_index: Option<usize>| {
with_pico8(&ctx, move |pico8| {
pico8.mset(Vec2::new(x, y), v, map_index, layer_index)
})
},
)
// map( celx, cely, sx, sy, celw, celh, [layer] )
.register(
"map",
|ctx: FunctionCallContext,
celx: Option<u32>,
cely: Option<u32>,
sx: Option<f32>,
sy: Option<f32>,
celw: Option<u32>,
celh: Option<u32>,
layer: Option<u8>,
map_index: Option<usize>| {
let id = with_pico8(&ctx, move |pico8| {
pico8.map(
UVec2::new(celx.unwrap_or(0), cely.unwrap_or(0)),
Vec2::new(sx.unwrap_or(0.0), sy.unwrap_or(0.0)),
UVec2::new(celw.unwrap_or(128), celh.unwrap_or(128)),
layer,
map_index,
)
})?;
let entity = N9Entity {
entity: id,
drop: DropPolicy::Nothing,
};
let world = ctx.world()?;
let reference = {
let allocator = world.allocator();
let mut allocator = allocator.write();
ReflectReference::new_allocated(entity, &mut allocator)
};
ReflectReference::into_script_ref(reference, world)
},
);
}
}