Survival Cart

--[[Cart class
Cart will regenerate/give players various stats based on what they have 'unlocked' in the 
skill tree.
ammo -      how much ammo players regenerate
damage - how much bonus damage players gain
fire -     how fast players' guns fire
health - how much health players regenerate
radius - cart's area of effect radius (2.5 meters to start)
shield - how much shielding players regenerate
speed -  how fast the cart moves
--]]

-- create global for other scripts to access base cart variables
_G.Cart = {_ammoRate = 2,_damageRate = 0,_fireRate = 0,_healRate = 2,_radiusRate = 775,_shieldRate = 0,_speedRate = 420, Manticoin = 0,_atStop = 1,_direction = nil, _distance = nil, _travelTime = 10, _restTime = 0, _stops = 103, _rotations = 0}

local cart = script.parent
local _midPoint = World.FindObjectByName("MidPoint")
_G._resource1 = "_onCart" --player on cart?
_G._resource2 = "_atStation" -- player at station?
_G._resource3 = "Manticoin" -- player's coins
_G._resource4 = "rounds" --ammo
Task.Wait(4) -- waits 4 seconds

local test = "apples"
test =  test.." bananas"
test = test.." cherries grapes"
--print ("Test: ",test)


-- give each joining player a gun
local EQUIPMENT_TEMPLATE = script:GetCustomProperty("Equipment Template")

function OnPlayerJoined(player)
    local equipment = World.SpawnAsset(EQUIPMENT_TEMPLATE)
    equipment:Equip(player)
end

Game.playerJoinedEvent:Connect(OnPlayerJoined)

-- giving all players 3 coins, plus the cart
local allPlayers = Game.GetPlayers()
for _, player in ipairs(allPlayers) do
    player:SetResource(_G._resource3, 3)
end
_G.Cart["Manticoin"] = _G.Cart["Manticoin"] + 3


--When player is NOT on cart (do damage over time) and do Healing when aboard
local ONCART_TRIGGER = script:GetCustomProperty("OnCartTrigger"):WaitForObject()

-- nil OnBeginOverlap(Trigger, Object)
-- A player is in the cart; they entered the trigger
function OnCartDetect(trigger, other)
    if other:IsA("Player") then
        other:SetResource(_G._resource1,1) -- gives this player a variable (_onCart) and sets it to 1
    end
end
function OffCartDetect(trigger, other)
    if other:IsA("Player") then
        other:SetResource(_G._resource1,0) -- gives this player a variable (_onCart) and sets it to 0
    end
end
-- Connect trigger overlap event
ONCART_TRIGGER.beginOverlapEvent:Connect(OnCartDetect)
ONCART_TRIGGER.endOverlapEvent:Connect(OffCartDetect)



function _goToNextStop()
    --print ("Cart coins: ",_G.Cart["Manticoin"])
    -- create target point var
    _G["_p".._G.Cart["_atStop"]] = World.FindObjectByName("p".._G.Cart["_atStop"])
    
    -- get distance (between the 2 points)
    local cn = cart:GetWorldPosition() -- cart's current position
    local mt = _G["_p".._G.Cart["_atStop"]]:GetWorldPosition() -- target node position
    
    _G.Cart["_distance"] = (cn - mt).size
    --print ("DISTANCE: ",_G.Cart["_distance"])
    
    -- factor travelTime by distance / speed
    _G.Cart["_travelTime"] = math.floor(_G.Cart["_distance"]/_G.Cart["_speedRate"])
    
    -- get cart's rotation (between the 2 points)
    _midPoint:LookAt(mt)
    cart:RotateTo(Rotation.New(_midPoint:GetWorldRotation()), 1.8)    
    
    -- START traveling
    cart:MoveTo(Vector3.New(mt), _G.Cart["_travelTime"])
    
    -- cart rests at this stop?
    _G.Cart['_restTime'] = _G["_p".._G.Cart["_atStop"]]:FindChildByName("point"):GetCustomProperty("rest")
    
    --_G.Cart['_restTime'] = _G["_p".._G.Cart["_atStop"]].child:GetCustomProperty("rest")
    Task.Wait(_G.Cart['_travelTime']) -- how long cart will travel
    
    if(_G.Cart['_atStop'] >= _G.Cart['_stops']) -- move our "stop" to the next one
    then
        _G.Cart['_atStop'] = 1
    else
        _G.Cart['_atStop'] = _G.Cart['_atStop'] + 1
    end
    -- STOP traveling
    Task.Wait(_G.Cart['_restTime']) -- followed by "rest" time    
    _goToNextStop() -- infinite loop!
end



function _startTheCart()
    local _startMoving = 1
    if(_startMoving == 1) -- we start the cart!
    then
        _goToNextStop()
    end    
end


_startTheCart() -- Start the cart's infinite loop. "All aboard!"




Latest Posts