Commits

jodoherty committed 48a19be

Tweaked the code controlling asteroid breakdown in order to improve the
way it looks and behaves by preventing unnatural jumps.

Comments (0)

Files changed (4)

 using namespace std;
 
 const float Asteroid::kMaxSize = 64.0f;
-const float Asteroid::kMinSize = 4.0f;
+const float Asteroid::kMinSize = 7.0f;
 
 static const GLfloat vertices[] = {
     -1.0f, -0.667f,
     angle = kPI/4;
     angular_velocity = kPI/64.0f/(rand()%3+1);
     if (size > kMaxSize)
-        this->size = kMaxSize;
-    if (size < kMinSize)
-        this->size = kMinSize;
+        size = kMaxSize;
+    else if (size < kMinSize)
+        size = kMinSize;
     disabled = false;
 }
 
     velocity.y = speed * sin(heading);
 }
 
+Asteroid::Asteroid(float x, float y, float size, Point center)
+                  : x(x), y(y), size(size)
+{
+    init();
+    float dy = y-center.y;
+    float dx = x-center.x;
+    float heading = atan(dy/dx);
+    float speed = static_cast<float>(rand()%55)/50.0f * (size/(size+0.5f)) *0.3f;
+    velocity.x = speed * cos(heading);
+    velocity.y = speed * sin(heading);
+}
+
 Asteroid::~Asteroid()
 {
     #ifdef DEBUG
 
 bool Asteroid::createChildren(Asteroid *children[]) 
 {
-    if (disabled || size/2.2f < kMinSize)
+    if (disabled || size/2.0f < kMinSize)
         return false;
-
-    children[0] = new Asteroid(x-size/4.0f, y+size/4.0f, size/2.2f);
-    children[1] = new Asteroid(x-size/4.0f, y-size/4.0f, size/2.2f);
-    children[2] = new Asteroid(x+size/4.0f, y+size/4.0f, size/2.2f);
-    children[3] = new Asteroid(x+size/4.0f, y-size/4.0f, size/2.2f);
+    children[0] = new Asteroid(x-size/2.5f, y, size/2.0f);
+    children[1] = new Asteroid(x, y-size/2.5f, size/2.0f);
+    children[2] = new Asteroid(x+size/2.5f, y, size/2.0f);
+    children[3] = new Asteroid(x, y+size/2.5f, size/2.0f);
     return true;
 }
 
 public:
     Asteroid(float size);
     Asteroid(float x, float y, float size);
+    Asteroid(float x, float y, float size, Point center);
     ~Asteroid();
 
     // Child factory function
                 break;
             }
         }
+        it = list_it;
+        for (it++; it != add_list.end(); it++) {
+            if ((*list_it)->detectCollision(**it)) {
+                erase = true;
+        	break;
+            }
+        }
         if (erase)
             list_it = add_list.erase(list_it);
         else
     // Now add all the objects we created in this update step
     for (list_it = add_list.begin(); list_it != add_list.end(); list_it++) {
         sprites.push_back(*list_it);
-    } 
+    }  
 }
 
 void Game::draw()
     Bullet *bullet = new Bullet(this->x + size*cos(this->direction),
                                 this->y + size*sin(this->direction),
                                 dx, dy);
-    this->dx -= dx*0.03f;
-    this->dy -= dy*0.03f;
+    this->dx -= dx*0.01f;
+    this->dy -= dy*0.01f;
     return bullet;
 }