Compare commits

..

3 Commits

Author SHA1 Message Date
Alexander Zhirov e37ff585f7 lesson 2 withot destroy 2021-12-25 08:46:23 +03:00
Alexander Zhirov 42495521c3 remove log-file 2021-12-21 02:42:14 +03:00
Alexander Zhirov 1043a44d7e lesson 1 2021-12-21 02:33:45 +03:00
12 changed files with 1812 additions and 320388 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,25 +2,41 @@
#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; {
y = ny; x = nx;
} 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;
uint16_t width; uint16_t width;
}; };

View File

@ -3,16 +3,75 @@
#include <string> #include <string>
#include <cstring> #include <cstring>
namespace MyTools { #include <iostream>
#include <vector>
void OpenLogFile(const std::string& FN); namespace MyTools
{
void CloseLogFile(); // void OpenLogFile(const std::string &FN);
// void CloseLogFile();
// void WriteToLog(const std::string &str);
// void WriteToLog(const std::string &str, int n);
// void WriteToLog(const std::string &str, double d);
void WriteToLog(const std::string& str); class Logger
{
public:
virtual void OpenLogFile(const std::string &FN) = 0;
virtual void CloseLogFile() = 0;
virtual void WriteToLog(const std::string &str) = 0;
virtual void WriteToLog(const std::string &str, int n) = 0;
virtual void WriteToLog(const std::string &str, double d) = 0;
virtual ~Logger() {}
};
void WriteToLog(const std::string& str, int n); class FileLoggerSingleton: public Logger
{
public:
static FileLoggerSingleton& getInstance()
{
static FileLoggerSingleton _instance;
return _instance;
}
void WriteToLog(const std::string& str, double d); void OpenLogFile(const std::string &FN) override;
void CloseLogFile() override;
void WriteToLog(const std::string &str) override;
void WriteToLog(const std::string &str, int n) override;
void WriteToLog(const std::string &str, double d) override;
private:
FileLoggerSingleton() {}
FileLoggerSingleton(const FileLoggerSingleton&) = delete;
FileLoggerSingleton& operator=(const FileLoggerSingleton&) = delete;
FileLoggerSingleton& operator=(FileLoggerSingleton&&) = delete;
};
}; // namespace MyTools class LoggerSingleton: public Logger
{
public:
static LoggerSingleton& getInstance()
{
static LoggerSingleton _instance;
return _instance;
}
void OpenLogFile(const std::string &FN) override;
void CloseLogFile() override;
void WriteToLog(const std::string &str) override;
void WriteToLog(const std::string &str, int n) override;
void WriteToLog(const std::string &str, double d) override;
~LoggerSingleton();
private:
LoggerSingleton() {}
LoggerSingleton(const LoggerSingleton&) = delete;
LoggerSingleton& operator=(const LoggerSingleton&) = delete;
LoggerSingleton& operator=(LoggerSingleton&&) = delete;
FileLoggerSingleton& LoadLoggerSingletone();
FileLoggerSingleton *_logger = NULL;
std::vector<double> times;
};
} // namespace MyTools

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,29 +27,30 @@ 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;
LevelGUI * FindLevelGUI() const; LevelGUI* FindLevelGUI() const;
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;
bool exitFlag; bool exitFlag;
uint64_t startTime, finishTime, passedTime; uint64_t startTime, finishTime, passedTime;
uint16_t bombsNumber, deltaTime, fps; uint16_t bombsNumber, deltaTime, fps;
int16_t score; int16_t score;
}; };

46
SBomber/include/Timer.h Normal file
View File

@ -0,0 +1,46 @@
/*
* Timer.h
*
* Created on: 21 дек. 2021 г.
* Author: alexander
*/
#pragma once
#include <chrono>
class Timer
{
public:
static Timer& getInstance()
{
static Timer _instance;
return _instance;
}
void start()
{
m_beg = clock_t::now();
}
double end() const
{
return elapsed() * 1000;
}
private:
using clock_t = std::chrono::high_resolution_clock;
using second_t = std::chrono::duration<double, std::ratio<1> >;
std::chrono::time_point<clock_t> m_beg;
double elapsed() const
{
return std::chrono::duration_cast<second_t>(clock_t::now() - m_beg).count();
}
Timer() {}
Timer(const Timer&) = delete;
Timer& operator=(const Timer&) = delete;
Timer& operator=(Timer&&) = delete;
};

File diff suppressed because it is too large Load Diff

View File

@ -2,60 +2,14 @@
#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::OpenLogFile("log.txt"); MyTools::LoggerSingleton::getInstance().OpenLogFile("log.txt");
SBomber game; SBomber game;
game.run();
do MyTools::LoggerSingleton::getInstance().CloseLogFile();
{
game.TimeStart();
if (_kbhit())
{
game.ProcessKBHit();
}
ScreenSingleton::getInstance().ClrScr();
game.DrawFrame();
game.MoveObjects();
game.CheckObjects();
game.TimeFinish();
} while (!game.GetExitFlag());
MyTools::CloseLogFile();
return 0; return 0;
} }

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

