Game Development Community

dev|Pro Game Development Curriculum

Plastic Gem #28: Zapper Mechanical Arm

by Paul Dana · 07/17/2008 (7:34 pm) · 0 comments

Download Code File


i936.photobucket.com/albums/ad202/vincismurf/banner.jpg


Plastic Gem # 28: Zapper Mechanical Arm

Difficulty: Easy

Before following this gem, you must follow the instructions in Gem #20, Gem #25 and #26, and #27, which require Gem #2, Gem #25, and optionally Gem #3 if you want ease, which you do.

www.plasticgames.com/dev/blog_images/droids_zapper.gifThe Zapper has a mechanical arm that swings down when you get close

Hi Paul Dana from Plastic Games again. Building on the previous gem, this gem will add mechanical arm behavior to the PlasticZapper defined in the previous few gems.

1) Unzipping the files

Remember you must first follow the instructions in Gem #20, Gem #25, Gem #26, Gem #27, Gem #2, Gem #25 (and optionally Gem #3). This will give you the thread upgrade and the zapper shape itself and the first two versions of the zapper.cs script file. Next unzip the pg28_zapperMechanicalArm.zip file that comes with this gem. In there you will find a script file called zapper.cs that defines the PlasticZapper class with this gem's updates.

2) Updating the script

Copy the zapper.cs file from this gem's zip file over the zapper.cs you created in the previous two gems. This version has all the stuff from those two versions, plus new stuff to make the mechanical arm swing down when you walk near and swing back when you walk away.

3) Testing it out

Just run the mission you made in the previous gem. When you get near the zapper the arm should swing down and when you walk away it should swing back.

4) Looking at the code

In the fan example from Gem #18, I just gave you a dump of ALL the code to make the fan work without describing how it all actaully worked. For the PlasticZapper object I am building up the functionality a bit at a time and try to explain a bit more what is going on as well. Let's take a look at what has changed in the code in zapper.cs.

OK in this gem we use a managed trigger like we did for the fan in Gem #18 . We need to define a datablock for that managed trigger:

// our managed trigger is about as big as the zapper pad (when scaled to 1/4 size as is the default behavior...)
datablock TriggerData(PlasticZapperTrigger : ManagedTriggerBase)
{
   tickPeriodMS  = 96; // 96 ms is approx 1/10 sec
   
   // a relatively small box same size as landing pad...
   triggerSize = "7.5 7 8";
   
   // box center a little off-center from middle of bottom of box
   polyhedronCenter = "-0.8 0.5 0.0";
};

We set the tickPeriodMS so this trigger "thinks" about once every 1/10th of a sec whenver there are players inside it. We set the triggerSize and polyhedronCenter to align the trigger where we want it.

We tell the TriggerManager that the PlasticZapper needs a managed trigger with this code added to the top of the ::onAdd() method:

// we need a managed trigger...
  TriggerManager.manageTrigger(%obj, PlasticZapperTrigger);

Finally we need some code to make the arm move when when a player enters or leaves the trigger area. But wait it's really a bit more subtle than that.

5) Oh crap, subtle

The idea of the zapper is that is refuels a player's energy. So yes it is true the arm should only come down when players are in the trigger and it should go back when players are not in the trigger area. But it is more than that. The arm shouldn't come down unless there is player in the trigger area that needs energy. Furthermore the arm should move back and stop refuling any players once the energy in the zapper itself has been depleted.

Sooooooo..the code provided here is structured to accomplish the desired goal. This means that some of the methods defined are rather pointless right now, but will be used in the next gem when we bring energy into the picture.

The final detail is notice the line of code that is commented out in the ::onTriggerTick() method and replaed with the line: if (true). In this gem we want to have it simply come down when we enter and go back when we leave. Next gem will uncomment that line.

Here is the final code added we have been talking about:

// TRIGGER ZONE stuff...

// trigger zone thinking...
function PlasticZapper::onTriggerEnter(%this,%obj,%trigger,%enterObj)
{
   echo("PlasticZapper::onTriggerEnter()");

   %enterObj.insideLandingZone = true;
   %obj.readyToRefuel = false;
   %obj.armActivated = false;
}

function PlasticZapper::onTriggerLeave(%this,%obj,%trigger,%enterObj)
{
   echo("PlasticZapper::onTriggerLeave()");

   %enterObj.insideLandingZone = false;
   %this.stopRefueling(%obj,%enterObj);
}

function PlasticZapper::onTriggerTick(%this, %obj, %trigger)
{
   echo("PlasticZapper::onTriggerTick()");

   // get the object in side
   for (%i=0; %i < %trigger.getNumObjects(); %i ++)
   {
       %enterObj = %trigger.getObject(%i);

      // dont do squat if we are full of energy or the zapper is empty...
      //if (%obj.getEnergyLevel() > 0 && %enterObj.getEnergyPercent() < 1.0)
      if (true)
      {
          // if we have not done so...activate
          if (!%obj.armActivated)
          {
             %obj.armActivated = true;
             // play the activate animation and que up the rezapping
             %obj.playThread(0);
             %obj.setThreadDir(0,true);
             %obj.activateThread = %this.schedule(2000,"activateFinished",%obj);
           }
      }
      else
      {
          %this.stopRefueling(%obj,%enterObj);
      }
   }
}

function PlasticZapper::activateFinished(%this,%obj)
{
  // just mark that its OK to refuel now
  error("PlasticZapper::activateFinished()");
  %obj.readyToRefuel = true;
  %obj.activeThread = 0;
}

function PlasticZapper::stopRefueling(%this,%obj,%enterObj)
{
    // if we are out of energy then turn off the electrode effect
    if (%obj.getEnergyLevel() <= 0)
    {
        if (%obj.electrode)
        {
          %obj.unmountObject(1);
          %obj.electrode.delete();
          %obj.electrode = 0;
        }
    }
  
    %obj.readyToRefuel = false;

    if (%obj.armActivated)
    {
      if (%obj.activeThread)
         cancel(%obj.activeThread);
      %obj.activeThread = 0;
      %obj.armActivated = false;
      %obj.setThreadDir(0,false);
    }
}

The Next Gem

That's all for this gem. Tomorrow's gem will show how to make a lightning zapper effect appear from the tip of the mechanical arm to the player after the arm swings down. Also the player will really be refueled with energy and the zapper will really by drained of energy. Also we will alter the zapper so the arm will only swing down and recharges players who need energy.