void tick() { process_events(); update_enemies(); update_towers(); update_player(); }
void Dodger::play() { if (skip_level) { // cheat to ease testing game_state = password; } update_dodger_position(); int new_cell = level.data[player_line][player_col]; // check if dead and update laser shots // need to check if dodger is hit before AND after updating the laser shots // to prevent the dodger and the shot to pass each other with no hit. bool hit = hit_by_laser(); update_enemies(); hit |= hit_by_laser(); if (hit || new_cell == Level::skull) { // Note: can't eat if dead game_state = dying; } else { check_if_eating(new_cell); if (level.food_count == 0) { std::cout << "level cleared" << std::endl; if (level_number == levels.size()) { game_state = well_done; } else { game_state = password; } } } }
void gameLoop() { // Set our game data shipRow = 55; shipCol = 0; enemyCol = 225; enemy_num = 0; projectile_num = 0; time_diff = 0; kills = 0; REG_DISPCNT = MODE3 | BG2_ENABLE; EntityClass player_ship; initialize_entity( &player_ship, SHIP_WIDTH, SHIP_HEIGHT, shipRow, shipCol ); EntityClass enemy; initialize_entity( &enemy, 0, 0, 0, 0 ); enemies[max_enemies+1] = enemy; while (lives != 0) { // Check if player won the level. if( kills == 10 ) { state = WIN; break; } // Draw background drawImage3( 0, 0, BACKGROUND_WIDTH, BACKGROUND_HEIGHT, background ); // Draw ship draw_entity( &player_ship, ship ); // Generate enemy if( time_diff <= 0 && enemy_num < 10 ) { time_diff = 75; int rand_row = ( rand() % (120+1-20) ) + 20; generate_enemy( rand_row, enemyCol ); } // Draw Enemy update_enemies(); // Draw each projectile update_projectiles(); // Check if player is dead. if( is_player_dead( &player_ship ) == 1 ) { lives--; gameLoop(); } check_collisions(); remove_old_projectiles(); remove_old_enemies(); // Draw lives for( int i = 0; i < lives; i++ ) { drawImage3( 140, 170 + ( i * 25 ), SHIP_WIDTH, SHIP_HEIGHT, ship ); } waitForVblank(); // If player hits 'select' return to titleScreen and start game over. if( KEY_DOWN_NOW( BUTTON_SELECT ) ) { state = TITLE; break; } // Player Actions if( KEY_DOWN_NOW( BUTTON_DOWN ) ) { if( get_row( &player_ship ) < 121 ) move_entity( &player_ship, 2, 0 ); } if( KEY_DOWN_NOW( BUTTON_UP ) ) { if( get_row( &player_ship ) > 20 ) move_entity( &player_ship, -2, 0 ); } if( KEY_DOWN_NOW( BUTTON_A ) ) { while( KEY_DOWN_NOW( BUTTON_A ) ) { ; } fire_weapon( &player_ship ); } time_diff--; } if( lives == 0 ) { state = GAME_OVER; } else if( state == WIN ) return; }
void loop(s4 &game_state) { ICameraSceneNode* camera = irrlicht->smgr->addCameraSceneNode(); matrix4 ortho; ortho.buildProjectionMatrixOrthoLH( irrlicht->driver->getScreenSize().Width/ortho_scale, irrlicht->driver->getScreenSize().Height/ortho_scale,-1.0,1000.0); camera->setProjectionMatrix(ortho); camera->setPosition({0,0,-100}); camera->setTarget({0,0,0}); irrlicht->hud->setActiveCamera(camera); IBillboardSceneNode* qnode = irrlicht->smgr->addBillboardSceneNode(); qnode->setMaterialFlag(EMF_WIREFRAME,true); // -------------------------------------- p("---- Game loop start ----"); d32 dt = 0.0f; const d32 maxDelta = 1.0f/60.0f * 5.0f; const d32 tick_ms = 0.01666f; // TODO: change this back to 30ms? d32 render_dt = 0.0f; const d32 render_ms = RENDER_TIME_STEP; uint32 time_physics_prev = btclock->getTimeMicroseconds(); while(irrlicht->device->run() && game_state == GAME_STATE_PLAY) { const uint32 time_physics_curr = btclock->getTimeMicroseconds(); const d32 frameTime = ((d32)(time_physics_curr - time_physics_prev)) / 1000000.0; // todo: is this truncated correctly? time_physics_prev = time_physics_curr; d32 capped_dt = frameTime; if (capped_dt > maxDelta) { capped_dt = maxDelta; } render_dt += capped_dt; if ( render_dt >= render_ms ) { render_dt = 0.0f; update_player(); update_enemies(); update_missiles(ply->missiles,ply->num_missile); irrlicht->hud->getRootSceneNode()->setPosition(camera->getPosition() + vector3df(0,0,100)); irrlicht->driver->beginScene(true, true, SColor(255,59,120,140)); irrlicht->smgr->drawAll(); irrlicht->driver->clearZBuffer(); irrlicht->hud->drawAll(); irrlicht->driver->endScene(); } dt += capped_dt; while( dt >= tick_ms ) { dt -= tick_ms; receiver->input(); clear_quads(quadtree); step_player(); step_enemies(); step_missiles(ply->missiles, ply->num_missile); QuadTree::Quad* fq = get_quad_from_pos(quadtree,ply->curr_pos); qnode->setSize(dimension2d<f32>(1,1)); qnode->setPosition(fq->pos.irr()); qnode->setSize(dimension2d<f32>(fq->width,fq->width)); if (receiver->debug_key.state) { for (s4 i = 0; i < fleet->num_squad; i++) { if (fleet->squads[i].mode != scatter) fleet->squads[i].mode = scatter; else fleet->squads[i].mode = to_positions; } } if (receiver->restart.state) { game_state = GAME_STATE_RESET; return; } if (receiver->QUIT) { game_state = GAME_STATE_QUIT; return; } empty_transient_soft(memory); } alpha = dt / tick_ms; core::stringw str = L"Coquelicot // score: "; str += score; str += L" // FPS: "; str += (s32)irrlicht->driver->getFPS(); irrlicht->device->setWindowCaption(str.c_str()); } }
void Game::update(float dt) { switch(current_mode) { case MODE_READY: fade_music(dt); if(start_pressed()) { current_mode = MODE_GAME; initialize(); } break; case MODE_GAME: { if(life <= 0) { // Delete all enemies. for(auto it = enemies.begin(); it != enemies.end(); ) { delete *it; it = enemies.erase(it); } //delete music; current_mode = MODE_HIGHSCORE; highscore->add_entry(score); score_text.set_alignment(Text::RIGHT_ALIGNED); score_text.set_number(score); score_text.set_color(highscore_color); score_text.set_scale(30.0 * hud_scale.x); score_text.set_position(glm::vec3(glm::vec2(150.f, 270.f) * hud_scale, 0.f)); int i = 0; bool selected = false; for(int e : highscore->get_entries()) { if(e > 0) { if(!selected && e == score) { highscore_entries[i].set_color(hud_font_color); } else { highscore_entries[i].set_color(highscore_color); } highscore_entries[i].set_number(e); } else { highscore_entries[i].set_text(""); } ++i; } #ifdef WIN32 if(useWII) WII->setRumble(false); #endif return; } // Change wind direction wind_velocity = glm::rotateY(wind_velocity, (float)(0.8f + sin(global_time))); update_wind_velocity(); //input.update_object(camera, dt); bool buttonFirePressed = (input.has_changed(Input::ACTION_0, 0.2f) && input.current_value(Input::ACTION_0) > 0.9f); bool buttonSwapForwPressed = (input.has_changed(Input::ACTION_3, 0.2f) && input.current_value(Input::ACTION_3) > 0.9f); bool buttonSwapBackPressed = (input.has_changed(Input::ACTION_2, 0.2f) && input.current_value(Input::ACTION_2) > 0.9f); bool buttonBreakPressed = (input.has_changed(Input::ACTION_1, 0.2f) && input.current_value(Input::ACTION_1) > 0.9f); bool buttonMutePressed = false; #ifdef WIN32 if (useWII) { float pitch = WII->getPitch(), roll = -1 * WII->getRoll(); // [-90, 90]. pitch = glm::clamp(pitch - 10, -90.0f, 90.0f); //pitch = glm::clamp(-10 + 90 * glm::sign(pitch) * glm::pow(glm::abs(1.0f * pitch / 90), 1.0f), -90.0f, 90.0f); //roll = glm::clamp(90 * glm::sign(roll) * glm::pow(glm::abs(1.0f * roll / 90), 1.0f), -90.0f, 90.0f); player.set_canon_pitch(pitch); player.set_canon_yaw(roll); buttonFirePressed = WII->getButtonAPressed(); buttonSwapBackPressed = WII->getArrowLeftPressed(); buttonSwapForwPressed = WII->getArrowRightPressed(); buttonBreakPressed = WII->getButtonBDown(); //buttonMutePressed = WII->getArrowUpPressed(); const bool wiiSwapAB = false; if (wiiSwapAB) std::swap(buttonFirePressed, buttonBreakPressed); } else { #endif player.set_canon_pitch(input.current_value(Input::MOVE_Y) * 90.f); player.set_canon_yaw(input.current_value(Input::MOVE_X) * 90.f); #ifdef WIN32 } #endif if (buttonFirePressed) { shoot(); #ifdef WIN32 if(useWII) WII->setRumble(true); } else { if(useWII) WII->setRumble(false); #endif } if(buttonMutePressed) { music_mute = !music_mute; if(music_mute) { music ->stop(); } //change_particles(-1); } if (buttonSwapForwPressed) { change_particles(1); } if (buttonSwapBackPressed) { change_particles(-1); } if( buttonBreakPressed && global_time - last_break > break_cooldown) last_break = global_time; float speed = current_movement_speed; if (global_time - last_break < break_duration) speed *= break_factor; player.update_position(path, player.path_position() + speed * dt); update_camera(); update_enemies(dt); smoke->update(dt); attack_particles->update(dt, enemies, this); dust->config.spawn_position = glm::vec4(path->at(player.path_position() + dust_spawn_ahead) - half_dust_spawn_area, 1.f); dust->update_config(); dust->update(dt); explosions->update(dt); life_text.set_number(life); score_text.set_number(score); dust->config.spawn_position = glm::vec4(path->at(player.path_position() + dust_spawn_ahead) - half_dust_spawn_area, 1.f); dust->update_config(); dust->update(dt); explosions->update(dt); active_sounds.remove_if([](const Sound * s) { if(s->is_done()) { delete s; return true; }; return false; }); // Really ugly way of looping the music: if(music != nullptr && music->is_done() && !music_mute) { delete music; music = new Sound("ecstacy.mp3", 5); music->play(); } } break; case MODE_HIGHSCORE: fade_music(dt); if(start_pressed()) { current_mode = MODE_READY; } break; } input.update(dt); }