Sunday, May 4, 2014

Things That Didn't Work

With any project, there are ideas that I was not able to get to work in the actual programming. These are the things I have been unable to do so far:

1. Perfectly sync the magic - sometimes magic will appear in random places when casting the spell, from the last magic spell.

2. Victory/Defeat game music - I haven't yet been able to 'stop' music (the .stop() or .close() functions haven't worked for me) in order to have a separate theme for the victory and defeat screen.

Thursday, May 1, 2014

Sword

The sword works differently from the magic spells. Press 's' on the key to use the sword. When using the sword, the wand is redrawn to look like a sword.

  void display()
  {
    
   if (type == 5)
   {
     stroke(200);
    strokeWeight(10);
    line(x,y+20,x+swing,y-50);
    if (edge == 0)
    {
      line(x-10,y,x+10,y);
    }
    else if (edge == 1)
    {
      line(x-30,y+10,x-10,y-10);
    }
    else
    {
      line(x+30,y+10,x+10,y-10);
    }
   

   }

Unlike the wand, the sword can move across the entire screen, as no code is called to move it. To use the sword, you simply click on the mouse. 


void mousePressed()
{  
  if (!cast)
  {
    if (type == 5)
    {
         cast = !cast;
         
        if (level == 1)
       {     
        if (goblin.swing())
        {
          goblin.impact(); 
        }
       }
       
       else if (level == 2)
       {
        if (snake.swing())
        {
          snake.impact(); 
        }
         
       }
       else if (level == 3)
       {
        if (wizard.swing())
        {
          wizard.impact(); 
        }
         
       }
      else if (level == 4)
       {
        if (dragon.swing())
        {
          dragon.impact(); 
        }
         
       }
    }


If the mouse (sword) is over the creature, it calls the impact function.


  boolean swing()
  {
    if ((mouseX >= x - 50) && (mouseX <= x + 50) && (mouseY >= y - 50) && (mouseY <= y + 50))
      {
        return true;
      }
      else
      {
        return false;
      }
      
  }


 else if (type == 5)
    {
       userSound.play();
      parry = random(1,10);
      if (parry < parryChance)
      {
        hit = random(1,5) * armor;
        damage = round(hit);
        creatureHP -= damage;
      }
      else
      {
       if (!parryText)
      {
       parryText = !parryText;
      if (low)
      {
       low = !low; 
      }
      if (missText)
      {
       missText = !missText; 
      }
      if (attaText)
      {
        attaText = !attaText;
      }
       if (magiText)
      {
       magiText = !magiText; 
      }
      } 
        
      }
    }

When a creature is hit by the sword, it can parry the blow (the higher the level, the greater the chance of parry is). It then deals the appropriate amount of damage to the creature.


  void swing()
  {
    if (cast)
    {
      if (random(-1,1) < 0)
      {
        swing -= 70;
        edge = 1;

      }
      else
      {
        swing += 70;
        edge = 2;

      }
      cast = !cast;
    }
    else
    {
     swing = 0; 
     edge = 0;
    }
  }

Finally, the sword has a 50/50 chance of 'swinging' in either direction when used.
  



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; 
        }
    }
  }