diff --git a/src/activities/house/magnet-shader.wgsl b/src/activities/house/magnet-shader.wgsl new file mode 100644 index 0000000..634e42b --- /dev/null +++ b/src/activities/house/magnet-shader.wgsl @@ -0,0 +1,5 @@ +@fragment +fn fs_main(in: VertexOutput) -> @location(0) vec4 { + let global_coord: vec3 = in.world_position; + return vec4(in.color[3],in.color[3],in.color[3], in.color[3]); +} \ No newline at end of file diff --git a/src/activities/house/mod.rs b/src/activities/house/mod.rs index a8f3184..ba04895 100644 --- a/src/activities/house/mod.rs +++ b/src/activities/house/mod.rs @@ -41,18 +41,19 @@ impl HouseState { } } -pub fn draw(state: &crate::State, _ctx: &comfy::EngineContext<'_>) { +pub fn draw(state: &crate::State, _ctx: &mut comfy::EngineContext<'_>) { let Some(house) = state.house() else { error!("How can I render a house when I'm not in one?!?"); return; }; //Draw House - house - .rooms - .get(house.current_room_id) - .unwrap() - .draw(house.human_layer, house.player.can_see_metal(0.1)); + house.rooms.get(house.current_room_id).unwrap().draw( + house.human_layer, + house.player.can_see_metal(0.1), + house.player.get_position(), + _ctx + ); //Draw Grid //state.house.grid.draw(); diff --git a/src/activities/house/player.rs b/src/activities/house/player.rs index d78cfbb..5d18043 100644 --- a/src/activities/house/player.rs +++ b/src/activities/house/player.rs @@ -32,6 +32,9 @@ impl Player { } } + pub fn get_position(&self) -> Vec2 { + self.position + } pub fn draw(&self) { draw_circle(self.position, 0.5, RED, 0); } diff --git a/src/activities/house/room.rs b/src/activities/house/room.rs index 16d19f7..daac405 100644 --- a/src/activities/house/room.rs +++ b/src/activities/house/room.rs @@ -1,7 +1,7 @@ use super::{furniture::Furniture, grid::Grid}; use crate::game; use comfy::{ - draw_rect, draw_rect_outline, draw_sprite, error, random_i32, vec2, EngineContext, HashSet, Index, RandomRange as _, Vec2, GREEN, PURPLE, RED, WHITE + create_reloadable_sprite_shader, draw_rect, draw_rect_outline, draw_sprite, error, hashmap, random_i32, set_uniform_f32, use_default_shader, use_shader, vec2, EngineContext, HashSet, Index, RandomRange as _, ReloadableShaderSource, UniformDef, Vec2, GREEN, PURPLE, RED, WHITE }; use indexmap::IndexSet; @@ -88,14 +88,17 @@ impl Room { empty_spots.swap_remove_index(random_idx) } - fn random_empty_spot_size(empty_spots: &mut IndexSet, size: u8) -> Option { + fn random_empty_spot_size( + empty_spots: &mut IndexSet, + size: u8 + ) -> Option { let mut empty_spots_size = IndexSet::::new(); for &index in empty_spots.iter() { let mut is_valid = true; - for offset in 0..size{ - if !empty_spots.contains(&(index + offset)){ + for offset in 0 .. size { + if !empty_spots.contains(&(index + offset)) { is_valid = false; break; } @@ -109,7 +112,7 @@ impl Room { return None; } let random_idx = usize::gen_range(0, empty_spots_size.len()); - for offset in (0..size).rev() { + for offset in (0 .. size).rev() { empty_spots.swap_remove_index(random_idx + offset as usize); } Some(random_idx as u8) @@ -281,7 +284,8 @@ impl Room { } }, - RoomType::LivingRoom => { + _ => { + //RoomType::LivingRoom => { let has_couch = match u8::gen_range(0, 2) { 0 => false, 1 => true, @@ -293,10 +297,9 @@ impl Room { pos: vec2(pos as f32, 0.0), size: vec2(3.0, 1.0), f: Furniture::new("bedroom", "couch", ctx), - z:0 + z: 0 }); } - } if let Some(pos) = random_empty_spot(&mut empty_spots) { @@ -304,7 +307,7 @@ impl Room { pos: vec2(pos as f32, 0.0), size: vec2(1.0, 2.0), f: Furniture::new("bedroom", "bookshelf", ctx), - z:0 + z: 0 }); } @@ -313,13 +316,11 @@ impl Room { pos: vec2(pos as f32, 0.0), size: vec2(0.5, 0.9), f: Furniture::new("bedroom", "mini_ac", ctx), - z:0 + z: 0 }); } - - }, - - _ => {} + } + //_ => {} } furnitures @@ -375,7 +376,13 @@ impl Room { Grid::new(nodes, connections) } - pub fn draw(&self, human_layer: bool, magnet_layer: bool) { + pub fn draw( + &self, + human_layer: bool, + magnet_layer: bool, + position: Vec2, + _ctx: &mut comfy::EngineContext<'_> + ) { let (width, height) = self.size; draw_rect( @@ -416,7 +423,27 @@ impl Room { } } - if magnet_layer { + if true { + let magnet_shader_id = Some( + create_reloadable_sprite_shader( + &mut _ctx.renderer.shaders.borrow_mut(), + "magnet-shader", + ReloadableShaderSource { + static_source: include_str!("magnet-shader.wgsl").to_string(), + path: "src/activities/house/magnet-shader.wgsl".to_string() + }, + hashmap! { + // "px".to_string() => UniformDef::F32(None), + // "py".to_string() => UniformDef::F32(None), + } + ) + .unwrap() + ).unwrap(); + + use_shader(magnet_shader_id); + // set_uniform_f32("px", position.x); + // set_uniform_f32("py", position.y); + if let Some(texture) = tile.f.get_magnet_texture_handle() { draw_sprite( texture, @@ -426,6 +453,7 @@ impl Room { tile.size * SCALE ); } + use_default_shader(); } if tile.f.is_on() { diff --git a/src/game.rs b/src/game.rs index 9cdbbe1..88a62da 100644 --- a/src/game.rs +++ b/src/game.rs @@ -103,7 +103,7 @@ pub fn update(state: &mut State, engine: &mut EngineContext<'_>) { } } -pub fn draw(state: &State, engine: &EngineContext<'_>) { +pub fn draw(state: &State, engine: &mut EngineContext<'_>) { match state.activity { Activity::House(_) => house::draw(state, engine), Activity::Overworld => overworld::draw(state, engine)