Some work on implementing underpill.
[poca-love.git] / anim.lua
blobe4bc255d46c947d27079488c0d1521fbbfaa497c
1 anim = {}
3 function animate(thing, dt, index)
4 --[[ This function keeps timing of running animations,
5 which are represented by "anim" array and drawn elsewhere.
6 Member has to have at least step, laststep, delay and speed defined.
7 Optional: paused, delay - postpones animation;
8 after[s]() is function to be executed after step s
9 update() is function to be executed at each step
11 if thing.paused then return end
12 thing.delay = thing.delay - dt
13 if thing.delay < 0 then
14 if thing.update then thing:update() end
15 if thing.after and thing.after[thing.step] then thing.after[thing.step]() end
16 thing.delay = thing.speed
17 thing.step = thing.step + 1
18 if thing.step > thing.laststep then
19 anim[index] = nil
20 end
21 end
22 end
25 function update_anim(dt)
26 if game.phase == 'play' then return end
27 for a, b in pairs(anim) do animate(b, dt, a) end
28 if not next(anim) then
29 next_phase()
30 end
31 end
34 function rotate_arrows(x, y, turns)
35 turns = turns or 1
36 local laststep = 6
37 table.insert(anim, {
38 paused = false,
39 step = 1,
40 laststep = laststep,
41 after = {[laststep] = function(self)
42 -- after animation, switch shape and redefine arrows
43 local pf = playfield[x][y].memory or playfield[x][y]
44 if turns % 2 == 1 then
45 if pf.shape == 'x' then pf.shape = '+' else pf.shape = 'x' end
46 end
47 pf.arrows = shape_to_arrows(pf.shape)
48 pf.center = nil
49 playfield[x][y].phase = 0
50 end},
51 delay = 0,
52 speed = 0.1,
53 update = function(self)
54 playfield[x][y].phase = self.step / 5 * turns
55 end
57 end
60 function explode_arrow(x, y, direction, distance, kill, target)
61 -- arrows with kill=true show dead player at the end of animation
62 local laststep = distance * 10 - 5
63 table.insert(anim, {
64 paused = paused,
65 step = 1,
66 laststep = laststep,
67 after = {[laststep] = function(self)
68 playfield[x][y].arrows[direction] = false
69 if kill then
70 game.showkill = true
71 -- remove arrows so they don't cause errror when reviving memories
72 playfield[playfield.clickedx][playfield.clickedy].arrows = nil
73 end
74 -- yellow arrows are marked with dot here, not earlier,
75 -- so that it blends with exploding arrow animation
76 if target then target.center = 'dot' end
77 end},
78 delay = 0,
79 speed = 0.02,
80 update = function(self)
81 playfield[x][y].arrows[direction] = self.step / 10
82 end
84 end
87 function grow_arrows(x, y)
88 -- this animation grows base of regenerating arrows
89 -- by changing the phase from -1 to 0
90 playfield[x][y].phase = -1
91 local laststep = 5
92 table.insert(anim, {
93 paused = false,
94 step = 1,
95 laststep = laststep,
96 delay = 0,
97 speed = 0.1,
98 update = function(self)
99 playfield[x][y].phase = (self.step - laststep) / laststep
105 function animate_gifts()
106 game.phase = 'opengifts'
107 playfield.giftsphase = 0
108 local laststep = 10
109 table.insert(anim, {
110 paused = false,
111 step = 1,
112 laststep = laststep,
113 after = {[laststep] = function(self)
114 -- unset ingift for opened gifts in case some additional gifts be created
115 inplayfield(function (x, y, p) p.ingift = nil end)
116 end},
117 delay = 0,
118 speed = 0.1,
119 update = function(self)
120 playfield.giftsphase = 1 - (laststep - self.step) / laststep
126 function animate_arrow_gift(x, y)
127 --game.phase = 'openarrowgift'
128 playfield[x][y].arrowgiftphase = .4
129 local laststep = 10
130 table.insert(anim, {
131 paused = false,
132 step = 1,
133 laststep = laststep,
134 after = {[laststep] = function(self)
135 -- acknowledge arrowgift
136 playfield[x][y].arrowgiftphase = nil
137 playfield[x][y] = playfield[x][y].arrowgift
138 if playfield[x][y].kind == 'gift' then fill_gifts(game.currentlevel) end
139 end},
140 delay = 0,
141 speed = 0.1,
142 update = function(self)
143 playfield[x][y].arrowgiftphase = .4 + .6 * self.step / laststep