/* animateAttack Parameters: animationClock: Clock used to determine if the animation should move to the next frame sprite: The sprite where the attack animation is contained returnToDefault: Determines if the sprite should return to the default frame (used to add "oomph" to the last attack in a string, for example). This method simply animates the attack. It returns true if the attack is still in the process of animating, false if the attack has finished animating. */ bool Attack::animateAttack(sf::Sprite* sprite, sf::Clock* clock, bool reset) { int spriteLeft = sprite->getTextureRect().left; if (reset) { resetAnimation(sprite, clock); return true; } if (spriteLeft / width < numFrames && spriteLeft / width != activeFrame && clock->getElapsedTime().asMilliseconds() > frameDelay) advanceFrame(sprite, clock); else if (spriteLeft / width == activeFrame && clock->getElapsedTime().asMilliseconds() > activeFrameDelay) { if (activeFrameDelay == specialHitStun) Game::camera->emphasisZoomOut(); advanceFrame(sprite, clock); } else if (spriteLeft / width == numFrames && clock->getElapsedTime().asMilliseconds() > finalFrameDelay) { clock->restart(); left = defaultLeft; activeFrameDelay = hitStun; return false; } return true; }
void Hero::update(Uint32 ticks) { if ( explosion ) { explosion->update(ticks); if ( explosion->chunkCount() == 0 ) { delete explosion; explosion = NULL; } return; } if(special != NULL) { special->advanceFrame(ticks); special->setPosition(this->getPosition()-Vector2f(20,50)); } advanceFrame(ticks); Vector2f incr = getVelocity() * static_cast<float>(ticks) * 0.001; setPosition(getPosition() + incr); if ( Y() <= 0) { velocityY( abs( velocityY() ) ); } if ( Y() >= worldHeight-frameHeight) { velocityY( -abs( velocityY() ) ); } if ( X() <= 0) { velocityX( abs( velocityX() ) ); right = true; } if ( X() >= worldWidth-frameWidth) { velocityX( -abs( velocityX() ) ); right = false; } }
void TwoWaySprite::update(Uint32 ticks) { advanceFrame(ticks); Vector2f incr = getVelocity() * static_cast<float>(ticks) * 0.001; setPosition(getPosition() + incr); if ( Y() < 0) { velocityY( abs( velocityY() ) ); } if ( Y() > worldHeight-frameHeight) { velocityY( -abs( velocityY() ) ); } if ( X() < 0) { velocityX( abs( velocityX() ) ); flipX = 1; } if ( X() > worldWidth-frameWidth) { velocityX( -abs( velocityX() ) ); flipX = -1; } }
void TwoWaySprite::update(Uint32 ticks) { if(explosion){ explosion->update(ticks); if(explosion->chunkCount()==0){ delete explosion; explosion = NULL; } return; } Vector2f incr = getVelocity() * static_cast<float>(ticks) * 0.001; setPosition(getPosition() + incr); advanceFrame(ticks); if ( Y() < 0) { velocityY( abs( velocityY() ) ); } if ( Y() > worldHeight-frameHeight) { velocityY( -abs( velocityY() ) ); } if ( X() < 0) { velocityX( abs( velocityX() ) ); } if ( X() > worldWidth-frameWidth) { velocityX( -abs( velocityX() ) ); } }
void Cat::update(Uint32 ticks) { //std::cout << ticks << std::endl; advanceFrame(ticks); if(t >= 0){ jump(ticks) ; } else if( t2 >= 0){ fall(ticks) ; } else{ Vector2f incr = getVelocity() * static_cast<float>(ticks) * 0.001; setPosition(getPosition() + incr); if ( Y() < 0) { velocityY( abs( velocityY() ) ); } if ( Y() > worldHeight-frameHeight) { velocityY( -abs( velocityY() ) ); } if ( X() < 0) { velocityX( abs( velocityX() ) ); } if ( X() > worldWidth-frameWidth) { velocityX( -abs( velocityX() ) ); } } }
void Bullet::update(Uint32 ticks) { Vector2f incr = getVelocity() * static_cast<float>(ticks) * 0.001; if ( getVelocity().magnitude() > 1.0 ) { advanceFrame(ticks); } if(keyPCheck == 1){ frames = framesLeft; } else if(keyPCheck == 2){ velocityX(-abs(velocityV[0])); frames = framesRight; }else{ frames = framesLeft; } Vector2f pos = getPosition(); setPosition(getPosition() + incr); distance += ( hypot(X()-pos[0], Y()-pos[1]) ); if (distance > maxDistance) tooFar = true; }
void TWRotateMultiSprite::update(Uint32 ticks) { advanceFrame(ticks); Vector2f incr = getVelocity() * static_cast<float>(ticks) * 0.001; setPosition(getPosition() + incr); setAngle(getAngle() + getAngleInc() ); if ( Y() < 0) { velocityY( abs( velocityY() ) ); } if ( Y() > worldHeight-frameHeight) { velocityY( -abs( velocityY() ) ); } if ( X() < 0) { velocityX( abs( velocityX() ) ); flag = true; } if ( X() > worldWidth-frameWidth) { velocityX( -abs( velocityX() ) ); flag = false; } }
void Player::update(Uint32 ticks) { advanceFrame(ticks); //Vector2f incr = getVelocity() * static_cast<float>(ticks) * 0.001; Vector2f incr(0.0, 12.0); setPosition(getPosition() + incr); //timeSinceLastBullet += ticks; //bullets.update(ticks); /* if ( Y() < 0) { velocityY( abs( velocityY() ) ); velocityY(0); } if ( Y() > worldHeight-frameHeight) { velocityY( -abs( velocityY() ) ); velocityY(0); } if ( X() < 0) { velocityX( abs( velocityX() ) ); velocityX(0); } if ( X() > worldWidth-frameWidth) { velocityX( -abs( velocityX() ) ); velocityX(0); } */ }
int OpticSprite::draw(LPD3DXSPRITE sprite, double time, D3DXCOLOR colour, double offsetX, double offsetY) { if(!this->animate) { sprite->Draw(texture, NULL, NULL, NULL, colour); return 1; } if(animation.lifetime() > time) { animation.updateState(aniState, time); colour = D3DXCOLOR(aniState.red, aniState.green, aniState.blue, colour.a < aniState.alpha? colour.a : aniState.alpha); float rotation = 6.28318531f * aniState.rotation; D3DXMATRIX mat, current; D3DXVECTOR2 scaling(aniState.scale_x, aniState.scale_y); D3DXVECTOR2 position(floor(((pResolution->width * aniState.position_x) - translateCentre.x) + offsetX), floor(((pResolution->height * aniState.position_y) - translateCentre.y) + offsetY)); D3DXMatrixTransformation2D(&mat, &transformCentre, 0.0f, &scaling, &transformCentre, rotation, &position); sprite->GetTransform(¤t); mat *= current; sprite->SetTransform(&mat); HRESULT res; //Spritesheet rect calculations are slightly iffy thanks to the texture scaling (rounding errors) if(this->spritesheet) { int x = (width * currFrame) % surfaceDesc.Width; //Hacky workaround int diff = x - surfaceDesc.Width; if(abs(diff) <= 10) { x = 0; } //End of hacky workaround int y = height * currRow; RECT source; source.top = y; source.left = x; source.right = x + width; source.bottom = y + height; res = sprite->Draw(texture, &source, NULL, NULL, colour); sprite->SetTransform(¤t); if(!advanceFrame(time, x, y)) { return 0; } } else { res = sprite->Draw(texture, NULL, NULL, NULL, colour); sprite->SetTransform(¤t); } if(res != S_OK) { throw OpticSpriteException("Rendering sprite failed!"); } return 1; } return 0; }
void Bullet::update(Uint32 ticks) { advanceFrame(ticks); Vector2f incr = getVelocity() * static_cast<float>(ticks) * 0.001; setPosition(getPosition() + incr); }
void CrabSprite::update(Uint32 ticks){ advanceFrame(ticks); Vector2f incr = getVelocity() * static_cast<float>(ticks) * 0.001; setPosition(getPosition() + incr); if (X() < 0) setOut(); }
void Player::right(Uint32 ticks) { advanceFrame(ticks); Vector2f incr(12.0, 0.0); setPosition(getPosition() + incr); direction = 0; if ( X() > worldWidth-frameWidth) { left(23); } }
void Player::up(Uint32 ticks) { advanceFrame(ticks); Vector2f incr(0.0, -12.0); setPosition(getPosition() + incr); if ( Y() < 0) { down(23); } }
void Player::down(Uint32 ticks) { advanceFrame(ticks); Vector2f incr(0.0, 12.0); setPosition(getPosition() + incr); if ( Y() > worldHeight-frameHeight) { up(23); } }
void Player::left(Uint32 ticks) { advanceFrame(ticks); Vector2f incr(-12.0, 0.0); setPosition(getPosition() + incr); direction = 1; if ( X() < 0) { right(23); } }
void MultiSprite::update(Uint32 ticks) { if (explosion) { // std::cout << "multisprite update" << std::endl; explosion->update(ticks); if (explosion->chunkCount() == 0) { delete explosion; explosion = NULL; } return; } advanceFrame(ticks); move(ticks); }
void Spectra::startVibrationAnimation() { // First calculate our frames, and then start our timer. m_totalFrames = m_molecule->coordinate3dCount(); m_currentFrame = 0; if (!m_timer) { m_timer = new QTimer(this); connect(m_timer, SIGNAL(timeout()), SLOT(advanceFrame())); } if (!m_timer->isActive()) { m_timer->start(50); } }
bool IAnimated::updateAnimation(float i_dt) { if (!d_currentAnimation || d_repeats == 0) return true; d_animationTime += i_dt; while (d_animationTime >= d_currentAnimation->interval) { if (!advanceFrame()) return false; d_animationTime -= d_currentAnimation->interval; } return true; }
void Enemy::updatePosition(Uint32 ticks) { // float width = static_cast<float> (sprites.at(curSprite)->getWidth()); // Cap the player's motion if they are trying to move off of the // left border of the world. if (x <= player->getX()) { xSpeed = abs(xSpeed); } // Cap the player's motion if they are trying to move off of the // right border of the world. if (x >= player->getX()) { xSpeed = -fabs(xSpeed); } float incr = xSpeed * static_cast<float> (ticks) * 0.001; x += incr; advanceFrame(ticks); }
void TwowaySprite::update(Uint32 ticks) { advanceFrame(ticks); Vector2f incr = getVelocity() * static_cast<float>(ticks) * 0.001; setPosition(getPosition() + incr); if ( Y() < 0) { velocityY( abs( velocityY() ) ); direction = 4; //Go down } if ( Y() > worldHeight-frameHeight) { velocityY( -abs( velocityY() ) ); direction = 3; //Go up } if ( X() < 0) { velocityX( abs( velocityX() ) ); direction = 1; //Go right } if ( X() > worldWidth-frameWidth) { velocityX( -abs( velocityX() ) ); direction = 2; //Go left } }
void player::update() { move(velPosX - velNegX, velPosY - velNegY); advanceFrame(); }
int main(int argc, char *argv[]) { int opt; int fullscreen = 0, resizable = 0, borderless = 0; const char *appTitle = DEFAULT_APP_TITLE; const char *game_dir = NULL; const char *pak_filename = "game.pak"; const char *rlog_filename = "rengine.log"; const char *startstate; struct game_state *gs = NULL; int demo = 0; SDL_version compiled, linked; log_init(rlog_filename); json_error = rerror; if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) < 0) { rerror("SDL_Init: %s", SDL_GetError()); return 1; } atexit(SDL_Quit); while((opt = getopt(argc, argv, "p:g:l:d?")) != -1) { switch(opt) { case 'p': { pak_filename = optarg; } break; case 'g' : { game_dir = optarg; pak_filename = NULL; } break; case 'l': { rlog_filename = optarg; } break; case 'd': { demo = 1; } break; case '?' : { usage(argv[0]); return 1; } } } if(!getcwd(initial_dir, sizeof initial_dir)) { rerror("error in getcwd(): %s", strerror(errno)); return 1; } rlog("Running engine from %s", initial_dir); if(!gdb_new()) { rerror("Unable to create Game Database"); return 1; } re_initialize(); states_initialize(); if(!snd_init()) { rerror("Terminating because of audio problem."); return 1; } /* Don't quite know how to use this in Windows yet. */ SDL_LogSetAllPriority(SDL_LOG_PRIORITY_WARN); SDL_Log("Testing Log capability."); SDL_VERSION(&compiled); SDL_GetVersion(&linked); rlog("SDL version %d.%d.%d (compile)", compiled.major, compiled.minor, compiled.patch); rlog("SDL version %d.%d.%d (link)", linked.major, linked.minor, linked.patch); if(!demo) { if(pak_filename) { rlog("Loading game PAK file: %s", pak_filename); if(!rs_read_pak(pak_filename)) { rerror("Unable to open PAK file '%s'; Playing demo mode.", pak_filename); goto start_demo; } } else { rlog("Not using a PAK file. Using '%s' instead.", game_dir); if(chdir(game_dir)) { rerror("Unable to change to '%s': %s", game_dir, strerror(errno)); return 1; } } game_ini = re_get_ini(GAME_INI); if(game_ini) { appTitle = ini_get(game_ini, "init", "appTitle", "Rengine"); screenWidth = atoi(ini_get(game_ini, "screen", "width", PARAM(SCREEN_WIDTH))); screenHeight = atoi(ini_get(game_ini, "screen", "height", PARAM(SCREEN_HEIGHT))); resizable = atoi(ini_get(game_ini, "screen", "resizable", "0")) ? SDL_WINDOW_RESIZABLE : 0; borderless = atoi(ini_get(game_ini, "screen", "borderless", "0")) ? SDL_WINDOW_BORDERLESS : 0; fullscreen = atoi(ini_get(game_ini, "screen", "fullscreen", "0")) ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0; fps = atoi(ini_get(game_ini, "screen", "fps", PARAM(DEFAULT_FPS))); if(fps <= 0) fps = DEFAULT_FPS; filter = !my_stricmp(ini_get(game_ini, "screen", "filter", "nearest"), "linear")? "1": "0"; virt_width = atoi(ini_get(game_ini, "virtual", "width", PARAM(VIRT_WIDTH))); virt_height = atoi(ini_get(game_ini, "virtual", "height", PARAM(VIRT_HEIGHT))); show_cursor = atoi(ini_get(game_ini, "mouse", "show-cursor", PARAM(1)))? 1 : 0; startstate = ini_get(game_ini, "init", "startstate", NULL); if(startstate) { gs = get_state(startstate); if(!gs) { rerror("Unable to get initial state: %s", startstate); return 1; } } else { rerror("No initial state in %s", GAME_INI); return 1; } } else { rerror("Unable to load %s", GAME_INI); return 1; } } else { start_demo: rlog("Starting demo mode"); gs = get_demo_state("demo"); } rlog("Initialising..."); if(!init(appTitle, fullscreen | borderless | resizable)) { return 1; } if(!bmf_init()){ rlog("Quiting; Unable to start FreeType font library"); return 1; } assert(gs); rlog("Entering initial state..."); if(!change_state(gs)) { rlog("Quiting, because of earlier problems with the initial state"); return 1; } frameStart = SDL_GetTicks(); rlog("Event loop starting..."); while(!quit) { gs = current_state(); if(!gs) { break; } if(gs->update) gs->update(gs, bmp); advanceFrame(); } rlog("Event loop stopped."); gs = current_state(); if(gs && gs->deinit) gs->deinit(gs); bm_free(bmp); SDL_DestroyTexture(tex); SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); SDL_Quit(); ini_free(game_ini); re_clean_up(); bmf_deinit(); gdb_save("dump.db"); /* For testing the game database functionality. Remove later. */ gdb_close(); snd_deinit(); rlog("Engine shut down."); return 0; }
void Player::update(Uint32 ticks) { int rangeY =worldHeight-frameHeight; int rangeX =worldWidth-frameWidth; advanceFrame(ticks); ShootingSprite::update(ticks); if (!flagStop){ if (up && count>0) { velocityY(velocityY()-95); --count; physics = true; } if (physics) { float deltaT = 0.05; Vector2f accel = Vector2f(0,60); Vector2f vnew = getVelocity() + accel* deltaT; setPosition(getPosition() + vnew * deltaT); setVelocity(vnew); } if (forward) { X(X()+8.0); flag=true; } if (back) { X(X()-8.0); flag=false; } } else{ score = 0; } // Collision Check with edge if ( Y() < 0) { velocityY( 0); Y(0); } if ( Y() > rangeY) { velocityY(0); velocityX(0); Y(rangeY); count=2; //std::cout<<"hit the ground"<<std::endl; //std::cout<<velocityY()<<std::endl; physics = false; } //else std::cout << "height = " << Y() << std::endl; if ( X() < 0) { velocityX( 0 ); X(0); } if ( X() > rangeX) { velocityX( 0 ); X( rangeX); } forward = back = up = down = false; }
void Player::update(Uint32 ticks) { if ( explosion ) { explosion->update(ticks); if ( explosion->chunkCount() == 0 ) { delete explosion; explosion = NULL; } return; } advanceFrame(ticks); Uint8 *keyPressed = SDL_GetKeyState(NULL); Vector2f incr = getVelocity() * static_cast<float>(ticks) * 0.001; setPosition(getPosition() + incr); if(keyPressed[SDLK_h]) { shoot(); } //vertical movement if(keyPressed[SDLK_w]) { if(Y() > 0) velocityY( -movSpeedY ); else Y(0); } else if(keyPressed[SDLK_s]) { if(Y() < worldHeight-frameHeight) velocityY( movSpeedY ); else Y(worldHeight-frameHeight); } else velocityY(0); //horizontal movement if(keyPressed[SDLK_d]) { if(X() < worldWidth-frameWidth) velocityX( movSpeedY ); else X(worldWidth-frameWidth); direction = 1; } else if(keyPressed[SDLK_a]) { if(X() > 0) velocityX( -movSpeedY ); else X(0); direction = 0; } else velocityX(0); //superspeed if(keyPressed[SDLK_SPACE]) velocityX( 2*velocityX() ); timeSinceLastBullet +=ticks; bullets.update(ticks); }
void MultiSprite::update(Uint32 ticks) { advanceFrame(ticks); switch(STATUS) { case 0: case 1: { Vector2f incr1 = getVelocity() * static_cast<float>(ticks) * 0.001; incr1[1] = 0; setPosition(getPosition() + incr1); break; } case 2: { float t = static_cast<float>(ticks) * 0.001; float g = 420; Vector2f v0 = getVelocity(); //Vector2f bug; Vector2f pos; //std::cout << "v0[0] = " << v0[0] << " " << "v0[1] = " << v0[1] << std::endl; Vector2f v = v0; // I want the X speed. Vector2f incr2; incr2[0] = v0[0] * t; incr2[1] = v0[1] * t + 0.5 * g * t * t; //std::cout << "incr2[1] = " << incr2[1] << std::endl; v[1] = v0[1] + g * t; //std::cout << "before setVelocity " << v[0] << ", " << v[1] << std::endl; setVelocity(v); //bug = getVelocity(); //std::cout << "after setVelocity " << bug[0] << ", " << bug[1] << std::endl << std::endl; pos = getPosition() + incr2; if (pos[1] == 330) { setVelocitytoZero(); setPosition(pos); } else { setPosition(pos); } break; } case 4: { setVelocitytoZero(); } default: break; } /*if ( Y() < 0) { velocityY( abs( velocityY() ) ); } if ( Y() > worldHeight-frameHeight) { velocityY( -abs( velocityY() ) ); } if ( X() < 0) { velocityX( abs( velocityX() ) ); } if ( X() > worldWidth-frameWidth) { velocityX( -abs( velocityX() ) ); }*/ }
void DriverBase<Scalar>::run() { initSimulationData(); for(unsigned int frame=start_frame_;frame<=end_frame_;++frame) advanceFrame(frame); }
void Splosion::update(Uint32 ticks) { totalTimeSploding += ticks; if (totalTimeSploding < 1500) advanceFrame(ticks); }
void Rayman::update(Uint32 ticks) { advanceFrame(ticks); Vector2f incr = getVelocity() * static_cast<float>(ticks) * 0.001; setPosition(getPosition() + incr); if ( X() < 0) { velocityX( 0 ); } if ( X() > worldWidth-frameWidth) { velocityX( 0 ); } //update every sprite in observer //std::cout<<observers.size()<<std::endl; std::list<pinkGear*>::iterator ptr = observers.begin(); while ( ptr != observers.end() ) { (*ptr)->setPlayerPos( getPosition() ); ++ptr; } switch(frameDirection){ case 1: if( !isCrawl && !isJump ){ // velocityX() != 500 frames = FrameFactory::getInstance().getFrames(frameName, "runRight"); velocityX(500); break; } if ( isCrawl ){ frames = FrameFactory::getInstance().getFrames(frameName, "crawlRight"); velocityX(500); break; } if ( isJump ){ jumpFunction(); break; } break; case -1: if( !isCrawl && !isJump ){ frames = FrameFactory::getInstance().getFrames(frameName, "runLeft"); velocityX(-500); break; } if ( isCrawl ){ frames = FrameFactory::getInstance().getFrames(frameName, "crawlLeft"); velocityX(-500); break; } if ( isJump ){ jumpFunction(); break; } break; case 0: //no moving action velocityX(0); if ( !isCrawl && !isJump && !isFist ){//keep standing if (faceDirection== -1) { frames = FrameFactory::getInstance().getFrames(frameName, "standLeft"); } if (faceDirection== 1){ frames = FrameFactory::getInstance().getFrames(frameName, "standRight"); } alreadyCrawling = false; break; } if ( isCrawl ){//keep crawling postion if(!alreadyCrawling){ if (faceDirection== -1) frames = FrameFactory::getInstance().getFrames(frameName, "standToLeftCrawl"); if (faceDirection== 1) frames = FrameFactory::getInstance().getFrames(frameName, "standToRightCrawl"); alreadyCrawling = true; } else{ if (faceDirection== -1) frames = FrameFactory::getInstance().getFrames(frameName, "keepLeftCrawl"); if (faceDirection== 1) frames = FrameFactory::getInstance().getFrames(frameName, "keepRightCrawl"); } break; } if ( isJump ){//jump action jumpFunction(); break; } if ( isFist ){//fist action if (faceDirection== -1){ frames = FrameFactory::getInstance().getFrames(frameName, "fistLeft"); } if (faceDirection== 1){ frames = FrameFactory::getInstance().getFrames(frameName, "fistRight"); } if (currentFrame == 8 && fistDuration != 3){ //total can insist 3 seconds currentFrame = 5; } break; } break; default: break; } }