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();
		}
	};
}
โš ๏ธ **GitHub.com Fallback** โš ๏ธ