Character template: /player_hud.lua — code sample - Stingray Lua API Reference

Character template: /player_hud.lua — code sample

Code

local Util = require 'core/appkit/lua/util'
local SimpleProject= require 'core/appkit/lua/simple_project'
Project.PlayerHud = Project.PlayerHud or {}
local PlayerHud = Project.PlayerHud

local Vector3 = stingray.Vector3
local Vector3Box = stingray.Vector3Box
local Keyboard = stingray.Keyboard

local hud_level = nil

--Array that holds the key IDs and the corresponding HUD data for Scaleform
local input_array = {
    {id = "w", hud_id = "up"},
    {id = "s", hud_id = "down"},
    {id = "a", hud_id = "left"},
    {id = "d", hud_id = "right"}
}

-- Scaleform is an engine plugin and is required for this file.
if scaleform == nil then 
    PlayerHud = { init_hud = function() end,
                  update   = function() end,
                  shutdown = function() end,
                  exit_level = function() end,
                  get_motion_input = function() assert(false) end,
                  check_toggle_camera = function() return false end,
                  check_sprint = function() return false end,
                  check_jump = function() return false end,
                  check_fire = function() return false end,
                  check_aim = function() return false end,
                  check_crouch = function() return false end,
                  set_crosshair_visibility = function() end
                }
    return PlayerHud
end

function PlayerHud:on_custom_event(evt)
    -- Ignore data that is not relevant
    if type(evt.data) ~= "table" then return end
    local action = self.actions[evt.data.action]
    if action then action(evt) end 
end

function PlayerHud:init_hud()
    --Used for mobile hud controls but it simplifies the logic in player if they're always available.
    self.input = {
        pan = Vector3Box(Vector3(0, 0, 0)),
        move = Vector3Box(Vector3(0, 0, 0)),
        jump = false,
        toggle_camera = false,
        sprint = false,
        exit_level = false,
        fire = false,
        aim = false,
        crouch = false
    }

    self.actions = {
        jump = function()
            self.input.jump = true
        end,
        walk = function(evt)
            local move = Vector3(evt.data.x, -evt.data.y, 0)
            self.input.move:store(Vector3.normalize(move))
        end,
        look = function(evt)
            local pan = Vector3(evt.data.x, evt.data.y, 0)
            self.input.pan:store(4 * Vector3.normalize(pan))
        end,
        start_sprint = function()
            self.input.sprint = true
            local move = Vector3(0, 1, 0)
            self.input.move:store(Vector3.normalize(move))
        end,
        stop_sprint = function()
            local move = Vector3(0, 0, 0)
            self.input.move:store(Vector3.normalize(move))
            self.input.sprint = false
        end,
        toggle_camera = function()
            self.input.toggle_camera = not self.input.toggle_camera
        end,
        exit_level = function()
            self.input.exit_level = true
        end,
        fire = function()
            self.input.fire = true
        end,
        aim = function()
            self.input.aim = not self.input.aim
        end,
        toggle_crouch = function()
            self.input.crouch = not self.input.crouch
        end
    }

    --On PC show only the dpad hud.
    if Util.is_pc() then 
        local current_level = SimpleProject.level
        scaleform.Stingray.load_project_and_scene("content/ui/template_hud.s2d/template_hud");
        hud_level = current_level
    end

    --On Touch devices display touch specific hud
    if Util.use_touch() then
        local current_level = SimpleProject.level
        scaleform.Stingray.load_project_and_scene("content/ui/character_mobile_hud.s2d/character_mobile_hud");
        hud_level = current_level

        --Register a custom listener to check for different controls
        local custom_listener = PlayerHud.custom_listener
        custom_listener = scaleform.EventListener.create(custom_listener, function (e) PlayerHud.on_custom_event(PlayerHud,e) end)
        PlayerHud.custom_listener = custom_listener
        scaleform.EventListener.connect(custom_listener, scaleform.EventTypes.Custom)
    end 

    -- On consoles we just show the cross hair
    if Util.is_console() then
        local current_level = SimpleProject.level
        scaleform.Stingray.load_project_and_scene("content/ui/console_hud.s2d/console_hud");
        hud_level = current_level
    end

end

function PlayerHud:shutdown(level)
    if scaleform then
        scaleform.Stingray.unload_project();
        if Util.use_touch() then
            PlayerHud.custom_listener = nil
        end
        hud_level = nil
    end
end

local function handle_key(id, hud_id)
    local event = {
        eventId = scaleform.EventTypes.Custom,
        name = nil,
        data = nil
    }

    if Keyboard.pressed(Keyboard.button_id(id)) then 
        event.name = "keypressed"
        event.data = hud_id
    elseif Keyboard.released(Keyboard.button_id(id)) then 
        event.name = "keyreleased"
        event.data = hud_id
    end

    if event.name and event.data then
        scaleform.Stage.dispatch_event(event)
    end
end

local function update_pc_hud()

    --Iterate through the array of keys
    local array_index = 1
    while input_array[array_index] do 
        local input = input_array[array_index]
        handle_key(input.id, input.hud_id)
        array_index = array_index + 1
    end
end

function PlayerHud:update(dt)
    if Util.is_pc() then
        update_pc_hud()
    end
end

--Implement handlers for touch controls to override appkit input_mapper and project specific controls.
function PlayerHud:get_motion_input()
    local input = self.input
    return {move = input.move:unbox(), pan = input.pan:unbox()}
end

function PlayerHud:check_toggle_camera()
    local ret = self.input.toggle_camera
    self.input.toggle_camera = false
    return ret
end

function PlayerHud:check_sprint()
    return self.input.sprint
end

function PlayerHud:check_jump()
    local ret = self.input.jump
    self.input.jump = false
    return ret
end

function PlayerHud:exit_level()
    local ret = self.input.exit_level
    self.input.exit_level = false
    return ret
end

function PlayerHud:check_fire()
    local ret = self.input.fire
    self.input.fire = false
    return ret
end

function PlayerHud:check_aim()
    return self.input.aim
end

function PlayerHud:check_crouch()
    return self.input.crouch
end

--Function for setting crosshair visibility
function PlayerHud:set_crosshair_visibility(is_visible)
    local event = {
        eventId = scaleform.EventTypes.Custom,
        name = "crosshair_handle",
        data = is_visible
    }
    if event.name then
        scaleform.Stage.dispatch_event(event)
    end
end

return PlayerHud