First up is the CompGameType. This is a simple component with only one variable, a bool called GameStarted:
Code: Select all
TypeSys::VarT<bool> m_GameStarted; /// Has the game started? Returns True or False.
Code: Select all
dofile("Games/Duel/Scripts/FileLoader.lua")
Code: Select all
Utility = {}
function Utility:GetTime()
return os.time()
end
Now we construct the script version of the component:
Code: Select all
local GameType = ... -- Retrieve the ComponentScriptT instance that is responsible for this script.
local Entity = GameType:GetEntity()
local GameState = Entity:GetComponent("GameType")
We retrieve the entity we are attached to. This is the map placed entity with the GameType component added to it.
We retrieve the GameType component and name it GameState. This can be whatever you want, but this is the 'class' you will call to get() and set() the component member variables.
Next up are the variables:
Code: Select all
-- GameType Variables
GameType.kGameStartTime = nil
GameType.kGameLoadTime = Utility.GetTime()
GameType.kGameStartDelay = 15
kGameLoadTime is a time value that is being recorded from the moment the entity loads the game.
kGameStartDelay is the time in secs we are going to delay before changing the GameStarted component variable from false to true, so the game can start.
Code: Select all
-- Returns a boolean value to indicate if game has started
function GameType:GetGameState()
return GameState:get("GameStarted")
end
The next section of code is so well commented I don't need to explain any further...
Code: Select all
-- Sets the bool value of GameStarted.
function GameType:SetGameState(state)
-- if the state sent doesn't match the current game state
if state ~= GameType.GetGameState() then
-- set the current game state to the new state
GameState:set("GameStarted", state)
-- if game has changed to started
if GameType:GetGameState() then
-- set the game start time
GameType.kGameStartTime = Utility.GetTime()
self:ResetGame()
end
end
end
Code: Select all
-- Run the update logic for the entity from Think()
function GameType:Think(FrameTime)
-- if the game isn't running yet
if not self:GetGameState() then
-- if current time is after game start delay
if Utility.GetTime() > (self.kGameLoadTime + self.kGameStartDelay) then
-- start the game
self:SetGameState(true)
-- if the game has started
if self:GetGameState() then
-- post the message in console
Console.Print("GameType:Think(), Game has started\n")
end
end
end
end
Code: Select all
-- Reset player scores, entities and respawn players
function GameType:ResetGame()
end