void MovableBox::update() {
    if (mGravityHitTimer <= 0) {
        mGravityDirection=1;
    } else {
        mGravityHitTimer--;
    }
    // walk
    int cur_x = getPosition().x;
    int cur_y = getPosition().y;

    setVelocity(float2(0.0f,40.f*getGravityDirection()));

    //setVelocity(float2(12.0f,.0f));
    moveWithCollision();

    //mFrame ++;
}
void MovingHookTile::update()
{
	Entity::update();
	
	hasHook = (mRoom->getHero()->getHookedEntity() == this);

	if (hasHook) {
		mVelocity.x = (mVelocity.x > 0) ? 40.0f : -40.0f;
		mFrameCounter++;
	} else {
		mVelocity.x = (mVelocity.x > 0) ? 20.0f : -20.0f;
	}

	unsigned int bumps = moveWithCollision();

	if ((bumps & (Direction_Left | Direction_Right)) != 0) {
		mVelocity.x = -mVelocity.x;
	}
}
Esempio n. 3
0
void Hero::update()
{
	if (myIsDead)
		return;

	Entity::update();

	if (mSpawnPoint.x < -100 && mSpawnPoint.y < -100)
	{
		mSpawnPoint = getPosition();
	}

	if (mRopeState == RopeState_Dissapearing) {
		mRopeDissapearCounter++;
		if (mRopeDissapearCounter >= ROPE_DISSAPPEAR_TICKS) {
			mRopeState = RopeState_Retracted;
		}
	}

	float acceleration = mOnGround ? GROUND_ACCELERATION : AIR_ACCELERATION;
	bool airRunning = false;

	if (Input::isHeld(Button_Left)) {
		mVelocity.x -= acceleration;

		if (mRopeState == RopeState_Attached && !mOnGround) {
			mFacingDirection = Direction_Left;
			airRunning = true;
			mMovementState = MovementState_AirRun;
		}
	}

	if (Input::isHeld(Button_Right)) {
		mVelocity.x += acceleration;

		if (mRopeState == RopeState_Attached && !mOnGround) {
			mFacingDirection = Direction_Right;
			airRunning = true;
			mMovementState = MovementState_AirRun;
		}
	}

	if (Input::isPressed(Button_Jump)) {
		if (!mJumpPrepressed && mOnGround)
			Sound::playSample("data/sounds/jump.wav");
		mJumpPrepressed = true;
	}

	if (mOnGround && mJumpPrepressed) {
		mVelocity.y = -JUMP_VELOCITY;
		if (mRopeState != RopeState_Attached) {
			mJumpHeld = true;
		}
		mJumpPrepressed = false;
	}

	if (Input::isReleased(Button_Jump)) {
		if (mJumpHeld && mVelocity.y < 0) {
			mVelocity.y *= 0.5f;
		}

		mJumpHeld = false;
		mJumpPrepressed = false;
	}

	if (Input::isReleased(Button_Fire)) {
		detachHook();
	}

	if (mVelocity.y >= 0) {
		mJumpHeld = false;
	}

	if (!airRunning) {
		mMovementState = MovementState_Still;
	}

	if (mOnGround) {
		if (mVelocity.x > 0) {
			mFacingDirection = Direction_Right;
		} else if (mVelocity.x < 0) {
			mFacingDirection = Direction_Left;
		}

		if (abs(mVelocity.x) > GROUND_STOP_VELOCITY)
		{
			mMovementState = MovementState_Run;
		}
	}
	else if (!airRunning)
	{
		if (mVelocity.y > 0)
			mMovementState = MovementState_Jump;
		else
			mMovementState = MovementState_Fall;
	}

	if (mMovementState == MovementState_Still)
	{
		mVelocity.x = 0;
	}

	if (Input::isPressed(Button_Fire)) {
		Sound::playSample("data/sounds/rope.wav");
		mRopeState = RopeState_Moving;
		mRopePosition = mPosition;
		mRopeVelocity = float2::ZERO;

		if (Input::isHeld(Button_Left)) {
			mRopeVelocity.x -= 1;
		}

		if (Input::isHeld(Button_Right)) {
			mRopeVelocity.x += 1;
		}

		if (Input::isHeld(Button_Up)) {
			mRopeVelocity.y -= 1;
		}

		if (Input::isHeld(Button_Down)) {
			mRopeVelocity.y += 1;
		}

		if (floatIsZero(mRopeVelocity)) {
			mRopeVelocity.x = (mFacingDirection == Direction_Left ? -1 : 1);
		}

		mRopeVelocity = adjustRopeDirection(normalize(mVelocity + normalize(mRopeVelocity) * ROPE_SPEED)) * ROPE_SPEED;

		if (mRopeVelocity.x < 0) {
			mFacingDirection = Direction_Left;
		} else if (mRopeVelocity.x > 0) {
			mFacingDirection = Direction_Right;
		}
	}

	if (mRopeState == RopeState_Moving) {
		const int substeps = 25;
		for (int i = 0; i < substeps; i++) {
			mRopePosition += mRopeVelocity / (substeps * Time::TicksPerSecond);
			int tileX = (int)(mRopePosition.x / mRoom->getTileWidth());
			int tileY = (int)(mRopePosition.y / mRoom->getTileHeight());
			if (mRoom->isHookable(tileX, tileY)) {
				//Sound::stopSample("data/sounds/rope.wav");
				Sound::playSample("data/sounds/hook.wav");
				mRopeState = RopeState_Attached;
				mJumpHeld = false;

				ParticleSystem* particleSystem = new ParticleSystem(
					mAnimationHookParticle,
					2,
					30,
					10,
					1,
					50,
					10,
					-normalize(mRopeVelocity)*10,
					1.0);

				particleSystem->setPosition(mRopePosition);

				mRoom->addEntity(particleSystem);

				break;
			}

			if (length(mRopePosition-mPosition) > mRopeMaxLenghth)
			{
				detachHook();
				Sound::playSample("data/sounds/no_hook.wav");
				break;
			}

			if (mRoom->isCollidable(tileX, tileY)) {
				detachHook();
				Sound::playSample("data/sounds/no_hook.wav");
				break;
			}

			if (mRoom->damageDone((int)(mRopePosition.x), (int)(mRopePosition.y))) {
				detachHook();
				break;
			}

			mHookedEntity = mRoom->findHookableEntity(mRopePosition);
			if (mHookedEntity != 0) {
				Sound::playSample("data/sounds/hook.wav");
				mRopeState = RopeState_Attached;
				mJumpHeld = false;
				mHookedEntityOffset = mRopePosition - mHookedEntity->getPosition();
				mHookedEntityOffset.x = floor(mHookedEntityOffset.x);
				mHookedEntityOffset.y = floor(mHookedEntityOffset.y);
			}
		}
	}

	if (mRopeState == RopeState_Attached) {
		if (mHookedEntity != 0) {
			mRopePosition = mHookedEntity->getPosition() + mHookedEntityOffset;
		}

		float2 ropeToHero = mPosition - mRopePosition;
		if (lengthCompare(ropeToHero, ROPE_REST_LENGTH) > 0) {
			float2 ropeRestPoint = mRopePosition + normalize(ropeToHero) * ROPE_REST_LENGTH;
			float2 heroToRestPoint = ropeRestPoint - mPosition;
			float2 ropeAcceleration = heroToRestPoint * ROPE_SPRING_CONSTANT;
			if (lengthCompare(ropeAcceleration, ROPE_MAX_ACCELERATION) > 0) {
				ropeAcceleration = normalize(ropeAcceleration) * ROPE_MAX_ACCELERATION;
			}
			mVelocity += ropeAcceleration;
		}

		if (Input::isHeld(Button_Up)) {
			mVelocity.y -= acceleration;
		}

		if (Input::isHeld(Button_Down)) {
			mVelocity.y += acceleration;
		}

		int ropeTileX = (int)(mRopePosition.x / mRoom->getTileWidth());
		int ropeTileY = (int)(mRopePosition.y / mRoom->getTileWidth());
		if (mHookedEntity == 0 && !mRoom->isHookable(ropeTileX, ropeTileY)) {
			detachHook();
		}
	}

	unsigned int bumps = moveWithCollision();
	
	if ((bumps & (Direction_Left | Direction_Right)) != 0) {
		mVelocity.x = 0;
	}

	if ((bumps & (Direction_Up | Direction_Down)) != 0) {
		mVelocity.y = 0;
	}

	float gravity = mJumpHeld ? JUMP_GRAVITY : GRAVITY;
	mVelocity.y += gravity;
	bool ground = ((bumps & Direction_Down) != 0);
	if (ground && !mOnGround && mRopeState != RopeState_Attached)
	{
		Sound::playSample("data/sounds/land.wav");
	}
	mOnGround = ground;

	float drag = mOnGround ? GROUND_DRAG : AIR_DRAG;
	mVelocity *= drag;

	mFrame ++;
	
	if (mBlinkingTicksLeft)
	{
		mBlinkingTicksLeft --;
	}
}
void SimpleWalkingMonster::update()
{
	switch(mState)
	{
	case State_Walking:
		{
			mVelocity.y += 6.0f;
			mVelocity.x = 20.0f * ((mFacing==Facing_Left)?-1:1);

			setVelocity(mVelocity);
			int bumps = moveWithCollision();

			if ((bumps & (Direction_Up | Direction_Down)) != 0) 
			{
				mVelocity.y = 0;
			}

			if ((bumps & (Direction_Left)) != 0)
			{
				mFacing = Facing_Right;
			}

			if ((bumps & (Direction_Right)) != 0)
			{
				mFacing = Facing_Left;
			}


			int offsetX = (mState==Facing_Right)?-getHalfSize().x-2:getHalfSize().x+2;
			int offsetY = getHalfSize().y+2;
		
			float2 position = getPosition();

			int x = (position.x+offsetX) / (float)mRoom->getTileWidth();
			int y = (position.y+offsetY) / (float)mRoom->getTileHeight();

			if (!mRoom->isCollidable(x, y))
			{
				if (mFacing == Facing_Left)
				{
					mFacing = Facing_Right;
				}
				else
				{
					mFacing = Facing_Left;
				}
			}

			if (mRand.getFloat(1.0f) < WALK_TO_IDLE_CHANCE)
			{
				mState = State_Idling;
			}
		}

		break;
	case State_Idling:

		if (mRand.getFloat(1.0f) < IDLE_TO_WALK_CHANCE)
		{
			if (mRand.getFloat(1.0f) > 0.5)
			{
				mFacing = Facing_Left;
			}
			else
			{
				mFacing = Facing_Right;
			}
			mState = State_Walking;
		}

		break;
	}

	Hero* hero = mRoom->getHero();
	if (Collides(hero->getCollisionRect(), getCollisionRect()))
	{
		hero->kill();
	}

	mFrame++;
}
Esempio n. 5
0
unsigned int Entity::moveWithCollision()
{
	return moveWithCollision(mVelocity / Time::TicksPerSecond);
}
void Shot::update(){
	if (++mLife > 180){
		setCollided(true);
	}
	if (touchesDangerousTile()){
		setCollided(true);
	}
	if(hasCollided()){
		if (--mCollidedTimer < 0){
			remove();
			//remove
		}
		return;
	}
	// move
	int cur_x = getPosition().x;
	int cur_y = getPosition().y;
	int floor_y, edge_x;
	int bumps;
	/*
	floor_y= cur_y+getHalfSize().y+5;
	if (mFacingDirection == Direction_Left){
		edge_x=cur_x-getHalfSize().x;
	} else {
		edge_x=cur_x+getHalfSize().x;
	}
	*/
	if(mGravity[mShotType] == ShotType_Bullet){
		setVelocity(float2(mInitialVelocity[mShotType].x*getDirection(),mInitialVelocity[mShotType].y));
	} else {
		setVelocity(float2(mInitialVelocity[mShotType].x*getDirection(),mInitialVelocity[mShotType].y+GRAVITY*mLife));
	}
	//setVelocity(float2(12.0f,.0f));
	bumps=moveWithCollision();

	if((bumps & Direction_Left) != 0 || (bumps & Direction_Right) != 0 || (bumps & Direction_Up) != 0|| (bumps & Direction_Down) != 0 ){
		setCollided(true);
		if (mShotType == ShotType_GravityGrenade || mShotType == ShotType_Fireball) { Sound::playSample("data/sounds/grenadeexplode.wav"); }
	}

	if(!mHurtPlayer){
		std::vector<Entity*> collidedEntity = mRoom->checkEntitiesForCollision(this);
		if(collidedEntity.size() > 0){
			setCollided(true);
			if (mShotType == ShotType_GravityGrenade || mShotType == ShotType_Fireball) { Sound::playSample("data/sounds/grenadeexplode.wav"); }
			for(int i=0;i<collidedEntity.size();i++){
				attack(collidedEntity[i]);
			}
		}
	} else {
		Hero* h = mRoom->getHero();
		Entity::CollisionRect cr= h->getCollisionRect();
		if(Collides(getCollisionRect(),cr)){
			setCollided(true);
			if (mShotType == ShotType_GravityGrenade || mShotType == ShotType_Fireball) { Sound::playSample("data/sounds/grenadeexplode.wav"); }
			if(mShotType == ShotType_Fireball || mShotType == ShotType_Bullet){
				h->die();
				return;
			}
			if(mShotType == ShotType_GravityGrenade){
				attack(h);
				return;
			}
		}
	}

	mFrame++;
	

}
void Hero::update()
{
	if (mySpawnPortal)
	{
		mySpawnPortalFrameCounter++;
		if (mySpawnPortalFrameCounter == 1)
		{
			Portal* portal = new Portal();
			portal->setPosition(getPosition());
			getRoom()->addEntity(portal);
			return;
		}
		else if (mySpawnPortalFrameCounter >= 60 * 5)
		{
			mySpawnPortal = false;
		}
		else 
		{
			return;
		}
	}

	if (myIsDead)
		return;

	Entity::update();
	mFramesSinceLandPlayed++;

	if (mGravityHitTimer <= 0){
		mGravityDirection=1;
	} else {
		mGravityHitTimer--;
	}

	if (touchesDangerousTile())
	{
		die();
	}
	//checkEnemies();

	if (mSpawnPoint.x < -100 && mSpawnPoint.y < -100)
	{
		mSpawnPoint = getPosition();
	}

	float acceleration = mOnGround ? GROUND_ACCELERATION : AIR_ACCELERATION;
	
	if (Input::isHeld(Button_Left)) {
		mVelocity.x -= acceleration;
		mFacingDirection = Direction_Left;
	}

	if (Input::isHeld(Button_Right)) {
		mVelocity.x += acceleration;
		mFacingDirection = Direction_Right;
	}

	if(Input::isPressed(Button_Fire) && GameState::getInt(GameState::POWERUP_GRENADE) != 0){
		mWeaponSelected = Weapon_Gravity_Grenade;
		fireWeapon();
	}

	if(Input::isPressed(Button_ToggleWeapon) && GameState::getInt(GameState::POWERUP_GUN) != 0){
		mWeaponSelected = Weapon_Gun;
		fireWeapon();
	}

	mVelocity.x = clamp(mVelocity.x, -MAX_X_VELOCITY, MAX_X_VELOCITY);
	
	if (Input::isPressed(Button_Jump)) {
		mJumpPrepressed = true;
	}

	if (mOnGround && mJumpPrepressed) {
		Sound::playSample("data/sounds/jump.wav");
		mVelocity.y = GameState::getInt(GameState::POWERUP_HIJUMP) ? -HI_JUMP_VELOCITY : -JUMP_VELOCITY;
		mJumpHeld = true;
		mJumpPrepressed = false;
	}

	if (Input::isReleased(Button_Jump)) {
		if (mJumpHeld && mVelocity.y < 0) {
			mVelocity.y *= 0.5f;
		}

		mJumpHeld = false;
		mJumpPrepressed = false;
	}

	if (mVelocity.y >= 0) {
		mJumpHeld = false;
	}

	mMovementState = MovementState_Still;
	
	if (mOnGround) {
		if (abs(mVelocity.x) > GROUND_STOP_VELOCITY)
		{
			mMovementState = MovementState_Run;
		}
	}
	else
	{
		if (mVelocity.y < 0) {
			mMovementState = MovementState_Jump;
		} else {
			mMovementState = MovementState_Fall;
		}
	}
	
	if (mMovementState == MovementState_Still)
	{
		mVelocity.x = 0;
	}

	bool ground = moveOutOfSolidEntities();

	float2 usedVelocity(mVelocity.x, clamp(mVelocity.y, -MAX_Y_VELOCITY, MAX_Y_VELOCITY));
	unsigned int bumps = moveWithCollision(usedVelocity / Time::TicksPerSecond);
	
	if ((bumps & (Direction_Left | Direction_Right)) != 0) {
		mVelocity.x = 0;
	}

	if ((bumps & (Direction_Up | Direction_Down)) != 0) {
		mVelocity.y = 0;
	}

	float gravity = mJumpHeld ? JUMP_GRAVITY : GRAVITY;
	mVelocity.y += gravity * getGravityDirection();
	ground |= ((bumps & Direction_Down) != 0);
	if (ground && !mOnGround)
	{
		if (mFramesSinceLandPlayed > 6)
		{
			Sound::playSample("data/sounds/land.wav");
		}
		mFramesSinceLandPlayed = 0;
	}
	mOnGround = ground;

	float drag = mOnGround ? GROUND_DRAG : AIR_DRAG;
	mVelocity *= drag;

	mFrame ++;
	if (mBlinkingTicksLeft)
	{
		mBlinkingTicksLeft --;
	}
}