Permalink
Browse files

Merge pull request #624 from robotboy655/hands

Better view model hands system
  • Loading branch information...
2 parents 045df1a + d0ece0b commit f2d19b24f69643be2d5fe86bd72bf51eedbb926c @UnderscoreKilburn UnderscoreKilburn committed Mar 3, 2014
@@ -15,22 +15,17 @@ function ENT:Initialize()
end
-function ENT:DoSetup( ply )
+function ENT:DoSetup( ply, spec )
-- Set these hands to the player
ply:SetHands( self )
self:SetOwner( ply )
- -- Which hands should we use?
- local info = player_manager.RunClass( ply, "GetHandsModel" )
- if ( info ) then
- self:SetModel( info.model )
- self:SetSkin( info.skin )
- self:SetBodyGroups( info.body )
- end
+ -- Which hands should we use? Let the gamemode decide
+ hook.Call( "PlayerSetHandsModel", GAMEMODE, spec or ply, self )
-- Attach them to the viewmodel
- local vm = ply:GetViewModel( 0 )
+ local vm = ( spec or ply ):GetViewModel( 0 )
self:AttachToViewmodel( vm )
vm:DeleteOnRemove( self )
@@ -592,6 +592,15 @@ end
function GM:PostDrawViewModel( ViewModel, Player, Weapon )
if ( !IsValid( Weapon ) ) then return false end
+
+ if ( Weapon.UseHands || !Weapon:IsScripted() ) then
+
+ local hands = Player:GetHands()
+ if ( IsValid( hands ) ) then
+ hands:DrawModel()
+ end
+
+ end
player_manager.RunClass( Player, "PostDrawViewModel", ViewModel, Weapon )
@@ -256,6 +256,8 @@ function GM:PlayerSpawn( pl )
-- Stop observer mode
pl:UnSpectate()
+ pl:SetupHands()
+
player_manager.OnPlayerSpawn( pl )
player_manager.RunClass( pl, "Spawn" )
@@ -278,6 +280,21 @@ function GM:PlayerSetModel( pl )
end
--[[---------------------------------------------------------
+ Name: gamemode:PlayerSetHandsModel( )
+ Desc: Sets the player's view model hands model
+-----------------------------------------------------------]]
+function GM:PlayerSetHandsModel( pl, ent )
+
+ local info = player_manager.RunClass( pl, "GetHandsModel" )
+ if ( info ) then
+ ent:SetModel( info.model )
+ ent:SetSkin( info.skin )
+ ent:SetBodyGroups( info.body )
+ end
+
+end
+
+--[[---------------------------------------------------------
Name: gamemode:PlayerLoadout( )
Desc: Give the player the default spawning weapons/ammo
-----------------------------------------------------------]]
@@ -41,7 +41,6 @@ end
--
function PLAYER:Init()
-
end
--
@@ -52,17 +51,6 @@ end
--
function PLAYER:Spawn()
- local oldhands = self.Player:GetHands();
- if ( IsValid( oldhands ) ) then
- oldhands:Remove()
- end
-
- local hands = ents.Create( "gmod_hands" )
- if ( IsValid( hands ) ) then
- hands:DoSetup( self.Player )
- hands:Spawn()
- end
-
end
--
@@ -131,15 +119,6 @@ end
--
function PLAYER:PostDrawViewModel( vm, weapon )
- if ( weapon.UseHands || !weapon:IsScripted() ) then
-
- local hands = self.Player:GetHands()
- if ( IsValid( hands ) ) then
- hands:DrawModel()
- end
-
- end
-
end
--
@@ -152,9 +131,9 @@ function PLAYER:GetHandsModel()
-- return { model = "models/weapons/c_arms_cstrike.mdl", skin = 1, body = "0100000" }
- local cl_playermodel = self.Player:GetInfo( "cl_playermodel" )
- return player_manager.TranslatePlayerHands( cl_playermodel )
+ local playermodel = player_manager.TranslateToPlayerModelName( self.Player:GetModel() )
+ return player_manager.TranslatePlayerHands( playermodel )
end
-player_manager.RegisterClass( "player_default", PLAYER, nil )
+player_manager.RegisterClass( "player_default", PLAYER, nil )
@@ -139,4 +139,13 @@ function PLAYER:CalcView( view )
end
+function PLAYER:GetHandsModel()
+
+ -- return { model = "models/weapons/c_arms_cstrike.mdl", skin = 1, body = "0100000" }
+
+ local cl_playermodel = self.Player:GetInfo( "cl_playermodel" )
+ return player_manager.TranslatePlayerHands( cl_playermodel )
+
+end
+
player_manager.RegisterClass( "player_sandbox", PLAYER, "player_default" )
@@ -54,13 +54,6 @@ function GM:Initialize()
self.BaseClass:Initialize()
end
-function GM:PostDrawViewModel( vm, ply, weapon )
- if weapon.UseHands or (not weapon:IsScripted()) then
- local hands = LocalPlayer():GetHands()
- if IsValid(hands) then hands:DrawModel() end
- end
-end
-
function GM:InitPostEntity()
MsgN("TTT Client post-init...")
@@ -86,23 +86,21 @@ function GM:PlayerSpawn(ply)
hook.Call("PlayerSetModel", GAMEMODE, ply)
hook.Call("TTTPlayerSetColor", GAMEMODE, ply)
- local oldhands = ply:GetHands()
- if IsValid(oldhands) then oldhands:Remove() end
-
- local hands = ents.Create( "gmod_hands" )
- if IsValid(hands) then
- ply:SetHands(hands)
- hands:SetOwner(ply)
-
- -- Find model and attach to vm, currently ours
- ply:SetPlayerHands(ply)
- ply:DeleteOnRemove(hands)
- hands:Spawn()
- end
+ ply:SetupHands()
SCORE:HandleSpawn(ply)
end
+function GM:PlayerSetHandsModel( pl, ent )
+ local simplemodel = player_manager.TranslateToPlayerModelName(pl:GetModel())
+ local info = player_manager.TranslatePlayerHands(simplemodel)
+ if info then
+ ent:SetModel(info.model)
+ ent:SetSkin(info.skin)
+ ent:SetBodyGroups(info.body)
+ end
+end
+
function GM:IsSpawnpointSuitable(ply, spwn, force, rigged)
if not IsValid(ply) or not ply:IsTerror() then return true end
if not rigged and (not IsValid(spwn) or not spwn:IsInWorld()) then return false end
@@ -343,7 +343,7 @@ function plymeta:SpectateEntity(ent)
oldSpectateEntity(self, ent)
if IsValid(ent) and ent:IsPlayer() then
- self:SetPlayerHands(ent)
+ self:SetupHands(ent)
end
end
@@ -356,27 +356,3 @@ end
function plymeta:GetAvoidDetective()
return self:GetInfoNum("ttt_avoid_detective", 0) > 0
end
-
--- Sets hands to be the correct model for given player and attaches to their
--- viewmodel. Note that the player is not necessarily *self*, but can be a
--- spectated player.
-function plymeta:SetPlayerHands(ply)
- local hands = self:GetHands()
- if not IsValid(hands) or not IsValid(ply) then return end
-
- -- Find hands model
- local simplemodel = util.GetSimpleModelName(ply:GetModel())
- local info = player_manager.TranslatePlayerHands(simplemodel)
- if info then
- hands:SetModel(info.model)
- hands:SetSkin(info.skin)
- hands:SetBodyGroups(info.body)
- end
-
- -- Attach to vm
- local vm = ply:GetViewModel(0)
- if vm then
- hands:AttachToViewmodel(vm)
- vm:DeleteOnRemove(hands)
- end
-end
@@ -372,20 +372,3 @@ function util.SimpleTime(seconds, fmt)
return string.format(fmt, m, s, ms)
end
-
-
--- Find the simple name that player_manager likes
-local simplemodel_cache = {}
-function util.GetSimpleModelName(model)
- local cached = simplemodel_cache[model]
- if cached then return cached end
-
- local lst = player_manager.AllValidModels()
- for simple, mdl in pairs(lst) do
- if mdl == model then
- simplemodel_cache[mdl] = simple
- return simple
- end
- end
- return "css_phoenix"
-end
@@ -186,4 +186,21 @@ end
-- Can use flashlight?
--
function meta:AllowFlashlight( bAble ) self.m_bFlashlight = bAble end
-function meta:CanUseFlashlight() return self.m_bFlashlight end
+function meta:CanUseFlashlight() return self.m_bFlashlight end
+
+-- A function to set up player hands, so coders don't have to copy all the code everytime.
+-- Call this in PlayerSpawn hook
+function meta:SetupHands( ply )
+
+ local oldhands = self:GetHands()
+ if ( IsValid( oldhands ) ) then
+ oldhands:Remove()
+ end
+
+ local hands = ents.Create( "gmod_hands" )
+ if ( IsValid( hands ) ) then
+ hands:DoSetup( self, ply )
+ hands:Spawn()
+ end
+
+end
@@ -13,6 +13,7 @@ module( "player_manager" )
-- Stores a table of valid player models
local ModelList = {}
+local ModelListRev = {}
local HandNames = {}
--[[---------------------------------------------------------
@@ -21,6 +22,7 @@ local HandNames = {}
function AddValidModel( name, model )
ModelList[ name ] = model
+ ModelListRev[ model ] = name
end
@@ -54,6 +56,16 @@ function TranslatePlayerModel( name )
return "models/player/kleiner.mdl"
end
+-- Translate from the full model name to simple model name
+function TranslateToPlayerModelName( model )
+
+ if ( ModelListRev[ model ] != nil ) then
+ return ModelListRev[ model ]
+ end
+
+ return "kleiner"
+end
+
--
-- Translate hands based on model
--

0 comments on commit f2d19b2

Please sign in to comment.