classes, better debugging and spritesheets
This commit is contained in:
parent
24b94f4a59
commit
1e299dfa84
8 changed files with 660 additions and 62 deletions
160
classes.lua
160
classes.lua
|
@ -1,4 +1,27 @@
|
|||
|
||||
local function newImage(path, alphaColor)
|
||||
if (not alphaColor) then
|
||||
return love.graphics.newImage(path)
|
||||
end
|
||||
|
||||
local imageData = love.image.newImageData(path)
|
||||
|
||||
imageData:mapPixel(function(x, y, r, g, b, a)
|
||||
if (r == alphaColor.r and
|
||||
g == alphaColor.g and
|
||||
b == alphaColor.b and
|
||||
a == alphaColor.a) then
|
||||
return 0, 0, 0, 0
|
||||
end
|
||||
|
||||
return r, g, b, a
|
||||
end)
|
||||
|
||||
local image = love.graphics.newImage(imageData)
|
||||
|
||||
return image
|
||||
end
|
||||
|
||||
--[[
|
||||
Sprite Class
|
||||
This is essentially just a Wrapper for LÖVEs Image
|
||||
|
@ -7,10 +30,10 @@
|
|||
Sprite = {}
|
||||
Sprite.__index = Sprite
|
||||
|
||||
function Sprite.new(path)
|
||||
function Sprite.new(path, alphaColor)
|
||||
local self = setmetatable({}, Sprite)
|
||||
|
||||
self.image = love.graphics.newImage(path)
|
||||
self.image = newImage(path, alphaColor)
|
||||
|
||||
return self
|
||||
end
|
||||
|
@ -21,41 +44,150 @@ end
|
|||
|
||||
|
||||
--[[
|
||||
Audio Class
|
||||
Spritesheet Class
|
||||
This is essentially just a Wrapper for LÖVEs Image, specifically made for Spritesheets
|
||||
]]--
|
||||
|
||||
Spritesheet = {}
|
||||
Spritesheet.__index = Spritesheet
|
||||
|
||||
function Spritesheet.new(path, width, height, alphaColor)
|
||||
local self = setmetatable({}, Spritesheet)
|
||||
|
||||
self.sprites = {}
|
||||
|
||||
if (string.find(path, ".json")) then
|
||||
local contents, size = love.filesystem.read(path)
|
||||
local sliceData = json.decode(contents)
|
||||
|
||||
print("games/" .. sliceData.image)
|
||||
self.image = newImage("games/" .. sliceData.image, alphaColor)
|
||||
|
||||
-- Create the slices according to the sliceData
|
||||
local imageWidth, imageHeight = self.image:getWidth(), self.image:getHeight()
|
||||
|
||||
local counter = 0
|
||||
for _, data in pairs(sliceData) do
|
||||
table.insert(self.sprites, love.graphics.newQuad(
|
||||
data.x,
|
||||
data.y,
|
||||
data.width,
|
||||
data.height,
|
||||
imageWidth,
|
||||
imageHeight
|
||||
))
|
||||
counter = counter + 1
|
||||
end
|
||||
|
||||
self.Count = counter
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
self.image = newImage(path, alphaColor)
|
||||
|
||||
-- Generate slices
|
||||
local imageWidth, imageHeight = self.image:getWidth(), self.image:getHeight()
|
||||
local columns = math.floor(imageWidth / width)
|
||||
local rows = math.floor(imageHeight / height)
|
||||
|
||||
local counter = 0
|
||||
for x = 0, columns - 1 do
|
||||
for y = 0, rows - 1 do
|
||||
table.insert(self.sprites, love.graphics.newQuad(
|
||||
x * width,
|
||||
y * height,
|
||||
width,
|
||||
height,
|
||||
imageWidth,
|
||||
imageHeight
|
||||
))
|
||||
counter = counter + 1
|
||||
end
|
||||
end
|
||||
|
||||
self.Count = counter
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
function Spritesheet:Draw(slice, x, y)
|
||||
local sprite = self.sprites[slice]
|
||||
if sprite then
|
||||
love.graphics.draw(self.image, sprite, x or 0, y or 0)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
--[[
|
||||
Sound Class
|
||||
This is essentially just a Wrapper for LÖVEs Source
|
||||
]]--
|
||||
|
||||
Audio = {}
|
||||
Audio.__index = Audio
|
||||
Sound = {}
|
||||
Sound.__index = Sound
|
||||
|
||||
function Audio.new(path, soundType)
|
||||
local self = setmetatable({}, Audio)
|
||||
function Sound.new(path, soundType)
|
||||
local self = setmetatable({}, Sound)
|
||||
|
||||
self.source = love.audio.newSource(path, soundType or "static")
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
function Audio:Play()
|
||||
function Sound:Play()
|
||||
self.source:play()
|
||||
end
|
||||
|
||||
function Audio:Stop()
|
||||
function Sound:Stop()
|
||||
self.source:stop()
|
||||
end
|
||||
|
||||
function Audio:Pause()
|
||||
function Sound:Pause()
|
||||
self.source:pause()
|
||||
end
|
||||
|
||||
function Audio:Resume()
|
||||
function Sound:Resume()
|
||||
self.source:play()
|
||||
end
|
||||
|
||||
function Audio:SetLooping(bool)
|
||||
function Sound:SetLooping(bool)
|
||||
self.source:setLooping(bool)
|
||||
end
|
||||
|
||||
function Audio:SetVolume(volume)
|
||||
function Sound:SetVolume(volume)
|
||||
self.source:setVolume(volume)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
--[[
|
||||
Color Class
|
||||
A convenient Color class that works with any love2D function
|
||||
]]--
|
||||
|
||||
function Color(r, g, b, a)
|
||||
r = (r or 255) / 255
|
||||
g = (g or 255) / 255
|
||||
b = (b or 255) / 255
|
||||
a = (a or 255) / 255
|
||||
|
||||
local color = {
|
||||
r = r, g = g, b = b, a = a,
|
||||
[1] = r, [2] = g, [3] = b, [4] = a -- love2D expects a sequential array, so heres the fix for that
|
||||
}
|
||||
|
||||
-- Return it as a class, that way we can use it freely anywhere we wish
|
||||
return setmetatable(color, {
|
||||
__index = function(tbl, key)
|
||||
if key == 1 then return tbl.r
|
||||
elseif key == 2 then return tbl.g
|
||||
elseif key == 3 then return tbl.b
|
||||
elseif key == 4 then return tbl.a
|
||||
else return rawget(tbl, key)
|
||||
end
|
||||
end,
|
||||
__tostring = function(tbl)
|
||||
return tbl
|
||||
end
|
||||
})
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue