items now purchasable

This commit is contained in:
wm-c 2021-04-24 22:52:40 -04:00
parent 8a174918c8
commit c59e6434c1
3 changed files with 233 additions and 125 deletions

View File

@ -19,13 +19,59 @@ impl StunGun {
duration: 0.75, duration: 0.75,
} }
} }
pub fn lvl3() -> Self {
Self {
range: 80.0,
duration: 1.0,
}
}
} }
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)] #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct AirBag; pub struct AirBag{
extra_oxygen: u32,
}
impl AirBag {
pub fn lvl1() -> Self {
Self {
extra_oxygen: 15,
}
}
pub fn lvl2() -> Self {
Self {
extra_oxygen: 30,
}
}
pub fn lvl3() -> Self {
Self {
extra_oxygen: 45,
}
}
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)] #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct Flashlight; pub struct Flashlight{
power_level: f32,
}
impl Flashlight{
pub fn lvl1() -> Self {
Self {
power_level: 0.25,
}
}
pub fn lvl2() -> Self {
Self {
power_level: 0.5,
}
}
pub fn lvl3() -> Self {
Self {
power_level: 1.0,
}
}
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)] #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct Flippers { pub struct Flippers {
@ -43,6 +89,11 @@ impl Flippers {
speed_increase: 1.5 speed_increase: 1.5
} }
} }
pub fn lvl3() -> Self {
Self {
speed_increase: 1.8
}
}
} }
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)] #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
@ -58,8 +109,8 @@ impl ShopItems{
pub fn get_inital_items() -> [ShopItems; 4]{ pub fn get_inital_items() -> [ShopItems; 4]{
[ShopItems::StunGun(1, 10, String::from("Stun Gun")), ShopItems::AirBag(1, 10, String::from("Air Bag")), [ShopItems::StunGun(0, 5, String::from("Stun Gun")), ShopItems::AirBag(0, 5, String::from("Air Bag")),
ShopItems::Flashlight(1, 12, String::from("Flash Light")), ShopItems::Flippers(1, 10, String::from("Flippers"))] ShopItems::Flashlight(0, 5, String::from("Flash Light")), ShopItems::Flippers(0, 5, String::from("Flippers"))]
} }
@ -80,10 +131,10 @@ impl ShopItems{
pub fn get_cost(item: &ShopItems) -> u8{ pub fn get_cost(item: &ShopItems) -> u8{
match item { match item {
ShopItems::StunGun(x, _, _) => *x, ShopItems::StunGun(_, x, _) => *x,
ShopItems::AirBag(x, _, _) => *x, ShopItems::AirBag(_, x, _) => *x,
ShopItems::Flashlight(x, _, _) => *x, ShopItems::Flashlight(_, x, _) => *x,
ShopItems::Flippers(x, _, _) => *x ShopItems::Flippers(_, x, _) => *x
} }
} }
@ -100,6 +151,8 @@ impl ShopItems{
} }
} }

View File

@ -102,7 +102,7 @@ impl Screen for InGameScreen {
}; };
// Creates items for shop // Creates items for shop
if draw_handle.get_time() - game_core.last_state_change_time >= 0.05{ if draw_handle.get_time() - game_core.last_state_change_time <= 0.05{
self.shop.create_items(Vector2::new(win_width as f32, win_height as f32)); self.shop.create_items(Vector2::new(win_width as f32, win_height as f32));
} }

View File

