Ejemplo n.º 1
0
void Player::update(ActionList _actionList)
{
	if (!canShootBullet && cooldownClock.getElapsedTime().asSeconds() > 0.1) {
		canShootBullet = true;
		cooldownClock.restart();
	}
	bool onGround = false;
	if ((GameScreen::gameMap->getMapPos(((x+13)/32), ((y+16)/32)) == 1 || GameScreen::gameMap->getMapPos(((x-13)/32), ((y+16)/32)) == 1) && (((int)y-5)/32 != ((int)y+5)/32))
		onGround = true;

	
	// Move right
	if (_actionList.checkAction('r')) {
		if (!_actionList.checkAction('~')) {
			if (direction != 1) {
				direction = 1;
				tileChunk.left = 0;
				myTexture.loadFromImage(spriteSheet, tileChunk); 
			}
		}
		if (xVelocity < 6)
			xVelocity++;
	}
	else if (xVelocity > 0 && !(_actionList.checkAction('l') || _actionList.checkAction('r')))
		xVelocity -= xVelocity/3;

	// Move left
	if (_actionList.checkAction('l')) {
		if (!_actionList.checkAction('~')) {
			if (direction != -1) {
				direction = -1;
				tileChunk.left = 32;
				myTexture.loadFromImage(spriteSheet, tileChunk);
			}
		}
		if (xVelocity > -6)
			xVelocity--;
	}
	else if (xVelocity < 0 && !(_actionList.checkAction('l') || _actionList.checkAction('r')))
		xVelocity -= xVelocity/3;

	// Jump
	if (_actionList.checkAction(' ') && onGround && yVelocity >= 0) {
		tileChunk.top = 0;
		myTexture.loadFromImage(spriteSheet, tileChunk);
		if (_actionList.checkAction('d')/* && !_actionList.checkAction('~')*/) {
			yVelocity = 9;
			onGround = false;
		}
		else {
			yVelocity = -8;
			onGround = false;
			tileChunk.top = 32;
			myTexture.loadFromImage(spriteSheet, tileChunk);
		}
	} else if (_actionList.checkAction(' ') && !onGround && yVelocity < -6) {
		gravity = 0.12;
	}

	// Shoot
	if (_actionList.checkAction('~') && canShootBullet) {
		shootBullet = true;
		canShootBullet = false;
		cooldownClock.restart();
		/*if (_actionList.checkAction('d')) {
			if (!onGround)
				y -= 4;
			shootdir = 2;
		} else if (_actionList.checkAction('u')) {
			if (!onGround)
				y += 4;
			shootdir = -2;
		} else {*/
			shootdir = direction;
			if (direction == 1)
				x -= 4;
			if (direction == -1)
				x += 4;
		//}
	}

	// Gravity
	if (!onGround && yVelocity < 9) {
		yVelocity += gravity;
		gravity = 1;
	} else if (onGround && yVelocity > 0){
		yVelocity = 0;
		if (GameScreen::gameMap->getMapPos((x/32), ((y)/32)) == 1 && (int)y % 32 != 0 && (int)y % 32 < 9) {
			y -= ((int)y % 32);
		}
		else if ((int)y % 32 != 0)
			y += (32 - ((int)y % 32));
	} if (GameScreen::gameMap->getMapPos(((x)/32), ((y)/32)) == -1) {
		dead = true;
	}
	if (!hitCheck)
		mySprite.setColor(sf::Color::White);
	if (hitCheck)
		hitCheck = false;

	if (healthPoints <= 0) {
		dead = true;
	}
	x += xVelocity;
	y += yVelocity;
	mySprite.setPosition(x, y);
}