[객체지향프로그래밍(2)]Finals Test Report : HelicopterGame만들기(2)
by 브이담곰더보기
12/23 할일
✅ RigidBody 컴포넌트 완성
✅ Collider 컴포넌트 완성 -> 충돌 처리 완료
✅ Timer 구현하기
✅ GameOver 구현
✔ Camera 컴포넌트 만들기
✔ 맵 이어붙이는 함수 만들기
📌 RigidBody 컴포넌트 만들기.
1. 먼저, Position 클래스의 == 검사를 위해 헤더 연산자 오버로딩 함수를 추가하였다.
bool operator==(const Position& other) { if (this->x == other.x && this->y == other.y) return true; return false; }
2. RigidBody 가장자리 검사 알고리즘
- 아이디어 노트

- Code
void init() //스프라이트 가장자리 리지드바디 라인 생성. { for (int i = 0; i < dim.y; i++) { int rightcount = 0;//한 행당 왼,오 2개만 push_back해야함. int leftcount = 0;//한 행당 왼,오 2개만 push_back해야함. for (int j = 0; j < dim.x; j++) { if(i == 0 && sprite[i+j] != ' ') upedges.push_back({ Direction::up,renderer->offset2Pos(i) }); if(i == dim.y-1 && sprite[i+j] != ' ') upedges.push_back({ Direction::up,renderer->offset2Pos(i) }); if (rightcount ==1 && leftcount ==1) continue; //left edge if (sprite[dim.x * i + j] != ' ' && leftcount == 0) { leftedges.push_back({ Direction::left,renderer->offset2Pos(dim.x * i + j) }); leftcount++; } //right edge if (sprite[dim.x * i + dim.x - j - 1] != ' ' && rightcount == 0) { rightedges.push_back({ Direction::right,renderer->offset2Pos(dim.x * i + dim.x - j - 1) }); rightcount++; } } }
좌,우는 의도대로 잘 나왔다.

그런데. 위,아래는 좀 뭔가 이상해서 다시 코드를 손봐야 할듯;;
✨ 약간의 오타와, 로직문제가 있어서 수정했다.


체크한 부분이 잘 벡터에 들어갔다. 이제 콜라이더에서 정보를 받아서 충돌처리를 해보자!
📌 Collider 컴포넌트 만들기.
bool OnCollisionEnter(Direction dir)//방향을 매개변수로 가져온다. { if (rigidbody == nullptr) return false; //component가 없을 경우 체크할 수 없음. return false; Position nextPos = gameObject->getTransform()->getPosition() + dir; //다음 게임오브젝트의 위치를 가져온다. vector<edgePosition> edges = rigidbody->getEdges(dir); //이동 방향에 해당되는 리지드바디 가장자리 위치정보를 벡터로 가져온다. //search for (int i = 0; i < edges.size(); i++) { int offset = renderer->pos2Offset(nextPos + edges[i].pos); Borland::gotoxy(10, 39); printf("Collide!! : %c\n", renderer->getShape()[offset]); if (renderer->getShape()[offset] != ' ') return true; } return false; }
rigidbody의 정보를 이용해서, Pos + directin의 위치에 블록이 있으면 움직일 수 없도록 함.
📌 Timer만들기
#include <time.h> 를 이용하여 만들었다.

class boardScript : public Behaviour { clock_t start, current; int duration; Button* button; public: boardScript(GameObject* gameObject) : Behaviour(gameObject) { button = gameObject->getComponent<Button>(); start = clock(); } void update() override { current = clock(); duration = (double)(current - start) / CLOCKS_PER_SEC; string timetxt = "Time:" + to_string(duration) + "s"; button->setText(timetxt); } };
📌 GameOver 되도록 만들기.
만약, 게임오버가 된다면, 위에 만든 Timer의 업데이트가 중지되고, GameOver가 뜨게 끔 구현하였다.

블로그의 정보
농담곰담곰이의곰담농
브이담곰