items now purchasable
This commit is contained in:
parent
8a174918c8
commit
c59e6434c1
69
src/items.rs
69
src/items.rs
@ -19,13 +19,59 @@ impl StunGun {
|
||||
duration: 0.75,
|
||||
}
|
||||
}
|
||||
pub fn lvl3() -> Self {
|
||||
Self {
|
||||
range: 80.0,
|
||||
duration: 1.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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)]
|
||||
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)]
|
||||
pub struct Flippers {
|
||||
@ -43,6 +89,11 @@ impl Flippers {
|
||||
speed_increase: 1.5
|
||||
}
|
||||
}
|
||||
pub fn lvl3() -> Self {
|
||||
Self {
|
||||
speed_increase: 1.8
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
|
||||
@ -58,8 +109,8 @@ impl ShopItems{
|
||||
|
||||
pub fn get_inital_items() -> [ShopItems; 4]{
|
||||
|
||||
[ShopItems::StunGun(1, 10, String::from("Stun Gun")), ShopItems::AirBag(1, 10, String::from("Air Bag")),
|
||||
ShopItems::Flashlight(1, 12, String::from("Flash Light")), ShopItems::Flippers(1, 10, String::from("Flippers"))]
|
||||
[ShopItems::StunGun(0, 5, String::from("Stun Gun")), ShopItems::AirBag(0, 5, String::from("Air Bag")),
|
||||
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{
|
||||
|
||||
match item {
|
||||
ShopItems::StunGun(x, _, _) => *x,
|
||||
ShopItems::AirBag(x, _, _) => *x,
|
||||
ShopItems::Flashlight(x, _, _) => *x,
|
||||
ShopItems::Flippers(x, _, _) => *x
|
||||
ShopItems::StunGun(_, x, _) => *x,
|
||||
ShopItems::AirBag(_, x, _) => *x,
|
||||
ShopItems::Flashlight(_, x, _) => *x,
|
||||
ShopItems::Flippers(_, x, _) => *x
|
||||
}
|
||||
|
||||
}
|
||||
@ -99,6 +150,8 @@ impl ShopItems{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -102,7 +102,7 @@ impl Screen for InGameScreen {
|
||||
};
|
||||
|
||||
// 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));
|
||||
}
|
||||
|
||||
|
@ -2,149 +2,204 @@ use std::u8;
|
||||
|
||||
use raylib::prelude::*;
|
||||
|
||||
use crate::{gamecore::GameCore, items::ShopItems};
|
||||
use crate::{gamecore::GameCore, items, items::ShopItems};
|
||||
|
||||
use super::{InGameScreen, InGameState};
|
||||
|
||||
|
||||
pub struct Item{
|
||||
|
||||
x_pose: i32,
|
||||
y_pose: i32,
|
||||
width: i32,
|
||||
height: i32,
|
||||
cost: u8,
|
||||
level: u8,
|
||||
name: String,
|
||||
|
||||
|
||||
pub struct Item {
|
||||
x_pose: i32,
|
||||
y_pose: i32,
|
||||
width: i32,
|
||||
height: i32,
|
||||
cost: u8,
|
||||
level: u8,
|
||||
name: String,
|
||||
}
|
||||
|
||||
|
||||
pub struct Shop{
|
||||
|
||||
shop_items: Vec<Item>,
|
||||
|
||||
pub struct Shop {
|
||||
shop_items: Vec<Item>,
|
||||
}
|
||||
|
||||
impl Shop {
|
||||
|
||||
pub fn new() -> Self {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
shop_items: Vec::new(),
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Creates all the items
|
||||
pub fn create_items(&mut self, screen_dimension: Vector2){
|
||||
// Creates all the items
|
||||
pub fn create_items(&mut self, screen_dimension: Vector2) {
|
||||
// gets every item.. hacky
|
||||
let items = ShopItems::get_inital_items();
|
||||
|
||||
// gets every item.. hacky
|
||||
let items = ShopItems::get_inital_items();
|
||||
// sets sizes any random number is just a number I think looks good
|
||||
let screen_width = screen_dimension.x as f32;
|
||||
let screen_height = screen_dimension.y as f32;
|
||||
|
||||
// sets sizes any random number is just a number I think looks good
|
||||
let screen_width = screen_dimension.x as f32;
|
||||
let screen_height = screen_dimension.y as f32;
|
||||
let box_height = screen_height * 0.15;
|
||||
let box_width = screen_width * 0.1;
|
||||
|
||||
let box_height = screen_height * 0.15;
|
||||
let box_width = screen_width * 0.1;
|
||||
let start_width = screen_width - (box_width * 4.0) - 40.0;
|
||||
let draw_height = screen_height - 20.0 - box_height;
|
||||
|
||||
let start_width = screen_width - (box_width * 4.0) - 40.0;
|
||||
let draw_height = screen_height - 20.0 - box_height;
|
||||
let mut item_vec = Vec::new();
|
||||
|
||||
let mut item_vec = Vec::new();
|
||||
|
||||
for box_num in 0..4 {
|
||||
let x_pose = start_width + box_width * box_num as f32;
|
||||
for box_num in 0..4 {
|
||||
let x_pose = start_width + box_width * box_num as f32;
|
||||
|
||||
// adds an item struct to the item list
|
||||
item_vec.push(Item{
|
||||
x_pose: ((x_pose + (5 * box_num) as f32) as i32),
|
||||
y_pose: (draw_height as i32),
|
||||
width: (box_width as i32),
|
||||
height: (box_height as i32),
|
||||
// Crazy hacky but this gets the data from the enum
|
||||
cost: (ShopItems::get_cost(&items.get(box_num).unwrap())),
|
||||
level: (ShopItems::get_level(&items.get(box_num).unwrap())),
|
||||
name: (ShopItems::get_name(&items.get(box_num).unwrap())),
|
||||
});
|
||||
|
||||
}
|
||||
// adds an item struct to the item list
|
||||
item_vec.push(Item {
|
||||
x_pose: ((x_pose + (5 * box_num) as f32) as i32),
|
||||
y_pose: (draw_height as i32),
|
||||
width: (box_width as i32),
|
||||
height: (box_height as i32),
|
||||
// Crazy hacky but this gets the data from the enum
|
||||
cost: (ShopItems::get_cost(&items.get(box_num).unwrap())),
|
||||
level: (ShopItems::get_level(&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(
|
||||
draw_handle: &mut RaylibDrawHandle,
|
||||
game_core: &mut GameCore,
|
||||
inGameScreen: &mut InGameScreen,
|
||||
) {
|
||||
|
||||
|
||||
draw_handle: &mut RaylibDrawHandle,
|
||||
game_core: &mut GameCore,
|
||||
inGameScreen: &mut InGameScreen,
|
||||
) {
|
||||
// Pressing F exits from buying
|
||||
if draw_handle.is_key_pressed(KeyboardKey::KEY_F) {
|
||||
inGameScreen.current_state = InGameState::SWIMMING;
|
||||
}
|
||||
|
||||
// Pressing F exits from buying
|
||||
if draw_handle.is_key_pressed(KeyboardKey::KEY_F){
|
||||
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
|
||||
for item in inGameScreen.shop.shop_items.iter() {
|
||||
// Draws in accordance to the struct
|
||||
// Draws shop boxes
|
||||
for mut item in inGameScreen.shop.shop_items.iter_mut() {
|
||||
|
||||
|
||||
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 draw_handle.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON) {// && game_core.player.coins >= item.cost as u32
|
||||
|
||||
game_core.world.spend_coins(item.cost.into());
|
||||
game_core.player.coins -= item.cost as u32;
|
||||
|
||||
let x = match &(item.name)[..] {
|
||||
"Stun Gun" => ("stunny"),
|
||||
"Air Bag" => ("Airy"),
|
||||
"Flash Light" => ("Flashy"),
|
||||
"Flippers" => ("Flippy"),
|
||||
_ => ("Among us irl")
|
||||
};
|
||||
|
||||
println!("{}", x);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}else{
|
||||
draw_handle.draw_rectangle_lines(item.x_pose, item.y_pose, item.width, item.height, Color::BLACK);
|
||||
draw_handle.draw_text(&format!("{}: ${}", item.name, item.cost), item.x_pose + 5, item.y_pose + 5, 12, Color::BLACK);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// If hovering on square draw full
|
||||
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.player.coins -= item.cost as u32;
|
||||
|
||||
// Upgrade item in inventory
|
||||
match &(item.name)[..] {
|
||||
"Stun Gun" => {
|
||||
match item.level {
|
||||
0 => {
|
||||
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;
|
||||
item.level += 1;
|
||||
}
|
||||
"Air Bag" => {
|
||||
match item.level {
|
||||
0 => {
|
||||
game_core.player.inventory.air_bag = Some(items::AirBag::lvl1());
|
||||
}
|
||||
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() {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user