Update to gui cams

This commit is contained in:
Cooldude2606
2018-09-11 17:45:42 +01:00
parent c37e66be27
commit 86f61dd17c

View File

@@ -66,32 +66,39 @@ function Gui.set_dropdown_index(dropdown,_item)
end end
Event.register(-1,function(event) Event.register(-1,function(event)
Server.new_thread{ if Server and Server._thread then
name='camera-follow', Server.new_thread{
data={cams={},cam_index=1,players={}} name='camera-follow',
}:on_event('tick',function(self) data={cams={},cam_index=1,players={}}
local _cam = self.data.cams[self.data.cam_index] }:on_event('tick',function(self)
if not _cam then self.data.cam_index = 1 _cam = self.data.cams[self.data.cam_index] end local update = 4
if not _cam then return end if self.data.cam_index > #self.data.cams then self.data.cam_index = 1 end
if not _cam.cam.valid then table.remove(self.data.cams,self.data.cam_index) if update > #self.data.cams then update = #self.data.cams end
elseif not _cam.entity.valid then table.remove(self.data.cams,self.data.cam_index) for cam_offset = 0,update do
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 local _cam = self.data.cams[self.data.cam_index+cam_offset]
end if not _cam then break end
end):on_event('error',function(self,err) if not _cam.cam.valid then table.remove(self.data.cams,self.data.cam_index)
-- posible error handling if needed elseif not _cam.entity.valid then table.remove(self.data.cams,self.data.cam_index)
error(err) else _cam.cam.position = _cam.entity.position if not _cam.surface then _cam.cam.surface_index = _cam.entity.surface.index end
end):on_event(defines.events.on_player_respawned,function(self,event) end
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 end
for _,index in pairs(remove) do self.data.cam_index = self.data.cam_index+update
table.remove(self.data.players[event.player_index],index) 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
if not cam.valid then table.insert(remove,index)
else cam.entity=Game.get_player(event).character end
end
for _,index in pairs(remove) do
table.remove(self.data.players[event.player_index],index)
end
end end
end end):open()
end):open() end
end) end)
--- Adds a camera that updates every tick (or less depeading on how many are opening) it will move to follow an entity --- Adds a camera that updates every tick (or less depeading on how many are opening) it will move to follow an entity
@@ -153,31 +160,40 @@ function Gui.cam_link(data)
return data.cam return data.cam
end end
Event.register(defines.events.on_tick, function(event) Event.register(-1,function(event)
if Gui.left and ((event.tick+10)/(3600*game.speed)) % 15 == 0 then if not Server or not Server._thread then
Gui.left.update() Event.register(defines.events.on_tick, function(event)
end if Gui.left and ((event.tick+10)/(3600*game.speed)) % 15 == 0 then
if Gui._global().cams and is_type(Gui._global().cams,'table') and #Gui._global().cams > 0 then Gui.left.update()
local _cam = Gui._global().cams[Gui._global().cam_index] end
if not _cam then Gui._global().cam_index = 1 _cam = Gui._global().cams[Gui._global().cam_index] end if Gui._global().cams and is_type(Gui._global().cams,'table') and #Gui._global().cams > 0 then
if not _cam then return end local update = 4
if not _cam.cam.valid then table.remove(Gui._global().cams,Gui._global().cam_index) if Gui._global().cam_index > #Gui._global().cams then Gui._global().cam_index = 1 end
elseif not _cam.entity.valid then table.remove(Gui._global().cams,Gui._global().cam_index) if update > #Gui._global().cams then update = #Gui._global().cams end
else _cam.cam.position = _cam.entity.position if not _cam.surface then _cam.cam.surface_index = _cam.entity.surface.index end Gui._global().cam_index = Gui._global().cam_index+1 for cam_offset = 0,update do
end local _cam = Gui._global().cams[Gui._global().cam_index]
end if not _cam then break end
end) if not _cam.cam.valid then table.remove(Gui._global().cams,Gui._global().cam_index)
elseif not _cam.entity.valid then table.remove(Gui._global().cams,Gui._global().cam_index)
else _cam.cam.position = _cam.entity.position if not _cam.surface then _cam.cam.surface_index = _cam.entity.surface.index end Gui._global().cam_index = Gui._global().cam_index+1
end
end
Gui._global().cam_index = Gui._global().cam_index+update
end
end)
Event.register(defines.events.on_player_respawned,function(event) Event.register(defines.events.on_player_respawned,function(event)
if Gui._global().players and is_type(Gui._global().players,'table') and #Gui._global().players > 0 and Gui._global().players[event.player_index] then if Gui._global().players and is_type(Gui._global().players,'table') and #Gui._global().players > 0 and Gui._global().players[event.player_index] then
local remove = {} local remove = {}
for index,cam in pairs(Gui._global().players[event.player_index]) do for index,cam in pairs(Gui._global().players[event.player_index]) do
if not cam.valid then table.insert(remove,index) if not cam.valid then table.insert(remove,index)
else cam.entity=Game.get_player(event).character end else cam.entity=Game.get_player(event).character end
end end
for _,index in pairs(remove) do for _,index in pairs(remove) do
table.remove(Gui._global().players[event.player_index],index) table.remove(Gui._global().players[event.player_index],index)
end end
end
end)
end end
end) end)