Last active
March 11, 2022 10:11
-
-
Save adamnejm/367c295404ff195468a07c95e4ff71d7 to your computer and use it in GitHub Desktop.
Starfall - Basic Event Chair Controller Example
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
--@name Basic event chair controller | |
--@author Name | |
--@server | |
-- USAGE: | |
-- Place chip on the ground, a chair will spawn, sit in it and press W or S | |
local owner, client, chip, world = owner(), player(), chip(), entity(0) | |
-- Create a frozen SENT chair 20 units above the chip | |
local chair = prop.createSent(chip:getPos() + chip:getUp() * 20, Angle(), "Seat_Jeep", true) | |
-- Create a basic hologram in front of the chair with a scale of 0.75 | |
local holo = holograms.create(chair:getPos() + chair:getForward() * 40, Angle(), "models/props_phx/games/chess/white_rook.mdl", Vector(0.75)) | |
-- Player that's currently sitting in the chair | |
local driver | |
-- Due to the fact that `PlayerEnteredVehicle` has a `role` parameter, but `PlayerLeaveVehicle` doesn't I can easily distinguish between the 2 hooks. | |
-- If a `role` argument is present, set the `driver` variable to the provided player, otherwise set it to `nil` | |
local function set_driver(ply, vehicle, role) | |
if vehicle ~= chair then return end -- If the vehicle in question isn't our chair, we don't care | |
driver = role and ply -- If role is "Truthy" then set `ply` as our driver, otherwise set `driver` to `role` which in this case will be `nil` | |
end | |
-- Attach the `set_driver` function to appropriate hooks | |
hook.add("PlayerEnteredVehicle", "SetDriver", set_driver) | |
hook.add("PlayerLeaveVehicle", "SetDriver", set_driver) | |
-- Variables responsible for the movement of the hologram | |
local velocity = 0 | |
local acceleration = 0 | |
-- Map of inputs and their acceleration values / forces | |
local inputs = { | |
[IN_KEY.FORWARD] = 10, | |
[IN_KEY.BACK] = -8, | |
} | |
-- Attach a callback to `KeyPress` so we can detect when players press their binds | |
hook.add("KeyPress", "KeyPress", function(ply, key) | |
if ply ~= driver then return end -- If the player isn't our driver then we don't care | |
if inputs[key] then -- If the key is present in our input map... | |
acceleration = inputs[key] -- then set acceleration to that value | |
end | |
end) | |
-- Attach a callback to `KeyRelease` so we can detect when players release their binds | |
hook.add("KeyRelease", "KeyRelease", function(ply, key) | |
if ply ~= driver then return end -- If the player isn't our driver then we don't care | |
if inputs[key] then -- Check if the key is one of allowed ones from the input map | |
-- Without this snippet, when player decides to press W and S at once, then release only one of them, the hologram will stop | |
-- This will simply check whether any other keys from our input map are pressed, if so, set the velocity to the first found one: | |
for key, force in pairs(inputs) do -- Loop through the input map... | |
if ply:keyDown(key) then -- If a key from the input map is still pressed... | |
acceleration = force -- set the acceleration to it's value | |
return -- Return from the whole function so that `acceleration = 0` doesn't get executed | |
end | |
end | |
-- If the code above didn't detect any pressed keys, set the acceleration to 0 | |
acceleration = 0 | |
end | |
end) | |
-- Update function blah blah blah | |
hook.add("Tick", "Update", function() | |
-- Add acceleration from user input to the velocity | |
velocity = velocity + acceleration | |
-- Create a new vector with Y axis based on the `velocity` and apply that to the hologram | |
holo:setVel(Vector(0, velocity, 0)) | |
-- Decrease the velocity, otherwise hologram will never stop | |
velocity = velocity * 0.9 | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment