Creating GamePlay Objects

A GamePlay object implements the IGamePlay interface but also provides one or more setter methods. The existence and names of the setter methods in the GamePlay object indicates to the kheljs runtime which actors are involved in the interaction. The kheljs runtime will inject the actors at runtime before calling isTrigerred() or performAction() methods. For example, to create an interaction between a player and any inventory item, create an object of the following class and register to the runtime from the game level or from a manager class.

XyzInteraction implements IGamePlay {
setPlayer( player: Player) { ... }
setInventoryItem( item: InventoryItem ) { ... }
isTriggered(): boolean{ ... }outou
performAction() { ... }

If the interaction between the player and an inventory item involves the player's inventory as well, that should be injected too as follows:

XyzInteraction implements IGamePlay {
setPlayer( player: Player) { ... }
setPlayerInventory( playerInventory: Inventory) { ... }
setInventoryItem( item: InventoryItem) { ... }
isTriggered(): boolean{ ... }
performAction() { ... }

Once the GamePlay object is registered with the khlejs runtime, the isTriggered() method will be invoked for each inventory item, player inventory and and player combination.

Example - Player picking up an inventory item

The game level generated by kheljs includes a few implementations of IGamePlay interface to demonstrate how interactions in the game can be created using such objects. Here is one such GamePlay object that picks up an inventory item for the player when player walks over the item.

Note how the isTriggered() method checks whether the player already acquired the inventory item or whether it is currently visible. Only then it checks for an intersection between player and inventory item's mesh. The performAction() method simply moves the inventory item from level inventory to players inventory and also sets the acquiredByPlayer flag on the item.

export class PlayerPicksInventoryItem implements GamePlay {
private player: Player;
private item: InventoryItem;
private playerInventory: Inventory;
private levelInventory: Inventory;
setPlayer( player: Player ){
this.player = player;
setInventoryItem( item: InventoryItem ){
this.item = item;
setPlayerInventory( inventory: Inventory ){
this.playerInventory = inventory;
setLevelInventory( inventory: Inventory ){
this.levelInventory = inventory;
isTriggered(): boolean {
if ( this.item.isAcquiredByPlayer() ) return false;
if ( !this.item.isVisible() ) return false;
return this.item.intersects( this.player.playerMesh );
performAction(): void {
if ( this.playerInventory.canItemBeAdded(this.item )){
// Unhighlight and hide the item
// Move item to player's inventory and make active
this.levelInventory.removeItem( this.item );
this.playerInventory.addItem( this.item );
// set as acquired by the player