Thursday, May 1, 2014

Falling Energy

In order to get back spell points, you can set your wand to "catcher" mode, and catch the falling purple triangles.

The triangles are set up as an class array, meaning 500 different triangles are created.

//class Energy

  color c =  color(255,0,255);
  float x1 = random(width);
  float y1 = -30;
  float x2 = x1 + 6;
  float y2 = -30;
  float x3 = x1 + 3;
  float y3 = -25;
  float f;
  float r = 3;                      //radius? Needed for intersect calculation.
  float shiftF = 24;
  float speed = 4;

The triangle's point 1 is set at a random x location - the triangle drawing is then computed from the x1 point. The 'radius' is used for the purposes of determining if the triangle was caught.

The triangles fall down in a straight line, simply by adding the speed to the 3 'y' locations.


  void move()
  {
     y1 += speed;
     y2 += speed;
     y3 += speed; 
  }

The function energyFall() will move the triangles down, and then check to see whether the magic intersects. Note that the intersection is ignored unless the wand is set to catch mode.


  for (int i = 0; i < e; i++)
  {
   energy[i].move();
  energy[i].fade();
  energy[i].display(); 
  if ((wand.intersect(energy[i])) && (type == 7))
   {
     energy[i].caught();
  }


To determine whether the player caught a triangle, we check with a function in the Wand class. The wandX value moves the catch point to the direction the wand is pointing:


  boolean intersect(Energy e)
  {
   float distance = dist(x-wandX,y,e.x2,e.y2);
   if (distance <= r - e.r)
   {
     return true;
   }
   else
   {
    return false; 
   }
   

Finally, if the triangle is in fact caught, spell points are awarded to the player and the triangle location resets:


  void caught()
  {
    y1 = -1005;
    y2 = -1005;
    y3 = -1000;
    userSound.rewind();
    userSound.play();


    if (sp < maxSP)
    {
        sp += 5;
        if (sp > maxSP)      //Under the current rules, there's no way to obtain an SP value between maxSP and maxSP-5, 
                            //but I'm adding this in the event I want to change this later.
        {
         sp = 100; 
        }
    }
  }

    



No comments:

Post a Comment