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.
  



No comments:

Post a Comment