Skip to content

Commit 9600136

Browse files
authored
Merge pull request #1340 from uspgamedev/fix/animation-lag
Stop wasting frames on non-animations
2 parents 36a7f33 + 67c5efe commit 9600136

File tree

6 files changed

+66
-30
lines changed

6 files changed

+66
-30
lines changed

game/domain/actor.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,12 @@ function Actor:canSee(target)
486486
if sector ~= target_sector then
487487
return false
488488
end
489-
local fov = self:getFov(sector)
490489
local i, j = target:getPos()
490+
return self:canSeePosition(i, j)
491+
end
492+
493+
function Actor:canSeePosition(i, j)
494+
local fov = self:getFov(self:getSector())
491495
local visible = fov[i][j]
492496
return visible and visible > 0
493497
end

game/gamestates/animation.lua

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
local SWITCHER = require 'infra.switcher'
23
local INPUT = require 'input'
34
local Util = require "steaming.util"
45
local Draw = require "draw"
@@ -11,23 +12,19 @@ local state = {}
1112
local _animation_task
1213
local _view
1314
local _alert
14-
local _route
15-
local _report
1615

1716
--[[ LOCAL FUNCTIONS ]]--
1817

1918
--[[ STATE FUNCTIONS ]]--
2019

21-
function state:init()
20+
function state:init() -- luacheck: no self
2221
-- dunno
2322
end
2423

25-
function state:enter(_, route, view, report)
24+
function state:enter(_, route, view, report) -- luacheck: no self
2625

2726
_view = view
2827
_alert = false
29-
_route = route
30-
_report = report
3128

3229
local ok, animation = pcall(function () return ANIMATIONS[report.type] end)
3330
if ok then
@@ -42,14 +39,13 @@ function state:enter(_, route, view, report)
4239

4340
end
4441

45-
function state:leave()
42+
function state:leave() -- luacheck: no self
4643

47-
_report = nil
4844
Util.destroyAll()
4945

5046
end
5147

52-
function state:update(dt)
48+
function state:update(_) -- luacheck: no self
5349

5450
if INPUT.wasAnyPressed(0.5) then
5551
_alert = true
@@ -63,7 +59,7 @@ function state:update(dt)
6359

6460
end
6561

66-
function state:draw()
62+
function state:draw() -- luacheck: no self
6763

6864
Draw.allTables()
6965

game/gamestates/play.lua

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,15 @@ local function _playTurns(...)
6161
_activity:changeSector(...)
6262
elseif request == "report" then
6363
local player = _route.getControlledActor()
64-
if extra.actor ~= player then
65-
_view.action_hud:disableTurn()
64+
local body = extra.body or (extra.actor and extra.actor:getBody())
65+
if body and player:canSee(body) then
66+
if extra.actor ~= player then
67+
_view.action_hud:disableTurn()
68+
end
69+
SWITCHER.push(GS.ANIMATION, _route, _view, extra)
70+
else
71+
return _playTurns()
6672
end
67-
SWITCHER.push(GS.ANIMATION, _route, _view, extra)
6873
end
6974
_next_action = nil
7075
end

game/infra/switcher.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
local Gamestate = require "steaming.extra_libs.hump.gamestate"
3-
local Queue = require 'lux.common.Queue'
43
local INPUT = require 'input'
54

65
local SWITCHER = {}
@@ -22,7 +21,7 @@ local _switched = false
2221

2322
function SWITCHER.init()
2423
for _,handle in ipairs(_INPUT_HANDLES) do
25-
love[handle] = function (...)
24+
love[handle] = function (...) -- luacheck: globals love
2625
return Gamestate[handle](...)
2726
end
2827
end

game/main.lua

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,38 @@ function love.load(arg)
8585
})
8686
end
8787

88+
--local SAMPLES = 10
89+
--local profiling = {}
90+
--local last_state
91+
--
92+
--function updateProfiling(dt)
93+
-- local state = last_state
94+
-- last_state = SWITCHER.current()
95+
-- if not state then return end
96+
-- local name = "???"
97+
-- for k,v in pairs(GS) do
98+
-- if v == state then
99+
-- name = k
100+
-- break
101+
-- end
102+
-- end
103+
-- if dt > 0.2 then
104+
-- print("lag on state", name)
105+
-- end
106+
-- local sample = profiling[name] or { times = {} , n = 1 }
107+
-- sample.times[sample.n] = dt
108+
-- sample.n = (sample.n % SAMPLES) + 1
109+
-- profiling[name] = sample
110+
--end
111+
--
112+
--function average(sample)
113+
-- local sum = 0
114+
-- for _,time in ipairs(sample.times) do
115+
-- sum = sum + time
116+
-- end
117+
-- return sum / math.max(#sample.times, SAMPLES)
118+
--end
119+
88120
function love.update(dt)
89121
MAIN_TIMER:update(dt)
90122
if INPUT.wasActionReleased('QUIT') then
@@ -97,6 +129,7 @@ function love.update(dt)
97129
SWITCHER.push(GS.DEVMODE)
98130
end
99131
SWITCHER.update(dt)
132+
--updateProfiling(dt)
100133
INPUT.flush() -- must be called afterwards
101134
Util.updateSubtype(dt, 'task')
102135
Draw.update(dt)
@@ -106,6 +139,16 @@ end
106139

107140
function love.draw()
108141
SWITCHER.draw()
142+
--local g = love.graphics
143+
--g.push()
144+
--g.origin()
145+
--g.setColor(1,1,1)
146+
--local i = 0
147+
--for name,sample in pairs(profiling) do
148+
-- g.print(("%s: %.2f"):format(name, 1 / average(sample)), 32, 32+i*32)
149+
-- i = i + 1
150+
--end
151+
--g.pop()
109152
end
110153

111154
function love.quit()

game/view/sector.lua

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ local DIALOGUEBOX = require 'view.dialoguebox'
1818
local SPRITEFX = require 'lux.pack' 'view.spritefx'
1919
local PLAYSFX = require 'helpers.playsfx'
2020
local vec2 = require 'cpml'.vec2
21-
local Util = require "steaming.util"
2221
local Class = require "steaming.extra_libs.hump.class"
2322
local ELEMENT = require "steaming.classes.primitives.element"
2423

@@ -159,19 +158,9 @@ function SectorView:startVFX(extra)
159158
if extra.type then
160159
local spritefx = SPRITEFX[extra.type]
161160
self.vfx = spritefx
162-
MAIN_TIMER:script(function(wait)
163-
local ann = Util.findId('announcement')
164-
if ann:isLocked() then
165-
while ann:isLocked() do wait(1) end
166-
wait(0.2)
167-
end
168-
spritefx.apply(self, extra)
169-
--Play SFX if any
170-
if extra.sfx then
171-
_playSFX(self.target, extra)
172-
end
173-
end)
174-
elseif extra.sfx then
161+
spritefx.apply(self, extra)
162+
end
163+
if extra.sfx then
175164
_playSFX(self.target, extra)
176165
end
177166
end

0 commit comments

Comments
 (0)