-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResourceManager.lua
More file actions
151 lines (130 loc) · 4.42 KB
/
Copy pathResourceManager.lua
File metadata and controls
151 lines (130 loc) · 4.42 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
local Text = require("util.Text")
-- Manages loading and accessing game resources like images, fonts, sounds, and animations.
ResourceManager = {}
ResourceManager.__index = ResourceManager
function ResourceManager:new()
local manager = {
shaders = {},
images = {},
fonts = {},
sounds = {},
animations = {},
dict = {}
}
setmetatable(manager, self)
return manager
end
function ResourceManager:loadShader(name, code)
if not self.shaders[name] then
self.shaders[name] = love.graphics.newShader(code)
end
return self.shaders[name]
end
function ResourceManager:getShader(name)
return self.shaders[name]
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:loadSound(name, path)
if not self.sounds[name] then
self.sounds[name] = love.audio.newSource(path, "static")
end
return self.sounds[name]
end
function ResourceManager:getSound(name)
return self.sounds[name]
end
function ResourceManager:loadCharacterAssets(characterManager)
for _, def in pairs(characterManager.characters) do
local base = "assets/characters/" .. def.name .. "/"
self:loadImage(def.idleSprite, base .. def.idleSprite .. ".png")
self:loadImage(def.castSprite, base .. def.castSprite .. ".png")
self:loadImage(def.deathSprite, base .. def.deathSprite .. ".png")
self:loadImage(def.spellPlaceholderSprite, base .. def.spellPlaceholderSprite .. ".png")
end
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")
-- 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
end
-- Load sounds
self:loadSound("music", "assets/sounds/music.mp3")
self:loadSound("hurt", "assets/sounds/hurt.wav")
-- 0.0 is grayscale, 1.0 is full color
self:loadShader("saturation", [[
extern float saturation;
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) {
vec4 pixel = Texel(texture, texture_coords);
float gray = dot(pixel.rgb, vec3(0.299, 0.587, 0.114));
vec3 grayscale = vec3(gray);
return vec4(mix(grayscale, pixel.rgb, saturation), pixel.a) * color;
}
]])
self:loadDictionary()
end
function ResourceManager:newAnimation(imageName, playMode, duration, width, height)
local playMode = playMode or "once"
local duration = duration or math.huge
local image = self.images[imageName]
if not image then
error("ResourceManager:newAnimation missing image '" .. tostring(imageName) .. "'")
end
local animation = Animation:new(image, width, height)
animation:setPlayMode(playMode, duration)
return animation
end
function ResourceManager:loadDictionary()
local file = io.open("assets/dictionaries/latinDict.txt", "r")
if not file then
error("Could not open dictionary file.")
end
self.dict = {}
for line in file:lines() do
local word = Text.trim(line)
if word ~= "" then
table.insert(self.dict, word)
end
end
file:close()
end
function ResourceManager:getRandomWords(count)
local words = {}
for i = 1, count do
local index = math.random(#self.dict)
table.insert(words, self.dict[index])
end
return words
end