Allow friction and temperature sampling

This commit is contained in:
Evan Pratten 2022-04-02 20:13:31 -04:00
parent 76c34ad8db
commit 6429b68654

View File

@ -13,7 +13,7 @@ use raylib::{
texture::Texture2D,
RaylibHandle, RaylibThread,
};
use tiled::{Loader, Map, ResourceCache, ResourcePath, ResourcePathBuf, Tileset};
use tiled::{Loader, Map, PropertyValue, ResourceCache, ResourcePath, ResourcePathBuf, Tileset};
/// Possible errors generated by the map loading process
#[derive(Debug, thiserror::Error)]
@ -131,22 +131,74 @@ impl MapRenderer {
})
}
pub fn sample_friction_at(&self, world_position: na::Vector2<f32>) -> f32 {
pub fn sample_friction_at(&self, world_position: na::Vector2<f32>) -> Option<f32> {
// Convert to a tile position
let tile_position = na::Vector2::new(
(world_position.x / 128.0).floor() as i32,
(world_position.y / 128.0).floor() as i32,
);
todo!()
// Get the first layer
let layer = self.map.layers().next().unwrap();
// Handle the layer type
match layer.layer_type() {
tiled::LayerType::TileLayer(layer) => {
// Get the tile
if let Some(tile) = layer.get_tile(tile_position.x, tile_position.y) {
if let Some(tile) = tile.get_tile() {
if let Some(data) = tile.data.properties.get("friction") {
match data {
PropertyValue::FloatValue(f) => Some(*f),
_ => None,
}
} else {
None
}
} else {
None
}
} else {
None
}
}
_ => None,
}
}
pub fn sample_temperature_at(&self, world_position: na::Vector2<f32>) -> f32 {
pub fn sample_temperature_at(&self, world_position: na::Vector2<f32>) -> Option<f32> {
// Convert to a tile position
let tile_position = na::Vector2::new(
(world_position.x / 128.0).floor() as i32,
(world_position.y / 128.0).floor() as i32,
);
todo!()
// Get the first layer
let layer = self.map.layers().next().unwrap();
// Handle the layer type
match layer.layer_type() {
tiled::LayerType::TileLayer(layer) => {
// Get the tile
if let Some(tile) = layer.get_tile(tile_position.x, tile_position.y) {
if let Some(tile) = tile.get_tile() {
if let Some(data) = tile.data.properties.get("temperature") {
match data {
PropertyValue::FloatValue(f) => Some(*f),
_ => None,
}
} else {
None
}
} else {
None
}
} else {
None
}
}
_ => None,
}
}
pub fn render_map(
@ -330,7 +382,7 @@ impl MapRenderer {
width: tex.width as f32,
height: tex.height as f32,
};
draw_handle.draw_texture_pro(
&tex,
r1,