Exemplo n.º 1
0
void Player::update(float frameTime, LevelController* lc) {
	// 1-Press NoClip
	if (noClipButtonReleased && input->isKeyDown(VK_F2)) {
		noClip = !noClip;
		noClipButtonReleased = false;
	} else {
		noClipButtonReleased = true;
	}
	velocityX = getVelocity().x;
	velocityY = getVelocity().y;
	// Handle NoClip
	if (noClip) {
		if (input->isKeyDown(PLAYER_RIGHT)) {
			velocityX = playerNS::NOCLIP_SPEED * frameTime;
			orientation = Right;
		} else if (input->isKeyDown(PLAYER_LEFT)) {
			velocityX = -playerNS::NOCLIP_SPEED * frameTime;
			orientation = Left;
		} else {
			velocityX = 0;
		}
		if (input->isKeyDown(PLAYER_UP)) {
			velocityY = -playerNS::NOCLIP_SPEED * frameTime;
			orientation = Up;
		} else if (input->isKeyDown(PLAYER_DOWN)) {
			velocityY = playerNS::NOCLIP_SPEED * frameTime;
			orientation = Down;
		} else {
			velocityY = 0;
		}
		velocity = VECTOR2(velocityX, velocityY);
		frameDelay = 1000000;
		Item* activeItem = inventory->getActiveItem()->getItem();
		if (inventory->getActiveItem()->getItem()->getItemType() == Item::Equipable) {
			Gun* gun = dynamic_cast<Gun*>(activeItem);
			if (gun != 0) {
				gun->update(frameTime, orientation, getX(), getY(), input, lc);
			}
		}
		Entity::update(frameTime);
		return;
	}
	// Set Obj Vars
	levelController = lc;
	// Debug Messages
	OSD::instance()->addLine("Jump Distance: " + std::to_string(jumpdistance) + " / " + std::to_string(playerNS::JUMP_HEIGHT));
	OSD::instance()->addLine("Can | Left: " + std::to_string(canMoveLeft(true)) + " | Right: " + std::to_string(canMoveRight(true)) + " | Up: " + std::to_string(canMoveUp(true)) + " | Down: " + std::to_string(canMoveDown(true)));
	// Stuck Hotfix
	if (!canMoveLeft() && !canMoveRight() && !canMoveUp() && !canMoveDown()) {
		setX(spawnPos.x);
		setY(spawnPos.y);
	}
	// Update Guns
	inventory->update(frameTime, input);
	// Boss Audio
	if (lc->getMapX() * -1.0 > 1900) {
		audio->stopCue(BK_MUSIC);
		audio->playCue(BOSS_MUSIC);
	}

	// Start of Player Movement
	if (healthStatus != Dead) {
		if (!canMoveDown()) {
			if (!input->isKeyDown(PLAYER_UP) && !input->isKeyDown(PLAYER_JUMP))
				canJump = true;
			canFall = false;
			falling = false;
		} else {
			canFall = true;
			falling = true;
		}
		// Move Left and Right
		if (input->isKeyDown(PLAYER_RIGHT) && canMoveRight()) {
			velocityX = playerNS::SPEED * frameTime;
			while (!canMoveRight()) {
				spriteData.x -= 0.1;
				velocityX = 0;
			}
			orientation = Right;
		} else if (input->isKeyDown(PLAYER_LEFT) && canMoveLeft()) {
			velocityX = -playerNS::SPEED * frameTime;
			while (!canMoveLeft()) {
				spriteData.x += 0.1;
				velocityX = 0;
			}
			orientation = Left;
		} else {
			velocityX = 0;
			if (input->isKeyDown(PLAYER_UP)) {
				orientation = Up;
			}

			if (input->isKeyDown(PLAYER_DOWN)) {
				orientation = Down;
			}
		}
		// Handle Jumping
		if (jumping || (((input->isKeyDown(PLAYER_JUMP) || input->isKeyDown(PLAYER_UP)) && canMoveUp() && canJump))) {
			jumpdistance = jumpOriginY - getY();
			if (canJump && !jumping)
				jumpOriginY = getY();
			if (jumpdistance > playerNS::JUMP_HEIGHT || !canMoveUp()) {
				jumping = false;
				canJump = false;
				falling = true;
			} else {
				if (!jumping)
					velocityY = -playerNS::JUMP_SPEED * frameTime;
				else
					velocityY += 0.5 * frameTime;
				jumping = true;
				canJump = false;
			}
		}
		if (!jumping)
			jumpOriginY = getY();
		if (falling && !jumping) {
			if (canMoveDown()) {
				velocityY = playerNS::FALLING_SPEED * frameTime;
			} else {
				velocityY = 0;
			}
		}
		// Handle Stuck
		while (canMoveUp() && !canMoveDown() && !canMoveLeft() && !canMoveRight()) {
			spriteData.y -= 0.1;
		}
		while (!canMoveUp() && !canMoveDown() && !canMoveLeft() && canMoveRight()) {
			spriteData.x += 0.1;
		}
		while (!canMoveUp() && !canMoveDown() && canMoveLeft() && !canMoveRight()) {
			spriteData.x -= 0.1;
		}
		// Final Sanity Check
		if (!canMoveLeft() && velocityX < 0 || !canMoveRight() && velocityX > 0)
			velocityX = 0;
		if (!canMoveUp() && velocityY < 0 || !canMoveDown() && velocityY > 0)
			velocityY = 0;
		setVelocity(VECTOR2(velocityX, velocityY));

		// Handle Orientations
		if (input->isKeyDown(PLAYER_UP))
			orientation = Up;
		else if (input->isKeyDown(PLAYER_DOWN))
			orientation = Down;
		switch (orientation) {
		case Right:
			currentFrame = 953;
			spriteData.flipHorizontal = true;
			break;
		case Down:
			currentFrame = 954;
			break;
		case Left:
			currentFrame = 953;
			spriteData.flipHorizontal = false;
			break;
		case Up:
			currentFrame = 952;
			break;
		}

		// Draw Items
		Item* activeItem = inventory->getActiveItem()->getItem();
		if (inventory->getActiveItem()->getItem()->getItemType() == Item::Equipable) {
			Gun* gun = dynamic_cast<Gun*>(activeItem);
			if (gun != 0) {
				gun->update(frameTime, orientation, getX(), getY(), input, lc);
			}
		}
		// Crate Collision
		if (lc->collidedWithCrate() == 1 && lc->getCrateItem() != -1) {
			audio->playCue(RELOAD);
			if (lc->collidedWithCrate() == 1 && lc->getCrateItem() != -1) {
				int itemid = lc->getCrateItem();
				InventoryItem *invItem;
				std::vector<InventoryItem*>* itemList = inventory->getItems();
				switch (itemid) {
				case playerNS::ItemType::shotGun:
					shotgun = new Shotgun();
					shotgun->initialize(gameptr, 136, 41, 2, gunTexture);
					shotgun->setCurrentFrame(6);
					invItem = new InventoryItem(shotgun);
					break;
				case playerNS::ItemType::machineGun:
					machineGun = new MachineGun();
					machineGun->initialize(gameptr, 136, 41, 2, gunTexture);
					machineGun->setCurrentFrame(0);
					invItem = new InventoryItem(machineGun);
					break;
				case 3:
					break;
				}
				for (int i = 0; i < itemList->size(); i++) {
					InventoryItem *iItem = itemList->at(i);
					Item* item = iItem->getItem();
					Item* newItem = invItem->getItem();
					if (item->getItemType() == Item::Equipable && newItem->getItemType() == Item::Equipable) {
						Gun* gunInvItem = dynamic_cast<Gun*>(item);
						Gun* gunNewItem = dynamic_cast<Gun*>(newItem);
						if (gunInvItem->getGunId() == gunNewItem->getGunId()) {
							gunInvItem->addAmmo();
							lc->setCrateCollided(0);
							return; // Should this be return or break?
						}
					} else if (item->getItemType() == Item::Usable && newItem->getItemType() == Item::Usable) {
						lc->setCrateCollided(0);
						return;
					} // Should this be return or break?
				}
				inventory->addItem(invItem);
				lc->setCrateCollided(0);
				lc->setCrateItem(-1);
			}
		}
		Entity::update(frameTime);
	}
}
Exemplo n.º 2
0
int main()
{
	init();

	resetTicks();

	bool end = false;
	list<Thing*>::iterator it;

	Gun *gun = new Gun();
	Scoreboard *sb = new Scoreboard();

	level = 4.0;
	int prev_button = 0;

	while(!end)
	{
		if (ticks > 0)
		{
			while(ticks-- > 0)
			{
				if (randomFloat(0, 48 - level) < 1.0)
				{
					if (randomInt(0, 15) == 0)
						world.push_back(new Bonus(randomInt(20, 620), level * randomFloat(0.99, 1.05)));
					else
						world.push_back(new Baddie(randomInt(20, 620), level * randomFloat(0.99, 1.05)));
				}

				for(it = world.begin(); it != world.end(); it++)
					(*it)->update();

				for(it = world.begin(); it != world.end();)
				{
					if (!(*it)->isAlive())
					{
						delete (*it);
						it = world.erase(it);
					}
					else
					{
						it++;
					}
				}
				gun->update();

				if (key[KEY_ESC])
				{
					end = true;
				}
				end |= (hp <= 0);

				level += 0.0012;

				if ((mouse_b == 1) && (mouse_b != prev_button) && (ammo > 0))
				{
					ammo--;
					for(it = world.begin(); it != world.end(); it++)
						(*it)->shootAt(mouse_x, mouse_y);
				}
				if ((mouse_b == 2) && (mouse_b != prev_button))
				{
					ammo = 6;
					score -= 250;
				}

				prev_button = mouse_b;
			}
		}

		clear_bitmap(buffer);
		for(it = world.begin(); it != world.end(); it++)
			(*it)->draw();
		gun->draw();
		sb->draw();

		blit(buffer, screen, 0, 0, 0, 0, 640, 480);
	}

	clear_bitmap(screen);
	textprintf_centre_ex(screen, font, 320, 220, makecol(255, 0, 0), -1, "G A M E    O V E R");
	textprintf_centre_ex(screen, font, 320, 240, makecol(255, 255, 255), -1, "Final score: %d", score);
	textprintf_centre_ex(screen, font, 320, 260, makecol(255, 255, 0), -1, "Press both mouse buttons to exit");

	while (mouse_b);
	while (mouse_b != 3);

	destroy();
}