2D_game_Inherit - 8BitsCoding/RobotMentor GitHub Wiki
yellow triangle, red circle, blue box ์ ๊ทธ๋ฆฌ๊ธฐ ์ํด์
์๋์ ๊ฐ์ด ๋งค๋ฒ ํจ์๋ฅผ ํธ์ถํ๋ ๊ฒ์ด ๋ฒ๊ฑฐ๋กญ๋ค.
namespace jm
{
class Example : public Game2D
{
public:
Example()
: Game2D()
{}
void update() override
{
// yellow triangle
beginTransformation();
{
translate(vec2{ -0.5f, -0.05f });
drawFilledTriangle(Colors::yellow, 0.3f);
}
endTransformation();
// red circle
beginTransformation();
{
drawFilledCircle(Colors::red, 0.15f);
}
endTransformation();
// blue box
beginTransformation();
{
translate(vec2{ 0.5f, 0.0f });
drawFilledBox(Colors::blue, 0.25f, 0.3f);
}
endTransformation();
}
};
}
๋ค์๊ณผ ๊ฐ์ด ํด๋์คํ ํ๋ค.
#include "Triangel.h"
namespace jm
{
class Example : public Game2D
{
public:
Example()
: Game2D()
{}
void update() override
{
Triangel my_tri;
my_tri.draw();
Circle my_cir;
my_cir.draw();
bluebox my_bb;
my_bb.draw();
// Triangel.h
#pragma once
#include "Game2D.h"
namespace jm
{
class Triangel
{
public:
void draw()
{
beginTransformation();
{
translate(vec2{ -0.5f, -0.05f });
drawFilledTriangle(Colors::yellow, 0.3f);
}
endTransformation();
}
};
}
// Circle.h
namespace jm
{
class Circle
{
public:
void draw()
{
beginTransformation();
{
drawFilledCircle(Colors::red, 0.15f);
}
endTransformation();
}
};
}
// bluebox.h
namespace jm
{
class bluebox
{
public:
void draw()
{
beginTransformation();
{
translate(vec2{ 0.5f, 0.0f });
drawFilledBox(Colors::blue, 0.25f, 0.3f);
}
endTransformation();
}
};
}
๋งค๋ฒ๋ฐ๋ณต๋๋ beginTransformation();, endTransformation();์ ์ค๋ณต์ ํผํ๊ณ ์ถ๋ค.
namespace jm
{
class GeometricObject
{
virtual void drawGeometry() const = 0;
void draw()
{
beginTransformation();
{
translate(pos);
drawGeometry();
}
endTransformation();
}
};
}
// Triangel.h
#pragma once
#include "GeometricObject"
#include "Game2D.h"
namespace jm
{
class Triangel : public GeometricObject
{
public:
void drawGeometry() override
{
translate(vec2{ -0.5f, -0.05f });
drawFilledTriangle(Colors::yellow, 0.3f);
}
};
}
์ด๋ฐ์์ผ๋ก ๊ณตํต๋ ๋ถ๋ถ์ ํ๋๋ก ๋ชจ์ ์ ์๋ค.
๋ง์ง๋ง์ผ๋ก ํ ๊ฐ์ง ๋ ์๊ฐํด ๋ด์ผํ ์ ...
๋งค๋ฒ #include "class.h"๋ฅผ ํด์ผํ๋?
-> Factory Pattern์ผ๋ก ํด๊ฒฐ์ด ๊ฐ๋ฅํ๋ค.
namespace jm
{
class Example : public Game2D
{
public:
std::vector<std::unique_ptr<GeometricObject>> my_objs;
Example()
: Game2D()
{
my_objs.push_back(std::make_unique<Triangle>(Colors::gold, vec2{ -0.5f, 0.2f }, 0.25f));
my_objs.push_back(std::make_unique<Circle>(Colors::olive, vec2{ 0.1f, 0.1f }, 0.2f));
my_objs.push_back(std::make_unique<Box>(Colors::blue, vec2{ 0.0f, 0.5f }, 0.15f, 0.25f));
}
~Example()
{
//delete is unnecessary with shared_ptr
//for (const auto & obj : my_objs)
// delete obj;
}
void update() override
{
for (const auto & obj : my_objs)
obj->draw();
}
};
}