Merge pull request #26 from Ewpratten/ewpratten/tile_metadata

Allow sampling of tile metadata
This commit is contained in:
Evan Pratten 2022-04-02 20:21:38 -04:00 committed by GitHub
commit c80e0a7311
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 113 additions and 11 deletions

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,114 @@ 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!()
// If there is an object here, let it override the output
for obj_ref in &self.world_objects.object_references {
if obj_ref.position.x == tile_position.x as f32
&& obj_ref.position.y == tile_position.y as f32
{
// Get access to the actual object definition
let object_key = format!("{}:{}", obj_ref.kind, obj_ref.name);
let obj_def = self
.world_objects
.object_definitions
.get(&object_key)
.unwrap();
// Check if there is a friction property
if let Some(friction) = obj_def.friction {
return Some(friction);
}
}
}
// 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!()
// If there is an object here, let it override the output
for obj_ref in &self.world_objects.object_references {
if obj_ref.position.x == tile_position.x as f32
&& obj_ref.position.y == tile_position.y as f32
{
// Get access to the actual object definition
let object_key = format!("{}:{}", obj_ref.kind, obj_ref.name);
let obj_def = self
.world_objects
.object_definitions
.get(&object_key)
.unwrap();
// Check if there is a temperature property
if let Some(temperature) = obj_def.temperature {
return Some(temperature);
}
}
}
// 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 +422,7 @@ impl MapRenderer {
width: tex.width as f32,
height: tex.height as f32,
};
draw_handle.draw_texture_pro(
&tex,
r1,

View File

@ -30,8 +30,13 @@ impl PlayableScene {
thread: &raylib::RaylibThread,
constants: &ProjectConstants,
) -> Self {
let map_renderer = MapRenderer::new("map_gameMap.tmx", "map_gameMap.objects.json", raylib_handle, thread).unwrap();
let map_renderer = MapRenderer::new(
"map_gameMap.tmx",
"map_gameMap.objects.json",
raylib_handle,
thread,
)
.unwrap();
// Load the game music
let game_soundtrack =
@ -105,10 +110,15 @@ impl PlayableScene {
let mut ctx2d = draw.begin_mode2D(self.camera);
// Render the map
self.world_map
.render_map(&mut ctx2d, &self.camera, true, self.player.position);
self.world_map.render_map(&mut ctx2d, &self.camera, true, self.player.position);
let player_size = (constants.tile_size as f32 * constants.player.start_size * self.player.size) as i32;
// NOTE: This is how to check friction and temperature
let current_friction = self.world_map.sample_friction_at(self.player.position);
let current_temperature = self.world_map.sample_temperature_at(self.player.position);
let player_size =
(constants.tile_size as f32 * constants.player.start_size * self.player.size) as i32;
ctx2d.draw_rectangle(
self.player.position[0] as i32 - player_size / 2,