Monday 19 October 2009

Messaging is such a bind

For every THelicopter entity is a little bit of code that accepts messages.



Function MessageReceiveCallback(entity:TEntity, Name:String, extra:Object)
Local heloid:String;
heloid = GetEntityKey(entity, "guid");
DebugLog("   MessageReceiveCallback :" + Name + " heloid:" + heloid + " GetEntityKey returns:" + GetEntityKey(entity, "guid"));
Select Name
Case ACTION_MOUNT BindHelicopterToPlayer(heloid);
End Select

End Function

' CALLED WHEN THELICOPTER MESSAGE IS RECIEVED
Function BindHelicopterToPlayer(linkid:String)
Local helo:THelicopter;

helo = THelicopter(game.Helicopters.ValueForKey(linkid));

If helo Then
game.SetPlayMode(PLAYMODE_HELICOPTER);
game.PlayerVehicle = helo.entity;
game.PlayerVehicleBody = helo.body;
EntityParent(game.PlayerVehicleMount, game.PlayerVehicle);
PositionEntity(game.PlayerVehicleMount, Vec3(0));
AlignToVector(game.PlayerVehicleMount, game.PlayerVehicle.Position, 2, 1, 180);


Local crewpos:TEntity = CreatePivot(game.PlayerVehicleMount);
Local newloc:TVec3 = TFormPoint(PLAYER_SLOT_APACHE[CurrentSlot], game.PlayerVehicleMount, Null);
PositionEntity(crewpos, newloc, 1);
EntityParent(game.scene.cam, crewpos, 1);
PositionEntity(game.scene.cam, Vec3(0));
RotateEntity(game.scene.cam, Vec3(0));

EntityParent(game.scene.cam, game.PlayerVehicleMount, 1);
Else
DebugLog("   BindHelicopterToPlayer :" + linkid + " failed. LinkID not found.");
End If
End Function
End Type


A short explanation, when the game sends a message to a helicopter TEntity object to mount (ACTION_MOUNT is defined as the text literal "mount") the BindHelicopterToPlayer(...) function is called which gets information from the game about that particular helicopter and binds the player and game camera to the current active position within the helicopter.

Using messaging has advantages in that it can be scripted externally by LUA sending the message to the helicopter, this a script can bind a player to any helicopter at the start of a mission, or during some scene, or as a result of multiplayer code.

The player has key commands to Unbind from the helicopter which will be locked off at some point in the future. We don't want players jumping out of helicopters mid-flight do we?

We have some logging in there to check what's going on for debugging. Each vehicle type we want the player to control will have a function like this, the main game code knows which vehicle mode is active and calls appropriate control input and vehicle update functions. Right now we have Apache helicopter and ground pounder.

You may notice that the BindHelicopterToPlayer parameter [linkid] is a string. All objects are identified by a texted based key, Apache Longbows use the base key "LONGBOW00xxx" where xxx is a number assigned automatically at object creation. For human interation, tail numbers can be assigned. Callsigns based on rifles and handgun names will be added later for the benefit of grouping helicopters for mission triggers and voice scripting.

No comments:

Post a Comment