Time to begin rendering

This commit is contained in:
Evan Pratten 2022-04-02 15:51:45 -04:00
parent 2d944dc2f3
commit 133e71a78f
3 changed files with 31 additions and 11 deletions

View File

@ -42,6 +42,8 @@ pub struct WorldObject {
pub physics_colliders: Vec<ObjectCollider>, pub physics_colliders: Vec<ObjectCollider>,
/// Temperature /// Temperature
pub temperature: Option<f32>, pub temperature: Option<f32>,
/// Friction
pub friction: Option<f32>,
} }
/// Used to reference an object in the world definition /// Used to reference an object in the world definition

View File

@ -1,6 +1,9 @@
use std::{collections::HashMap, path::PathBuf, sync::Arc}; use std::{collections::HashMap, path::PathBuf, sync::Arc};
use crate::asset_manager::{load_texture_from_internal_data, InternalData}; use crate::{
asset_manager::{load_texture_from_internal_data, InternalData},
model::world_object_package::WorldObjectPackage,
};
use nalgebra as na; use nalgebra as na;
use raylib::{ use raylib::{
camera::Camera2D, camera::Camera2D,
@ -75,12 +78,14 @@ impl ResourceCache for ProgramDataTileCache {
pub struct MapRenderer { pub struct MapRenderer {
map: Map, map: Map,
tile_textures: HashMap<PathBuf, Texture2D>, tile_textures: HashMap<PathBuf, Texture2D>,
world_objects: WorldObjectPackage,
} }
impl MapRenderer { impl MapRenderer {
/// Construct a new MapRenderer. /// Construct a new MapRenderer.
pub fn new( pub fn new(
tmx_path: &str, tmx_path: &str,
objects_path: &str,
raylib: &mut RaylibHandle, raylib: &mut RaylibHandle,
raylib_thread: &RaylibThread, raylib_thread: &RaylibThread,
) -> Result<Self, MapRenderError> { ) -> Result<Self, MapRenderError> {
@ -116,7 +121,14 @@ impl MapRenderer {
} }
} }
Ok(Self { map, tile_textures }) // Load the world objects
let world_objects = WorldObjectPackage::load(raylib, raylib_thread, objects_path).unwrap();
Ok(Self {
map,
tile_textures,
world_objects,
})
} }
pub fn sample_friction_at(&self, position: na::Vector2<f32>) -> f32 { pub fn sample_friction_at(&self, position: na::Vector2<f32>) -> f32 {
@ -127,7 +139,12 @@ impl MapRenderer {
todo!() todo!()
} }
pub fn render_map(&self, draw_handle: &mut RaylibMode2D<RaylibDrawHandle>, camera: &Camera2D, show_debug_grid:bool) { pub fn render_map(
&self,
draw_handle: &mut RaylibMode2D<RaylibDrawHandle>,
camera: &Camera2D,
show_debug_grid: bool,
) {
// Get the window corners in world space // Get the window corners in world space
let screen_width = draw_handle.get_screen_width(); let screen_width = draw_handle.get_screen_width();
let screen_height = draw_handle.get_screen_height(); let screen_height = draw_handle.get_screen_height();
@ -187,8 +204,8 @@ impl MapRenderer {
tile_y * tile_height as i32, tile_y * tile_height as i32,
Color::WHITE, Color::WHITE,
); );
} }
if show_debug_grid { if show_debug_grid {
draw_handle.draw_rectangle_lines( draw_handle.draw_rectangle_lines(
tile_x * tile_width as i32, tile_x * tile_width as i32,

View File

@ -7,7 +7,6 @@ use raylib::prelude::*;
use crate::{ use crate::{
discord::DiscordChannel, discord::DiscordChannel,
global_resource_package::GlobalResources, global_resource_package::GlobalResources,
model::world_object_package::WorldObjectPackage,
rendering::utilities::{anim_texture::AnimatedTexture, map_render::MapRenderer}, rendering::utilities::{anim_texture::AnimatedTexture, map_render::MapRenderer},
}; };
@ -16,7 +15,6 @@ pub struct TestFoxScene {
fox_animation: AnimatedTexture, fox_animation: AnimatedTexture,
world_map: MapRenderer, world_map: MapRenderer,
camera: Camera2D, camera: Camera2D,
objects: WorldObjectPackage,
} }
impl TestFoxScene { impl TestFoxScene {
@ -26,9 +24,13 @@ impl TestFoxScene {
let fox = AnimatedTexture::new(raylib_handle, thread, "chr", "testFox").unwrap(); let fox = AnimatedTexture::new(raylib_handle, thread, "chr", "testFox").unwrap();
// Load the map // Load the map
let map_renderer = MapRenderer::new("map_gameMap.tmx", raylib_handle, thread).unwrap(); let map_renderer = MapRenderer::new(
let objects = "map_gameMap.tmx",
WorldObjectPackage::load(raylib_handle, thread, "map_gameMap.objects.json").unwrap(); "map_gameMap.objects.json",
raylib_handle,
thread,
)
.unwrap();
// Create a camera // Create a camera
let camera = Camera2D { let camera = Camera2D {
@ -45,7 +47,6 @@ impl TestFoxScene {
fox_animation: fox, fox_animation: fox,
world_map: map_renderer, world_map: map_renderer,
camera, camera,
objects,
} }
} }