( ** 인프런에서 들은 강의 '홍정모의 게임 만들기 연습 문제 패키지'를 통해서 공부한 내용과 연습 문제 풀이입니다. **)
// MyExample4
class Person {
public:
float time = 0.0f; // 시간
float dx = 0.0f; // x 거리
float dy = 0.0f; // y 거리
Person(float _dx) : dx(_dx)
{ }
void move() {
// gold face
beginTransformation();
translate(0.0f + dx, 0.12f);
drawFilledCircle(Colors::gold, 0.08f);
translate(0.05f, 0.03f);
drawFilledCircle(Colors::white, 0.01f); // while eye
endTransformation();
// green leg2
beginTransformation();
translate(0.0f + dx, -0.6f);
translate(0.0f, 0.2f);
rotate(-sinf(time + 3.141592f) * 30.0f); // animation!
translate(0.0f , -0.2f);
drawFilledBox(Colors::green, 0.1f, 0.4f);
endTransformation();
// yellow arm2
beginTransformation();
translate(0.0f + dx, 0.0f);
rotate(-sinf(time) * 30.0f); // animation!
scale(1.0f, 2.0f);
translate(0.0f, -0.1f);
drawFilledBox(Colors::yellow, 0.05f, 0.18f);
endTransformation();
// red body
beginTransformation();
scale(1.0f, 2.0f);
translate(0.0f + dx, -0.1f);
drawFilledBox(Colors::red, 0.13f, 0.2f);
endTransformation();
// yellow arm1
beginTransformation();
translate(0.0f + dx, 0.0f);
rotate(sin(time) * 30.0f); // animation!
scale(1.0f, 2.0f);
translate(0.0f, -0.1f);
drawFilledBox(Colors::yellow, 0.05f, 0.18f);
endTransformation();
// green leg1
beginTransformation();
translate(0.0f + dx, -0.6f);
translate(0.0f, 0.2f);
rotate(sinf(time + 3.141592f) * 30.0f); // animation!
translate(0.0f, -0.2f);
drawFilledBox(Colors::green, 0.1f, 0.4f);
endTransformation();
}
void jump() {
// gold face
beginTransformation();
translate(0.0f + dx, 0.12f + dy);
drawFilledCircle(Colors::gold, 0.08f);
translate(0.05f, 0.03f);
drawFilledCircle(Colors::white, 0.01f); // while eye
endTransformation();
// red body
beginTransformation();
translate(0.0f, dy);
scale(1.0f, 2.0f);
translate(0.0f + dx, -0.1f);
drawFilledBox(Colors::red, 0.13f, 0.2f);
endTransformation();
// green leg
beginTransformation();
translate(0.0f + dx, -0.6f + dy);
drawFilledBox(Colors::green, 0.1f, 0.4f);
endTransformation();
// yellow arm
beginTransformation();
translate(0.0f + dx, 0.0f + dy);
scale(2.0f, 1.0f);
translate(0.08f, -0.05f);
drawFilledBox(Colors::yellow, 0.18f, 0.05f);
endTransformation();
// skyblue beam
beginTransformation();
translate(0.0f + dx, 0.0f + dy);
scale(1.0f, 1.0f);
translate(0.4f, -0.05f);
drawFilledBox(Colors::skyblue, 0.05f, 0.05f);
endTransformation();
}
};
class MyExample4 : public Game2D
{
private:
Person * ps[5];
bool up_jumping = false;
bool down_jumping = false;
public:
MyExample4()
{
for (int i = 0; i < 5; i++)
ps[i] = new Person(-1.0f + i * 0.5f);
}
void update() override
{
//make ground
beginTransformation();
translate(0.0f, -1.0f);
drawFilledBox(Colors::green, 5.0f, 0.5f);
endTransformation();
//setting jump
if (isKeyPressedAndReleased(GLFW_KEY_SPACE))
{
if (!up_jumping && !down_jumping)
up_jumping = true;
}
// up jump
if (up_jumping)
{
// up..
if (ps[0]->dy >= 0.0f && ps[0]->dy <= 0.5f)
{
for (int i = 0; i < 5; i++)
{
ps[i]->dy += 0.001f;
ps[i]->jump();
ps[i]->time += this->getTimeStep();
}
if (ps[0]->dy >= 0.4f)
{
up_jumping = false;
down_jumping = true;
}
}
}
// down jump
else if (down_jumping)
{
// down..
if (ps[0]->dy >= 0.0f && ps[0]->dy <= 0.5f)
{
for (int i = 0; i < 5; i++)
{
ps[i]->dy -= 0.001f;
ps[i]->jump();
ps[i]->time += this->getTimeStep();
}
if (ps[0]->dy <= 0.0f)
{
for (int i = 0; i < 5; i++)
{
ps[i]->dy = 0.0f;
}
down_jumping = false;
}
}
}
//move
else
{
for (int i = 0; i < 5; i++) {
ps[i]->move();
ps[i]->time += this->getTimeStep();
}
}
}
};
**** 키워드 : sin과 cos을 이용한 애니메이션 구현, 점프 예외처리 ****
'스터디 > OpenGL' 카테고리의 다른 글
[ OpenGL ] 공 튀기기 시뮬레이션 (0) | 2019.10.31 |
---|---|
[ OpenGL ] 랜덤 함수와 집 객체 (0) | 2019.10.31 |
[ OpenGL ] 탱크 이동 및 발포 구현 ( 메모리 누수 관리와 총알 중첩 생성 ) (0) | 2019.10.31 |
[ OpenGL ] 도형 익히기 (0) | 2019.10.31 |
[ OpenGL ] 미니 태양계 (0) | 2019.10.31 |