diff --git a/src/items.rs b/src/items.rs index 3866b45..f49fc88 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1,4 +1,9 @@ -use raylib::{math::Rectangle, prelude::RaylibDrawHandle, texture::Texture2D}; +use raylib::{ + color::Color, + math::{Rectangle, Vector2}, + prelude::{RaylibDraw, RaylibDrawHandle}, + texture::Texture2D, +}; use serde::{Deserialize, Serialize}; use crate::resources::GlobalResources; @@ -8,7 +13,12 @@ pub trait ItemBase { fn get_level(&self) -> u8; fn get_name(&self) -> String; fn get_description(&self) -> String; - fn get_texture(&self, draw_handle: &RaylibDrawHandle, resources: &GlobalResources, dest: Rectangle); + fn get_texture( + &self, + draw_handle: &mut RaylibDrawHandle, + resources: &GlobalResources, + dest: Rectangle, + ); } #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)] @@ -59,8 +69,31 @@ impl ItemBase for StunGun { return "Stun your enemies!\nJust don't point it at yourself.".to_string(); } - fn get_texture(&self, draw_handle: &RaylibDrawHandle, resources: &GlobalResources, dest: Rectangle) { - todo!() + fn get_texture( + &self, + draw_handle: &mut RaylibDrawHandle, + resources: &GlobalResources, + dest: Rectangle, + ) { + let texture = match self.get_level() { + 1 => (&resources.stun_gun_one), + 2 => (&resources.stun_gun_two), + 3 | _ => (&resources.stun_gun_three), + }; + + draw_handle.draw_texture_pro( + texture, + Rectangle { + x: 0.0, + y: 0.0, + width: texture.width as f32, + height: texture.height as f32, + }, + dest, + Vector2 { x: 0.0, y: 0.0 }, + 0.0, + Color::WHITE, + ); } fn get_level(&self) -> u8 { self.level @@ -111,8 +144,31 @@ impl ItemBase for AirBag { return "Its.. a bag.\nFilled with air. Duh".to_string(); } - fn get_texture(&self, draw_handle: &RaylibDrawHandle, resources: &GlobalResources, dest: Rectangle) { - todo!() + fn get_texture( + &self, + draw_handle: &mut RaylibDrawHandle, + resources: &GlobalResources, + dest: Rectangle, + ) { + let texture = match self.get_level() { + 1 => (&resources.air_one), + 2 => (&resources.air_two), + 3 | _ => (&resources.air_three), + }; + + draw_handle.draw_texture_pro( + texture, + Rectangle { + x: 0.0, + y: 0.0, + width: texture.width as f32, + height: texture.height as f32, + }, + dest, + Vector2 { x: 0.0, y: 0.0 }, + 0.0, + Color::WHITE, + ); } fn get_level(&self) -> u8 { self.level @@ -163,8 +219,31 @@ impl ItemBase for Flashlight { return "See better for longer".to_string(); } - fn get_texture(&self, draw_handle: &RaylibDrawHandle, resources: &GlobalResources, dest: Rectangle) { - todo!() + fn get_texture( + &self, + draw_handle: &mut RaylibDrawHandle, + resources: &GlobalResources, + dest: Rectangle, + ) { + let texture = match self.get_level() { + 1 => (&resources.flashlight_one), + 2 => (&resources.flashlight_two), + 3 | _ => (&resources.flashlight_three), + }; + + draw_handle.draw_texture_pro( + texture, + Rectangle { + x: 0.0, + y: 0.0, + width: texture.width as f32, + height: texture.height as f32, + }, + dest, + Vector2 { x: 0.0, y: 0.0 }, + 0.0, + Color::WHITE, + ); } fn get_level(&self) -> u8 { self.level @@ -215,8 +294,31 @@ impl ItemBase for Flippers { return "Swim faster, and look stupid\nat the same time!".to_string(); } - fn get_texture(&self, draw_handle: &RaylibDrawHandle, resources: &GlobalResources, dest: Rectangle) { - todo!() + fn get_texture( + &self, + draw_handle: &mut RaylibDrawHandle, + resources: &GlobalResources, + dest: Rectangle, + ) { + let texture = match self.get_level() { + 1 => (&resources.flippers_one), + 2 => (&resources.flippers_two), + 3 | _ => (&resources.flippers_three), + }; + + draw_handle.draw_texture_pro( + texture, + Rectangle { + x: 0.0, + y: 0.0, + width: texture.width as f32, + height: texture.height as f32, + }, + dest, + Vector2 { x: 0.0, y: 0.0 }, + 0.0, + Color::WHITE, + ); } fn get_level(&self) -> u8 { self.level diff --git a/src/logic/shop/mainui.rs b/src/logic/shop/mainui.rs index 55eec57..d33da9c 100644 --- a/src/logic/shop/mainui.rs +++ b/src/logic/shop/mainui.rs @@ -151,15 +151,18 @@ pub fn render_shop( draw_handle.draw_rectangle_rec(box_bounds, Color::WHITE); draw_handle.draw_rectangle_lines_ex(box_bounds, 3, Color::BLACK); - // TODO: draw item sprite - draw_handle.draw_rectangle_v( - Vector2 { + + hovered_item.get_texture( + draw_handle, + &game_core.resources, + Rectangle { x: box_bounds.x + (box_bounds.width / 2.0) - 40.0, y: box_bounds.y + 10.0, - }, - Vector2 { x: 80.0, y: 80.0 }, - Color::BLACK, + width: (80.0), + height: (80.0), + } ); + // Render item description draw_handle.draw_text( diff --git a/src/resources.rs b/src/resources.rs index 1a82e27..83c7b29 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -35,6 +35,25 @@ pub struct GlobalResources { // Shop & items pub shop_background: Texture2D, + pub flashlight_one: Texture2D, + pub flashlight_two: Texture2D, + pub flashlight_three: Texture2D, + + pub stun_gun_one: Texture2D, + pub stun_gun_two: Texture2D, + pub stun_gun_three: Texture2D, + + pub air_one: Texture2D, + pub air_two: Texture2D, + pub air_three: Texture2D, + + pub flippers_one: Texture2D, + pub flippers_two: Texture2D, + pub flippers_three: Texture2D, + + // Treasure + pub transponder: FrameAnimationWrapper, + } impl GlobalResources { @@ -152,6 +171,64 @@ impl GlobalResources { &thread, &Image::load_image("./assets/img/map/shopHighRes.png")?, )?, + flashlight_one: (raylib.load_texture_from_image( + &thread, + &Image::load_image("./assets/img/items/flashlight1.png")?, + )?), + flashlight_two: (raylib.load_texture_from_image( + &thread, + &Image::load_image("./assets/img/items/flashlight2.png")?, + )?), + flashlight_three: (raylib.load_texture_from_image( + &thread, + &Image::load_image("./assets/img/items/flashlight3.png")?, + )?), + stun_gun_one: (raylib.load_texture_from_image( + &thread, + &Image::load_image("./assets/img/items/stun1.png")?, + )?), + stun_gun_two: (raylib.load_texture_from_image( + &thread, + &Image::load_image("./assets/img/items/stun2.png")?, + )?), + stun_gun_three: (raylib.load_texture_from_image( + &thread, + &Image::load_image("./assets/img/items/stun3.png")?, + )?), + air_one: (raylib.load_texture_from_image( + &thread, + &Image::load_image("./assets/img/items/air1.png")?, + )?), + air_two: (raylib.load_texture_from_image( + &thread, + &Image::load_image("./assets/img/items/air2.png")?, + )?), + air_three: (raylib.load_texture_from_image( + &thread, + &Image::load_image("./assets/img/items/air3.png")?, + )?), + flippers_one: (raylib.load_texture_from_image( + &thread, + &Image::load_image("./assets/img/items/flippers1.png")?, + )?), + flippers_two: (raylib.load_texture_from_image( + &thread, + &Image::load_image("./assets/img/items/flippers2.png")?, + )?), + flippers_three: (raylib.load_texture_from_image( + &thread, + &Image::load_image("./assets/img/items/flippers3.png")?, + )?), + transponder: FrameAnimationWrapper::new( + raylib.load_texture_from_image( + &thread, + &Image::load_image("./assets/img/map/transponder.png")?, + )?, + Vector2 { x: 10.0, y: 20.0 }, + 6, + 1, + ), + }) } }