void gs_playing::update(StringHash eventType,VariantMap& eventData) { if(globals::instance()->game_states.size()>1) return; Input* input=GetSubsystem<Input>(); float timeStep=eventData[Update::P_TIMESTEP].GetFloat(); timer_playing+=timeStep; // check if there should be rocks spawned due to a distance trigger for(level_rock_spawn& rs:current_level.rock_spawns) if(!rs.rocks_spawned&&rs.trigger_area.IsInside(player_->node->GetPosition())) { rs.rocks_spawned=true; for(int i=0;i<rs.rock_count;i++) { auto node_stone=globals::instance()->scene->CreateChild("Stone"); nodes.push_back(node_stone); StaticModel* boxObject=node_stone->CreateComponent<StaticModel>(); boxObject->SetModel(globals::instance()->cache->GetResource<Model>("Models/rock.mdl")); boxObject->SetMaterial(globals::instance()->cache->GetResource<Material>("Materials/rock.xml")); boxObject->SetCastShadows(true); float s=1.0+Random(3.0f); node_stone->SetScale(s); boxObject->SetOccludee(true); boxObject->SetDrawDistance(200); boxObject->SetShadowDistance(200); PhysicsRaycastResult result; Vector3 pos; pos.x_=Random(rs.spawn_area.min_.x_,rs.spawn_area.max_.x_); pos.y_=Random(rs.spawn_area.min_.y_,rs.spawn_area.max_.y_); pos.z_=Random(rs.spawn_area.min_.z_,rs.spawn_area.max_.z_); Ray ray(pos,Vector3(0,-1,0)); globals::instance()->physical_world->SphereCast(result,ray,2,100); if(result.distance_<=1000) pos=result.position_+Vector3(0,5,0); auto body_stone=node_stone->CreateComponent<RigidBody>(); body_stone->SetPosition(pos); body_stone->SetCollisionLayer(2); body_stone->SetMass(50.0*s*s); body_stone->SetLinearDamping(0.2f); body_stone->SetAngularDamping(0.2f); //body_stone->SetAngularFactor(Vector3(0,1,0)); body_stone->SetFriction(0.6); CollisionShape* shape=node_stone->CreateComponent<CollisionShape>(); //shape->SetCapsule(1,1.2); shape->SetConvexHull(globals::instance()->cache->GetResource<Model>("Models/rock.mdl")); } } std::string str; { static double last_second=0; static double last_second_frames=1; static timer this_second; static double this_second_frames=0; this_second_frames++; if(this_second.until_now()>=1) { last_second=this_second.until_now(); last_second_frames=this_second_frames; this_second.reset(); this_second_frames=0; } if(last_second!=0) str.append(std::to_string(last_second_frames/last_second).substr(0,6)); str.append(" FPS Position: "); str.append(std::to_string(player_->node->GetPosition().x_).substr(0,6)); str.append(", "); str.append(std::to_string(player_->node->GetPosition().y_).substr(0,6)); str.append(", "); str.append(std::to_string(player_->node->GetPosition().z_).substr(0,6)); str.append("\nLevel Time: "); if(goal_time>0) str.append(std::to_string(goal_time)); else str.append(std::to_string(timer_playing)); str.append("s Remaining Flags: "); str.append(std::to_string(flag_nodes.size())); str.append("/"); str.append(std::to_string(current_level.flag_positions.size())); str.append("\nUse WASD and shift to move and F to toggle flashlight."); if(goal_time>0) str.append("\nFinished!"); String s(str.c_str(),str.size()); window_text->SetText(s); } player_->update(input,timeStep); Vector3 player_pos=player_->node->GetPosition(); for(int i=0;i<flag_nodes.size();i++) { auto n=flag_nodes[i]; n->Yaw(64*timeStep); if((player_pos-n->GetPosition()).Length()<2) { player_->sound_source_flag->Play(player_->sound_flag); flag_nodes.erase(flag_nodes.begin()+i); n->Remove(); for(int j=0;j<nodes.size();j++) if(nodes[j]==n) { nodes.erase(nodes.begin()+j); break; } if(flag_nodes.size()==0) { goal_time=timer_playing; auto e=new gs_level_end; std::string str="You collected all flags!\nNeeded time: "+std::to_string(goal_time)+"s"; map_times highscores; // update if time not set or time better as the current highscore if(highscores.get(last_level_filename)<1||goal_time<highscores.get(last_level_filename)) { highscores.insert(last_level_filename,goal_time); str+="\nNew record!"; } highscores.save(); e->text_finished->SetText(str.c_str()); globals::instance()->game_states.emplace_back(e); } break; } } if(player_->node->GetWorldPosition().y_<level_min_height-10) // die if below level geometry globals::instance()->game_states.emplace_back(new gs_death); }
void gs_main_menu::update(StringHash eventType,VariantMap& eventData) { float timeStep=eventData[Update::P_TIMESTEP].GetFloat(); static double last_second=0; static double last_second_frames=1; static timer this_second; static double this_second_frames=0; this_second_frames++; if(this_second.until_now()>=1) { last_second=this_second.until_now(); last_second_frames=this_second_frames; this_second.reset(); this_second_frames=0; } std::string str="WASD, mouse and shift to move. T to toggle fill mode,\nG to toggle GUI, Tab to toggle mouse mode, Esc to quit.\n"; if(last_second!=0) { str.append(std::to_string(last_second_frames/last_second).substr(0,6)); str.append(" FPS "); } String s(str.c_str(),str.size()); window_text->SetText(s); // Movement speed as world units per second float MOVE_SPEED=10.0f; // Mouse sensitivity as degrees per pixel const float MOUSE_SENSITIVITY=0.1f; // camera movement Input* input=GetSubsystem<Input>(); Node* cameraNode_=globals::instance()->camera->GetNode(); if(input->GetQualifierDown(1)) // 1 is shift, 2 is ctrl, 4 is alt MOVE_SPEED*=10; if(input->GetKeyDown('W')) cameraNode_->Translate(Vector3(0,0, 1)*MOVE_SPEED*timeStep); if(input->GetKeyDown('S')) cameraNode_->Translate(Vector3(0,0,-1)*MOVE_SPEED*timeStep); if(input->GetKeyDown('A')) cameraNode_->Translate(Vector3(-1,0,0)*MOVE_SPEED*timeStep); if(input->GetKeyDown('D')) cameraNode_->Translate(Vector3( 1,0,0)*MOVE_SPEED*timeStep); if(!GetSubsystem<Input>()->IsMouseVisible()) { IntVector2 mouseMove=input->GetMouseMove(); if(mouseMove.x_>-2000000000&&mouseMove.y_>-2000000000) { static float yaw_=84; static float pitch_=64; yaw_+=MOUSE_SENSITIVITY*mouseMove.x_; pitch_+=MOUSE_SENSITIVITY*mouseMove.y_; pitch_=Clamp(pitch_,-90.0f,90.0f); // Reset rotation and set yaw and pitch again cameraNode_->SetDirection(Vector3::FORWARD); cameraNode_->Yaw(yaw_); cameraNode_->Pitch(pitch_); } } }