//----------------------------------------------------------------------------- // Plastic Gems -- Copyright (C) PlasticGames.com, LLC // see readme_plastic.txt in the same folder as this source file for more info // also see readme_tweak.txt for more info on the Tweak system //----------------------------------------------------------------------------- // Plastic Gem #26: Zapper Power Meter // ZAPPER shape - a shape using Plastic Gems thread enhancements (Gem #2 and // optionally Gem #3). datablock StaticShapeData(PlasticZapper) { category = "Plastic"; shapeFile = "~/data/shapes/zapper/zapper.dts"; maxEnergy = 100; // we use this to keep track of battery energy rechargeRate = 0; // we don't recharge at all... }; // ok this object is electric effect for the tesla coil datablock StaticShapeData(PlasticZapperElectrode) { shapeFile = "~/data/shapes/shocker/shocker.dts"; }; function PlasticZapper::onAdd(%this,%obj) { // make zapper 1/4 size... if (%obj.scale $= "1 1 1") %obj.setScale("0.25 0.25 0.25"); // we use thread 0 for the mechanical arm... %obj.playThread(0,"activate"); %obj.stopThread(0); // .. and thread 1 for the power meter gauge %obj.playThread(1,"ifl_gauge"); %obj.pauseThread(1); // call top level method to set energy level & meter at once... %this.setPower(%obj, %this.maxEnergy); // this shows dynamic creation from Gem #20...otherwise you would have to do this: %electrode = new StaticShape() { dataBlock = PlasticZapperElectrode; }; MissionCleanup.add(%electrode); %electrode = PlasticZapperElectrode.createStaticShape(); // mount electrode effect %obj.mountObject(%electrode,1); %obj.electrode = %electrode; %electrode.playThread(0,"electrode"); %electrode.setThreadSpeed(0, 0.5); } // set the power for this zapper...this sets the energy level and then // udpates the power meter IFL to match... function PlasticZapper::setPower(%this,%obj, %power) { %obj.setEnergyLevel(%power); %this.setPowerMeter(%obj, %power); } // set power meter to a given value in range 0...maxEnergy function PlasticZapper::setPowerMeter(%this,%obj, %power) { %max = %this.maxEnergy; // update thread according to energy %obj.setThreadPos(1,1.0-(%power/%max)); } // update the power meter to reflect the current energy function PlasticZapper::updatePowerMeter(%this,%obj) { %energy = %obj.getEnergyLevel(); %this.setPowerMeter(%obj, %energy); } // NOTES: // remember that util methods are a way to call the methods defined in the datablock namspace // as if they were in the namespace of the object itself...they just redirect to the datablock // they are useful but also dangerous in that if you change parameters in your method in the datablock // namspace you must always keep the util methods up to date. Note also that util methods only // expose the "top level" methods we intend to use from script... // UTIL methods on object itself function StaticShape::setPower(%this, %power) { %this.getDataBlock().setPower(%this, %power); }