void Player::CheckLaserAsteroidCollisions(std::list<Asteroid*>* asteroids) { std::list<Asteroid*>::iterator a_iter = asteroids->begin(); std::list<Asteroid*> a_tbd; std::list<Laser*> l_tbd; while(a_iter != asteroids->end()) { Asteroid *ast = a_iter._Ptr->_Myval; std::list<Laser*>::iterator l_iter = lasers.begin(); while(l_iter != lasers.end()) { Laser *las = l_iter._Ptr->_Myval; if(ast->GetBSphere().Contains(las->GetPosition())) { //The laser is inside the asteroid bsphere a_tbd.push_back(ast); l_tbd.push_back(las); AddScore(ast->GetBSphere().GetRadius()); } l_iter++; } a_iter++; } //Do the deletions std::list<Laser*>::iterator l_iter = l_tbd.begin(); while(l_iter != l_tbd.end()) { lasers.remove(l_iter._Ptr->_Myval); l_iter++; } a_iter = a_tbd.begin(); while(a_iter != a_tbd.end()) { asteroids->remove(a_iter._Ptr->_Myval); a_iter++; } }
void Player::Update(const float time_step) { if(Keyboard::IsKeyDown((KeyCode)left_key)) { //rotate left rotation += (rot_accel * time_step); } if(Keyboard::IsKeyDown((KeyCode)right_key)) { //rotate right rotation -= (rot_accel * time_step); } if(Keyboard::IsKeyDown((KeyCode)thrust_key)) { //increase thrust thrust += thrust_accel * time_step; } else { thrust = 0; } if(thrust < 0) { thrust = 0; } else if(thrust > MAX_THRUST) { thrust = MAX_THRUST; } if(Keyboard::IsKeyDown((KeyCode)fire_key)) { //fire a laser curr_laser_time -= time_step; if(curr_laser_time < 0) { FireLaser(); curr_laser_time = LASER_RECHARGE_TIME; } } using RustyLib::Matrix44; Matrix44 *rot_mat = Matrix44::CreateRotationZ(rotation); acceleration = (*rot_mat * Vector3::INV_Y_AXIS()); acceleration *= thrust; velocity += acceleration; float curr_vel = velocity.Magnitude(); if(curr_vel > MAX_VELOCITY) { curr_vel = MAX_VELOCITY; } velocity.Normalise(); velocity *= curr_vel; GameObject::Update(time_step); //Do some collision detection b_sphere.SetPosition(position); //First check whether player is on screen still if(! play_region.Contains(b_sphere) ) { //Player needs moving Vector3 min = play_region.GetMin(); Vector3 max = play_region.GetMax(); if(position.x < min.x - b_sphere.GetRadius()) { position.x = max.x + (b_sphere.GetRadius()-1.0f); } if(position.x > max.x + b_sphere.GetRadius()) { position.x = min.x - (b_sphere.GetRadius()+1.0f); } if(position.y < min.y - b_sphere.GetRadius()) { position.y = max.y + (b_sphere.GetRadius()-1.0f); } if(position.y > max.y + b_sphere.GetRadius()) { position.y = min.y - (b_sphere.GetRadius()+1.0f); } b_sphere.SetPosition(position); } std::list<Laser*>::iterator iter = lasers.begin(); std::list<Laser*> tbd;//lasers to be deleted int ct = 0; while(iter != lasers.end()) { Laser* l = iter._Ptr->_Myval; l->Update(time_step); if(!play_region.Contains(l->GetPosition())) { //laser has left screen tbd.push_back(l); } iter++; ct++; } iter = tbd.begin(); while(iter != tbd.end()) { lasers.remove(iter._Ptr->_Myval); iter++; } tbd.clear(); }