setup shop structure

This commit is contained in:
wm-c 2021-04-24 15:05:44 -04:00
parent 12ed150a17
commit 2844e5ac0b
3 changed files with 210 additions and 18 deletions

View File

@ -3,8 +3,55 @@ use serde::{Serialize, Deserialize};
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
#[serde(tag = "t", content = "c")]
pub enum ShopItems {
StunGun(u8),
AirBag,
Flashlight(u8),
Flippers(u8)
StunGun(u8, u8, String),
AirBag(u8, u8, String),
Flashlight(u8, u8, String),
Flippers(u8, u8, String)
}
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"))]
}
pub fn get_level(item: &ShopItems) -> u8{
match item {
ShopItems::StunGun(x, _, _) => *x,
ShopItems::AirBag(x, _, _) => *x,
ShopItems::Flashlight(x, _, _) => *x,
ShopItems::Flippers(x, _, _) => *x
}
}
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
}
}
pub fn get_name(item: &ShopItems) -> String{
match item {
ShopItems::StunGun(_, _, x) => x.to_string(),
ShopItems::AirBag(_, _, x) => x.to_string(),
ShopItems::Flashlight(_, _, x) => x.to_string(),
ShopItems::Flippers(_, _, x) => x.to_string()
}
}
}

View File

@ -1,15 +1,16 @@
mod hud;
mod playerlogic;
mod shop;
use raylib::prelude::*;
use crate::{
gamecore::{GameCore, GameState},
lib::wrappers::audio::player::AudioPlayer,
};
use crate::{gamecore::{self, GameCore, GameState}, lib::wrappers::audio::player::AudioPlayer};
use self::shop::Shop;
use super::screen::Screen;
#[derive(PartialEq)]
pub enum InGameState {
BUYING,
SWIMMING,
@ -17,12 +18,14 @@ pub enum InGameState {
pub struct InGameScreen {
current_state: InGameState,
shop: Shop,
}
impl InGameScreen {
pub fn new() -> Self {
Self {
current_state: InGameState::SWIMMING,
current_state: InGameState::BUYING,
shop: Shop::new(),
}
}
@ -46,6 +49,31 @@ impl Screen for InGameScreen {
// Calculate DT
let dt = draw_handle.get_time() - game_core.last_frame_time;
// Window dimensions
let win_height = draw_handle.get_screen_height();
let win_width = draw_handle.get_screen_width();
let window_center = Vector2 {
x: (win_width as f32 / 2.0),
y: (win_height as f32 / 2.0),
};
// Creates items for shop
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));
}
if draw_handle.get_time() - game_core.last_state_change_time >= 0.05
&& self.current_state == InGameState::BUYING{
shop::render_shop(draw_handle, game_core, self);
}else{
// Update player movement
playerlogic::update_player_movement(draw_handle, game_core, window_center);
}
// Clear frame
draw_handle.clear_background(Color::BLUE);
@ -54,16 +82,11 @@ impl Screen for InGameScreen {
return Some(GameState::PauseMenu);
}
// Window dimensions
let win_height = draw_handle.get_screen_height();
let win_width = draw_handle.get_screen_width();
let window_center = Vector2 {
x: (win_width as f32 / 2.0),
y: (win_height as f32 / 2.0),
};
// Update player movement
playerlogic::update_player_movement(draw_handle, game_core, window_center);
// Open a 2D context
{
@ -86,6 +109,8 @@ impl Screen for InGameScreen {
// Render the hud
hud::render_hud(draw_handle, game_core, window_center);
// Handle player out of breath
if game_core.player.breath_percent == 0.0 {
return Some(GameState::GameEnd);

120
src/logic/ingame/shop.rs Normal file
View File

@ -0,0 +1,120 @@
use std::u8;
use raylib::prelude::*;
use crate::{gamecore::GameCore, 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 Shop{
shop_items: Vec<Item>,
}
impl Shop {
pub fn new() -> Self {
Self {
shop_items: Vec::new(),
}
}
pub fn create_items(&mut self, screen_dimension: Vector2){
let items = ShopItems::get_inital_items();
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 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();
for box_num in 0..4 {
let x_pose = start_width + box_width * box_num as f32;
item_vec.push(Item{
x_pose: (x_pose as i32),
y_pose: (draw_height as i32),
width: (box_width as i32),
height: (box_height as i32),
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;
}
}
pub fn render_shop(
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;
}
// Get Screen dimemensions
let screen_width = draw_handle.get_screen_width() as f32;
let screen_height = draw_handle.get_screen_height() as f32;
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;
//draw_handle.draw_rectangle(start_index as i32, (screen_height - 20.0 - box_height) as i32, box_width as i32, box_height as i32, Color::BLACK);
// Draws shop boxes
for box_num in 0..4 {
let x_pose = start_width + box_width * box_num as f32;
draw_handle.draw_rectangle_lines(x_pose as i32, draw_height as i32, box_width as i32, box_height as i32, Color::BLACK);
}
}
pub fn shop_logic() {
}