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
|
-- `Cloud`
-- That white thing moving in the background.
-- WHOLE CODE HAS FLAG OF "need a cleanup"
-- Metatable of `Cloud`
Cloud = {
x = 0, -- position horizontal
y = 0, -- position vertical
t = 1, -- type (sprite number)
v = 13, -- velocity
sprite = love.graphics.newImage("assets/clouds.png"),
quads = {
[1] = love.graphics.newQuad( 1, 1, 158,47, 478,49),
[2] = love.graphics.newQuad(160, 1, 158,47, 478,49),
[3] = love.graphics.newQuad(319, 1, 158,47, 478,49)
}
}
-- Constructor of `Cloud`
function Cloud:new(x, y, t, v)
-- Meta
local o = {}
setmetatable(o, self)
self.__index = self
-- Set filter
local min, mag = self.sprite:getFilter()
if min ~= "nearest" or
mag ~= "nearest" then
self.sprite:setFilter("nearest", "nearest")
end
-- Init
o.x = x or self.x
o.y = y or self.y
o.t = t or self.t
o.v = v or self.v
return o
end
-- Update of `Cloud`, returns x for world to delete cloud after reaching right corner
function Cloud:update(dt)
self.x = self.x + self.v*dt
return self.x
end
-- Draw `Cloud`
function Cloud:draw(offset_x, offset_y, scale)
-- defaults
local offset_x = offset_x or 0
local offset_y = offset_y or 0
local scale = scale or 1
-- draw
love.graphics.setColor(255,255,255,255)
love.graphics.draw(self.sprite, self.quads[self.t], (self.x+offset_x)*scale, (self.y+offset_y)*scale, 0, scale, scale)
end
|