Game Development Community

Stun Player

by John Eckhardt · 03/06/2009 (12:10 pm) · 7 comments

If you want to completely stop the player from moving, you could remove the key bindings so they do nothing. However that doesn't work in all cases. A better way is to modify the engine so that the players are guaranteed to not be able to move.

Developed in TGE 1.5.2

The pros:
-Can still use the keyboard for other functions (e.g. console, exit game, etc.)

The cons:
-Can still use the keyboard for other functions (e.g. casting a spell)

First, in shapeBase.h

class ShapeBase : public GameBase
{
   typedef GameBase Parent;
   friend class ShapeBaseConvex;
   friend class ShapeBaseImageData;
   friend void physicalZoneFind(SceneObject*, void *);

public:
		[b]bool mStunned;   //are we stunned or NOT?[/b]

...

	  ServerIdMask    = Parent::NextFreeMask << 8,
[b]	  StunMask        = Parent::NextFreeMask << 9,
	  SoundMaskN      = Parent::NextFreeMask << 10,       ///< Extends + MaxSoundThreads bits
[/b]

...

   /// Returns damage amount.
   S32  getDamageLevel()  { return mDamage; }

[b]   //the stun structure
   void setStunState(bool stunned);
   bool isStunned() { return mStunned; }[/b]

Then, over in shapeBase.cc:

In the constructor (ShapeBase::ShapeBase()):
[b]mStunned = false;[/b]

After function void ShapeBase::onCollision(ShapeBase* object,VectorF vec) add this function:
//1 is stunned, 0 is not.
void ShapeBase::setStunState(bool stunned)
{
   if (stunned == mStunned)
      return;
   setMaskBits(StunMask);

   mStunned = stunned;
}

In ShapeBase::packUpdate:
Change this line:

if(!stream->writeFlag(mask & (NameMask | DamageMask | SoundMask |
         ThreadMask | ImageMask | CloakMask | MountedMask | InvincibleMask |
		 ShieldMask | SkinMask | ServerIdMask)))
      return retMask;


to this:

if(!stream->writeFlag(mask & (NameMask | DamageMask | SoundMask |
         ThreadMask | ImageMask | CloakMask | MountedMask | InvincibleMask |
		 ShieldMask | SkinMask | ServerIdMask [b]| StunMask[/b])))
      return retMask;

Right below that, add this:

[b]if (stream->writeFlag(mask & StunMask)) {
	   stream->writeFlag(mStunned);
}[/b]

In ShapeBase::unpackUpdate, add these lines:
if(!stream->readFlag())
         return;

[b]	if (stream->readFlag()) {
		setStunState(stream->readFlag());
        }[/b]

Then down by all the other console functions, add these two functions:
ConsoleMethod( ShapeBase, setStun, void, 3, 3, "(bool isStunned)")
{
   object->setStunState(dAtob(argv[2]));
}

ConsoleMethod( ShapeBase, isStunned, bool, 2, 2, "")
{
	return object->isStunned();
}

Now, we have successfully added a stunned variable to the shapebase class. Good. But it doesn't do anything yet. Go to player.cc.

In Player::updateMove, change these lines:
// Trigger images
   if (mDamageState == Enabled [b]&& mStunned == false[/b]) {
      setImageTriggerState(0,move->trigger[0]);
      setImageSecondTriggerState(0,move->trigger[1]);
[b]   } else if (mDamageState == Enabled && mStunned == true) {
      setImageTriggerState(0,0);
      setImageSecondTriggerState(0,0);[/b]
   }

...

   // Update current orientation
   if (mDamageState == Enabled [b]&& mStunned == false[/b]) {

...

   // Desired move direction & speed
   VectorF moveVec;
   F32 moveSpeed;
   if (mState == MoveState && mDamageState == Enabled [b]&& mStunned == false[/b])

What these last three changes will do is ignore inputs to Move, Rotate the camera (aim) and Shoot when the player is stunned. Build it and you should be set.

To stun a player, call %player.setStunState(1); and to unstun, call %player.setStunState(0);



#1
03/06/2009 (2:30 pm)
pretty cool. I would have never been ablke to figure out this lol.
#2
03/06/2009 (2:42 pm)
Has this been tested on TGEA 1.8.1?
#3
03/06/2009 (2:47 pm)
I did this with TGE 1.5.2 I would assume that TGEA hasn't changed their representations of ShapeBase and Player objects. I don't have TGEA, so I didn't test it there. Let me know if you try it out.
#4
03/06/2009 (2:55 pm)
@Tek0
It looks like it should drop into TGEA 1.8.1 no prob.
I'll be testing tonight.

@John
Elegant, thank you.
I've been using a "stun" camera and it was kind of ugly. :p
#5
03/06/2009 (3:34 pm)
@BrokeAss,
Thanks please let me know what happens.
#6
03/09/2009 (4:58 pm)
Personally I'd prefer it to be adjustable, i.e. instead of (or in addition to) just a boolean "on-off", there should be a "Stunnedness" factor that could then be used to adjust player movement speed or other abilities/settings; e.g. "float mStunFactor", where "0.0" = none, and "1.0" = completely incapacitated.

Edit: Oh, and you need to use Markup Lite for formatting; e.g. "[ b]" instead of "<b>" etc.
#7
08/22/2009 (6:51 pm)
sure @Kevin, i want to see these to learn... ;D
so when you submit these changes?