Game Development Community

Variable cloak level

by Quest Johnny · in Torque Game Engine · 10/08/2005 (11:53 am) · 2 replies

Hello folks,

This is a fix for allowing you to set an object's cloak level to anything oyu want from 0.0 to 1.0 in the datablock.

This isn't a complicated change, and in fact, somebody may already have posted it, but I didn't see one. I just take every opportunity I can to give back to Torque and the community. I have tested this once or twice. It works with a cloakTexture or not. Use at your own risk, but if you find fixes or bugs, do please share them and maybe we can fix them.

Here are the details of the change. Excuse my idiosyncratic commenting around the code I add in to the engine, but I am paranoid. The code i have added has //WOOFY //ENDWOOFY around it in each case, the other code is just to help you find where I added it.

In shapeBase.h:
find...

   StringTableEntry  shapeName;
   StringTableEntry  cloakTexName;
   //WOOFY
   F32 maxCloak;	//added by woofy, the maximum level you can cloak to
   //ENDWOOFY

In shapebase.cc:
void ShapeBaseData::initPersistFields()
{
   Parent::initPersistFields();

   addGroup("Render");
   addField("shapeFile",      TypeFilename, Offset(shapeName,      ShapeBaseData));
   addField("cloakTexture",   TypeFilename, Offset(cloakTexName,      ShapeBaseData));
   //WOOFY
   addField("maxCloak",		  TypeF32,      Offset(maxCloak,         ShapeBaseData));
   //ENDWOOFY
   ....
 
ShapeBaseData::ShapeBaseData()
{
   shapeName = "";
   cloakTexName = "";
   //WOOFY
   maxCloak = 1.0;	//assumes full cloak - is this value correct?
   //ENDWOOFY
   mass = 1;
   ....

void ShapeBaseData::packData(BitStream* stream)
{
   Parent::packData(stream);

   if(stream->writeFlag(computeCRC))
      stream->write(mCRC);

   stream->writeString(shapeName);
   stream->writeString(cloakTexName);
   if(stream->writeFlag(mass != gShapeBaseDataProto.mass))
      stream->write(mass);
   //WOOFY has to be in exact same place in both be careful - right after mass! I love sayin mass!
   if(stream->writeFlag(maxCloak != gShapeBaseDataProto.maxCloak))
      stream->write(maxCloak);
   //ENDWOOFY
   ....
 
void ShapeBaseData::unpackData(BitStream* stream)
{
   Parent::unpackData(stream);
   computeCRC = stream->readFlag();
   if(computeCRC)
      stream->read(&mCRC);

   shapeName = stream->readSTString();
   cloakTexName = stream->readSTString();

   if(stream->readFlag())
      stream->read(&mass);
   else
      mass = gShapeBaseDataProto.mass;

   //WOOFY - right after mass
   if(stream->readFlag())
      stream->read(&maxCloak);
   else
      maxCloak = gShapeBaseDataProto.maxCloak;
   //ENDWOOFY
   .....

void ShapeBase::advanceTime(F32 dt)
{
   // On the client, the shape threads and images are
   // advanced at framerate.
   advanceThreads(dt);
   updateAudioPos();
   for (int i = 0; i < MaxMountedImages; i++)
      if (mMountedImageList[i].dataBlock)
         updateImageAnimation(i,dt);

   //WOOFY
   F32 maxCloak = 1.0;
   //first determine maxCloak
   if(mDataBlock) { maxCloak = mDataBlock->maxCloak; }
   if(maxCloak>1.0) maxCloak = 1.0;
   if(maxCloak<0.0) maxCloak = 0.0;
   //basic sets
   //ENDWOOFY

   //This section has also been edited, see comments for where previous lines were
   // Cloaking takes 0.5 seconds
   if (mCloaked && mCloakLevel != maxCloak) {	//changed from 1.0
      mCloakLevel += dt * 2;
      //if (mCloakLevel >= 1.0)
	  if (mCloakLevel >= maxCloak) {
         //mCloakLevel = 1.0;
         Con::printf("hit maxCLoak %f", maxCloak);
		 mCloakLevel = maxCloak;
	  }//endif
	  //ENDWOOFY
   } else if (!mCloaked && mCloakLevel != 0.0) {
      mCloakLevel -= dt * 2;
      if (mCloakLevel <= 0.0)
         mCloakLevel = 0.0;
   }

In script you only have to add

maxCloak = 0.5

#1
10/08/2005 (12:16 pm)
Additionally, I added this change which causes the "cloak" texture to only change when the chr is in motion, i.e., like "predator" - the texture constantly swirling seemed to draw attention to the cloaked figure, which kinda defeats the purpose of cloak.

Anyway it's a simple change, you will find this in Player::renderImage()

//WOOFY it strikes me that, this is just incrementing on every render by 1, to shift
 //the texture slightly.. now how hard would it be to only shift it on MOVEMENT?
	     F32 motion = mVelocity.len();

		 if(motion>0.2) {
			shiftX = (shiftX + 1) % 128;
			shiftY = (shiftY + 1) % 127;
		 }//endif
		 //ENDWOOFY

ps naturally from these it would be child's play to reduce your alpha (ie., become more visible) when in motion, tho I'm not going to make that change here.
#2
02/13/2006 (4:57 am)
I added this and now mounted objects wont cloak unless i set maxCloak to 1