-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResourceManager.lua
More file actions
125 lines (105 loc) · 3.53 KB
/
ResourceManager.lua
File metadata and controls
125 lines (105 loc) · 3.53 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
-- Manages loading and accessing game resources like images, fonts, sounds, and animations.
ResourceManager = {}
ResourceManager.__index = ResourceManager
function ResourceManager:new()
local manager = {
images = {},
fonts = {},
sounds = {},
animations = {},
dict = {}
}
setmetatable(manager, self)
return manager
end
function ResourceManager:loadFont(name, path, size)
if not self.fonts[name] then
self.fonts[name] = love.graphics.newFont(path, size, "mono")
self.fonts[name]:setLineHeight(0.75)
end
return self.fonts[name]
end
function ResourceManager:getFont(name)
return self.fonts[name]
end
function ResourceManager:loadImage(name, path)
if not self.images[name] then
self.images[name] = love.graphics.newImage(path)
end
return self.images[name]
end
function ResourceManager:getImage(name)
return self.images[name]
end
function ResourceManager:loadAllAssets(cardNames)
if not cardNames then
error("ResourceManager:loadAllAssets(cardNames) requires cardNames")
end
-- Load fonts
self:loadFont("fontXL", "assets/fonts/Habbo.ttf", 96)
self:loadFont("fontL", "assets/fonts/Habbo.ttf", 48)
self:loadFont("fontM", "assets/fonts/Habbo.ttf", 32)
self:loadFont("fontS", "assets/fonts/Habbo.ttf", 16)
-- Load images
self:loadImage("background", "assets/background.png")
self:loadImage("wizardIdle", "assets/wizardIdle.png")
self:loadImage("wizardDeath", "assets/wizardDeath.png")
self:loadImage("wizardCast", "assets/wizardCast.png")
self:loadImage("placeholder", "assets/placeholder.png")
-- Load all card images
for i, cardName in ipairs(cardNames) do
local path = "assets/cards/" .. cardName .. ".png"
local cardImageFile = io.open(path, "r")
if cardImageFile then
cardImageFile:close()
self:loadImage("card_" .. cardName, path)
end
if not self:getImage("card_" .. cardName) then
self.images["card_" .. cardName] = self:getImage("placeholder")
end
end
self:loadDictionary()
end
function ResourceManager:newAnimation(imageName, width, height)
local animation = {}
animation.spriteSheet = self.images[imageName]
animation.quads = {}
width = width or SPRITE_PIXEL_SIZE
height = height or SPRITE_PIXEL_SIZE
for y = 0, animation.spriteSheet:getHeight() - height, height do
for x = 0, animation.spriteSheet:getWidth() - width, width do
table.insert(animation.quads, lg.newQuad(x, y, width, height, animation.spriteSheet:getDimensions()))
end
end
local fps = 12
animation.frameDuration = 1 / fps
animation.currentFrame = 1
animation.accumulator = 0
animation.timeLeft = nil
animation.playMode = nil -- loop | once | loop_for
-- transformations
animation.rotation = 0
animation.scaleX = PIXEL_TO_GAME_SCALE
animation.scaleY = PIXEL_TO_GAME_SCALE
animation.offsetX = 0
animation.offsetY = 0
return animation
end
function ResourceManager:loadDictionary()
local file = io.open("assets/dict.txt", "r")
if not file then
error("Could not open dictionary file.")
end
self.dict = {}
for line in file:lines() do
local word = line:match("^%s*(.-)%s*$") -- trim whitespace
if word ~= "" then
table.insert(self.dict, word)
end
end
file:close()
end
function ResourceManager:getRandomWord()
local index = math.random(#self.dict)
return self.dict[index]
end