void Mogh::collision(gen::Sprite* other, gen::World* w){ if(Troll* ch = dynamic_cast<Troll*>(other)){ damaged = true; setCurrentAnimationVector("DAMAGED"); setHealth(getHealth() - 10); setTimer( SDL_GetTicks() + 2000 ); setCollidable(false); setVelocityX(getVelocityX()*2); setVelocityY(getVelocityY()*2); } else if(gen::Object* o = dynamic_cast<gen::Object*>(other)){ std::string direction = getKeyOfCurrentAnimationVector(); if(direction == "LEFT"){ setMovementX(getVelocityX()); moveX(w); } else if(direction == "RIGHT"){ setMovementX(-getVelocityX()); moveX(w); } else if(direction == "BACK"){ setMovementY(getVelocityY()); moveY(w); } else if(direction == "FRONT"){ setMovementY(-getVelocityY()); moveY(w); } } else if(SeaStar* s = dynamic_cast<SeaStar*>(other)){ damaged = true; setCurrentAnimationVector("DAMAGED"); setHealth(getHealth() - s->getDamage()); setTimer( SDL_GetTicks() + 2000 ); setCollidable(false); setVelocityX(getVelocityX()*2); setVelocityY(getVelocityY()*2); } }
void UfoObject::deadState(){ HP = 0; plingSFX->play(0.3f); addGravityToObject(true); this->bWallCollisions = false; setVelocityY(70); this->unschedule(schedule_selector(UfoObject::AIUpdate)); this->unschedule(schedule_selector(UfoObject::update)); removeWhenBelowZero(); setVelocityX(getVelocityX()*0.1); setVelocityY(getVelocityY()*0.1); objectSprite->setRotation(25); dropCoin(3); }
void Plane::update(int deltaTime, bool gameOver) { //If the game is over continue to make the plane fall if (gameOver) { setVelocityY(getVelocityY() + GRAVITY * (deltaTime/1000.0f)); setOffsetY(getOffsetY() + getVelocityY()); //Check if the plane has reach the bottom maximum bounds if (getOffsetY() >= SPRITE_MAX_BOTTOM) { setOffsetY(getOffsetY() - getVelocityY()); } } elapseTime_ += deltaTime; if (elapseTime_ > FPS) { frameCounter_++; if (frameCounter_ == 3) { frameCounter_ = 0; } elapseTime_ = 0; } //Update the puff cloud puffCloud_.setOffsetY(getOffsetY() + 10.0f); puffCloud_.update(deltaTime); //Shift the collision boxes shiftCollisionBoxes(); }
void Mogh::tick(gen::GameEngine* ge){ if(getHealth() < 1){ SDL_Flip(gen::global.screen); SDL_Delay(4000); ge->setGameOver(true); return; } if(damaged){ if(SDL_GetTicks() > getTimer()){ setCollidable(true); setVelocityX(getVelocityX()/2); setVelocityY(getVelocityY()/2); damaged = false; } } Hero::tick(ge); if(hpText == 0){ hpText = gen::Text::createText("c:\\WINDOWS\\Fonts\\arial.ttf", 30, "0", 0, 0, 0); hpText->setPositions(10, 10); } std::stringstream s; s << "HP: "; s << getHealth(); hpText->setMessage(s.str()); if(amountKilled == 0){ amountKilled = gen::Text::createText("c:\\WINDOWS\\Fonts\\arial.ttf", 30, "0", 0, 0, 0); amountKilled->setPositions(200, 10); } std::stringstream s2; s2 << "Killscore: "; s2 << Character::getAmountRemoved(); amountKilled->setMessage(s2.str()); hpText->tick(ge); amountKilled->tick(ge); }
/* * This updates the planes offsets and frame * * @param input is used to check for player input * @param deltaTime is used to update the frame of the plane */ void Plane::update(Input* input, int deltaTime) { //std::cout << "Delta is: " << deltaTime << std::endl; //If player press the space key then make the plane jump if (input->isKeyHit(SDLK_SPACE)) { startJump(); } //If plane is falling if (motion_ == MOTION_Falling) { setVelocityY(getVelocityY() + GRAVITY * (deltaTime/1000.0f)); setOffsetY(getOffsetY() + getVelocityY()); //Check if the plane has reach the bottom maximum bounds if (getOffsetY() >= SPRITE_MAX_BOTTOM) { setOffsetY(getOffsetY() - getVelocityY()); } } //Else if plane is jumping else if (motion_ == MOTION_Jumping) { jump_.update(deltaTime); if (jump_.getJumpTimeRemaining() > 0) { //Set the velocity setVelocityY( getVelocityY() + JUMP_VELOCITY); setOffsetY(getOffsetY() - getVelocityY()); //Check if the plane has reach the top maximum bounds if (getOffsetY() <= SPRITE_MAX_TOP) { setOffsetY(getOffsetY() + getVelocityY()); //Stop jumping stopJump(); } } //Max jump is reach else { stopJump(); } } elapseTime_ += deltaTime; if (elapseTime_ > FPS) { frameCounter_++; if (frameCounter_ == 3) { frameCounter_ = 0; } elapseTime_ = 0; } //Update the puff cloud puffCloud_.setOffsetY(getOffsetY() + 10.0f); puffCloud_.update(deltaTime); //Shift the collision boxes shiftCollisionBoxes(); }
void Rock::update(float timeStep) { setY(getY() + (getVelocityY() * timeStep)); if (getY() > config::W_HEIGHT) getManager()->remove(this); }
void Enemy::updateMovement(float timeStep) { //Get target X,Y so we can ignore sprite sizes int targetYCompare = (int)(currentTarget->getY() - ((getHeight() - currentTarget->getHeight()) / 2.f)); int targetXCompare = (int)(currentTarget->getX() - ((getWidth() - currentTarget->getWidth()) / 2.f)); bool tmp_movingY = m_movingY; bool tmp_movingX = m_movingX; float delta_x = 0.0; float delta_y = 0.0; //Move vertical if( tmp_movingY && ((getY() < targetYCompare - (config::W_HEIGHT / m_diagonalSensitivity)) || (getY() > targetYCompare + (config::W_HEIGHT / m_diagonalSensitivity)) )) tmp_movingX = true; if ( tmp_movingX && ((getX() < targetXCompare - (config::W_WIDTH / m_diagonalSensitivity)) || (getX() > targetXCompare + (config::W_WIDTH / m_diagonalSensitivity)) )) tmp_movingY = true; if(tmp_movingY && (getY() == targetYCompare)) tmp_movingX = true; if(tmp_movingX && (getX() == targetXCompare)) tmp_movingY = true; if(tmp_movingY) { if ( getY() < targetYCompare ) { delta_y += (getVelocityY() * timeStep); if ( (getY() + delta_y) + getHeight() > config::W_HEIGHT) { setY(static_cast<float>(config::W_HEIGHT - getHeight())); //Prevent from going out of screen delta_y = 0.0; } if ( (getY() + delta_y) > targetYCompare) { setY(float(targetYCompare)); delta_y = 0.0; } if (tmp_movingX == false) { m_direction = DOWN; } } else if ( getY() > targetYCompare) { delta_y -= (getVelocityY() * timeStep); if ( (getY() + delta_y) < 0) { setY(0); delta_y = 0.0; } if ( (getY() + delta_y) < targetYCompare) { setY(float(targetYCompare)); delta_y = 0.0; } if( tmp_movingX == false ) { m_direction = UP; } } } //Move horizontal if(tmp_movingX) { if (getX() < targetXCompare) { delta_x += (getVelocityX() * timeStep); if ( (getX() + delta_x) + getWidth() > config::W_WIDTH) { setX(static_cast<float>(config::W_WIDTH - getWidth())); delta_x = 0.0; } if ( (getX() + delta_x) > targetXCompare) { setX(float(targetXCompare)); delta_x = 0.0; } if( tmp_movingY == false ) { m_direction = RIGHT; } } else if (getX() > targetXCompare) { delta_x -= (getVelocityX() * timeStep); if ( (getX() + delta_x) < 0) { setX(0); delta_x = 0.0; } if ( (getX() + delta_x) < targetXCompare) { setX(float(targetXCompare)); delta_x = 0.0; } if( tmp_movingY == false ) { m_direction = LEFT; } } } // Update the calculated position if (delta_x != 0.0 && delta_y != 0.0 && !config::OLD_DIAGONAL_SPEED) { /* Going to explain the multiplier value here * | * / | * sqrt(2*2 + 3*3)/ | * / | 3 * / | * / - - - - | * 2 * And the value I want is ((2+3)/2) / sqrt(2*2 + 3*3) * which is : 2.5 / sqrt(13) * 2.5 / 3.605551275463989 = 0.69~ * However in our case we can assume the enemy always moves with equal speed in both X and Y, * because FPS::FPSControl.GetSpeedFactor() doesn't change between the modifications of delta_x and delta_y. * The formula would look like (x / sqrt( 2(x*x) ) * ... which is 0.7071067811865475. */ /* float d = abs(delta_x); float multiplier = d / sqrt(2*(d*d)); */ float multiplier = 0.7071068f; //fast-forward to answer, skipping calculation delta_x *= multiplier; delta_y *= multiplier; } setX(getX() + delta_x); setY(getY() + delta_y); }
void testCannonball() { const double feilmargin = 0.0001; //AcclX assert(avvik(0.0, acclX()) < feilmargin); //AcclY assert(avvik(-9.81, acclY()) < feilmargin); //velX assert(avvik(50.0, velX(50.0, 0)) < feilmargin); assert(avvik(50.0, velX(50.0, 2.5)) < feilmargin); assert(avvik(50.0, velX(50.0, 5.0)) < feilmargin); //velY assert(avvik(25.0, velY(25.0, 0)) < feilmargin); assert(avvik(0.475, velY(25.0, 2.5)) < feilmargin); assert(avvik(-24.05, velY(25.0, 5.0)) < feilmargin); //velIntY assert(avvik(25.0, velIntY(25.0, 0)) < feilmargin); assert(avvik(0.475, velIntY(25.0, 2.5)) < feilmargin); assert(avvik(-24.05, velIntY(25.0, 5.0)) < feilmargin); //posX assert(avvik(0.0, posX(50.0, 0)) < feilmargin); assert(avvik(125.0, posX(50.0, 2.5)) < feilmargin); assert(avvik(250.0, posX(50.0, 5.0)) < feilmargin); //posIntX assert(avvik(0.0, posIntX(50.0, 0)) < feilmargin); assert(avvik(125.0, posIntX(50.0, 2.5)) < feilmargin); assert(avvik(250.0, posIntX(50.0, 5.0)) < feilmargin); //posY assert(avvik(0.0, posY(25.0, 0)) < feilmargin); assert(avvik(31.84, posY(25.0, 2.5)) < feilmargin); assert(avvik(2.375, posY(25.0, 5.0)) < feilmargin); //posIntY assert(avvik(0.0, posIntY(25.0, 0)) < feilmargin); assert(avvik(31.84, posIntY(25.0, 2.5)) < feilmargin); assert(avvik(2.375, posIntY(25.0, 5.0)) < feilmargin); //flightTime assert(avvik(2.03874, flightTime(10.0)) < feilmargin); //getVelocityX assert(avvik(7.071067812, getVelocityX( asin(1) / 2, 10.0 )) < feilmargin); //getVelocityY assert(avvik(7.071067812, getVelocityY( asin(1) / 2, 10.0 )) < feilmargin); //getDistanceTraveled assert(avvik(20.3874, getDistanceTraveled( 10.0, 10.0 )) < feilmargin); //optimalAngleForMaxDistance assert(avvik(0.7853981634, optimalAngleForMaxDistance( 10.0 )) < feilmargin); //getDistanceTraveled assert(avvik(-0.3874, targetPractice( 20.0, 10.0, 10.0 )) < feilmargin); }