Asset Manager
The Asset Manager is what is used to store the games textures, fonts, music, soundFx, and shaders. Before the assets can be used in the game, they must be added into the asset manager successfully.
Functions:
add_texture
Function | Description |
---|---|
add_texture(string, string, bool) | Attemps to add a new texture to the asset manager. It takes a string for the asset name, a string for the texture filepath, and a flag to set the texture to pixel art. Returns true if successful, false otherwise |
Example:
-- Add the hero texture to the asset manager
if AssetManager.add_texture("hero", "./assets/textures/hero.png", true) then
print("Added texture [hero] successfully")
else
print("Failed to add the [hero] texture.")
end
-- Consider adding all the textures and their paths to a table and looping.
add_music
Function | Description |
---|---|
add_music(string, string) | Attemps to add a new music object to the asset manager. It takes a string for the asset name, a string for the music filepath. Returns true if successful, false otherwise |
Example:
-- Add the title music to the asset manager
if AssetManager.add_music("title", "./assets/music/title.mp3") then
print("Added music [title] to Asset Manager successfully")
else
print("Failed to add the [title] music to the Asset Manager.")
end
add_soundfx
Function | Description |
---|---|
add_soundfx(string, string) | Attemps to add a new soundFx object to the asset manager. It takes a string for the asset name, a string for the soundFx filepath. Returns true if successful, false otherwise |
Example:
-- Add the sword SoundFx to the asset manager
if AssetManager.add_soundfx("sword", "./assets/soundfx/sword.wav") then
print("Added soundFx [sword] to Asset Manager successfully")
else
print("Failed to add the [sword] soundFx to the Asset Manager.")
end
add_font
Function | Description |
---|---|
add_font(string, string, float) | Attemps to add a new font .ttf object to the asset manager. It takes a string for the font name, a string for the font filepath, and a float for the font size (default 32.0). Returns true if successful, false otherwise |
Example:
-- Add the Pixel font to the asset manager
if AssetManager.add_font("pixel", "./assets/fonts/pixel.ttf", 32.0) then
print("Added Font [pixel] to Asset Manager successfully")
else
print("Failed to add the [pixel] font to the Asset Manager.")
end
Sample Lua Load Assets Function
-- Sample Assets table
AssetDefs =
{
textures =
{
{ name = "test_texture", path = "assets/textures/test_texture.png", pixel_art = true },
},
music =
{
{ name = "test_music", path = "assets/music/test_music.mp3" },
},
sound_fx =
{
{ name = "test_sound", path = "assets/soundfx/test_sound.wav"},
}
}
-- Sample LoadAssets function takes in the above assets table and loads
-- the entries into the AssetManager
function LoadAssets(assets)
for k, v in pairs(assets.textures) do
if not AssetManager.add_texture(v.name, v.path, v.pixel_art) then
print("Failed to load texture [" ..v.name .."] at path [" ..v.path .."]")
else
print("Loaded Texture [" ..v.name .."]")
end
end
for k, v in pairs(assets.music) do
if not AssetManager.add_music(v.name, v.path) then
print("Failed to load music [" ..v.name .."] at path [" ..v.path .."]")
else
print("Loaded music [" ..v.name .."]")
end
end
for k, v in pairs(assets.sound_fx) do
if not AssetManager.add_soundfx(v.name, v.path) then
print("Failed to load soundfx [" ..v.name .."] at path [" ..v.path .."]")
else
print("Loaded soundfx [" ..v.name .."]")
end
end
-- TODO: Add other loading of assets as needed
end