Item wrapper

This commit is contained in:
Evan Pratten 2021-04-25 11:37:53 -04:00
parent 0d862760bd
commit 5bb8261119
3 changed files with 76 additions and 13 deletions

View File

@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
pub trait ItemBase {
fn get_cost(&self) -> u32;
fn get_level(&self) -> u8;
fn get_name(&self) -> String;
fn get_description(&self) -> String;
fn get_texture(&self) -> &Texture2D;
@ -59,6 +60,9 @@ impl ItemBase for StunGun {
fn get_texture(&self) -> &Texture2D {
todo!()
}
fn get_level(&self) -> u8 {
self.level
}
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
@ -108,6 +112,9 @@ impl ItemBase for AirBag {
fn get_texture(&self) -> &Texture2D {
todo!()
}
fn get_level(&self) -> u8 {
self.level
}
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
@ -157,6 +164,9 @@ impl ItemBase for Flashlight {
fn get_texture(&self) -> &Texture2D {
todo!()
}
fn get_level(&self) -> u8 {
self.level
}
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
@ -206,4 +216,7 @@ impl ItemBase for Flippers {
fn get_texture(&self) -> &Texture2D {
todo!()
}
fn get_level(&self) -> u8 {
self.level
}
}

View File

@ -1,16 +1,66 @@
use std::marker::PhantomData;
use raylib::prelude::*;
use crate::{items::ItemBase, player::Player, world::World};
use super::itemui::ShopItemUi;
// pub struct ShopItemWrapper<T> {
// bounds: Rectangle,
// ui: ShopItemUi
// }
pub struct ShopItemWrapper<T: ItemBase + Clone> {
bounds: Rectangle,
ui: ShopItemUi,
item: T,
}
// impl ShopItemWrapper<T> {
impl<T: ItemBase + Clone> ShopItemWrapper<T> {
pub fn new(
name: String,
item: T,
from_inventory: &Option<T>,
first_item_bounds: Rectangle,
index: u8,
player: &Player,
) -> Self {
// Build new bounds for the UI row
let new_bounds = Rectangle {
x: first_item_bounds.x,
y: first_item_bounds.y + (first_item_bounds.height + 5.0 * index as f32),
width: first_item_bounds.width,
height: first_item_bounds.height,
};
// pub fn new(name: String, from_inventory: Option<T>, first_item_bounds: Rectangle, index: u8){
Self {
bounds: new_bounds,
ui: ShopItemUi::new(
name,
match from_inventory {
Some(x) => x.get_level(),
None => 0,
},
3,
item.get_cost(),
),
item,
}
}
// }
pub fn get_item(&self) -> &T {
&self.item
}
// }
pub fn can_player_afford(&self, player: &Player) -> bool {
return player.coins >= self.item.get_cost();
}
pub fn purchase(&self, player: &mut Player) -> T {
// Take the currency from the player
player.coins -= self.item.get_cost();
// Return a clone of the item
return self.item.clone();
}
pub fn render(&self, draw_handle: &mut RaylibDrawHandle, player: &Player) {
self.ui.render(draw_handle, self.bounds, self.can_player_afford(player));
}
}

View File

@ -52,11 +52,11 @@ impl World {
Ok(result)
}
pub fn spend_coins(&mut self, count: usize) {
for _ in 0..count {
self.fish.pop();
}
}
// pub fn spend_coins(&mut self, count: usize) {
// for _ in 0..count {
// self.fish.pop();
// }
// }
pub fn reset(&mut self) {
for fish in self.fish.iter_mut() {