Appkit: /player_util.lua — code sample - Stingray Lua API Reference

Appkit: /player_util.lua — code sample

Code

--[[

Utility class used by the Templates to create a player object that can fly
around the scene.

]]--

Appkit.PlayerUtil = Appkit.PlayerUtil or {}
local PlayerUtil = Appkit.PlayerUtil

local UnitController = require 'core/appkit/lua/unit_controller'
local CameraWrapper = require 'core/appkit/lua/camera_wrapper'
local ComponentManager = require 'core/appkit/lua/component_manager'

local Vector3 = stingray.Vector3

local free_cam_move_speed = stingray.Vector3Box(Vector3(3.8,3.8,3.8))
local free_cam_sprint_speed = stingray.Vector3Box(Vector3(9,9,8))
local free_cam_yaw_speed = 0.085
local free_cam_pitch_speed = 0.075

-- A "Free Cam" is a player with no model that has basic input functionality to
-- fly around and view the world. This is the basic player mode used for
-- Test Levels and empty projects. This utility function creates this basic player.
function PlayerUtil.spawn_free_cam_player(level, camera_unit, camera_index, position, rotation, input_mapper, is_wwise_listener_enabled)
    -- A container for the freecam objects
    local free_cam_player = {}
    free_cam_player.unit = camera_unit

    -- Camera
    local camera = CameraWrapper(free_cam_player, camera_unit, camera_index)
    camera:set_local_position(position)
    camera:set_local_rotation(rotation)
    camera:set_wwise_listener_enabled(is_wwise_listener_enabled)
    camera:enable()

    -- Add camera input movement. Starts enabled.
    local controller = UnitController(free_cam_player, camera_unit, input_mapper)
    controller:set_move_speed(free_cam_move_speed:unbox())
    controller:set_yaw_speed(free_cam_yaw_speed)
    controller:set_pitch_speed(free_cam_pitch_speed)

    return free_cam_player
end

function PlayerUtil.despawn_freecam(free_cam_player)
    if free_cam_player then
        ComponentManager.remove_components(free_cam_player)
        free_cam_player.unit = nil
    end
end