-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGhostingContainer.lua
More file actions
129 lines (109 loc) · 3.93 KB
/
GhostingContainer.lua
File metadata and controls
129 lines (109 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
-- ghosting container shit
require 'GhostingSprite'
GhostingContainer = {}
GhostingContainer._inactiveGhostingSpritesPool = {}
GhostingContainer.spawnRate = 0
GhostingContainer.nextSpawnTime = 0
GhostingContainer.trailLength = 1
--GhostingContainer.sortingLayer = 0
GhostingContainer.desiredAlpha = .8*255
GhostingContainer._ghostingSpritesQueue = {}
-- table.getn(t)
GhostingContainer.hasStarted = false
GhostingContainer.effectDuration = 1
GhostingContainer.refSpriteAnimator = nil
GhostingContainer.ghostingReference = nil
function GhostingContainer._inactiveGhostingSpritesPool:get()
end
function GhostingContainer:new(o)
o = o or {} -- create object if user does not provide one
setmetatable(o, self)
self.__index = self
return o
end
function GhostingContainer:init(maxGhosts,spawnRate,sprite,effectDuration,ghostingReference)
self.trailLength = maxGhosts
self.spawnRate = spawnRate
self.effectDuration = maxGhosts * spawnRate * .95
self.refSpriteAnimator = sprite
self.nextSpawnTime = love.timer.getTime() + spawnRate
--self.sortingLayer = refSprite
self.hasStarted = true
self.ghostingReference = ghostingReference
end
function GhostingContainer:StopEffect()
self.hasStarted = false
end
function GhostingContainer:StartEffect()
self.hasStarted = true
end
function GetCurrentSprite(animator)
local frame = animator.action[animator.actual]
return frame.img[frame.position]
end
function GhostingContainer:InitializeGhost(ghost)
ghost:init(self.effectDuration,self.desiredAlpha,GetCurrentSprite(self.refSpriteAnimator),0,self.ghostingReference,0,0) -- offset might be player.y and player.x
end
function GhostingContainer:GetNextGhost()
for _,ghost in ipairs(self._inactiveGhostingSpritesPool) do
if ghost.canBeReused then
self:InitializeGhost(ghost)
return ghost
end
end
return GhostingSprite:new()
-- body
end
function GhostingContainer:update()
for _,ghost in ipairs(self._ghostingSpritesQueue) do
ghost:update()
if ghost.a <= 0 then
local newGhost = table.remove(self._ghostingSpritesQueue,_)
--table.insert(self._inactiveGhostingSpritesPool,newGhost)
end
end
if self.hasStarted then
if love.timer.getTime() >= self.nextSpawnTime then
if table.getn(self._ghostingSpritesQueue) == self.trailLength then
local peekedGhostingSprite = self._ghostingSpritesQueue[1]
local canBeReused = peekedGhostingSprite.canBeReused
--if canBeReused then
-- initialize the new ghosting sprite
--peekedGhostingSprite = GhostingSprite:new()
table.remove(self._ghostingSpritesQueue,1) -- dequeue
self:InitializeGhost(peekedGhostingSprite)
table.insert(self._ghostingSpritesQueue,peekedGhostingSprite) -- enqueue
self.nextSpawnTime = self.nextSpawnTime + self.spawnRate
--else
--end
end
-- if count is less than length, we need to create a new ghosting sprite
if table.getn(self._ghostingSpritesQueue) < self.trailLength then
local newGhostSprite = self:GetNextGhost()
self:InitializeGhost(newGhostSprite)
table.insert(self._ghostingSpritesQueue, newGhostSprite)
self.nextSpawnTime = self.nextSpawnTime + self.spawnRate
end
--if greater than dequeue items off the queue, they aren't needed
if table.getn(self._ghostingSpritesQueue) > self.trailLength then
local difference = table.getn(self._ghostingSpritesQueue) - self.trailLength
for i = 1,difference do
local inactiveGhost = table.remove(self._ghostingSpritesQueue,1) -- pop off the queue
table.insert(self._inactiveGhostingSpritesPool, inactiveGhost)
end
return
end
end
--elseif (love.timer.getTime() >= self.nextSpawnTime) and self._ghostingSpritesQueue[1] ~= nil then
elseif (love.timer.getTime()>= self.nextSpawnTime) then
--table.remove(self._ghostingSpritesQueue,1)
self.nextSpawnTime = self.nextSpawnTime + self.spawnRate
end
end
function GhostingContainer:draw()
--if self.hasStarted then
for _,ghost in ipairs(self._ghostingSpritesQueue) do
ghost:draw()
end
--end
end