Game Development Community

Adding Interfaces to PlayerController

by Sean Monahan · in Torque X Platformer Kit · 09/15/2008 (2:02 pm) · 4 replies

I'm trying to add an interface to PlayerController.cs and I've had no luck, so I thought I'd ask here.

My goal is to have an actor that is controlled with an Xbox 360 gamepad. To start, I want the actor to move left and right, jump, and aim in eight directions. Movement will be controlled with the left analog stick, aiming with the right stick and jumping with one of the gamepad triggers.

The problem I'm having is with the eight directions of aiming. I'll give an example to illustrate the issue:

When the actor is facing left he should always been facing left, no matter which direction he is aiming. So he can aim forward, facing left, backward facing left, backward at a 45 degree angle up, facing left, etc. In other words, the actor can be moving to the left, but aiming and shooting backward.

I have a component, AimingComponent.cs, that handles all the aiming just fine. The problem is I cannot determine the direction of the player in AimingComponent. To do so I'd have to set an InputMap for the left stick in AimingComponent, which then overrides the InputMap in PlayerController. This means that when the actor is facing, say, left, pushing up and left correctly makes the actor aim up and left. But when the actor is facing right, pushing up and left makes him aim up and right. So the actor and animations are being correctly flipped, but the control from the right stick is not.

Since I'm already receiving input on my actor via PlayerContoller I'd like to add another variable to PlayerController that tracks the direction the actor faces based on input from the left stick. I would then like to expose this variable through an interface so AimingComponent can make use of it. I've been using this tutorial (http://www.garagegames.com/docs/torqueX/official/content/documentation/Tutorials/Airplane.html#HeaderLink4) as a basis for using interfaces in Torque X. The problem, so far as I can tell, is that PlayerController does not inherit from TorqueComponent. As such it does not have a _RegisterInterfaces method.

Can anyone offer some input on either exposing interfaces in PlayerController or some other method to achieve my goal?

Thanks.

#2
09/16/2008 (8:56 am)
Thanks for the link. I had previously used the TDN article linked above to develop my AimingComponent. It was quite useful for that. The code in that article doesn't take the direction of the player into account so that's where I got stuck.

Last night I did find a way to handle my problem. Instead of exposing an interface on PlayerController I was able to determine the direction of the player with the following code in my AimingComponent:

float velocityX = SceneObject.Physics.VelocityX;

Using this code, any velocityX greater than zero means the player is moving right and, hence, facing right. Any velocityX less than zero means the player is moving left and facing left. If velocityX is zero the player is facing the last known direction. E.g., if velocityX is greater than zero but then becomes zero the player will be facing right.

So I managed to stumble onto a solution on my own.
#3
09/16/2008 (10:19 pm)
That's a clever idea!
#4
09/16/2008 (10:29 pm)
Thanks!