@ -2,13 +2,11 @@ use std::u8;
use raylib::prelude::*; use raylib::prelude::*;
use crate::{gamecore::GameCore, items::ShopItems}; use crate::{gamecore::GameCore, items, items::ShopItems};
use super::{InGameScreen, InGameState}; use super::{InGameScreen, InGameState};
pub struct Item {
pub struct Item{
x_pose: i32, x_pose: i32,
y_pose: i32, y_pose: i32,
width: i32, width: i32,
@ -16,29 +14,21 @@ pub struct Item{
cost: u8, cost: u8,
level: u8, level: u8,
name: String, name: String,
} }
pub struct Shop {
pub struct Shop{
shop_items: Vec<Item>, shop_items: Vec<Item>,
} }
impl Shop { impl Shop {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
shop_items: Vec::new(), shop_items: Vec::new(),
} }
} }
// Creates all the items // Creates all the items
pub fn create_items(&mut self, screen_dimension: Vector2){ pub fn create_items(&mut self, screen_dimension: Vector2) {
// gets every item.. hacky // gets every item.. hacky
let items = ShopItems::get_inital_items(); let items = ShopItems::get_inital_items();
@ -58,7 +48,7 @@ impl Shop {
let x_pose = start_width + box_width * box_num as f32; let x_pose = start_width + box_width * box_num as f32;
// adds an item struct to the item list // adds an item struct to the item list
item_vec.push(Item{ item_vec.push(Item {
x_pose: ((x_pose + (5 * box_num) as f32) as i32), x_pose: ((x_pose + (5 * box_num) as f32) as i32),
y_pose: (draw_height as i32), y_pose: (draw_height as i32),
width: (box_width as i32), width: (box_width as i32),
@ -68,83 +58,148 @@ impl Shop {
level: (ShopItems::get_level(&items.get(box_num).unwrap())), level: (ShopItems::get_level(&items.get(box_num).unwrap())),
name: (ShopItems::get_name(&items.get(box_num).unwrap())), name: (ShopItems::get_name(&items.get(box_num).unwrap())),
}); });
} }
self.shop_items = item_vec; self.shop_items = item_vec;
} }
} }
pub fn render_shop( pub fn render_shop(
draw_handle: &mut RaylibDrawHandle, draw_handle: &mut RaylibDrawHandle,
game_core: &mut GameCore, game_core: &mut GameCore,
inGameScreen: &mut InGameScreen, inGameScreen: &mut InGameScreen,
) { ) {
// Pressing F exits from buying // Pressing F exits from buying
if draw_handle.is_key_pressed(KeyboardKey::KEY_F){ if draw_handle.is_key_pressed(KeyboardKey::KEY_F) {
inGameScreen.current_state = InGameState::SWIMMING; inGameScreen.current_state = InGameState::SWIMMING;
} }
let mouse_position = draw_handle.get_mouse_position(); let mouse_position = draw_handle.get_mouse_position();
draw_handle.draw_text(
&format!("Coins: {}", game_core.player.coins),
15,
15,
30,
Color::WHITE,
);
// Draws shop boxes // Draws shop boxes
for item in inGameScreen.shop.shop_items.iter() { for mut item in inGameScreen.shop.shop_items.iter_mut() {
// Draws in accordance to the struct
if mouse_position.x >= item.x_pose as f32 &&
mouse_position.x <= item.x_pose as f32 + item.width as f32 &&
mouse_position.y >= item.y_pose as f32 &&
mouse_position.y <= item.y_pose as f32 + item.width as f32{
draw_handle.draw_rectangle(item.x_pose, item.y_pose, item.width, item.height, Color::BLACK);
// MAKE SURE TO CHECK VALUE!!!!!!!!!!!!!!!!!!!!!!!!!! // If hovering on square draw full
if draw_handle.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON) {// && game_core.player.coins >= item.cost as u32 if mouse_position.x >= item.x_pose as f32
&& mouse_position.x <= item.x_pose as f32 + item.width as f32
&& mouse_position.y >= item.y_pose as f32
&& mouse_position.y <= item.y_pose as f32 + item.width as f32
{
// Draw rect
draw_handle.draw_rectangle(
item.x_pose,
item.y_pose,
item.width,
item.height,
Color::BLACK,
);
// Preform purchasing functions
if draw_handle.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON)
&& game_core.player.coins >= item.cost as u32
{
// Remove currency
game_core.world.spend_coins(item.cost.into()); game_core.world.spend_coins(item.cost.into());
game_core.player.coins -= item.cost as u32; game_core.player.coins -= item.cost as u32;
let x = match &(item.name)[..] { // Upgrade item in inventory
"Stun Gun" => ("stunny"), match &(item.name)[..] {
"Air Bag" => ("Airy"), "Stun Gun" => {
"Flash Light" => ("Flashy"), match item.level {
"Flippers" => ("Flippy"), 0 => {
_ => ("Among us irl") game_core.player.inventory.stun_gun = Some(items::StunGun::lvl1())
}
1 => {
game_core.player.inventory.stun_gun = Some(items::StunGun::lvl2())
}
2 => {
game_core.player.inventory.stun_gun = Some(items::StunGun::lvl3())
}
_ => (return),
}; };
item.cost += 5;
println!("{}", x); item.level += 1;
} }
"Air Bag" => {
match item.level {
}else{ 0 => {
draw_handle.draw_rectangle_lines(item.x_pose, item.y_pose, item.width, item.height, Color::BLACK); game_core.player.inventory.air_bag = Some(items::AirBag::lvl1());
draw_handle.draw_text(&format!("{}: ${}", item.name, item.cost), item.x_pose + 5, item.y_pose + 5, 12, Color::BLACK);
} }
1 => {
game_core.player.inventory.air_bag = Some(items::AirBag::lvl2());
} }
2 => {
game_core.player.inventory.air_bag = Some(items::AirBag::lvl3());
}
_ => (return),
};
item.cost += 5;
item.level += 1;
}
"Flash Light" => {
match item.level {
0 => {
game_core.player.inventory.flashlight = Some(items::Flashlight::lvl1());
}
1 => {
game_core.player.inventory.flashlight = Some(items::Flashlight::lvl2());
}
2 => {
game_core.player.inventory.flashlight = Some(items::Flashlight::lvl3());
}
_ => (return),
};
item.cost += 5;
item.level += 1;
},
"Flippers" => {
match item.level {
0 => {
game_core.player.inventory.flippers = Some(items::Flippers::lvl1());
}
1 => {
game_core.player.inventory.flippers = Some(items::Flippers::lvl2());
}
2 => {
game_core.player.inventory.flippers = Some(items::Flippers::lvl3());
}
_ => (return),
};
item.cost += 5;
item.level += 1;
},
_ => (return),
};
}
} else {
// outlines if not hovered
draw_handle.draw_rectangle_lines(
item.x_pose,
item.y_pose,
item.width,
item.height,
Color::BLACK,
);
}
// Draw text about object
draw_handle.draw_text(
&format!("{}: ${}", item.name, item.cost),
item.x_pose + 5,
item.y_pose + 5,
12,
Color::BLACK,
);
}
} }
pub fn shop_logic() {
}