MGT Project DevLog #4

Game Items

There’s no adventure game without powerups, keys and doors. For this prupose, I needed a generic class that handles the basic operations on items :
– the item knows when it interacts with the player and calls the proper method/function.
– the item can be picked, destroyed, unlocked, and this state should remain if the player left the room.
– the item can increase the vitals/energy/shield of the player, or appear in the game inventory

To perform these actions, and any other kind of possible future needs, I wrote a very simple class called ‘GameItem


//--------------
class GameItem
//--------------
{

item_trigger = 0
inside_trigger = false

enabled = true

project_script = 0

tank = 0
tank_script = 0

function OnUpdate(item)
{
if (!enabled)
return

if (tank == 0)
{
tank = SceneFindItem(ItemGetScene(item), “tank”)
tank_script = ItemGetScriptInstanceFromClass(tank, “SteamTank”)
}
else
{
// Test the player against the trigger
if (item_trigger != 0)
{
if (TriggerTestItem(item_trigger, tank))
{
if (!inside_trigger)
{
print(“GameItem::OnUpdate() tank inside”)
ItemTaken(item)
inside_trigger = true
}
}
else
inside_trigger = false
}
}
}

function ItemTaken(item)
{
print(“GameItem::ItemTaken() – Stub method called.”)
}

function OnSetup(item)
{
print(“GameItem::OnSetup(” + ItemGetName(item) + “)”)

local _item_trigger
_item_trigger = ItemGetChild(item, “item_trigger”)
if (ObjectIsValid(_item_trigger))
item_trigger = ItemCastToTrigger(_item_trigger)

project_script = ProjectGetScriptInstance(g_project)
enabled = project_script.GetItemStateEnabled(item)

if (enabled)
Enable(item)
else
Disable(item)
}

function Enable(item)
{
print(“GameItem::Enable(” + ItemGetName(item) + “)”)

ItemActivateHierarchy(item, true)
enabled = true
project_script.SetItemStateEnabled(item, true)
}

function Disable(item)
{
print(“GameItem::Disable(” + ItemGetName(item) + “)”)

ItemActivateHierarchy(item, false)
enabled = false
project_script.SetItemStateEnabled(item, false)
}
}

Basically, when setup, the class will look for a trigger parented to the main item. Then, during the update loop, the class will test the player against the trigger. If the item is triggered by the player, a stub method ItemTaken() is called.

Based on this generic class, I can derive (or extend) any kind of specific class, and overload the ItemTaken() method.

The class also provide two methods to enable and disable the item and its children. This is very handy when it comes to hide several items and lights. When disabled, the state of the item will be stored by the project, so that the item will know if it is enabled or not in case the player exits then enters the room again. Here, all the state logic is handled by two accessors methods I wrote directly in the project script : SetState() and GetState()

Here’s an example of a typical ‘power up’ class inherited from the base GameItem class. You will note how the ItemTaken() class is overloaded in order to perform the proper action : here it increases the shield level of the player.


class ShieldCellItem extends GameItem
{

function OnUpdate(item)
{
base.OnUpdate(item)
}

function ItemTaken(item)
{
print(“ShieldCellItem::ItemTaken()”)
tank_script.ShieldUp()
Disable(item)
}

function OnSetup(item)
{
base.OnSetup(item)
}

}

 

The Inventory

Some items can be picked and must be kept by the player. In this purpose, they will invoke another class called Inventory. At the moment the implementation of this class is really basic. It is more or less a few accessors to handle an array that contains the game items picked by the player.


class Inventory
{
inventory = 0

constructor()
{
inventory = []
}

function AddItem(item_name)
{
inventory.append(item_name)
}

function RemoveItem(item_name)
{
print(“Inventory::RemoveItem(” + item_name + “)”)

local idx, val
foreach(idx,val in inventory)
if (val == item_name)
{
inventory.remove(idx)
return true
}

return false
}

function FindItem(item_name)
{
local i
foreach(i in inventory)
if (i == item_name)
return true

return false
}
}

 

Video Capture

The following video shows the player that grabs several items (a powerup and a key), opens a first door, unlock a second door with the key he just grabbed. The key is marked as a X in the inventory, you can see it appear, the disappear when used.

Leave a Reply

Your email address will not be published. Required fields are marked *