|
| 1 | +-- ========================================================= |
| 2 | +-- CTWorldManager.lua — FS25_CustomTriggerCreator |
| 3 | +-- Manages world-space proximity zones for player-created |
| 4 | +-- triggers that have a world position stored in config. |
| 5 | +-- |
| 6 | +-- Each positioned trigger gets a CTTriggerActivatable that |
| 7 | +-- is added to / removed from FS25's ActivatableObjectsSystem |
| 8 | +-- as the player enters / leaves the interaction radius. |
| 9 | +-- This is what shows "Press [E] to ..." and handles E-key. |
| 10 | +-- ========================================================= |
| 11 | + |
| 12 | +CTWorldManager = {} |
| 13 | +CTWorldManager._mt = { __index = CTWorldManager } |
| 14 | + |
| 15 | +CTWorldManager.INTERACT_RADIUS = 3.0 -- metres |
| 16 | +CTWorldManager.INTERACT_RADIUS_SQ = 9.0 -- radius^2, avoids sqrt in update |
| 17 | + |
| 18 | +---Create a new CTWorldManager. |
| 19 | +---@return CTWorldManager |
| 20 | +function CTWorldManager.new() |
| 21 | + local self = setmetatable({}, CTWorldManager._mt) |
| 22 | + -- id -> { activatable: CTTriggerActivatable, inRange: bool, record: table } |
| 23 | + self._zones = {} |
| 24 | + return self |
| 25 | +end |
| 26 | + |
| 27 | +-- --------------------------------------------------------------------------- |
| 28 | +-- Public API |
| 29 | +-- --------------------------------------------------------------------------- |
| 30 | + |
| 31 | +---Rebuild zones to match the current registry state. |
| 32 | +---Call after trigger create, delete, or savegame load. |
| 33 | +---@param registry TriggerRegistry |
| 34 | +function CTWorldManager:refresh(registry) |
| 35 | + if not registry then return end |
| 36 | + local all = registry:getAll() |
| 37 | + local active = {} |
| 38 | + |
| 39 | + for _, t in ipairs(all) do |
| 40 | + active[t.id] = true |
| 41 | + local cfg = t.config |
| 42 | + if cfg and cfg.worldX and cfg.worldZ then |
| 43 | + local zone = self._zones[t.id] |
| 44 | + if not zone then |
| 45 | + -- New positioned trigger |
| 46 | + self._zones[t.id] = { |
| 47 | + activatable = CTTriggerActivatable.new(t), |
| 48 | + inRange = false, |
| 49 | + record = t, |
| 50 | + } |
| 51 | + Logger.debug("CTWorldManager: zone registered for " .. t.id) |
| 52 | + else |
| 53 | + -- Trigger updated (toggle/rename) — refresh live references |
| 54 | + zone.record = t |
| 55 | + zone.activatable.record = t |
| 56 | + zone.activatable.activateText = "Activate: " .. (t.name or "Trigger") |
| 57 | + end |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + -- Remove zones for deleted triggers |
| 62 | + for id, zone in pairs(self._zones) do |
| 63 | + if not active[id] then |
| 64 | + self:_removeZone(id, zone) |
| 65 | + end |
| 66 | + end |
| 67 | +end |
| 68 | + |
| 69 | +---Per-frame proximity check. Adds/removes activatables as player moves. |
| 70 | +---@param dt number Delta time in ms (FS25 convention) |
| 71 | +function CTWorldManager:update(dt) |
| 72 | + if not g_localPlayer or not g_localPlayer.rootNode then return end |
| 73 | + local activSys = g_currentMission and g_currentMission.activatableObjectsSystem |
| 74 | + if not activSys then return end |
| 75 | + |
| 76 | + local px, py, pz = getWorldTranslation(g_localPlayer.rootNode) |
| 77 | + |
| 78 | + for id, zone in pairs(self._zones) do |
| 79 | + local cfg = zone.record and zone.record.config |
| 80 | + local wx = cfg and cfg.worldX |
| 81 | + local wz = cfg and cfg.worldZ |
| 82 | + if wx and wz then |
| 83 | + local wy = cfg.worldY or 0 |
| 84 | + local dx = px - wx |
| 85 | + local dy = py - wy |
| 86 | + local dz = pz - wz |
| 87 | + local distSq = dx * dx + dy * dy + dz * dz |
| 88 | + local near = distSq <= CTWorldManager.INTERACT_RADIUS_SQ |
| 89 | + |
| 90 | + if near ~= zone.inRange then |
| 91 | + zone.inRange = near |
| 92 | + if near then |
| 93 | + activSys:addActivatable(zone.activatable) |
| 94 | + Logger.debug("CTWorldManager: player entered zone " .. id) |
| 95 | + else |
| 96 | + activSys:removeActivatable(zone.activatable) |
| 97 | + Logger.debug("CTWorldManager: player left zone " .. id) |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | + end |
| 102 | +end |
| 103 | + |
| 104 | +---Clean up all zones on mod unload. |
| 105 | +function CTWorldManager:delete() |
| 106 | + local activSys = g_currentMission and g_currentMission.activatableObjectsSystem |
| 107 | + for id, zone in pairs(self._zones) do |
| 108 | + if zone.inRange and activSys then |
| 109 | + activSys:removeActivatable(zone.activatable) |
| 110 | + end |
| 111 | + end |
| 112 | + self._zones = {} |
| 113 | + Logger.module("CTWorldManager", "Cleaned up") |
| 114 | +end |
| 115 | + |
| 116 | +-- --------------------------------------------------------------------------- |
| 117 | +-- Internal |
| 118 | +-- --------------------------------------------------------------------------- |
| 119 | + |
| 120 | +function CTWorldManager:_removeZone(id, zone) |
| 121 | + if zone.inRange then |
| 122 | + local activSys = g_currentMission and g_currentMission.activatableObjectsSystem |
| 123 | + if activSys then |
| 124 | + activSys:removeActivatable(zone.activatable) |
| 125 | + end |
| 126 | + end |
| 127 | + self._zones[id] = nil |
| 128 | + Logger.debug("CTWorldManager: zone removed for " .. tostring(id)) |
| 129 | +end |
0 commit comments