Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local M = {}
- local scenario = require("scenario/scenarios")
- local helper = require("scenario/scenariohelper")
- local player = nil
- local playerPos = nil
- local playerDistance = nil
- local nonPlayers = {}
- local racers = {}
- local racerPos = nil
- local racerVel = nil
- local sceneTimer = 0
- local score = 0
- local totalRacers = 0
- local Racer = {instance = nil, state = true, arrestTimer = 0}
- function Racer:new(instance, state, arrestTimer) --Racer object
- local self = setmetatable({}, Racer)
- self.__index = self
- self.instance = instance or nil
- self.state = state or true
- self.arrestTimer = arrestTimer or 0
- return self
- end
- local function arrestRacer(theRacer) --Triggers when conditions are true
- if theRacer.state then
- if theRacer.arrestTimer % 1 == 0 then
- local countdown = 5 - math.modf(theRacer.arrestTimer)
- if countdown > 0 then
- helper.flashUiMessage((tostring(countdown).."..."), 0.5, true)
- end
- end
- theRacer.arrestTimer = theRacer.arrestTimer + 0.25
- else
- theRacer.arrestTimer = 0
- end
- if theRacer.arrestTimer > 5 then
- helper.flashUiMessage("Busted!", 2, true)
- helper.setAiMode(theRacer.instance.name, "stopping")
- theRacer.arrestTimer = 0
- score = score + 1;
- if score >= totalRacers then
- scenario.finish({msg = "You got them all!"}) --Scenario win trigger and message
- end
- theRacer.state = false
- end
- end
- local function onScenarioUIReady(state) --Always executes at the scenario introduction
- sceneTimer = 0
- score = 0
- totalRacers = 0
- player = scenetree.findObject("scenario_player0")
- nonPlayers = {scenetree.findObject("scenario_ai0"), scenetree.findObject("scenario_ai1"), scenetree.findObject("scenario_ai2")} --Edit this to match your AI vehicles
- for i, v in ipairs(nonPlayers) do
- racers[i] = Racer:new(nonPlayers[i], true, 0)
- helper.setAiMode(racers[i].instance.name, "flee")
- totalRacers = totalRacers + 1
- end
- end
- local function onRaceTick(raceTickTime) --Always executes while a scenario is running
- for i, v in ipairs(racers) do
- playerPos = player:getPosition()
- racerPos = racers[i].instance:getPosition()
- racerVel = map.objects[map.objectNames[racers[i].instance.name]].vel:length()
- playerDistance = (playerPos - racerPos):len()
- if racers[i].state then
- if sceneTimer >= 5 and playerDistance <= 50 and racerVel <= 3 then --If: more than 5 seconds post start, within distance, within speed; can be edited
- arrestRacer(racers[i])
- else
- racers[i].arrestTimer = 0
- end
- end
- sceneTimer = sceneTimer + 0.25
- end
- end
- M.onScenarioUIReady = onScenarioUIReady
- M.onRaceTick = onRaceTick
- return M
Add Comment
Please, Sign In to add comment