Skip to content

Commit

Permalink
Sort of working companion logic, vehicle outputs are still messy
Browse files Browse the repository at this point in the history
  • Loading branch information
ZehMatt committed Jun 2, 2024
1 parent b69811f commit f9f9806
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
56 changes: 56 additions & 0 deletions gamemode/sh_vehicles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ if SERVER then
DbgPrint("No guns enabled")
end

-- Akward hack to know if an NPC passenger is inside. We handle this with our AcceptInput hook.
vehicle:Fire("AddOutput", "OnCompanionEnteredVehicle !self,LambdaCompanionEnteredVehicle,0,-1", "0.0")
vehicle:Fire("AddOutput", "OnCompanionExitedVehicle !self,LambdaCompanionExitedVehicle,1,-1", "0.0")

-- We only get a model next frame, delay it.
util.RunNextFrame(function()
if not IsValid(vehicle) then return end
Expand Down Expand Up @@ -98,6 +102,8 @@ if SERVER then
end)

vehicle.AllowVehicleCheckpoint = true
vehicle.LambdaVehicleType = vehicleType

local tracker = ents.Create("lambda_vehicle_tracker")
tracker:AttachToVehicle(vehicle)
tracker:Spawn()
Expand Down Expand Up @@ -154,6 +160,44 @@ if SERVER then
jalopy:SetNWEntity("PassengerSeat", seat)
end

function GM:OnCompanionEnteredVehicle(jalopy, passenger)
DbgPrint("Companion entered vehicle")
jalopy:SetNWEntity("LambdaPassenger", passenger)

local seat = jalopy:GetNWEntity("PassengerSeat")
if IsValid(seat) then
-- Lock this seat as we have the passenger inside the vehicle.
seat:Fire("Lock")
else
DbgPrint("No passenger seat on jalopy: " .. tostring(jalopy))
end
end

function GM:OnCompanionExitedVehicle(jalopy, passenger)
DbgPrint("Companion exited vehicle")
jalopy:SetNWEntity("LambdaPassenger", NULL)

local seat = jalopy:GetNWEntity("PassengerSeat")
if IsValid(seat) then
-- Lock this seat as we have the passenger inside the vehicle.
seat:Fire("Unlock")
end
end

function GM:OnPlayerPassengerEnteredVehicle(ply, vehicle, seat)
DbgPrint("Player entered passenger seat", ply, vehicle, seat)

-- Prevent NPC companions from entering as passengers.
vehicle:Fire("LockEntrance")
end

function GM:OnPlayerPassengerExitedVehicle(ply, vehicle, seat)
DbgPrint("Player exited passenger seat", ply, vehicle, seat)

-- Allow NPC companions to enter as passengers.
vehicle:Fire("UnlockEntrance")
end

function GM:HandleAirboatCreation(airboat)
end

Expand Down Expand Up @@ -187,6 +231,13 @@ if SERVER then
ang = vehicle:WorldToLocalAngles(ang)
ply:SetEyeAngles(ang)

if vehicle:GetNWBool("IsPassengerSeat", false) then
local parent = vehicle:GetParent()
if IsValid(parent) and parent:GetNWEntity("PassengerSeat") == vehicle then
self:OnPlayerPassengerEnteredVehicle(ply, parent, vehicle)
end
end

if self.MapScript ~= nil and self.MapScript.OnEnteredVehicle ~= nil then
self.MapScript:OnEnteredVehicle(ply, vehicle, role)
end
Expand Down Expand Up @@ -237,6 +288,11 @@ if SERVER then
local exitang = (pos - exitpos):Angle()
ply:TeleportPlayer(exitpos, exitang)
vehicle:GetParent().Passenger = nil

local parent = vehicle:GetParent()
if IsValid(parent) and parent:GetNWEntity("PassengerSeat") == vehicle then
self:OnPlayerPassengerExitedVehicle(ply, parent, vehicle)
end
end
end

Expand Down
12 changes: 12 additions & 0 deletions gamemode/sv_inputoutput.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,16 @@ function GM:AcceptInput(ent, inputName, activator, caller, value)
elseif inputName == "EnableDraw" then
ent:SetNoDraw(false)
end

-- MORE HACKHACK: Handle the case where companions entered the jalopy.
if inputName == "LambdaCompanionEnteredVehicle" and activator:IsVehicle() then
print("LambdaCompanionEnteredVehicle", ent, inputName, activator, caller, value)
self:OnCompanionEnteredVehicle(activator, caller)
return true
elseif inputName == "LambdaCompanionExitedVehicle" and activator:IsVehicle() then
print("LambdaCompanionExitedVehicle", ent, inputName, activator, caller, value)
self:OnCompanionExitedVehicle(activator, caller)
return true
end

end

0 comments on commit f9f9806

Please sign in to comment.