-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspritesheet.lua
52 lines (43 loc) · 1.42 KB
/
spritesheet.lua
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
local SpriteSheet = Object:extend()
--- Instance a new SpriteSheet
-- @param image Image of the tileset
-- @param quad Portion of the image
-- @param tile_size_x Width of the tiles
-- @param tile_sizey Height of the tiles
function SpriteSheet:new(image, quad, tile_size_x, tile_size_y)
self.txt = txt
self.image = image
self.ss_x, self.ss_y, self.ss_w, self.ss_h = quad:getViewport()
self.ss_w = self.ss_w + self.ss_x
self.ss_h = self.ss_h + self.ss_y
self.tile_size_x = tile_size_x
self.tile_size_y = tile_size_y
self.quads = {}
local count = 0
for x = self.ss_x, self.ss_w - self.tile_size_x, self.tile_size_x do
count = count + 1
table.insert(self.quads, {})
for y=self.ss_y, self.ss_h - self.tile_size_y, self.tile_size_y do
local quad = love.graphics.newQuad(x, y, tile_size_x,
tile_size_y,
image:getDimensions())
table.insert(self.quads[count], quad)
end
end
end
--- Get image of the SpriteSheet
-- @return image
function SpriteSheet:get_image()
return self.image
end
--- Get a tile's quad
-- @return quad
function SpriteSheet:get_quad(x, y)
return self.quads[x][y]
end
--- Get width and height of the main quad
-- @return { w, h }
function SpriteSheet:get_quads_len()
return { #self.quads, #self.quads[1] }
end
return SpriteSheet