diff options
| author | JAROWMR <jarorutjes07@gmail.com> | 2025-01-09 21:14:35 +0100 | 
|---|---|---|
| committer | JAROWMR <jarorutjes07@gmail.com> | 2025-01-09 21:14:35 +0100 | 
| commit | f6683fa71fa670cfbe6aa6233fafb869295da11a (patch) | |
| tree | a46ee1d8856b5f0e0298abbab9c92ba21509d830 | |
| parent | 296c7b8becc04fa0eeef7c732e5c62a26de45570 (diff) | |
added shop functionality
| -rw-r--r-- | game/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | game/menus/ButtonSubScene.cpp | 10 | ||||
| -rw-r--r-- | game/menus/ButtonSubScene.h | 2 | ||||
| -rw-r--r-- | game/menus/shop/ButtonBuySelectBubbleScript.cpp | 36 | ||||
| -rw-r--r-- | game/menus/shop/ButtonBuySelectBubbleScript.h | 14 | ||||
| -rw-r--r-- | game/menus/shop/ButtonBuySelectBulletScript.cpp | 36 | ||||
| -rw-r--r-- | game/menus/shop/ButtonBuySelectBulletScript.h | 14 | ||||
| -rw-r--r-- | game/menus/shop/ShopLoadScript.cpp | 131 | ||||
| -rw-r--r-- | game/menus/shop/ShopLoadScript.h | 10 | ||||
| -rw-r--r-- | game/menus/shop/ShopMenuScene.cpp | 103 | ||||
| -rw-r--r-- | game/menus/shop/Shopconfig.h | 16 | 
11 files changed, 366 insertions, 9 deletions
| diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 9dfd2b4..1a03b20 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -33,6 +33,9 @@ target_sources(main PUBLIC  	# mainscenes  	GameScene.cpp  	menus/shop/ShopMenuScene.cpp +	menus/shop/ShopLoadScript.cpp +	menus/shop/ButtonBuySelectBubbleScript.cpp +	menus/shop/ButtonBuySelectBulletScript.cpp  	menus/mainmenu/MainMenuScene.cpp  	PreviewScene.cpp  	main.cpp diff --git a/game/menus/ButtonSubScene.cpp b/game/menus/ButtonSubScene.cpp index 436ff5f..baf154c 100644 --- a/game/menus/ButtonSubScene.cpp +++ b/game/menus/ButtonSubScene.cpp @@ -15,6 +15,8 @@  #include "../Config.h"  #include "mainmenu/CreditsSubScript.h" +#include "menus/shop/ButtonBuySelectBubbleScript.h" +#include "menus/shop/ButtonBuySelectBulletScript.h"  #include <crepe/api/BehaviorScript.h>  #include <crepe/api/Button.h> @@ -88,6 +90,14 @@ void ButtonSubScene::set_script(crepe::GameObject & button_object, const Data &  		button_object.add_component<BehaviorScript>()  				.set_script<PreviewStopRecSubScript>();  			break; +		case ScriptSelect::SHOP_BULLET: +		button_object.add_component<BehaviorScript>() +				.set_script<ButtonBuySelectBulletScript>(); +			break; +		case ScriptSelect::SHOP_BUBBLE: +		button_object.add_component<BehaviorScript>() +				.set_script<ButtonBuySelectBubbleScript>(); +			break;  		case ScriptSelect::NONE:  			button_object.add_component<BehaviorScript>().set_script<IButtonScript>();  			break; diff --git a/game/menus/ButtonSubScene.h b/game/menus/ButtonSubScene.h index 8f086f0..d4c7223 100644 --- a/game/menus/ButtonSubScene.h +++ b/game/menus/ButtonSubScene.h @@ -22,6 +22,8 @@ public:  		PREVIEW_START,  		PREVIEW_STOP,  		PREVIEW_REPLAY, +		SHOP_BULLET, +		SHOP_BUBBLE,  		NONE,  	};  	//icon enum diff --git a/game/menus/shop/ButtonBuySelectBubbleScript.cpp b/game/menus/shop/ButtonBuySelectBubbleScript.cpp new file mode 100644 index 0000000..21dbe1a --- /dev/null +++ b/game/menus/shop/ButtonBuySelectBubbleScript.cpp @@ -0,0 +1,36 @@ +#include "ButtonBuySelectBubbleScript.h" +#include "../MenusConfig.h" +#include "Config.h" +#include "ValueBroker.h" +#include "manager/SaveManager.h" +#include "menus/shop/Shopconfig.h" + +using namespace crepe; +using namespace std; + +void ButtonBuySelectBubbleScript::init() { +	IButtonScript::init(); +	this->subscribe<ButtonPressEvent>([this](const ButtonPressEvent & e) { +		return this->on_button_press(e); +	}); +} + +bool ButtonBuySelectBubbleScript::on_button_press(const ButtonPressEvent & e) { +	SaveManager & save = this->get_save_manager(); +	ValueBroker<int> buy_bullet = save.get<int>(BUY_BUBBLE_SAVE,0); +	if(!buy_bullet.get()){ +		ValueBroker<int> coins = save.get<int>(TOTAL_COINS_GAME,0); +		if(coins.get() >= 1000) +		{ +			int coin = coins.get(); +			coin -= 1000; +			save.set(TOTAL_COINS_GAME,coin); +			save.set(BUY_BUBBLE_SAVE,1); +		} +	} +	else { +		save.set(JETPACK_PARTICLES,1); +	} +	this->trigger_event<ShopUpdate>(); +	return false; +} diff --git a/game/menus/shop/ButtonBuySelectBubbleScript.h b/game/menus/shop/ButtonBuySelectBubbleScript.h new file mode 100644 index 0000000..ce276ef --- /dev/null +++ b/game/menus/shop/ButtonBuySelectBubbleScript.h @@ -0,0 +1,14 @@ +#pragma once + +#include "../IButtonScript.h" + +#include <crepe/api/Script.h> + +class ButtonBuySelectBubbleScript : public IButtonScript { +public: +	void init() override; +	bool on_button_press(const crepe::ButtonPressEvent & e); + +protected: +	bool transition = false; +}; diff --git a/game/menus/shop/ButtonBuySelectBulletScript.cpp b/game/menus/shop/ButtonBuySelectBulletScript.cpp new file mode 100644 index 0000000..71d8b76 --- /dev/null +++ b/game/menus/shop/ButtonBuySelectBulletScript.cpp @@ -0,0 +1,36 @@ +#include "ButtonBuySelectBulletScript.h" +#include "../MenusConfig.h" +#include "Config.h" +#include "ValueBroker.h" +#include "manager/SaveManager.h" +#include "menus/shop/Shopconfig.h" + +using namespace crepe; +using namespace std; + +void ButtonBuySelectBulletScript::init() { +	IButtonScript::init(); +	this->subscribe<ButtonPressEvent>([this](const ButtonPressEvent & e) { +		return this->on_button_press(e); +	}); +} + +bool ButtonBuySelectBulletScript::on_button_press(const ButtonPressEvent & e) { +	SaveManager & save = this->get_save_manager(); +	ValueBroker<int> buy_bullet = save.get<int>(BUY_BULLET_SAVE,0); +	if(!buy_bullet.get()){ +		ValueBroker<int> coins = save.get<int>(TOTAL_COINS_GAME,0); +		if(coins.get() >= 0) +		{ +			int coin = coins.get(); +			coin -= 0; +			save.set(TOTAL_COINS_GAME,coin); +			save.set(BUY_BULLET_SAVE,1); +		} +	} +	else { +		save.set(JETPACK_PARTICLES,0); +	} +	this->trigger_event<ShopUpdate>(); +	return false; +} diff --git a/game/menus/shop/ButtonBuySelectBulletScript.h b/game/menus/shop/ButtonBuySelectBulletScript.h new file mode 100644 index 0000000..86de0ac --- /dev/null +++ b/game/menus/shop/ButtonBuySelectBulletScript.h @@ -0,0 +1,14 @@ +#pragma once + +#include "../IButtonScript.h" + +#include <crepe/api/Script.h> + +class ButtonBuySelectBulletScript : public IButtonScript { +public: +	void init() override; +	bool on_button_press(const crepe::ButtonPressEvent & e); + +protected: +	bool transition = false; +}; diff --git a/game/menus/shop/ShopLoadScript.cpp b/game/menus/shop/ShopLoadScript.cpp new file mode 100644 index 0000000..a9f9bfe --- /dev/null +++ b/game/menus/shop/ShopLoadScript.cpp @@ -0,0 +1,131 @@ +#include "ShopLoadScript.h" +#include <crepe/ValueBroker.h> +#include "api/Button.h" +#include "api/Sprite.h" +#include "Shopconfig.h" +#include "api/Text.h" +#include "manager/SaveManager.h" + +using namespace crepe; +using namespace std; + +void ShopLoadScript::init() { +	this->update(); +	this->subscribe<ShopUpdate>([this](const ShopUpdate e) { +		return this->update(); +	}); +} + +bool ShopLoadScript::update(){ +	SaveManager & save = this->get_save_manager(); +	ValueBroker<int> buy_bullet = save.get<int>(BUY_BULLET_SAVE,0); +	ValueBroker<int> buy_bubble = save.get<int>(BUY_BUBBLE_SAVE,0); + + +	if(buy_bullet.get()){ +		auto sprites = this->get_components_by_tag<Sprite>(BUY_BULLET); +		for(auto sprite : sprites){ +			sprite.get().active = false; +		} +		auto buttons = this->get_components_by_tag<Button>(BUY_BULLET); +		for(auto btn : buttons){ +			btn.get().active = false; +		} +		auto texts = this->get_components_by_tag<Text>(BUY_BULLET); +		for(auto txt : texts){ +			txt.get().active = false; +		} +		auto sprites1 = this->get_components_by_tag<Sprite>(SELECT_BULLET); +		for(auto sprite : sprites1){ +			sprite.get().active = true; +		} +		auto buttons1 = this->get_components_by_tag<Button>(SELECT_BULLET); +		for(auto btn : buttons1){ +			btn.get().active = true; +		} +		auto texts1 = this->get_components_by_tag<Text>(SELECT_BULLET); +		for(auto txt : texts1){ +			txt.get().active = true; +		} +	} +	else { +		auto sprites = this->get_components_by_tag<Sprite>(SELECT_BULLET); +		for(auto sprite : sprites){ +			sprite.get().active = false; +		} +		auto buttons = this->get_components_by_tag<Button>(SELECT_BULLET); +		for(auto btn : buttons){ +			btn.get().active = false; +		} +		auto texts = this->get_components_by_tag<Text>(SELECT_BULLET); +		for(auto txt : texts){ +			txt.get().active = false; +		} +		auto sprites1 = this->get_components_by_tag<Sprite>(BUY_BULLET); +		for(auto sprite : sprites1){ +			sprite.get().active = true; +		} +		auto buttons1 = this->get_components_by_tag<Button>(BUY_BULLET); +		for(auto btn : buttons1){ +			btn.get().active = true; +		} +		auto texts1 = this->get_components_by_tag<Text>(BUY_BULLET); +		for(auto txt : texts1){ +			txt.get().active = true; +		} +	} + +	if(buy_bubble.get()){ +		auto sprites = this->get_components_by_tag<Sprite>(BUY_BUBBLE); +		for(auto sprite : sprites){ +			sprite.get().active = false; +		} +		auto buttons = this->get_components_by_tag<Button>(BUY_BUBBLE); +		for(auto btn : buttons){ +			btn.get().active = false; +		} +		auto texts = this->get_components_by_tag<Text>(BUY_BUBBLE); +		for(auto txt : texts){ +			txt.get().active = false; +		} +		auto sprites1 = this->get_components_by_tag<Sprite>(SELECT_BUBBLE); +		for(auto sprite : sprites1){ +			sprite.get().active = true; +		} +		auto buttons1 = this->get_components_by_tag<Button>(SELECT_BUBBLE); +		for(auto btn : buttons1){ +			btn.get().active = true; +		} +		auto texts1 = this->get_components_by_tag<Text>(SELECT_BUBBLE); +		for(auto txt : texts1){ +			txt.get().active = true; +		} +	} +	else { +		auto sprites = this->get_components_by_tag<Sprite>(SELECT_BUBBLE); +		for(auto sprite : sprites){ +			sprite.get().active = false; +		} +		auto buttons = this->get_components_by_tag<Button>(SELECT_BUBBLE); +		for(auto btn : buttons){ +			btn.get().active = false; +		} +		auto texts = this->get_components_by_tag<Text>(SELECT_BUBBLE); +		for(auto txt : texts){ +			txt.get().active = false; +		} +		auto sprites1 = this->get_components_by_tag<Sprite>(BUY_BUBBLE); +		for(auto sprite : sprites1){ +			sprite.get().active = true; +		} +		auto buttons1 = this->get_components_by_tag<Button>(BUY_BUBBLE); +		for(auto btn : buttons1){ +			btn.get().active = true; +		} +		auto texts1 = this->get_components_by_tag<Text>(BUY_BUBBLE); +		for(auto txt : texts1){ +			txt.get().active = true; +		} +	} +	return false; +} diff --git a/game/menus/shop/ShopLoadScript.h b/game/menus/shop/ShopLoadScript.h new file mode 100644 index 0000000..b17bf1c --- /dev/null +++ b/game/menus/shop/ShopLoadScript.h @@ -0,0 +1,10 @@ +#pragma once + +#include <crepe/api/Script.h> +#include <crepe/manager/SaveManager.h> + +class ShopLoadScript : public crepe::Script { +public: +	void init() override; +	bool update(); +}; diff --git a/game/menus/shop/ShopMenuScene.cpp b/game/menus/shop/ShopMenuScene.cpp index d1ea81d..fc7323b 100644 --- a/game/menus/shop/ShopMenuScene.cpp +++ b/game/menus/shop/ShopMenuScene.cpp @@ -2,14 +2,17 @@  #include "ShopMenuScene.h"  #include "../../Config.h" -#include "../BannerSubScene.h"  #include "../ButtonSubScene.h"  #include "../MenusConfig.h" +#include "api/BehaviorScript.h" +#include "menus/BannerSubScene.h" +#include "menus/shop/ShopLoadScript.h"  #include "types.h"  #include <crepe/api/Camera.h>  #include <crepe/api/Sprite.h>  #include <crepe/api/Text.h> +#include "Shopconfig.h"  using namespace crepe;  using namespace std; @@ -40,6 +43,7 @@ void ShopMenuScene::load_scene() {  			.position_offset {0},  		}  	); +	menu_background.add_component<BehaviorScript>().set_script<ShopLoadScript>();  	ButtonSubScene button;  	button.create( @@ -54,6 +58,12 @@ void ShopMenuScene::load_scene() {  		}  	); + + +	const float CHAR_SIZE = 16; +	const float CHAR_SIZE_COIN = 16; +	crepe::vec2 size; +  	GameObject shop_item_bullet = this->new_object("bullet", "shop_item", vec2(-100, 0));  	shop_item_bullet.add_component<Sprite>(  		Asset("asset/other_effects/effect_rocketmgshell_TVOS.png"), @@ -63,13 +73,17 @@ void ShopMenuScene::load_scene() {  			.position_offset = {0, 0},  		}  	); + +	const string BULLETS_STRING = "BULLETS"; +	size = {CHAR_SIZE*BULLETS_STRING.size(), (CHAR_SIZE*BULLETS_STRING.size() / BULLETS_STRING.size()) * 2}; +  	shop_item_bullet.add_component<Text>( -		vec2 {100, 50}, FONT, +		size, FONT,  		Text::Data {  			.world_space = true,  			.text_color = Color::WHITE,  		}, -		vec2 {0, -75}, "BULLETS" +		vec2 {0, -75}, BULLETS_STRING  	);  	shop_item_bullet.add_component<Sprite>(  		Asset("asset/ui/buttonCoinsSmall.png"), @@ -79,13 +93,16 @@ void ShopMenuScene::load_scene() {  			.position_offset = {25, 75},  		}  	); + +	const string BULLETS_GOLD_STRING = "0"; +	size = {CHAR_SIZE_COIN*BULLETS_GOLD_STRING.size(), (CHAR_SIZE_COIN*BULLETS_GOLD_STRING.size() / BULLETS_GOLD_STRING.size()) * 2};  	shop_item_bullet.add_component<Text>( -		vec2 {37.5, 37.5}, FONT, +		size, FONT,  		Text::Data {  			.world_space = true,  			.text_color = Color::GOLD,  		}, -		vec2 {-25, 75}, "0" +		vec2 {-5, 75}, BULLETS_GOLD_STRING  	);  	GameObject shop_item_bubble = this->new_object("bubble", "shop_item", vec2(100, 0)); @@ -97,13 +114,16 @@ void ShopMenuScene::load_scene() {  			.position_offset = {0, 0},  		}  	); + +	const string BUBBLE_STRING = "BUBBLE"; +	size = {CHAR_SIZE*BUBBLE_STRING.size(), (CHAR_SIZE*BUBBLE_STRING.size() / BUBBLE_STRING.size()) * 2};  	shop_item_bubble.add_component<Text>( -		vec2 {100, 50}, FONT, +		size, FONT,  		Text::Data {  			.world_space = true,  			.text_color = Color::WHITE,  		}, -		vec2 {0, -75}, "BUBBLE" +		vec2 {0, -75}, BUBBLE_STRING  	);  	shop_item_bubble.add_component<Sprite>(  		Asset("asset/ui/buttonCoinsSmall.png"), @@ -113,14 +133,79 @@ void ShopMenuScene::load_scene() {  			.position_offset = {45, 75},  		}  	); + +	const string BUBBLE_GOLD_STRING = "1000"; +	size = {CHAR_SIZE_COIN*BUBBLE_GOLD_STRING.size(), (CHAR_SIZE_COIN*BUBBLE_GOLD_STRING.size() / BUBBLE_GOLD_STRING.size()) * 2};  	shop_item_bubble.add_component<Text>( -		vec2 {100, 25}, FONT, +		size, FONT,  		Text::Data {  			.world_space = true,  			.text_color = Color::GOLD,  		}, -		vec2 {-25, 75}, "1000" +		vec2 {-10, 75}, BUBBLE_GOLD_STRING +	); + +	button.create( +		*this, +		ButtonSubScene::Data { +			.text = "BUY", +			.text_width = 100, +			.position = vec2(-100, 120), +			.script_type = ButtonSubScene::ScriptSelect::SHOP_BULLET, +			.button_type = ButtonSubScene::ButtonSelect::LARGE, +			.scale = 0.4, +			.tag = BUY_BULLET, +			.sorting_layer_offset = 20, +			.btn_side_color = ButtonSubScene::ButtonSideColor::PURPLE + +		} +	); + +	button.create( +		*this, +		ButtonSubScene::Data { +			.text = "BUY", +			.text_width = 100, +			.position = vec2(100, 120), +			.script_type = ButtonSubScene::ScriptSelect::SHOP_BUBBLE, +			.button_type = ButtonSubScene::ButtonSelect::LARGE, +			.scale = 0.4, +			.tag = BUY_BUBBLE, +			.sorting_layer_offset = 20, +			.btn_side_color = ButtonSubScene::ButtonSideColor::PURPLE +		}  	); + +	button.create( +		*this, +		ButtonSubScene::Data { +			.text = "SELECT", +			.text_width = 100, +			.position = vec2(-100, 120), +			.script_type = ButtonSubScene::ScriptSelect::SHOP_BULLET, +			.button_type = ButtonSubScene::ButtonSelect::LARGE, +			.scale = 0.4, +			.tag = SELECT_BULLET, +			.sorting_layer_offset = 20, +			.btn_side_color = ButtonSubScene::ButtonSideColor::PURPLE +		} +	); + +	button.create( +		*this, +		ButtonSubScene::Data { +			.text = "SELECT", +			.text_width = 100, +			.position = vec2(100, 120), +			.script_type = ButtonSubScene::ScriptSelect::SHOP_BUBBLE, +			.button_type = ButtonSubScene::ButtonSelect::LARGE, +			.scale = 0.4, +			.tag = SELECT_BUBBLE, +			.sorting_layer_offset = 20, +			.btn_side_color = ButtonSubScene::ButtonSideColor::PURPLE +		} +	); +  }  string ShopMenuScene::get_name() const { return SHOP_SCENE; } diff --git a/game/menus/shop/Shopconfig.h b/game/menus/shop/Shopconfig.h new file mode 100644 index 0000000..04c8c5c --- /dev/null +++ b/game/menus/shop/Shopconfig.h @@ -0,0 +1,16 @@ +#pragma once +#include "api/Event.h" + +//tags +static constexpr const char * BUY_BULLET = "BUY_BULLET"; +static constexpr const char * SELECT_BULLET = "SELECT_BULLET"; +static constexpr const char * BUY_BUBBLE = "BUY_BUBBLE"; +static constexpr const char * SELECT_BUBBLE = "SELECT_BUBBLE"; + + +//save_data +static constexpr const char * BUY_BULLET_SAVE = "BUY_BULLET_SAVE"; +static constexpr const char * BUY_BUBBLE_SAVE = "BUY_BUBBLE_SAVE"; + + +struct ShopUpdate : public crepe::Event {}; |