lesson_8 #3

Open
alexander wants to merge 3 commits from lesson_8 into master
7 changed files with 427 additions and 306 deletions
Showing only changes of commit 652bb12e7b - Show all commits

View File

@ -5,7 +5,7 @@
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="-1286560408718923769" id="org.eclipse.cdt.managedbuilder.core.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="-1277994465197165617" id="org.eclipse.cdt.managedbuilder.core.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<language-scope id="org.eclipse.cdt.core.gcc"/>
<language-scope id="org.eclipse.cdt.core.g++"/>
</provider>
@ -16,7 +16,7 @@
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="-1286562333461475541" id="org.eclipse.cdt.managedbuilder.core.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<provider class="org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector" console="false" env-hash="-1277996664550349269" id="org.eclipse.cdt.managedbuilder.core.GCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<language-scope id="org.eclipse.cdt.core.gcc"/>
<language-scope id="org.eclipse.cdt.core.g++"/>
</provider>

View File

@ -12,6 +12,8 @@ public:
virtual inline uint16_t GetScore() const = 0;
virtual DestroyableGroundObject* clone() const = 0;
protected:
};

View File

@ -3,16 +3,32 @@
#include "DestroyableGroundObject.h"
#include <cstdint>
class House : public DestroyableGroundObject {
class House: public DestroyableGroundObject
{
public:
House() {}
House(const House &h)
{
x = h.x;
y = h.y;
width = h.width;
}
bool isInside(double x1, double x2) const override;
inline uint16_t GetScore() const override {
inline uint16_t GetScore() const override
{
return score;
}
void Draw() const override;
House* clone() const
{
return new House(*this);
}
private:
const uint16_t score = 40;
};

View File

@ -42,6 +42,8 @@ private:
void DropBomb();
void AnimateScrolling();
std::vector<DynamicObject*> vecDynamicObj;
std::vector<GameObject*> vecStaticObj;

View File

@ -7,6 +7,14 @@
class Tank : public DestroyableGroundObject
{
public:
Tank() {}
Tank(const Tank &t)
{
x = t.x;
y = t.y;
width = t.width;
}
bool isInside(double x1, double x2) const override;
@ -14,6 +22,11 @@ public:
void Draw() const override;
Tank* clone() const
{
return new Tank(*this);
}
private:
const uint16_t score = 30;

View File

@ -54,7 +54,7 @@ int main(void)
game.TimeFinish();
} while (!game.GetExitFlag());
game.AnimateScrolling();
MyTools::LoggerSingleton::getInstance().CloseLogFile();
return 0;

View File

@ -1,4 +1,3 @@
#include "MyTools.h"
#include "SBomber.h"
#include "Bomb.h"
@ -10,18 +9,19 @@
#include <chrono>
#include <thread>
SBomber::SBomber()
: exitFlag(false), startTime(0), finishTime(0), deltaTime(0), passedTime(0),
fps(0), bombsNumber(10), score(0) {
extern int _kbhit();
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");
Plane* p = new Plane;
Plane *p = new Plane;
p->SetDirection(1, 0.1);
p->SetSpeed(4);
p->SetPos(5, 10);
vecDynamicObj.push_back(p);
LevelGUI* pGUI = new LevelGUI;
LevelGUI *pGUI = new LevelGUI;
pGUI->SetParam(passedTime, fps, bombsNumber, score);
const uint16_t maxX = ScreenSingleton::getInstance().GetMaxX();
const uint16_t maxY = ScreenSingleton::getInstance().GetMaxY();
@ -33,13 +33,13 @@ SBomber::SBomber()
pGUI->SetFinishX(offset + width - 4);
vecStaticObj.push_back(pGUI);
Ground* pGr = new Ground;
Ground *pGr = new Ground;
const uint16_t groundY = maxY - 5;
pGr->SetPos(offset + 1, groundY);
pGr->SetWidth(width - 2);
vecStaticObj.push_back(pGr);
Tank* pTank = new Tank;
Tank *pTank = new Tank;
pTank->SetWidth(13);
pTank->SetPos(30, groundY - 1);
vecStaticObj.push_back(pTank);
@ -49,7 +49,7 @@ SBomber::SBomber()
pTank->SetPos(50, groundY - 1);
vecStaticObj.push_back(pTank);
House* pHouse = new House;
House *pHouse = new House;
pHouse->SetWidth(13);
pHouse->SetPos(80, groundY - 1);
vecStaticObj.push_back(pHouse);
@ -64,49 +64,65 @@ SBomber::SBomber()
*/
}
SBomber::~SBomber() {
for (size_t i = 0; i < vecDynamicObj.size(); i++) {
if (vecDynamicObj[i] != nullptr) {
SBomber::~SBomber()
{
for (size_t i = 0; i < vecDynamicObj.size(); i++)
{
if (vecDynamicObj[i] != nullptr)
{
delete vecDynamicObj[i];
}
}
for (size_t i = 0; i < vecStaticObj.size(); i++) {
if (vecStaticObj[i] != nullptr) {
for (size_t i = 0; i < vecStaticObj.size(); i++)
{
if (vecStaticObj[i] != nullptr)
{
delete vecStaticObj[i];
}
}
}
void SBomber::MoveObjects() {
void SBomber::MoveObjects()
{
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked");
for (size_t i = 0; i < vecDynamicObj.size(); i++) {
if (vecDynamicObj[i] != nullptr) {
for (size_t i = 0; i < vecDynamicObj.size(); i++)
{
if (vecDynamicObj[i] != nullptr)
{
vecDynamicObj[i]->Move(deltaTime);
}
}
};
}
;
void SBomber::CheckObjects() {
void SBomber::CheckObjects()
{
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked");
CheckPlaneAndLevelGUI();
CheckBombsAndGround();
};
}
;
void SBomber::CheckPlaneAndLevelGUI() {
if (FindPlane()->GetX() > FindLevelGUI()->GetFinishX()) {
void SBomber::CheckPlaneAndLevelGUI()
{
if (FindPlane()->GetX() > FindLevelGUI()->GetFinishX())
{
exitFlag = true;
}
}
void SBomber::CheckBombsAndGround() {
void SBomber::CheckBombsAndGround()
{
std::vector<Bomb*> vecBombs = FindAllBombs();
Ground* pGround = FindGround();
Ground *pGround = FindGround();
const double y = pGround->GetY();
for (size_t i = 0; i < vecBombs.size(); i++) {
if (vecBombs[i]->GetY() >= y) {
for (size_t i = 0; i < vecBombs.size(); i++)
{
if (vecBombs[i]->GetY() >= y)
{
pGround->AddCrater(vecBombs[i]->GetX());
CheckDestoyableObjects(vecBombs[i]);
DeleteDynamicObj(vecBombs[i]);
@ -114,54 +130,66 @@ void SBomber::CheckBombsAndGround() {
}
}
void SBomber::CheckDestoyableObjects(Bomb* pBomb) {
std::vector<DestroyableGroundObject*> vecDestoyableObjects =
FindDestoyableGroundObjects();
void SBomber::CheckDestoyableObjects(Bomb *pBomb)
{
std::vector<DestroyableGroundObject*> vecDestoyableObjects = FindDestoyableGroundObjects();
const double size = pBomb->GetWidth();
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 x2 = x1 + size;
if (vecDestoyableObjects[i]->isInside(x1, x2)) {
if (vecDestoyableObjects[i]->isInside(x1, x2))
{
score += vecDestoyableObjects[i]->GetScore();
DeleteStaticObj(vecDestoyableObjects[i]);
}
}
}
void SBomber::DeleteDynamicObj(DynamicObject* pObj) {
void SBomber::DeleteDynamicObj(DynamicObject *pObj)
{
auto it = vecDynamicObj.begin();
for (; it != vecDynamicObj.end(); it++) {
if (*it == pObj) {
for (; it != vecDynamicObj.end(); it++)
{
if (*it == pObj)
{
vecDynamicObj.erase(it);
break;
}
}
}
void SBomber::DeleteStaticObj(GameObject* pObj) {
void SBomber::DeleteStaticObj(GameObject *pObj)
{
auto it = vecStaticObj.begin();
for (; it != vecStaticObj.end(); it++) {
if (*it == pObj) {
for (; it != vecStaticObj.end(); it++)
{
if (*it == pObj)
{
vecStaticObj.erase(it);
break;
}
}
}
std::vector<DestroyableGroundObject*> SBomber::FindDestoyableGroundObjects() const {
std::vector<DestroyableGroundObject*> SBomber::FindDestoyableGroundObjects() const
{
std::vector<DestroyableGroundObject*> vec;
Tank* pTank;
House* pHouse;
for (size_t i = 0; i < vecStaticObj.size(); i++) {
Tank *pTank;
House *pHouse;
for (size_t i = 0; i < vecStaticObj.size(); i++)
{
pTank = dynamic_cast<Tank*>(vecStaticObj[i]);
if (pTank != nullptr) {
if (pTank != nullptr)
{
vec.push_back(pTank);
continue;
}
pHouse = dynamic_cast<House*>(vecStaticObj[i]);
if (pHouse != nullptr) {
if (pHouse != nullptr)
{
vec.push_back(pHouse);
continue;
}
@ -170,12 +198,15 @@ std::vector<DestroyableGroundObject*> SBomber::FindDestoyableGroundObjects() con
return vec;
}
Ground* SBomber::FindGround() const {
Ground* pGround;
Ground* SBomber::FindGround() const
{
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]);
if (pGround != nullptr) {
if (pGround != nullptr)
{
return pGround;
}
}
@ -183,12 +214,15 @@ Ground* SBomber::FindGround() const {
return nullptr;
}
std::vector<Bomb*> SBomber::FindAllBombs() const {
std::vector<Bomb*> SBomber::FindAllBombs() const
{
std::vector<Bomb*> vecBombs;
for (size_t i = 0; i < vecDynamicObj.size(); i++) {
Bomb* pBomb = dynamic_cast<Bomb*>(vecDynamicObj[i]);
if (pBomb != nullptr) {
for (size_t i = 0; i < vecDynamicObj.size(); i++)
{
Bomb *pBomb = dynamic_cast<Bomb*>(vecDynamicObj[i]);
if (pBomb != nullptr)
{
vecBombs.push_back(pBomb);
}
}
@ -196,10 +230,13 @@ std::vector<Bomb*> SBomber::FindAllBombs() const {
return vecBombs;
}
Plane* SBomber::FindPlane() const {
for (size_t i = 0; i < vecDynamicObj.size(); i++) {
Plane* p = dynamic_cast<Plane*>(vecDynamicObj[i]);
if (p != nullptr) {
Plane* SBomber::FindPlane() const
{
for (size_t i = 0; i < vecDynamicObj.size(); i++)
{
Plane *p = dynamic_cast<Plane*>(vecDynamicObj[i]);
if (p != nullptr)
{
return p;
}
}
@ -207,10 +244,13 @@ Plane* SBomber::FindPlane() const {
return nullptr;
}
LevelGUI* SBomber::FindLevelGUI() const {
for (size_t i = 0; i < vecStaticObj.size(); i++) {
LevelGUI* p = dynamic_cast<LevelGUI*>(vecStaticObj[i]);
if (p != nullptr) {
LevelGUI* SBomber::FindLevelGUI() const
{
for (size_t i = 0; i < vecStaticObj.size(); i++)
{
LevelGUI *p = dynamic_cast<LevelGUI*>(vecStaticObj[i]);
if (p != nullptr)
{
return p;
}
}
@ -218,16 +258,19 @@ LevelGUI* SBomber::FindLevelGUI() const {
return nullptr;
}
void SBomber::ProcessKBHit() {
void SBomber::ProcessKBHit()
{
int c = getchar();
if (c == 224) {
if (c == 224)
{
c = getchar();
}
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked. key = ", c);
switch (c) {
switch (c)
{
case 27: // esc
exitFlag = true;
@ -249,22 +292,43 @@ void SBomber::ProcessKBHit() {
DropBomb();
break;
case 'd':
{
DestroyableGroundObject *clone = nullptr;
std::vector<DestroyableGroundObject*> vecDestoyableObjects = FindDestoyableGroundObjects();
for (size_t i = 0; i < vecDestoyableObjects.size(); i++)
{
if (!clone) clone = vecDestoyableObjects[i]->clone();
if (!vecDestoyableObjects[i]->isInside(clone->GetX(), clone->GetY()))
{
vecStaticObj.push_back(clone);
}
}
break;
}
default:
break;
}
}
void SBomber::DrawFrame() {
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) {
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) {
for (size_t i = 0; i < vecStaticObj.size(); i++)
{
if (vecStaticObj[i] != nullptr)
{
vecStaticObj[i]->Draw();
}
}
@ -275,30 +339,32 @@ void SBomber::DrawFrame() {
FindLevelGUI()->SetParam(passedTime, fps, bombsNumber, score);
}
void SBomber::TimeStart() {
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();
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();
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);
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " deltaTime = ", (int) deltaTime);
}
void SBomber::DropBomb() {
if (bombsNumber > 0) {
void SBomber::DropBomb()
{
if (bombsNumber > 0)
{
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__func__) + " was invoked");
Plane* pPlane = FindPlane();
Plane *pPlane = FindPlane();
double x = pPlane->GetX() + 4;
double y = pPlane->GetY() + 2;
Bomb* pBomb = new Bomb;
Bomb *pBomb = new Bomb;
pBomb->SetDirection(0.3, 1);
pBomb->SetSpeed(2);
pBomb->SetPos(x, y);
@ -309,3 +375,25 @@ void SBomber::DropBomb() {
score -= Bomb::BombCost;
}
}
void SBomber::AnimateScrolling()
{
MyTools::LoggerSingleton::getInstance().WriteToLog(std::string(__FUNCTION__) + " was invoked");
const size_t windowHeight = 10; // Размер окна для скроллинга
const size_t startX = MyTools::GetMaxX() / 2 - ScrollWidth / 2;
const size_t startY = MyTools::GetMaxY() / 2 - windowHeight / 2;
double curPos = 0;
do {
TimeStart();
MyTools::ClrScr();
// вывод windowHeight строк из ppScroll используя смещение curPos
// ...
MyTools::GotoXY(0, 0);
TimeFinish();
curPos += deltaTime * 0.0015;
} while (!_kbhit() && int(curPos) <= (ScrollHeight - windowHeight));
MyTools::ClrScr();
}