lesson 2 withot destroy

This commit is contained in:
Alexander Zhirov 2021-12-25 08:46:23 +03:00
parent 42495521c3
commit e37ff585f7
7 changed files with 1622 additions and 343 deletions

View File

@ -6,7 +6,7 @@ class Bomb : public DynamicObject
{ {
public: public:
static const uint16_t BombCost = 10; // ñòîèìîñòü áîìáû â î÷êàõ static const uint16_t BombCost = 10;
void Draw() const override; void Draw() const override;
@ -14,3 +14,17 @@ private:
}; };
class BombDecorator: public DynamicObject
{
public:
BombDecorator(DynamicObject *bomb) : m_bomb(bomb)
{
}
void Move(uint16_t time) override;
void Draw() const override;
void SetPos(double nx, double ny) override;
uint16_t GetWidth() const override;
private:
DynamicObject *m_bomb;
};

View File

@ -2,23 +2,39 @@
#include <cstdint> #include <cstdint>
class GameObject { class GameObject
{
public: public:
GameObject() : x(0.0), y(0.0), width(0) {} GameObject() : x(0.0), y(0.0), width(0)
{
}
virtual ~GameObject() = default; virtual ~GameObject() = default;
virtual void Draw() const = 0; virtual void Draw() const = 0;
inline void SetPos(double nx, double ny) { virtual inline void SetPos(double nx, double ny)
{
x = nx; x = nx;
y = ny; y = ny;
} }
inline double GetY() const { return y; } virtual inline double GetY() const
inline double GetX() const { return x; } {
return y;
}
virtual inline double GetX() const
{
return x;
}
inline void SetWidth(uint16_t widthN) { width = widthN; } virtual inline void SetWidth(uint16_t widthN)
inline uint16_t GetWidth() const { return width; } {
width = widthN;
}
virtual inline uint16_t GetWidth() const
{
return width;
}
protected: protected:
double x, y; double x, y;

View File

@ -11,11 +11,13 @@
class SBomber class SBomber
{ {
public: public:
SBomber(); SBomber();
~SBomber(); ~SBomber();
inline bool GetExitFlag() const { return exitFlag; } inline bool GetExitFlag() const
{
return exitFlag;
}
void ProcessKBHit(); void ProcessKBHit();
void TimeStart(); void TimeStart();
@ -25,14 +27,15 @@ public:
void MoveObjects(); void MoveObjects();
void CheckObjects(); void CheckObjects();
private: void run();
private:
void CheckPlaneAndLevelGUI(); void CheckPlaneAndLevelGUI();
void CheckBombsAndGround(); void CheckBombsAndGround();
void CheckDestoyableObjects(Bomb *pBomb); void CheckDestoyableObjects(Bomb *pBomb);
void DeleteDynamicObj(DynamicObject * pBomb); // void DeleteDynamicObj(DynamicObject *pBomb);
void DeleteStaticObj(GameObject* pObj); // void DeleteStaticObj(GameObject *pObj);
Ground* FindGround() const; Ground* FindGround() const;
Plane* FindPlane() const; Plane* FindPlane() const;
@ -40,7 +43,7 @@ private:
std::vector<DestroyableGroundObject*> FindDestoyableGroundObjects() const; std::vector<DestroyableGroundObject*> FindDestoyableGroundObjects() const;
std::vector<Bomb*> FindAllBombs() const; std::vector<Bomb*> FindAllBombs() const;
void DropBomb(); // void DropBomb();
std::vector<DynamicObject*> vecDynamicObj; std::vector<DynamicObject*> vecDynamicObj;
std::vector<GameObject*> vecStaticObj; std::vector<GameObject*> vecStaticObj;

View File

@ -2,58 +2,12 @@
#include "MyTools.h" #include "MyTools.h"
#include "ScreenSingleton.h" #include "ScreenSingleton.h"
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
int _kbhit()
{
static const int STDIN = 0;
static bool initialized = false;
if (!initialized)
{
// Use termios to turn off line buffering
termios term;
tcgetattr(STDIN, &term);
term.c_lflag &= ~ICANON;
tcsetattr(STDIN, TCSANOW, &term);
setbuf(stdin, NULL);
initialized = true;
}
int bytesWaiting;
ioctl(STDIN, FIONREAD, &bytesWaiting);
return bytesWaiting;
}
int main(void) int main(void)
{ {
MyTools::LoggerSingleton::getInstance().OpenLogFile("log.txt"); MyTools::LoggerSingleton::getInstance().OpenLogFile("log.txt");
SBomber game; SBomber game;
game.run();
do
{
game.TimeStart();
if (_kbhit())
{
game.ProcessKBHit();
}
ScreenSingleton::getInstance().ClrScr();
game.DrawFrame();
game.MoveObjects();
game.CheckObjects();
game.TimeFinish();
} while (!game.GetExitFlag());
MyTools::LoggerSingleton::getInstance().CloseLogFile(); MyTools::LoggerSingleton::getInstance().CloseLogFile();

View File

@ -7,3 +7,26 @@ void Bomb::Draw() const {
ScreenSingleton::getInstance().GotoXY(x, y); ScreenSingleton::getInstance().GotoXY(x, y);
std::cout << "*"; std::cout << "*";
} }
void BombDecorator::Move(uint16_t time)
{
m_bomb->Move(time * 1.6);
}
void BombDecorator::Draw() const
{
m_bomb->Draw();
// Некоторое изменение внешнего вида бомбы
ScreenSingleton::getInstance().GotoXY(m_bomb->GetX(), m_bomb->GetY() - 1);
std::cout << "|";
}
void BombDecorator::SetPos(double nx, double ny)
{
m_bomb->SetPos(nx, ny);
}
uint16_t BombDecorator::GetWidth() const
{
return m_bomb->GetWidth();
}

View File

@ -1,4 +1,3 @@
#include "MyTools.h" #include "MyTools.h"
#include "SBomber.h" #include "SBomber.h"
#include "Bomb.h" #include "Bomb.h"
@ -10,18 +9,61 @@
#include <chrono> #include <chrono>
#include <thread> #include <thread>
SBomber::SBomber() #include <iostream>
: exitFlag(false), startTime(0), finishTime(0), deltaTime(0), passedTime(0), #include <fstream>
fps(0), bombsNumber(10), score(0) { #include <unistd.h>
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked"); #include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
Plane* p = new Plane; int _kbhit()
{
static const int STDIN = 0;
static bool initialized = false;
if (!initialized)
{
// Use termios to turn off line buffering
termios term;
tcgetattr(STDIN, &term);
term.c_lflag &= ~ICANON;
tcsetattr(STDIN, TCSANOW, &term);
setbuf(stdin, NULL);
initialized = true;
}
int bytesWaiting;
ioctl(STDIN, FIONREAD, &bytesWaiting);
return bytesWaiting;
}
class IFactory
{
public:
virtual ~IFactory() {}
DynamicObject* createPlane() const
{
Plane *p = createPlaneInstance();
p->SetDirection(1, 0.1); p->SetDirection(1, 0.1);
p->SetSpeed(4); p->SetSpeed(4);
p->SetPos(5, 10); p->SetPos(5, 10);
vecDynamicObj.push_back(p); return p;
}
LevelGUI* pGUI = new LevelGUI; DynamicObject* createBomb(const double x, const double y) const
{
Bomb *pBomb = createBombInstance();
pBomb->SetDirection(0.3, 1);
pBomb->SetSpeed(2);
pBomb->SetPos(x, y);
pBomb->SetWidth(SMALL_CRATER_SIZE);
return pBomb;
}
GameObject* createUI(const uint64_t passedTime, const uint64_t fps, const uint16_t bombsNumber, const int16_t score) const
{
LevelGUI *pGUI = createUIInstance();
pGUI->SetParam(passedTime, fps, bombsNumber, score); pGUI->SetParam(passedTime, fps, bombsNumber, score);
const uint16_t maxX = ScreenSingleton::getInstance().GetMaxX(); const uint16_t maxX = ScreenSingleton::getInstance().GetMaxX();
const uint16_t maxY = ScreenSingleton::getInstance().GetMaxY(); const uint16_t maxY = ScreenSingleton::getInstance().GetMaxY();
@ -31,137 +73,310 @@ SBomber::SBomber()
pGUI->SetWidth(width); pGUI->SetWidth(width);
pGUI->SetHeight(maxY - 4); pGUI->SetHeight(maxY - 4);
pGUI->SetFinishX(offset + width - 4); pGUI->SetFinishX(offset + width - 4);
vecStaticObj.push_back(pGUI); return pGUI;
}
Ground* pGr = new Ground; virtual GameObject* createHouse(double posX) const
{
const uint16_t maxY = ScreenSingleton::getInstance().GetMaxY();
const uint16_t groundY = maxY - 5;
House *pHouse = createHouseInstance();
pHouse->SetWidth(13);
pHouse->SetPos(posX, groundY - 1);
return pHouse;
}
virtual GameObject* createGround() const
{
const uint16_t maxX = ScreenSingleton::getInstance().GetMaxX();
const uint16_t maxY = ScreenSingleton::getInstance().GetMaxY();
const uint16_t offset = 3;
const uint16_t width = maxX - 7;
Ground *pGr = createGroundInstance();
const uint16_t groundY = maxY - 5; const uint16_t groundY = maxY - 5;
pGr->SetPos(offset + 1, groundY); pGr->SetPos(offset + 1, groundY);
pGr->SetWidth(width - 2); pGr->SetWidth(width - 2);
vecStaticObj.push_back(pGr); return pGr;
Tank* pTank = new Tank;
pTank->SetWidth(13);
pTank->SetPos(30, groundY - 1);
vecStaticObj.push_back(pTank);
pTank = new Tank;
pTank->SetWidth(13);
pTank->SetPos(50, groundY - 1);
vecStaticObj.push_back(pTank);
House* pHouse = new House;
pHouse->SetWidth(13);
pHouse->SetPos(80, groundY - 1);
vecStaticObj.push_back(pHouse);
/*
Bomb* pBomb = new Bomb;
pBomb->SetDirection(0.3, 1);
pBomb->SetSpeed(2);
pBomb->SetPos(51, 5);
pBomb->SetSize(SMALL_CRATER_SIZE);
vecDynamicObj.push_back(pBomb);
*/
} }
SBomber::~SBomber() { virtual GameObject* createTank(double posX) const
for (size_t i = 0; i < vecDynamicObj.size(); i++) { {
if (vecDynamicObj[i] != nullptr) { const uint16_t maxY = ScreenSingleton::getInstance().GetMaxY();
const uint16_t groundY = maxY - 5;
Tank *pTank = createTankInstance();
pTank->SetWidth(13);
pTank->SetPos(posX, groundY - 1);
return pTank;
}
private:
virtual Plane* createPlaneInstance() const = 0;
virtual Bomb* createBombInstance() const = 0;
virtual LevelGUI* createUIInstance() const = 0;
virtual Ground* createGroundInstance() const = 0;
virtual Tank* createTankInstance() const = 0;
virtual House* createHouseInstance() const = 0;
};
class RegularFactory : public IFactory
{
Plane* createPlaneInstance() const override
{
return new Plane;
}
Bomb* createBombInstance() const override
{
return new Bomb;
}
LevelGUI* createUIInstance() const override
{
return new LevelGUI;
}
Ground* createGroundInstance() const override
{
return new Ground;
}
Tank* createTankInstance() const override
{
return new Tank;
}
House* createHouseInstance() const override
{
return new House;
}
};
class Command
{
public:
virtual ~Command() {}
virtual void Execute() = 0;
};
template <class Object>
class DeleteCommand : public Command
{
public:
DeleteCommand(std::vector<Object*>& vecObj) : m_vecObj(vecObj), m_pObj(nullptr) {}
void setObj(Object *pObj)
{
m_pObj = pObj;
}
void Execute()
{
if (!m_pObj)
{
return;
}
auto it = m_vecObj.begin();
for (; it != m_vecObj.end(); it++)
{
if (*it == m_pObj)
{
m_vecObj.erase(it);
break;
}
}
}
private:
std::vector<Object*>& m_vecObj;
Object *m_pObj;
};
class DropCommand : public Command
{
public:
DropCommand(std::vector<DynamicObject*>& vecDynamicObj) : m_vecDynamicObj(vecDynamicObj), m_pPlane(nullptr), m_bombsNumber(nullptr), m_score(nullptr) {}
void setParams(Plane *pPlane, uint16_t *bombsNumber, int16_t *score)
{
m_pPlane = pPlane;
m_bombsNumber = bombsNumber;
m_score = score;
}
void Execute()
{
if (*m_bombsNumber > 0)
{
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked");
double x = m_pPlane->GetX() + 4;
double y = m_pPlane->GetY() + 2;
auto pFactory = new RegularFactory;
m_vecDynamicObj.push_back(pFactory->createBomb(x, y));
delete pFactory;
(*m_bombsNumber)--;
*m_score -= Bomb::BombCost;
}
}
private:
std::vector<DynamicObject*>& m_vecDynamicObj;
Plane *m_pPlane;
uint16_t *m_bombsNumber;
int16_t *m_score;
};
class DropBigCommand : public Command
{
public:
DropBigCommand(std::vector<DynamicObject*>& vecDynamicObj) : m_vecDynamicObj(vecDynamicObj), m_pPlane(nullptr), m_bombsNumber(nullptr), m_score(nullptr) {}
void setParams(Plane *pPlane, uint16_t *bombsNumber, int16_t *score)
{
m_pPlane = pPlane;
m_bombsNumber = bombsNumber;
m_score = score;
}
void Execute()
{
if (*m_bombsNumber > 0)
{
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked");
double x = m_pPlane->GetX() + 4;
double y = m_pPlane->GetY() + 2;
auto pFactory = new RegularFactory;
m_vecDynamicObj.push_back(new BombDecorator(pFactory->createBomb(x, y)));
delete pFactory;
(*m_bombsNumber)--;
*m_score -= Bomb::BombCost;
}
}
private:
std::vector<DynamicObject*>& m_vecDynamicObj;
Plane *m_pPlane;
uint16_t *m_bombsNumber;
int16_t *m_score;
};
SBomber::SBomber() : exitFlag(false), startTime(0), finishTime(0), deltaTime(0), passedTime(0), fps(0), bombsNumber(10), score(0)
{
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked");
auto pFactory = new RegularFactory;
vecDynamicObj.push_back(pFactory->createPlane());
vecStaticObj.push_back(pFactory->createUI(passedTime, fps, bombsNumber, score));
vecStaticObj.push_back(pFactory->createGround());
vecStaticObj.push_back(pFactory->createTank(30));
vecStaticObj.push_back(pFactory->createTank(50));
vecStaticObj.push_back(pFactory->createHouse(80));
delete pFactory;
}
SBomber::~SBomber()
{
for (size_t i = 0; i < vecDynamicObj.size(); i++)
{
if (vecDynamicObj[i] != nullptr)
{
delete vecDynamicObj[i]; delete vecDynamicObj[i];
} }
} }
for (size_t i = 0; i < vecStaticObj.size(); i++) { for (size_t i = 0; i < vecStaticObj.size(); i++)
if (vecStaticObj[i] != nullptr) { {
if (vecStaticObj[i] != nullptr)
{
delete vecStaticObj[i]; delete vecStaticObj[i];
} }
} }
} }
void SBomber::MoveObjects() { void SBomber::MoveObjects()
{
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked"); MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked");
for (size_t i = 0; i < vecDynamicObj.size(); i++) { for (size_t i = 0; i < vecDynamicObj.size(); i++)
if (vecDynamicObj[i] != nullptr) { {
if (vecDynamicObj[i] != nullptr)
{
vecDynamicObj[i]->Move(deltaTime); vecDynamicObj[i]->Move(deltaTime);
} }
} }
}; }
;
void SBomber::CheckObjects() { void SBomber::CheckObjects()
{
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked"); MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked");
CheckPlaneAndLevelGUI(); CheckPlaneAndLevelGUI();
CheckBombsAndGround(); CheckBombsAndGround();
}; }
;
void SBomber::CheckPlaneAndLevelGUI() { void SBomber::CheckPlaneAndLevelGUI()
if (FindPlane()->GetX() > FindLevelGUI()->GetFinishX()) { {
if (FindPlane()->GetX() > FindLevelGUI()->GetFinishX())
{
exitFlag = true; exitFlag = true;
} }
} }
void SBomber::CheckBombsAndGround() { void SBomber::CheckBombsAndGround()
{
std::vector<Bomb*> vecBombs = FindAllBombs(); std::vector<Bomb*> vecBombs = FindAllBombs();
Ground *pGround = FindGround(); Ground *pGround = FindGround();
const double y = pGround->GetY(); const double y = pGround->GetY();
for (size_t i = 0; i < vecBombs.size(); i++) { for (size_t i = 0; i < vecBombs.size(); i++)
if (vecBombs[i]->GetY() >= y) { {
if (vecBombs[i]->GetY() >= y)
{
pGround->AddCrater(vecBombs[i]->GetX()); pGround->AddCrater(vecBombs[i]->GetX());
CheckDestoyableObjects(vecBombs[i]); CheckDestoyableObjects(vecBombs[i]);
DeleteDynamicObj(vecBombs[i]);
DeleteCommand<DynamicObject> delCom(SBomber::vecDynamicObj);
delCom.setObj(vecBombs[i]);
delCom.Execute();
} }
} }
} }
void SBomber::CheckDestoyableObjects(Bomb* pBomb) { void SBomber::CheckDestoyableObjects(Bomb *pBomb)
std::vector<DestroyableGroundObject*> vecDestoyableObjects = {
FindDestoyableGroundObjects(); std::vector<DestroyableGroundObject*> vecDestoyableObjects = FindDestoyableGroundObjects();
const double size = pBomb->GetWidth(); const double size = pBomb->GetWidth();
const double size_2 = size / 2; const double size_2 = size / 2;
for (size_t i = 0; i < vecDestoyableObjects.size(); i++) { for (size_t i = 0; i < vecDestoyableObjects.size(); i++)
{
const double x1 = pBomb->GetX() - size_2; const double x1 = pBomb->GetX() - size_2;
const double x2 = x1 + size; const double x2 = x1 + size;
if (vecDestoyableObjects[i]->isInside(x1, x2)) { if (vecDestoyableObjects[i]->isInside(x1, x2))
{
score += vecDestoyableObjects[i]->GetScore(); score += vecDestoyableObjects[i]->GetScore();
DeleteStaticObj(vecDestoyableObjects[i]);
DeleteCommand<GameObject> delCom(SBomber::vecStaticObj);
delCom.setObj(vecDestoyableObjects[i]);
delCom.Execute();
} }
} }
} }
void SBomber::DeleteDynamicObj(DynamicObject* pObj) { std::vector<DestroyableGroundObject*> SBomber::FindDestoyableGroundObjects() const
auto it = vecDynamicObj.begin(); {
for (; it != vecDynamicObj.end(); it++) {
if (*it == pObj) {
vecDynamicObj.erase(it);
break;
}
}
}
void SBomber::DeleteStaticObj(GameObject* pObj) {
auto it = vecStaticObj.begin();
for (; it != vecStaticObj.end(); it++) {
if (*it == pObj) {
vecStaticObj.erase(it);
break;
}
}
}
std::vector<DestroyableGroundObject*> SBomber::FindDestoyableGroundObjects() const {
std::vector<DestroyableGroundObject*> vec; std::vector<DestroyableGroundObject*> vec;
Tank *pTank; Tank *pTank;
House *pHouse; House *pHouse;
for (size_t i = 0; i < vecStaticObj.size(); i++) { for (size_t i = 0; i < vecStaticObj.size(); i++)
{
pTank = dynamic_cast<Tank*>(vecStaticObj[i]); pTank = dynamic_cast<Tank*>(vecStaticObj[i]);
if (pTank != nullptr) { if (pTank != nullptr)
{
vec.push_back(pTank); vec.push_back(pTank);
continue; continue;
} }
pHouse = dynamic_cast<House*>(vecStaticObj[i]); pHouse = dynamic_cast<House*>(vecStaticObj[i]);
if (pHouse != nullptr) { if (pHouse != nullptr)
{
vec.push_back(pHouse); vec.push_back(pHouse);
continue; continue;
} }
@ -170,12 +385,15 @@ std::vector<DestroyableGroundObject*> SBomber::FindDestoyableGroundObjects() con
return vec; return vec;
} }
Ground* SBomber::FindGround() const { Ground* SBomber::FindGround() const
{
Ground *pGround; Ground *pGround;
for (size_t i = 0; i < vecStaticObj.size(); i++) { for (size_t i = 0; i < vecStaticObj.size(); i++)
{
pGround = dynamic_cast<Ground*>(vecStaticObj[i]); pGround = dynamic_cast<Ground*>(vecStaticObj[i]);
if (pGround != nullptr) { if (pGround != nullptr)
{
return pGround; return pGround;
} }
} }
@ -183,12 +401,15 @@ Ground* SBomber::FindGround() const {
return nullptr; return nullptr;
} }
std::vector<Bomb*> SBomber::FindAllBombs() const { std::vector<Bomb*> SBomber::FindAllBombs() const
{
std::vector<Bomb*> vecBombs; std::vector<Bomb*> vecBombs;
for (size_t i = 0; i < vecDynamicObj.size(); i++) { for (size_t i = 0; i < vecDynamicObj.size(); i++)
{
Bomb *pBomb = dynamic_cast<Bomb*>(vecDynamicObj[i]); Bomb *pBomb = dynamic_cast<Bomb*>(vecDynamicObj[i]);
if (pBomb != nullptr) { if (pBomb != nullptr)
{
vecBombs.push_back(pBomb); vecBombs.push_back(pBomb);
} }
} }
@ -196,10 +417,13 @@ std::vector<Bomb*> SBomber::FindAllBombs() const {
return vecBombs; return vecBombs;
} }
Plane* SBomber::FindPlane() const { Plane* SBomber::FindPlane() const
for (size_t i = 0; i < vecDynamicObj.size(); i++) { {
for (size_t i = 0; i < vecDynamicObj.size(); i++)
{
Plane *p = dynamic_cast<Plane*>(vecDynamicObj[i]); Plane *p = dynamic_cast<Plane*>(vecDynamicObj[i]);
if (p != nullptr) { if (p != nullptr)
{
return p; return p;
} }
} }
@ -207,10 +431,13 @@ Plane* SBomber::FindPlane() const {
return nullptr; return nullptr;
} }
LevelGUI* SBomber::FindLevelGUI() const { LevelGUI* SBomber::FindLevelGUI() const
for (size_t i = 0; i < vecStaticObj.size(); i++) { {
for (size_t i = 0; i < vecStaticObj.size(); i++)
{
LevelGUI *p = dynamic_cast<LevelGUI*>(vecStaticObj[i]); LevelGUI *p = dynamic_cast<LevelGUI*>(vecStaticObj[i]);
if (p != nullptr) { if (p != nullptr)
{
return p; return p;
} }
} }
@ -218,53 +445,63 @@ LevelGUI* SBomber::FindLevelGUI() const {
return nullptr; return nullptr;
} }
void SBomber::ProcessKBHit() { void SBomber::ProcessKBHit()
{
int c = getchar(); int c = getchar();
if (c == 224) { if (c == 224)
{
c = getchar(); c = getchar();
} }
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked. key = ", c); MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked. key = ", c);
switch (c) { switch (c)
{
case 27: // esc case 27: // esc
exitFlag = true; exitFlag = true;
break; break;
case 72: // up case 72: // up
FindPlane()->ChangePlaneY(-0.25); FindPlane()->ChangePlaneY(-0.25);
break; break;
case 80: // down case 80: // down
FindPlane()->ChangePlaneY(0.25); FindPlane()->ChangePlaneY(0.25);
break; break;
case 'b': case 'b':
DropBomb(); {
DropCommand dropCom(SBomber::vecDynamicObj);
dropCom.setParams(FindPlane(), &bombsNumber, &score);
dropCom.Execute();
break; break;
}
case 'B': case 'B':
DropBomb(); {
DropBigCommand dropCom(SBomber::vecDynamicObj);
dropCom.setParams(FindPlane(), &bombsNumber, &score);
dropCom.Execute();
break; break;
}
default: default:
break; break;
} }
} }
void SBomber::DrawFrame() { void SBomber::DrawFrame()
{
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked"); MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked");
for (size_t i = 0; i < vecDynamicObj.size(); i++) { for (size_t i = 0; i < vecDynamicObj.size(); i++)
if (vecDynamicObj[i] != nullptr) { {
if (vecDynamicObj[i] != nullptr)
{
vecDynamicObj[i]->Draw(); vecDynamicObj[i]->Draw();
} }
} }
for (size_t i = 0; i < vecStaticObj.size(); i++) { for (size_t i = 0; i < vecStaticObj.size(); i++)
if (vecStaticObj[i] != nullptr) { {
if (vecStaticObj[i] != nullptr)
{
vecStaticObj[i]->Draw(); vecStaticObj[i]->Draw();
} }
} }
@ -275,37 +512,39 @@ void SBomber::DrawFrame() {
FindLevelGUI()->SetParam(passedTime, fps, bombsNumber, score); FindLevelGUI()->SetParam(passedTime, fps, bombsNumber, score);
} }
void SBomber::TimeStart() { void SBomber::TimeStart()
{
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked"); MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked");
startTime = std::chrono::duration_cast<std::chrono::milliseconds>( startTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
std::chrono::high_resolution_clock::now().time_since_epoch()).count();
} }
void SBomber::TimeFinish() { void SBomber::TimeFinish()
finishTime = std::chrono::duration_cast<std::chrono::milliseconds>( {
std::chrono::high_resolution_clock::now().time_since_epoch()).count(); finishTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
deltaTime = uint16_t(finishTime - startTime); deltaTime = uint16_t(finishTime - startTime);
passedTime += deltaTime; passedTime += deltaTime;
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " deltaTime = ", (int) deltaTime); MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " deltaTime = ", (int) deltaTime);
} }
void SBomber::DropBomb() { void SBomber::run()
if (bombsNumber > 0) { {
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked"); do
{
TimeStart();
Plane* pPlane = FindPlane(); if (_kbhit())
double x = pPlane->GetX() + 4; {
double y = pPlane->GetY() + 2; ProcessKBHit();
Bomb* pBomb = new Bomb;
pBomb->SetDirection(0.3, 1);
pBomb->SetSpeed(2);
pBomb->SetPos(x, y);
pBomb->SetWidth(SMALL_CRATER_SIZE);
vecDynamicObj.push_back(pBomb);
bombsNumber--;
score -= Bomb::BombCost;
} }
ScreenSingleton::getInstance().ClrScr();
DrawFrame();
MoveObjects();
CheckObjects();
TimeFinish();
} while (!GetExitFlag());
} }

1030
hw2.patch Normal file

File diff suppressed because it is too large Load Diff