Created
November 11, 2024 19:41
-
-
Save na-stewart/c45e441d969c162eb406a2f23f481f20 to your computer and use it in GitHub Desktop.
Garry's Mod entity spawner based on random player positions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EntitySpawner = EntitySpawner or {} -- Globally accessable table of spawner functions. | |
local spawnPositions = {} | |
local spawnDelay = 15 | |
local spawnIterations = 16 -- Can be thought as a time limit: (spawnDelay * spawnIterations) seconds. | |
local maxAliveEntities = 6 | |
local minAlivePlayers = 5 | |
local enabled = true | |
local entity = "sent_ball" | |
if not enabled then return end | |
function EntitySpawner.Spawn() | |
if #ents.FindByClass( entity ) > maxAliveEntities then return end | |
local spawnPosition = spawnPositions[ math.random( 1, #spawnPositions ) ] | |
if spawnPosition ~= nil then | |
local sentEntity = ents.Create( entity ) | |
sentEntity:SetPos( spawnPosition ) | |
sentEntity:Spawn() | |
sentEntity:PhysWake() | |
end | |
end | |
function EntitySpawner.Reset() | |
table.Empty( spawnPositions ) | |
timer.Remove( "EntitySpawner" ) | |
end | |
function EntitySpawner.Initialize() | |
for _, player in ipairs( player.GetAll() ) do | |
if player:GetObserverMode() == 0 then | |
table.insert( spawnPositions, player:GetPos() ) | |
end | |
end | |
if #spawnPositions < minAlivePlayers then | |
EntitySpawner.Reset() | |
else | |
timer.Simple( spawnDelay, function() timer.Create( "EntitySpawner", spawnDelay, spawnIterations, EntitySpawner.Spawn ) end ) | |
end | |
end | |
if engine.ActiveGamemode() == "terrortown" then | |
hook.Add( "TTTBeginRound", "InitalizeEntitySpawner", EntitySpawner.Initialize ) | |
hook.Add( "TTTEndRound", "ResetEntitySpawner", EntitySpawner.Reset ) | |
else | |
hook.Add( "InitPostEntity", "InitalizeEntitySpawner", function() | |
EntitySpawner.Initialize() | |
timer.Create( "EntitySpawningRefresher", spawnDelay * spawnIterations, 0, function() | |
EntitySpawner.Reset() | |
EntitySpawner.Initialize() | |
end ) -- Spawner does not have a time limit for non TTT gamemodes, rather spawn positions are refreshed. | |
end ) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment