From 824a14266f329c22eba3c4e93e15d395b12c37fc Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Tue, 13 Mar 2018 17:03:46 +0000 Subject: [PATCH] Moved Cam Follow Thread --- ExpCore/gui.lua | 59 +++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/ExpCore/gui.lua b/ExpCore/gui.lua index 6c2fc1f9..90e123be 100644 --- a/ExpCore/gui.lua +++ b/ExpCore/gui.lua @@ -64,37 +64,38 @@ function Gui.set_dropdown_index(dropdown,_item) return dropdown end +Event.register(-1,function(event) + Server.new_thread{ + name='camera-follow', + data={cams={},cam_index=1,players={}} + }:on_event('tick',function(self) + local _cam = self.data.cams[self.data.cam_index] + if not _cam then self.data.cam_index = 1 _cam = self.data.cams[self.data.cam_index] end + if not _cam then return end + if not _cam.cam.valid then table.remove(self.data.cams,self.data.cam_index) + elseif not _cam.entity.valid then table.remove(self.data.cams,self.data.cam_index) + else _cam.cam.position = _cam.entity.position if not _cam.surface then _cam.cam.surface_index = _cam.entity.surface.index end self.data.cam_index = self.data.cam_index+1 + end + end):on_event('error',function(self,err) + -- posible error handling if needed + error(err) + end):on_event(defines.events.on_player_respawned,function(self,event) + if self.data.players[event.player_index] then + local remove = {} + for index,cam in pairs(self.data.players[event.player_index]) do + Gui.cam_link{cam=cam,entity=Game.get_player(event).character} + if not cam.valid then table.insert(remove,index) end + end + for _,index in pairs(remove) do + table.remove(self.data.players[event.player_index],index) + end + end + end):open() +end) + local function _thread() local thread = Server.get_thread('camera-follow') - if not thread then - thread = Server.new_thread{ - name='camera-follow', - data={cams={},cam_index=1,players={}} - }:on_event('tick',function(self) - local _cam = self.data.cams[self.data.cam_index] - if not _cam then self.data.cam_index = 1 _cam = self.data.cams[self.data.cam_index] end - if not _cam then return end - if not _cam.cam.valid then table.remove(self.data.cams,self.data.cam_index) - elseif not _cam.entity.valid then table.remove(self.data.cams,self.data.cam_index) - else _cam.cam.position = _cam.entity.position if not _cam.surface then _cam.cam.surface_index = _cam.entity.surface.index end self.data.cam_index = self.data.cam_index+1 - end - end):on_event('error',function(self,err) - -- posible error handling if needed - error(err) - end):on_event(defines.events.on_player_respawned,function(self,event) - if self.data.players[event.player_index] then - local remove = {} - for index,cam in pairs(self.data.players[event.player_index]) do - Gui.cam_link{cam=cam,entity=Game.get_player(event).character} - if not cam.valid then table.insert(remove,index) end - end - for _,index in pairs(remove) do - table.remove(self.data.players[event.player_index],index) - end - end - end) - thread:open() - end + if not thread then error('Camera Follow Thread Is Missing') end return thread end --- Adds a camera that updates every tick (or less depeading on how many are opening) it will move to follow an entity