Created
August 12, 2019 02:02
-
-
Save boudr/a84fd61497b67572f64c992904114f77 to your computer and use it in GitHub Desktop.
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
if CLIENT then | |
function BTK.TeamsMenu() | |
local frame = vgui.Create() | |
end | |
--Client can stop here | |
return | |
end | |
if SERVER then | |
BTK.ActiveTeams = {} | |
local Team = { | |
Name = "base", | |
FormalName = "Base Team", | |
WalkSpeed = 400, -- How fast to move when not running | |
RunSpeed = 600, -- How fast to move when running | |
CrouchedWalkSpeed = 0.3, -- Multiply move speed by this when crouching | |
DuckSpeed = 0.3, -- How fast to go from not ducking, to ducking | |
UnDuckSpeed = 0.3, -- How fast to go from ducking, to not ducking | |
JumpPower = 200, -- How powerful our jump should be | |
CanUseFlashlight = true, -- Can we use the flashlight | |
MaxHealth = 100, -- Max health we can have | |
StartHealth = 100, -- How much health we start with | |
StartArmor = 0, -- How much armour we start with | |
DropWeaponOnDie = false, -- Do we drop our weapon when we die | |
TeammateNoCollide = true, -- Do we collide with teammates or run straight through them | |
AvoidPlayers = true, -- Automatically swerves around other players | |
UseVMHands = true, -- Uses viewmodel hands | |
Model = nil, -- Team Player Model | |
Members = {}, | |
isJoinable = true, | |
TeamColor = nil, | |
Index = 0, | |
--Hooks | |
PlayerSpawn = function( self, ply ) | |
end, | |
PlayerLoadout = function( self, ply ) | |
print("This is being called!") | |
ply:RemoveAllAmmo() | |
ply:StripWeapons() | |
for _,v in pairs(BTK.DefaultLoadout) do | |
ply:Give(v) | |
end | |
return true | |
end, | |
--This isn't working right... I'll try again later. For some reason, the models are T-posing instead of animating. | |
PlayerSetModel = function( self, ply ) | |
local cl_playermodel = ply:GetInfo( "cl_playermodel" ) | |
local modelname = self.Model or player_manager.TranslatePlayerModel( cl_playermodel ) | |
--util.PrecacheModel( modelname ) | |
ply:SetModel( modelname ) | |
if self.TeamColor then | |
ply:SetPlayerColor(self.TeamColor) | |
end | |
end, | |
--Meta-Methods | |
GetFormalName = function(self) | |
return self.FormalName | |
end, | |
GetName = function(self) | |
return self.Name | |
end, | |
GetMembers = function(self) | |
return self.Members | |
end, | |
GetMemberByName = function(self, name) | |
for _, v in pairs(self.Members) do | |
if v:GetNick() == name then | |
return v | |
end | |
end | |
print("Player not found: " .. name) | |
return nil | |
end, | |
--This needs to be called for all players disconnecting. | |
RemoveMember = function(self, ply) | |
if not ply:GetNWBool("is_in_team") then | |
print("Player was not in a team to begin with!") | |
return | |
end | |
for k,v in pairs(self.Members) do | |
if ply:SteamID() == v then | |
table.remove(self.Members, k) | |
--Player may have already disconnected so there is no point in doing this if they are gone | |
if ply:IsValid() then | |
ply:SetNWString("team", "") | |
ply:SetNWBool("is_in_team", false) | |
end | |
print(ply:Nick() .. " has left " .. self.FormalName) | |
--This function should not return in-case there are duplicate copies of the same player for some reason. | |
end | |
end | |
end, | |
AddMember = function(self, ply) | |
table.insert(self.Members, ply:SteamID()) | |
ply:SetNWString("team", self.Name) | |
ply:SetNWBool("is_in_team", true) | |
print(ply:Nick() .. " joined " .. self.FormalName) | |
end, | |
GetIndex = function(self) | |
return self.Index | |
end | |
} | |
Team.__index = Team | |
--Team gloabl functions-- | |
--This function will create and return a team object | |
function BTK.CreateTeam( index, name, teamColor, isJoinable, team_model ) | |
team_model = team_model or nil | |
local team = { | |
FormalName = name, | |
Name = string.Replace(string.lower(name), " ", ""), | |
TeamColor = teamColor, | |
IsJoinable = isJoinable, | |
Model = team_model, | |
Index = index | |
} | |
setmetatable(team, Team) | |
table.insert(BTK.ActiveTeams, team) | |
return team | |
end | |
function BTK.GetTeams() | |
return BTK.ActiveTeams | |
end | |
function BTK.GetTeamsNames() | |
local temp = {} | |
for _,v in pairs(BTK.ActiveTeams) do | |
table.insert(temp, v:GetName()) | |
end | |
return temp | |
end | |
function BTK.GetTeamsFormalNames() | |
local temp = {} | |
for _,v in pairs(BTK.ActiveTeams) do | |
table.insert(temp, v:GetFormalName()) | |
end | |
return temp | |
end | |
function BTK.GetTeam( name ) | |
for _, v in pairs(BTK.ActiveTeams) do | |
if v:GetName() == name then | |
return v | |
end | |
end | |
print("No team found by: " .. name) | |
return nil | |
end | |
function BTK.DestroyTeam( name ) | |
local t = BTK.GetTeam( name ) | |
if not t then | |
print("Team " .. name .. " can't be destroyed because it does not exist.") | |
return | |
end | |
local members = t:GetMembers() | |
for _,v in pairs(members) do | |
t:RemoveMember(v) | |
end | |
table.remove(BTK.ActiveTeams, t:GetIndex()) | |
end | |
--Team Player Functions-- | |
local playerMeta = FindMetaTable("Player") | |
function playerMeta:IsInTeam() | |
return self:GetNWBool("is_in_team") | |
end | |
function playerMeta:GetTeam() | |
return BTK.GetTeam(self:GetNWString("team")) | |
end | |
function playerMeta:GetTeamName() | |
return self:GetNWString("team") | |
end | |
function playerMeta:LeaveTeam() | |
local t = self:GetTeam() | |
if not t then | |
print("Unable to leave team! Oh no!") | |
return | |
end | |
t:RemoveMember(self) | |
end | |
---Base Teams-- | |
BTK.CreateTeam( 1, "Civilians", Color(0,5,3,255), true ) | |
BTK.CreateTeam( 2, "EGI Guard", BTK.EGI_Guard_Color, true, BTK.EGI_Guard_Model) | |
BTK.CreateTeam( 3, "Revolutionaries", BTK.Revolutionary_Color, true, BTK.Revolutionary_Model ) | |
--Console Command | |
concommand.Add("join_team", function(ply, cmd, args, argStr) | |
if args[1] == ply:GetTeamName() then | |
ply:ChatPrint("You are already in that team!") | |
return | |
end | |
if ply:IsInTeam() then | |
ply:LeaveTeam() | |
end | |
local t = BTK.GetTeam( args[1], false ) | |
if not t then | |
ply:ChatPrint("Team " .. args[1] .. " is not a valid team.") | |
return | |
end | |
t:AddMember(ply) | |
ply:ChatPrint("You have joined " .. t:GetFormalName() .. "!") | |
ply:Kill() | |
end) | |
function GM:PlayerLoadout(ply) | |
local t = ply:GetTeam() | |
if not t then return end | |
t:PlayerLoadout(ply) | |
end | |
function GM:PlayerSetModel(ply) | |
local t = ply:GetTeam() | |
if not t then return end | |
t:PlayerSetModel(ply) | |
end | |
hook.Add("PlayerInitialSpawn", "team.PlayerInitialSpawn", function(ply) | |
local t = BTK.GetTeam(BTK.StartTeam) | |
if not t then return end | |
t:AddMember(ply) | |
end) | |
hook.Add("PlayerDisconnected", "team.PlayerDisconnected", function(ply) | |
if ply:IsInTeam() then | |
ply:LeaveTeam() | |
end | |
end) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment