mirror of
https://github.com/devZoGok/PlanetFleet.git
synced 2026-08-26 19:43:29 +00:00
implemented instant trading
This commit is contained in:
@@ -17,7 +17,7 @@ UnitId = {
|
||||
STEALTH_SUBMARINE = 14,
|
||||
LAND_FACTORY = 15,
|
||||
NAVAL_FACTORY = 16,
|
||||
MARKET = 17,
|
||||
TRADE_CENTER = 17,
|
||||
LAB = 18,
|
||||
POINT_DEFENSE = 19,
|
||||
EXTRACTOR = 20,
|
||||
@@ -37,7 +37,7 @@ UnitClass = {
|
||||
SUBMARINE = 8,
|
||||
LAND_FACTORY = 9,
|
||||
NAVAL_FACTORY = 10,
|
||||
MARKET = 11,
|
||||
TRADE_CENTER = 11,
|
||||
LAB = 12,
|
||||
POINT_DEFENSE = 13,
|
||||
EXTRACTOR = 14,
|
||||
@@ -128,7 +128,7 @@ units = {
|
||||
buildableUnits = {
|
||||
{id = UnitId.LAND_FACTORY, buildable = true},
|
||||
{id = UnitId.NAVAL_FACTORY, buildable = true},
|
||||
{id = UnitId.MARKET, buildable = true},
|
||||
{id = UnitId.TRADE_CENTER, buildable = true},
|
||||
{id = UnitId.LAB, buildable = true},
|
||||
{id = UnitId.POINT_DEFENSE, buildable = true},
|
||||
{id = UnitId.FORT, buildable = false},
|
||||
@@ -486,7 +486,7 @@ units = {
|
||||
deathSfx = PATH .. 'Sounds/SFX/Explosions/explosion01.ogg',
|
||||
},
|
||||
{
|
||||
unitClass = UnitClass.MARKET,
|
||||
unitClass = UnitClass.TRADE_CENTER,
|
||||
unitType = UnitType.LAND,
|
||||
isVehicle = false,
|
||||
health = 500,
|
||||
@@ -495,9 +495,10 @@ units = {
|
||||
size = {x = 1, y = 1.15, z = 2},
|
||||
hitboxOffset = {x = 0, y = 0, z = 0},
|
||||
lineOfSight = 5,
|
||||
name = 'Market',
|
||||
basePath = PATH .. structurePrefix .. 'Markets/',
|
||||
meshPath = 'market.xml',
|
||||
name = 'Trade center',
|
||||
basePath = PATH .. structurePrefix .. 'TradeCenters/',
|
||||
meshPath = 'tradeCenter.xml',
|
||||
guiScreen = 'tradingCenterCommands.lua',
|
||||
selectionSfx = PATH .. 'Sounds/Units/Sample/selection.ogg',
|
||||
deathSfx = PATH .. 'Sounds/SFX/Explosions/explosion01.ogg',
|
||||
},
|
||||
|
||||
@@ -28,7 +28,11 @@ ButtonType = {
|
||||
NAVAL_FACTORY_TRAIN = 24,
|
||||
FORT_TRAIN = 25,
|
||||
STATISTICS = 26,
|
||||
RESEARCH = 27
|
||||
RESEARCH = 27,
|
||||
BUY_REFINEDS = 28,
|
||||
SELL_REFINEDS = 29,
|
||||
BUY_RESEARCH = 30,
|
||||
SELL_RESEARCH = 31,
|
||||
}
|
||||
|
||||
ListboxType = {
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
res = graphics.resolution
|
||||
Size = {x = 100, y = 100}
|
||||
|
||||
gui = {
|
||||
{
|
||||
pos = {x = res.x - 200, y = res.y - 200},
|
||||
size = Size,
|
||||
name = 'Buy ref',
|
||||
imagePath = '',
|
||||
guiType = GuiType.BUTTON,
|
||||
buttonType = ButtonType.BUY_REFINEDS,
|
||||
trigger = 73,
|
||||
},
|
||||
{
|
||||
pos = {x = res.x - Size.x - 200, y = res.y - 200},
|
||||
size = Size,
|
||||
imagePath = '',
|
||||
name = 'Sell ref',
|
||||
guiType = GuiType.BUTTON,
|
||||
buttonType = ButtonType.SELL_REFINEDS,
|
||||
trigger = 79,
|
||||
},
|
||||
{
|
||||
pos = {x = res.x - 200, y = res.y - 400},
|
||||
size = Size,
|
||||
imagePath = '',
|
||||
name = 'Buy rsch',
|
||||
guiType = GuiType.BUTTON,
|
||||
buttonType = ButtonType.BUY_RESEARCH,
|
||||
trigger = 75,
|
||||
},
|
||||
{
|
||||
pos = {x = res.x - Size.x - 200, y = res.y - 400},
|
||||
size = Size,
|
||||
imagePath = '',
|
||||
name = 'Sell rsch',
|
||||
guiType = GuiType.BUTTON,
|
||||
buttonType = ButtonType.SELL_RESEARCH,
|
||||
trigger = 76,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
trading = {
|
||||
refinedsRate = 10,
|
||||
researchRate = 20,
|
||||
fluctuationRate = 1000,
|
||||
fluctuationAmmount = 2
|
||||
}
|
||||
+2
-2
@@ -8,7 +8,7 @@ set(CMAKE_BUILD_TYPE Debug)
|
||||
set(BUILD_TESTS ON)
|
||||
cmake_policy(SET CMP0015 NEW)
|
||||
|
||||
set(UNIT_BUTTONS unitButton.cpp buildButton.cpp trainButton.cpp researchButton.cpp)
|
||||
set(UNIT_BUTTONS unitButton.cpp tradeButton.cpp buildButton.cpp trainButton.cpp researchButton.cpp)
|
||||
set(OPTIONS_BUTTONS optionsButton.cpp tabButton.cpp okButton.cpp defaultsButton.cpp)
|
||||
set(IN_GAME_APP_STATE_BUTTONS mainMenuButton.cpp)
|
||||
set(MAP_EDITOR_BUTTONS mapEditorButton.cpp newMapButton.cpp loadMapButton.cpp exportButton.cpp)
|
||||
@@ -26,7 +26,7 @@ set(FX fx.cpp)
|
||||
set(VEHICLES vehicle.cpp engineer.cpp resourceRover.cpp)
|
||||
set(STRUCTURES structure.cpp factory.cpp pointDefense.cpp extractor.cpp researchStruct.cpp)
|
||||
set(UNITS gameObjectFactory.cpp unit.cpp ${STRUCTURES} ${VEHICLES})
|
||||
set(CONTENT player.cpp pathfinder.cpp map.cpp gameObject.cpp gameObjectFrame.h resourceDeposit.cpp ${UNITS} ${PROJECTILES} ${FX})
|
||||
set(CONTENT player.cpp trader.cpp pathfinder.cpp map.cpp gameObject.cpp gameObjectFrame.h resourceDeposit.cpp ${UNITS} ${PROJECTILES} ${FX})
|
||||
set(GAME_SRC ${STATES} ${GUI} ${CORE} ${CONTENT} ${UTIL})
|
||||
|
||||
set(SFML_DIR external/SFML)
|
||||
|
||||
+3
-3
@@ -84,9 +84,9 @@ namespace battleship{
|
||||
void ActiveGameState::update() {
|
||||
ConcreteGuiManager *guiManager = ConcreteGuiManager::getSingleton();
|
||||
guiManager->getText("depth")->setText(L"Depth: " + to_wstring(depth));
|
||||
guiManager->getText("refineds")->setText(L"Refineds: " + to_wstring(mainPlayer->getRefineds()));
|
||||
guiManager->getText("wealth")->setText(L"Wealth: " + to_wstring(mainPlayer->getWealth()));
|
||||
guiManager->getText("research")->setText(L"Research: " + to_wstring(mainPlayer->getResearch()));
|
||||
guiManager->getText("refineds")->setText(L"Refineds: " + to_wstring(mainPlayer->getResource(ResourceType::REFINEDS)));
|
||||
guiManager->getText("wealth")->setText(L"Wealth: " + to_wstring(mainPlayer->getResource(ResourceType::WEALTH)));
|
||||
guiManager->getText("research")->setText(L"Research: " + to_wstring(mainPlayer->getResource(ResourceType::RESEARCH)));
|
||||
|
||||
if(!isSelectionBox && leftMouseClicked && getTime() - lastLeftMouseClicked > 10)
|
||||
isSelectionBox = true;
|
||||
|
||||
+2
-12
@@ -1,6 +1,7 @@
|
||||
#include "addResourceCommand.h"
|
||||
#include "game.h"
|
||||
#include "player.h"
|
||||
#include "trader.h"
|
||||
|
||||
namespace battleship{
|
||||
void AddResourceCommand::validate(){
|
||||
@@ -22,18 +23,7 @@ namespace battleship{
|
||||
|
||||
void AddResourceCommand::addResource(){
|
||||
Player *player = Game::getSingleton()->getPlayer(playerId);
|
||||
|
||||
switch(resourceId){
|
||||
case 0:
|
||||
player->addRefineds(resourceAmmount);
|
||||
break;
|
||||
case 1:
|
||||
player->addWealth(resourceAmmount);
|
||||
break;
|
||||
case 2:
|
||||
player->addResearch(resourceAmmount);
|
||||
break;
|
||||
}
|
||||
player->updateResource(ResourceType(resourceId), resourceAmmount, true);
|
||||
}
|
||||
|
||||
void AddResourceCommand::execute(){
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "trainButton.h"
|
||||
#include "statsButton.h"
|
||||
#include "researchButton.h"
|
||||
#include "tradeButton.h"
|
||||
|
||||
namespace battleship{
|
||||
using namespace std;
|
||||
@@ -200,6 +201,18 @@ namespace battleship{
|
||||
case RESEARCH:
|
||||
button = new ResearchButton(pos, size, name, (int)guiTable["trigger"], (string)guiTable["imagePath"], (int)SOL_LUA_STATE["UnitId"]["LAB"], (int)guiTable["techId"]);
|
||||
break;
|
||||
case BUY_REFINEDS:
|
||||
button = new TradeButton(pos, size, name, (int)guiTable["trigger"], (string)guiTable["imagePath"], (int)SOL_LUA_STATE["UnitId"]["TRADE_CENTER"], TradeButton::Type::BUY_REFINEDS);
|
||||
break;
|
||||
case SELL_REFINEDS:
|
||||
button = new TradeButton(pos, size, name, (int)guiTable["trigger"], (string)guiTable["imagePath"], (int)SOL_LUA_STATE["UnitId"]["TRADE_CENTER"], TradeButton::Type::SELL_REFINEDS);
|
||||
break;
|
||||
case BUY_RESEARCH:
|
||||
button = new TradeButton(pos, size, name, (int)guiTable["trigger"], (string)guiTable["imagePath"], (int)SOL_LUA_STATE["UnitId"]["TRADE_CENTER"], TradeButton::Type::BUY_RESEARCH);
|
||||
break;
|
||||
case SELL_RESEARCH:
|
||||
button = new TradeButton(pos, size, name, (int)guiTable["trigger"], (string)guiTable["imagePath"], (int)SOL_LUA_STATE["UnitId"]["TRADE_CENTER"], TradeButton::Type::SELL_RESEARCH);
|
||||
break;
|
||||
}
|
||||
|
||||
int typeArr[2]{(int)GuiElementType::BUTTON, (int)type};
|
||||
|
||||
@@ -42,7 +42,11 @@ namespace battleship{
|
||||
NAVAL_FACTORY_TRAIN,
|
||||
FORT_TRAIN,
|
||||
STATISTICS,
|
||||
RESEARCH
|
||||
RESEARCH,
|
||||
BUY_REFINEDS,
|
||||
SELL_REFINEDS,
|
||||
BUY_RESEARCH,
|
||||
SELL_RESEARCH
|
||||
};
|
||||
enum ListboxType {
|
||||
CONTROLS,
|
||||
|
||||
@@ -53,6 +53,7 @@ namespace battleship{
|
||||
"Scripts/Core/player.lua",
|
||||
"Scripts/Technologies/technologyData.lua",
|
||||
"Scripts/Abilities/abilityData.lua",
|
||||
"Scripts/Trading/traderData.lua"
|
||||
};
|
||||
|
||||
const static Bind staticBinds[numAppStates][maxStaticBinds]{
|
||||
|
||||
+2
-2
@@ -59,9 +59,9 @@ namespace battleship{
|
||||
sol::table targTable = generateView()["units"][order.targets[0].unit->getId()];
|
||||
int costRate = (int)targTable["cost"] / 100, buildRate = (int)targTable["buildTime"] / 100;
|
||||
|
||||
if(structure->getBuildStatus() < 100 && player->getRefineds() >= costRate && getTime() - lastIncrementTime > buildRate){
|
||||
if(structure->getBuildStatus() < 100 && player->getResource(ResourceType::REFINEDS) >= costRate && getTime() - lastIncrementTime > buildRate){
|
||||
structure->incrementBuildStatus();
|
||||
player->setRefineds(player->getRefineds() - costRate);
|
||||
player->updateResource(ResourceType::REFINEDS, -costRate, true);
|
||||
lastIncrementTime = getTime();
|
||||
}
|
||||
else if(structure->getBuildStatus() >= 100){
|
||||
|
||||
+2
-2
@@ -64,9 +64,9 @@ namespace battleship{
|
||||
sol::table targTable = generateView()["units"][unitQueue[0] + 1];
|
||||
int costRate = (int)targTable["cost"] / 100, trainRate = (int)targTable["buildTime"] / 100;
|
||||
|
||||
if(player->getRefineds() >= costRate && getTime() - lastTrainTime > trainRate){
|
||||
if(player->getResource(ResourceType::REFINEDS) >= costRate && getTime() - lastTrainTime > trainRate){
|
||||
trainingStatus++;
|
||||
player->setRefineds(player->getRefineds() - costRate);
|
||||
player->updateResource(ResourceType::REFINEDS, -costRate, true);
|
||||
lastTrainTime = getTime();
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ namespace battleship{
|
||||
return new Extractor(player, id, pos, rot, buildStatus);
|
||||
case UnitClass::LAB:
|
||||
return new ResearchStruct(player, id, pos, rot, buildStatus);
|
||||
case UnitClass::MARKET:
|
||||
case UnitClass::TRADE_CENTER:
|
||||
case UnitClass::REFINERY:
|
||||
return new Structure(player, id, pos, rot, buildStatus, Unit::State::STAND_GROUND);
|
||||
case UnitClass::ENGINEER:
|
||||
|
||||
+28
-1
@@ -12,11 +12,13 @@ namespace battleship{
|
||||
using namespace gameBase;
|
||||
using namespace std;
|
||||
|
||||
Player::Player(int diff, int fac, int t, Vector3 col, bool cpuPl, Vector3 sp, string n) : difficulty(diff), faction(fac), team(t), color(col), cpuPlayer(cpuPl), spawnPoint(sp), name(n), refineds(30000) {}
|
||||
Player::Player(int diff, int fac, int t, Vector3 col, bool cpuPl, Vector3 sp, string n) : difficulty(diff), faction(fac), team(t), color(col), cpuPlayer(cpuPl), spawnPoint(sp), name(n), refineds(30000), trader(new Trader()) {}
|
||||
|
||||
Player::~Player() {}
|
||||
|
||||
void Player::update() {
|
||||
trader->update();
|
||||
|
||||
vector<Unit*> units = this->units;
|
||||
for(Unit *u : units){
|
||||
if(!u->isRemove())
|
||||
@@ -187,4 +189,29 @@ namespace battleship{
|
||||
void Player::addTechnology(int techId){
|
||||
technologies.push_back(techId);
|
||||
}
|
||||
|
||||
int Player::getResource(ResourceType type){
|
||||
switch(type){
|
||||
case ResourceType::REFINEDS:
|
||||
return refineds;
|
||||
case ResourceType::WEALTH:
|
||||
return wealth;
|
||||
case ResourceType::RESEARCH:
|
||||
return research;
|
||||
}
|
||||
}
|
||||
|
||||
void Player::updateResource(ResourceType type, int ammount, bool add){
|
||||
switch(type){
|
||||
case ResourceType::REFINEDS:
|
||||
refineds = (add ? refineds + ammount : ammount);
|
||||
break;
|
||||
case ResourceType::WEALTH:
|
||||
wealth = (add ? wealth + ammount : ammount);
|
||||
break;
|
||||
case ResourceType::RESEARCH:
|
||||
research = (add ? research + ammount : ammount);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "gameManager.h"
|
||||
#include "trader.h"
|
||||
#include "unit.h"
|
||||
|
||||
namespace battleship{
|
||||
@@ -11,6 +12,8 @@ namespace battleship{
|
||||
class Projectile;
|
||||
class Unit;
|
||||
|
||||
enum class ResourceType{REFINEDS, WEALTH, RESEARCH};
|
||||
|
||||
class Player {
|
||||
public:
|
||||
Player(int, int, int, vb01::Vector3, bool = true, vb01::Vector3 = vb01::Vector3::VEC_ZERO, std::string = "");
|
||||
@@ -27,6 +30,9 @@ namespace battleship{
|
||||
std::vector<Unit*> getUnitsById(int, int = -1);
|
||||
std::vector<Unit*> getUnitsByClass(UnitClass, int = -1);
|
||||
void addTechnology(int);
|
||||
int getResource(ResourceType);
|
||||
void updateResource(ResourceType, int, bool);
|
||||
inline Trader* getTrader(){return trader;}
|
||||
inline void deselectUnits(){selectedUnits.clear();}
|
||||
inline Unit* getSelectedUnit(int id){return selectedUnits[id];}
|
||||
inline std::vector<Unit*> getSelectedUnits(){return selectedUnits;}
|
||||
@@ -47,18 +53,6 @@ namespace battleship{
|
||||
inline int getNumUnits(){return units.size();}
|
||||
inline int getFaction(){return faction;}
|
||||
inline vb01::Vector3 getSpawnPoint(){return spawnPoint;}
|
||||
inline int getRefineds(){return refineds;}
|
||||
inline void setRefineds(int ref){this->refineds = ref;}
|
||||
inline void addRefineds(int ref){this->refineds += ref;}
|
||||
inline void subtractRefineds(int ref){this->refineds -= ref;}
|
||||
inline int getWealth(){return wealth;}
|
||||
inline void setWealth(int w){this->wealth = w;}
|
||||
inline void addWealth(int w){this->wealth += w;}
|
||||
inline void subtractWealth(int w){this->wealth -= w;}
|
||||
inline int getResearch(){return research;}
|
||||
inline void setResearch(int r){this->research = r;}
|
||||
inline void addResearch(int r){this->research += r;}
|
||||
inline void subtractResearch(int r){this->research -= r;}
|
||||
inline bool isCpuPlayer(){return cpuPlayer;}
|
||||
inline int getNumVehiclesBuilt(){return vehiclesBuilt;}
|
||||
inline int getNumVehiclesDestroyed(){return vehiclesDestroyed;}
|
||||
@@ -83,6 +77,7 @@ namespace battleship{
|
||||
int faction, difficulty, team;
|
||||
int vehiclesBuilt = 0, vehiclesDestroyed = 0, vehiclesLost = 0;
|
||||
int structuresBuilt = 0, structuresDestroyed = 0, structuresLost = 0;
|
||||
Trader *trader = nullptr;
|
||||
std::string name;
|
||||
std::vector<Unit*> units, selectedUnits;
|
||||
std::vector<Projectile*> projectiles;
|
||||
|
||||
+4
-4
@@ -38,9 +38,9 @@ namespace battleship{
|
||||
bool mainPlayerSelecting = (activeState && find(selectingPlayers.begin(), selectingPlayers.end(), mainPlayer) != selectingPlayers.end());
|
||||
|
||||
if(researchQueue.empty()){
|
||||
if(health > .3 * maxHealth && player->getRefineds() >= researchCost && canUpdateResearch()){
|
||||
player->addResearch(generationSpeed);
|
||||
player->subtractRefineds(researchCost);
|
||||
if(health > .3 * maxHealth && player->getResource(ResourceType::REFINEDS) >= researchCost && canUpdateResearch()){
|
||||
player->updateResource(ResourceType::RESEARCH, generationSpeed, true);
|
||||
player->updateResource(ResourceType::REFINEDS, researchCost, true);
|
||||
lastUpdateTime = getTime();
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ namespace battleship{
|
||||
Unit::displayUnitStats(researchStatusForeground, researchStatusBackground, researchStatus, 100, mainPlayer == player && mainPlayerSelecting, Vector2(0, -10));
|
||||
|
||||
int techCost = Game::getSingleton()->getTechnology(researchQueue[0]).cost;
|
||||
int playerResearch = player->getResearch();
|
||||
int playerResearch = player->getResource(ResourceType::RESEARCH);
|
||||
|
||||
if(techCost > playerResearch && canUpdateResearch()){
|
||||
researchStatus += int(100 * ((float)playerResearch / techCost));
|
||||
|
||||
+1
-1
@@ -78,7 +78,7 @@ namespace battleship{
|
||||
if(nearestRefinery && nearestRefinery->getPos().getDistanceFrom(pos) <= minDist){
|
||||
if(canUnload()){
|
||||
load -= loadSpeed;
|
||||
player->addRefineds(1);
|
||||
player->updateResource(ResourceType::REFINEDS, 1, true);
|
||||
lastLoadTime = getTime();
|
||||
}
|
||||
else if(load == 0 && nearestExtractor)
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
#include "tradeButton.h"
|
||||
#include "activeGameState.h"
|
||||
#include "player.h"
|
||||
|
||||
#include <stateManager.h>
|
||||
|
||||
namespace battleship{
|
||||
using namespace std;
|
||||
using namespace vb01;
|
||||
using namespace gameBase;
|
||||
|
||||
TradeButton::TradeButton(Vector2 pos, Vector2 size, string name, int trigger, string imagePath, int uid, Type t) : UnitButton(pos, size, name, GameManager::getSingleton()->getPath() + "Fonts/batang.ttf", trigger, imagePath, uid), type(t){}
|
||||
|
||||
void TradeButton::onClick(){
|
||||
ActiveGameState *activeState = (ActiveGameState*)(GameManager::getSingleton()->getStateManager()->getAppStateByType((int)AppStateType::ACTIVE_STATE));
|
||||
Player *player = activeState->getPlayer();
|
||||
|
||||
int resType, ammount = 10;
|
||||
bool buy;
|
||||
|
||||
switch(type){
|
||||
case Type::BUY_REFINEDS:
|
||||
resType = (int)ResourceType::REFINEDS;
|
||||
buy = true;
|
||||
break;
|
||||
case Type::SELL_REFINEDS:
|
||||
resType = (int)ResourceType::REFINEDS;
|
||||
buy = false;
|
||||
break;
|
||||
case Type::BUY_RESEARCH:
|
||||
resType = (int)ResourceType::RESEARCH;
|
||||
buy = true;
|
||||
break;
|
||||
case Type::SELL_RESEARCH:
|
||||
resType = (int)ResourceType::RESEARCH;
|
||||
buy = false;
|
||||
break;
|
||||
}
|
||||
|
||||
player->getTrader()->trade(player, resType, ammount, buy);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef TRADE_BUTTON_H
|
||||
#define TRADE_BUTTON_H
|
||||
|
||||
#include "unitButton.h"
|
||||
|
||||
namespace battleship{
|
||||
class TradeButton : public UnitButton{
|
||||
public:
|
||||
enum class Type{BUY_REFINEDS, SELL_REFINEDS, BUY_RESEARCH, SELL_RESEARCH};
|
||||
|
||||
TradeButton(vb01::Vector2, vb01::Vector2, std::string, int, std::string, int, Type);
|
||||
void onClick();
|
||||
private:
|
||||
Type type;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef TRADE_CENTER_H
|
||||
#define TRADE_CENTER_H
|
||||
|
||||
#include "structure.h"
|
||||
|
||||
namespace battleship{
|
||||
class TradeCenter : public Structure{
|
||||
public:
|
||||
TradeCenter(Player*, int, vb01::Vector3, vb01::Quaternion, int);
|
||||
private:
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
#include "trader.h"
|
||||
#include "player.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <solUtil.h>
|
||||
|
||||
namespace battleship{
|
||||
using namespace std;
|
||||
using namespace vb01;
|
||||
using namespace gameBase;
|
||||
|
||||
Trader::Trader(){
|
||||
initProperties();
|
||||
}
|
||||
|
||||
void Trader::initProperties(){
|
||||
sol::table traderTable = generateView()["trading"];
|
||||
fluctuationRate = traderTable["fluctuationRate"];
|
||||
fluctuationAmmount = traderTable["fluctuationAmmount"];
|
||||
refinedsRate = traderTable["refinedsRate"];
|
||||
researchRate = traderTable["researchRate"];
|
||||
}
|
||||
|
||||
void Trader::update(){
|
||||
if(getTime() - lastUpdateTime > fluctuationRate){
|
||||
int flucFact = rand() % 3;
|
||||
int flSpd;
|
||||
|
||||
switch(flucFact){
|
||||
case 0:
|
||||
flSpd = -fluctuationAmmount;
|
||||
break;
|
||||
case 1:
|
||||
flSpd = 0;
|
||||
break;
|
||||
case 2:
|
||||
flSpd = fluctuationAmmount;
|
||||
break;
|
||||
}
|
||||
|
||||
refinedsRate += fluctuationAmmount;
|
||||
researchRate += fluctuationAmmount;
|
||||
lastUpdateTime = getTime();
|
||||
}
|
||||
}
|
||||
|
||||
void Trader::trade(Player *tradingPlayer, int rt, int ammount, bool buying){
|
||||
int tradeResRate;
|
||||
|
||||
switch((ResourceType)rt){
|
||||
case ResourceType::REFINEDS:
|
||||
tradeResRate = refinedsRate;
|
||||
break;
|
||||
case ResourceType::RESEARCH:
|
||||
tradeResRate = researchRate;
|
||||
break;
|
||||
}
|
||||
|
||||
if(buying && ammount * tradeResRate <= tradingPlayer->getResource(ResourceType::WEALTH)){
|
||||
tradingPlayer->updateResource((ResourceType)rt, ammount, true);
|
||||
tradingPlayer->updateResource(ResourceType::WEALTH, -ammount * tradeResRate, true);
|
||||
}
|
||||
else if(!buying && tradingPlayer->getResource((ResourceType)rt) >= ammount){
|
||||
tradingPlayer->updateResource((ResourceType)rt, -ammount, true);
|
||||
tradingPlayer->updateResource(ResourceType::WEALTH, ammount * tradeResRate, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef TRADER_H
|
||||
#define TRADER_H
|
||||
|
||||
#include <util.h>
|
||||
|
||||
namespace battleship{
|
||||
class Player;
|
||||
|
||||
class Trader{
|
||||
public:
|
||||
Trader();
|
||||
void update();
|
||||
void trade(Player*, int, int, bool);
|
||||
private:
|
||||
void initProperties();
|
||||
|
||||
vb01::s64 lastUpdateTime = 0;
|
||||
int fluctuationRate, refinedsRate, researchRate, fluctuationAmmount;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user