コード例 #1
0
void Game::CheckCollision() {

	// TODO all X and Y coords to absolute (level) values as opposed to screen values refactor this
	std::map<std::pair<int, int>, Entity*>* entities = level->getEntities();
	for (std::map<std::pair<int, int>, Entity*>::iterator i = entities->begin(); i != entities->end(); i++) {
		Entity* entity = i->second;
		Platform* p = dynamic_cast<Platform*>(entity);

		int levelBlockX = i->first.second;
		int levelBlockY = i->first.first;
		
		if (p != NULL && p->getState() == dislodged) {
			p->setYOffset(p->getYOffset() + 5); //TODO needs to be a variable

			int currentLevelBlockX = levelBlockX;
			int currentLevelBlockY = levelBlockY + (p->getYOffset() + 50) / Level::BLOCK_HEIGHT_PX;

			if (level->getLevelCodeByBlock(currentLevelBlockX, currentLevelBlockY) == '_') {
				p->setState(broken);
				level->setLevelCodeByBlock(currentLevelBlockX,currentLevelBlockY, '$');
				level->setLevelCodeByBlock(levelBlockX, levelBlockY, ' ');
			}
			else {
				p->setY(int(p->getY() + 5) % (Level::BLOCK_HEIGHT_PX * Level::SCENE_HEIGHT_BLK));
				//p->setY((levelBlockY - level->getSceneY()) * Level::BLOCK_HEIGHT_PX + p->getYOffset() - p->getAnim()->getSheet()->getFrameHeight());

				if (!level->inScene(levelBlockX, levelBlockY) && level->inScene(currentLevelBlockX, currentLevelBlockY)) {
					p->Animate(&graphics);
				}
			}
		}	
	}


	collision->CheckPrinceCollision(prince);
	CheckCombatCollision();
}
コード例 #2
0
void Game::DrawBackground() {
	int yOff = 0;
	int xOff = 0;

	//TODO this value is being reused in check collision move it in a common location or eliminate collision detection when drawing completely
	int princeXOffset = 22;

	for (int y = Level::SCENE_HEIGHT_BLK; y >= 0; y--) {
		for (int x = 0; x <= Level::SCENE_WIDTH_BLK; x++) {

			yOff = Level::BLOCK_HEIGHT_PX * y;
			xOff = -Level::BLOCK_WIDTH_PX + Level::BLOCK_WIDTH_PX * x;

			switch (level->getSceneCodeByBlock(y, x)) {
			case '|':
				graphics.DrawSprite(xOff, yOff - getSprite("tileCornerLeft")->height, getSprite("tileCornerLeft"));
				break;
			case 'T':
				graphics.DrawSprite(xOff, yOff - getSprite("tileCornerLeft")->height, getSprite("tileCornerLeft"));
				graphics.DrawSprite(xOff - 1, yOff - getSprite("columnBack")->height - 15, getSprite("columnBack"));
				break;
			case ']':
				graphics.DrawSprite(xOff, yOff - getSprite("blockCornerRight")->height, getSprite("blockCornerRight"));
				break;
			case '_':
				graphics.DrawSprite(xOff, yOff - getSprite("tileCornerLeft")->height, getSprite("tileCornerLeft"));
				break;
			case '1':
				graphics.DrawSprite(xOff, yOff - getSprite("tileCornerLeft")->height, getSprite("tileCornerLeft"));
				break;
			case '~': {
				std::map<std::pair<int, int>, Entity*>* entities = level->getEntities();
				int blockX = level->getLevelBlockX(x);
				int blockY = level->getLevelBlockY(y);
				std::pair<int, int> platformKey = std::make_pair(blockY, blockX);
				Entity* e = entities->at(platformKey);
				Platform* p = dynamic_cast<Platform*>(e);
				if (p != NULL) {
					int currentLevelBlockX = blockX;
					int currentLevelBlockY = blockY + (p->getYOffset() + 120) / Level::BLOCK_HEIGHT_PX;

					if (level->inScene(currentLevelBlockX, currentLevelBlockY)) {
						p->Animate(&graphics);
					}
				}
				if (level->getSceneBlockXByCoord(prince->getMidX() - princeXOffset) == x &&
					level->getSceneBlockYByCoord(prince->getMidY()) == y && prince->getState() != sJumping) {
					p->Drop();
				}
			} break;

			case 'H':
				graphics.DrawSprite(xOff, yOff - getSprite("tileCornerLeft")->height, getSprite("tileCornerLeft"));
				break;
			case '-':
				if (level->getSceneCodeByCoord(prince->getMidX() - princeXOffset, prince->getMidY()) == '-') {
					if (level->getSceneBlockXByCoord(prince->getMidX() - princeXOffset) == x &&
						level->getSceneBlockYByCoord(prince->getMidY() == y) &&
						prince->getState() != sJumping) {
						graphics.DrawSprite(xOff, yOff - getSprite("tileCornerLeft")->height, getSprite("tileCornerLeft"));
					}
					else {
						graphics.DrawSprite(xOff, yOff - getSprite("activate")->height, getSprite("activate"));
					}
				}
				else {
					graphics.DrawSprite(xOff, yOff - getSprite("activate")->height, getSprite("activate"));
				}
				break;
			case '=':
				if (level->getSceneCodeByCoord(prince->getMidX() - princeXOffset, prince->getMidY()) == '=') {
					if (level->getSceneBlockXByCoord(prince->getMidX() - princeXOffset) == x &&
						level->getSceneBlockYByCoord(prince->getMidY() == y) &&
						prince->getState() != sJumping
						) {
						graphics.DrawSprite(xOff, yOff + 2 - getSprite("trap")->height, getSprite("trap"));
					}
					else {
						graphics.DrawSprite(xOff, yOff - getSprite("trap")->height, getSprite("trap"));
					}
				}
				else {
					graphics.DrawSprite(xOff, yOff - getSprite("trap")->height, getSprite("trap"));
				}
				break;
			case '*':
				graphics.DrawSprite(xOff, yOff - getSprite("bricks")->height, getSprite("bricks"));
				break;
			case '/':
				graphics.DrawSprite(xOff, yOff - getSprite("holyFloor")->height, getSprite("holyFloor"));
				break;
			case 'S':
				graphics.DrawSprite(xOff, yOff - getSprite("deadSk")->height, getSprite("deadSk"));
				break;
			case 'G':
				graphics.DrawSprite(xOff, yOff - getSprite("doorFrameBack")->height, getSprite("doorFrameBack"));
				break;
			case '#':
				graphics.DrawSprite(xOff, yOff - getSprite("tileCornerLeft")->height, getSprite("tileCornerLeft"));
				graphics.DrawSprite(xOff, yOff - getSprite("bricks")->height, getSprite("bricks"));
				break;
			case 'P':
				graphics.DrawSprite(xOff, yOff - getSprite("tileCornerLeft")->height, getSprite("tileCornerLeft"));
				break;
			case 'K':
				graphics.DrawSprite(xOff, yOff - getSprite("tileCornerLeft")->height, getSprite("tileCornerLeft"));
				break;
			case 'E':
				graphics.DrawSprite(xOff, yOff - getSprite("tileCornerLeft")->height, getSprite("tileCornerLeft"));
				break;
			case '$':
				graphics.DrawSprite(xOff, yOff - getSprite("tileCornerLeft")->height, getSprite("tileCornerLeft"));
				graphics.DrawSprite(xOff, yOff - getSprite("rubbleBack")->height, getSprite("rubbleBack"));
				break;
			case '^':
				graphics.DrawSprite(xOff, yOff - getSprite("tileCornerLeft")->height, getSprite("tileCornerLeft"));
				break;
			case '!':
				graphics.DrawSprite(xOff, yOff - getSprite("tileCornerLeft")->height, getSprite("tileCornerLeft"));
				break;
			case ' ':
				break;

			}
		}
	}
}