@ -9,42 +9,110 @@
#include <time.h> #include <time.h>
#include <stdio.h> #include <stdio.h>
#include <numeric>
#include "Timer.h"
namespace MyTools { namespace MyTools
{
std::ofstream logOut; std::ofstream logOut;
void OpenLogFile(const std::string &FN) { logOut.open(FN, std::ios_base::out); } void FileLoggerSingleton::OpenLogFile(const std::string &FN)
{
logOut.open(FN, std::ios_base::out);
}
void CloseLogFile() { void FileLoggerSingleton::CloseLogFile()
if (logOut.is_open()) { {
logOut.close(); if (logOut.is_open())
} {
} logOut.close();
}
}
std::string GetCurDateTime() { std::string GetCurDateTime()
auto cur = std::chrono::system_clock::now(); {
time_t time = std::chrono::system_clock::to_time_t(cur); auto cur = std::chrono::system_clock::now();
char* buf = ctime(&time); time_t time = std::chrono::system_clock::to_time_t(cur);
return std::string(buf); char *buf = ctime(&time);
} return std::string(buf);
}
void WriteToLog(const std::string &str) { void FileLoggerSingleton::WriteToLog(const std::string &str)
if (logOut.is_open()) { {
logOut << GetCurDateTime() << " - " << str << std::endl; if (logOut.is_open())
} {
} logOut << GetCurDateTime() << " - " << str << std::endl;
}
}
void WriteToLog(const std::string &str, int n) { void FileLoggerSingleton::WriteToLog(const std::string &str, int n)
if (logOut.is_open()) { {
logOut << GetCurDateTime() << " - " << str << n << std::endl; if (logOut.is_open())
} {
} logOut << GetCurDateTime() << " - " << str << n << std::endl;
}
}
void WriteToLog(const std::string &str, double d) { void FileLoggerSingleton::WriteToLog(const std::string &str, double d)
if (logOut.is_open()) { {
logOut << GetCurDateTime() << " - " << str << d << std::endl; if (logOut.is_open())
} {
} logOut << GetCurDateTime() << " - " << str << d << std::endl;
}
}
void LoggerSingleton::OpenLogFile(const std::string &FN)
{
Timer::getInstance().start();
LoadLoggerSingletone().OpenLogFile(FN);
times.push_back(Timer::getInstance().end());
}
void LoggerSingleton::CloseLogFile()
{
Timer::getInstance().start();
LoadLoggerSingletone().CloseLogFile();
times.push_back(Timer::getInstance().end());
}
void LoggerSingleton::WriteToLog(const std::string &str)
{
Timer::getInstance().start();
LoadLoggerSingletone().WriteToLog(str);
times.push_back(Timer::getInstance().end());
}
void LoggerSingleton::WriteToLog(const std::string &str, int n)
{
Timer::getInstance().start();
LoadLoggerSingletone().WriteToLog(str, n);
times.push_back(Timer::getInstance().end());
}
void LoggerSingleton::WriteToLog(const std::string &str, double d)
{
Timer::getInstance().start();
LoadLoggerSingletone().WriteToLog(str, d);
times.push_back(Timer::getInstance().end());
}
LoggerSingleton::~LoggerSingleton()
{
if (times.size())
{
std::cout << "Среднее время выполнения операций: " << std::accumulate(times.begin(), times.end(), 0.0) / times.size() << " секунд" << std::endl;
}
}
FileLoggerSingleton& LoggerSingleton::LoadLoggerSingletone()
{
if (!_logger)
{
_logger = &FileLoggerSingleton::getInstance();
}
return *_logger;
}
} // namespace MyTools } // namespace MyTools

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

View File

@ -33,15 +33,15 @@ IScreen& getInternalInstance() {
class ScreenSingletonProxy : public IScreen { class ScreenSingletonProxy : public IScreen {
public: public:
virtual void ClrScr() override { virtual void ClrScr() override {
MyTools::WriteToLog("ClrScr invoke begin"); MyTools::LoggerSingleton::getInstance().WriteToLog("ClrScr invoke begin");
getInternalInstance().ClrScr(); getInternalInstance().ClrScr();
MyTools::WriteToLog("ClrScr invoke end"); MyTools::LoggerSingleton::getInstance().WriteToLog("ClrScr invoke end");
} }
virtual void GotoXY(double x, double y) override { virtual void GotoXY(double x, double y) override {
MyTools::WriteToLog("GotoXY invoke begin"); MyTools::LoggerSingleton::getInstance().WriteToLog("GotoXY invoke begin");
getInternalInstance().GotoXY(x, y); getInternalInstance().GotoXY(x, y);
MyTools::WriteToLog("GotoXY invoke end"); MyTools::LoggerSingleton::getInstance().WriteToLog("GotoXY invoke end");
} }
virtual uint16_t GetMaxX() override { virtual uint16_t GetMaxX() override {
return getInternalInstance().GetMaxX(); return getInternalInstance().GetMaxX();

1030
hw2.patch Normal file

File diff suppressed because it is too large Load Diff