Skip to content

Commit

Permalink
Added toggle option to context menu
Browse files Browse the repository at this point in the history
Also experimented a bit with affecting player gravity.
  • Loading branch information
viral32111 committed Jul 13, 2020
1 parent abc5fb5 commit 091011b
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 3 deletions.
105 changes: 105 additions & 0 deletions lua/autorun/client/cl_gravity_arrow.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
--[[------------------------------------------------------------------------
Gravity Arrow - A small blue arrow that controls the force of world gravity.
Copyright (C) 2020 viral32111 (https://viral32111.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see https://www.gnu.org/licenses.
------------------------------------------------------------------------]]--

properties.Add( "gravityarrow", {

-- Set the display text and icon
MenuLabel = "Gravity Arrow",
MenuIcon = "icon16/arrow_down.png",

-- Set to display at the top
Order = 0,

-- Only show for entities that pass this check
Filter = function( self, entity, player )

-- Don't continue if the entity is somehow invalid
if not IsValid( entity ) then return false end

-- Don't continue if the CanProperty hook returns false
if not hook.Run( "CanProperty", player, "gravityarrow", entity ) then return false end

-- Return true/false if the entity is a Gravity Arrow
return entity:GetClass() == "gravity_arrow"

end,

-- Runs when the property is added
MenuOpen = function( self, option, entity, trace )

-- Create the submenu
local subMenu = option:AddSubMenu()

-- Enable/disable completely option
if entity:GetEnabled() then
-- Add the disable option
subMenu:AddOption( "Disable", function()
if not hook.Run( "CanProperty", LocalPlayer(), "gravityarrow_disable", entity ) then return end

net.Start( "gravityArrowToggle" )
net.WriteEntity( entity )
net.WriteBool( false )
net.SendToServer()
end ):SetIcon( "icon16/cross.png" )
else
-- Add the enable option
subMenu:AddOption( "Enable", function()
if not hook.Run( "CanProperty", LocalPlayer(), "gravityarrow_enable", entity ) then return end

net.Start( "gravityArrowToggle" )
net.WriteEntity( entity )
net.WriteBool( true )
net.SendToServer()
end ):SetIcon( "icon16/tick.png" )
end

-- Affect players option
--[[ https://github.com/conspiracy-servers/gravity-arrow/projects/1#card-41700985
if entity:GetAffectPlayers() then
local subOption = subMenu:AddOption( "Ignore Players", function()
if not hook.Run( "CanProperty", LocalPlayer(), "gravityarrow_ignoreplayers", entity ) then return end
entity:SetAffectPlayers( false )
end )
subOption:SetIcon( "icon16/cross.png" )
subOption:SetEnabled( entity:GetEnabled() )
else
local subOption = subMenu:AddOption( "Affect Players", function()
if not hook.Run( "CanProperty", LocalPlayer(), "gravityarrow_affectplayers", entity ) then return end
entity:SetAffectPlayers( true )
end )
subOption:SetIcon( "icon16/tick.png" )
subOption:SetEnabled( entity:GetEnabled() )
end
-- Force option
local subOption = subMenu:AddOption( "Set Force", function()
if not hook.Run( "CanProperty", LocalPlayer(), "gravityarrow_setforce", entity ) then return end
print( "yolo" )
end )
subOption:SetIcon( "icon16/brick_go.png" )
subOption:SetEnabled( entity:GetEnabled() )
]]

end,

-- Runs when the property is clicked
Action = function( self, entity, trace )

end

} )
32 changes: 32 additions & 0 deletions lua/autorun/server/sv_gravity_arrow.lua
Original file line number Diff line number Diff line change
@@ -1,2 +1,34 @@
--[[------------------------------------------------------------------------
Gravity Arrow - A small blue arrow that controls the force of world gravity.
Copyright (C) 2020 viral32111 (https://viral32111.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see https://www.gnu.org/licenses.
------------------------------------------------------------------------]]--

-- Add the custom spawnmenu icon to the client download queue
resource.AddSingleFile( "materials/vgui/entities/gravity_arrow.vtf" )
resource.AddSingleFile( "materials/vgui/entities/gravity_arrow.vmt" )

-- Add the network string for enabling or disabling the arrow
util.AddNetworkString( "gravityArrowToggle" )

-- Runs when the client requests that the arrow be enabled or disabled
net.Receive( "gravityArrowToggle", function( length, player )
-- Read the network variables
local entity = net.ReadEntity()
local state = net.ReadBool()

-- Update the arrow's network var
entity:SetEnabled( state )
end )
24 changes: 21 additions & 3 deletions lua/entities/gravity_arrow/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,28 @@ end

function ENT:Think()

physenv.SetGravity( self:GetUp() * 600 )
if self:GetEnabled() then
local gravity = self:GetUp() * self:GetForce()
physenv.SetGravity( gravity )

--[[ https://github.com/conspiracy-servers/gravity-arrow/projects/1#card-41700985
if self:GetAffectPlayers() then
for index, player in ipairs( player.GetAll() ) do
local physicsObject = player:GetPhysicsObject()
if IsValid( physicsObject ) then
physicsObject:Wake()
end
player:SetVelocity( gravity )
end
end
]]
else
physenv.SetGravity( Vector( 0, 0, -600 ) )
end

self:NextThink( CurTime() + 0.1 )

return true

end
Expand All @@ -72,4 +90,4 @@ function ENT:OnRemove()

physenv.SetGravity( Vector( 0, 0, -600 ) )

end
end
14 changes: 14 additions & 0 deletions lua/entities/gravity_arrow/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,17 @@ ENT.Contact = "https://viral32111.com"

ENT.Spawnable = true
ENT.AdminOnly = false

function ENT:SetupDataTables()

self:NetworkVar( "Bool", 0, "Enabled" )
self:NetworkVar( "Bool", 1, "AffectPlayers" )
self:NetworkVar( "Int", 0, "Force" )

if SERVER then
self:SetEnabled( true )
self:SetAffectPlayers( false )
self:SetForce( 600 )
end

end

0 comments on commit 091011b

Please # to comment.