From 2a50467a41b0c53f645f8dea9fc31a0060802972 Mon Sep 17 00:00:00 2001 From: Nikolaos Date: Thu, 30 Jun 2022 22:17:28 +0100 Subject: [PATCH] Temporary commit because I know this works --- Lua/enginecontrollercomponent.lua | 82 +++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 Lua/enginecontrollercomponent.lua diff --git a/Lua/enginecontrollercomponent.lua b/Lua/enginecontrollercomponent.lua new file mode 100644 index 0000000..a8c2315 --- /dev/null +++ b/Lua/enginecontrollercomponent.lua @@ -0,0 +1,82 @@ +local allItems = {} + +local defaultTable = { + velocity_x_in = 0, + current_velocity_x_in = 0, + set_trm_target = 20, + set_trm_aggression = 600, + set_mode = 1, + set_silent = 0, + acceleration = 0, +} + +Hook.Add("item.created", "enginecontrollercomponent.init", function(item) + if item.Prefab.Identifier == "enginecontrollercomponent" then + allItems[item] = {} + for k, v in pairs(defaultTable) do + allItems[item][k] = v + end + end +end) + +local signalReceived = function(signal, connection) + local this = connection.Item + local mem = allItems[this] + + local signalNum = tonumber(signal.value) or defaultTable[connection.Name] + + if connection.Name == "current_velocity_x_in" then + mem.acceleration = (signalNum - mem.current_velocity_x_in) + mem.current_velocity_x_in = signalNum + else + mem[connection.Name] = signalNum + end + + -- Skip signals for optimization + if connection.Name ~= "velocity_x_in" then + return + end + + local main_force_out + local boost_force_out + + if mem.set_mode == 1 then + main_force_out = mem.velocity_x_in + boost_force_out = 0 + + elseif mem.set_mode == 2 then + main_force_out = mem.velocity_x_in + boost_force_out = mem.velocity_x_in + + elseif mem.set_mode == 0 then + local absolute_current_velocity = math.abs(mem.current_velocity_x_in) + + if mem.set_trm_target -0.25 < absolute_current_velocity and mem.velocity_x_in * mem.current_velocity_x_in > 0 then + local target_acceleration = - (absolute_current_velocity * mem.set_trm_aggression) + (mem.set_trm_target * mem.set_trm_aggression) + + if math.abs(mem.velocity_x_in) > math.abs(target_acceleration) then + if mem.current_velocity_x_in > 0 then + main_force_out = target_acceleration + else + main_force_out = -target_acceleration + end + else + main_force_out = mem.velocity_x_in + end + else + main_force_out = mem.velocity_x_in + end + + boost_force_out = 0 + end + + if mem.set_silent == 1 then + main_force_out = main_force_out * 0.2 + boost_force_out = boost_force_out * 0.2 + end + + this.SendSignal(main_force_out, "main_force_out") + this.SendSignal(boost_force_out, "boost_force_out") +end + +Hook.Add("signalreceived.enginecontrollercomponent", "enginecontrollercomponent.signalReceived", signalReceived)