diff --git a/lua/autorun/client/cl_gravity_arrow.lua b/lua/autorun/client/cl_gravity_arrow.lua new file mode 100644 index 0000000..19cf2e8 --- /dev/null +++ b/lua/autorun/client/cl_gravity_arrow.lua @@ -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 + +} ) diff --git a/lua/autorun/server/sv_gravity_arrow.lua b/lua/autorun/server/sv_gravity_arrow.lua index dfb88af..1a25d19 100644 --- a/lua/autorun/server/sv_gravity_arrow.lua +++ b/lua/autorun/server/sv_gravity_arrow.lua @@ -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 ) diff --git a/lua/entities/gravity_arrow/init.lua b/lua/entities/gravity_arrow/init.lua index fbc3db2..8c3f73c 100644 --- a/lua/entities/gravity_arrow/init.lua +++ b/lua/entities/gravity_arrow/init.lua @@ -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 @@ -72,4 +90,4 @@ function ENT:OnRemove() physenv.SetGravity( Vector( 0, 0, -600 ) ) -end +end \ No newline at end of file diff --git a/lua/entities/gravity_arrow/shared.lua b/lua/entities/gravity_arrow/shared.lua index 841aa2d..5a087ca 100644 --- a/lua/entities/gravity_arrow/shared.lua +++ b/lua/entities/gravity_arrow/shared.lua @@ -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