--[[
Fadable rooms
Made by MetalZelda
Inspired by: Project Zelda Engine
Feb.2018 - Rev Mar.2018
Version 1.6
REQUIRED:
- [Multi Events] by Christopho
(http://forum.solarus-games.org/index.php/topic,784.0.html)
Simulate multiple rooms by hiding one using a table
{
texture = {
src = "texture_path",
opacity = 0 - 255,
}
x = x position of the roof
y = y position of the roof
width = horizontal lenght of the roof
height = vertical lenght of the roof
color = {r, g, b} (color of the roof)
}
color can be omnitted if using a texture
texture can be omnitted if using a color preset
You can combine both by using a semi-transparent texture and apply a color, the color will be drawn bellow
Trigger zone is defined by the script bellow
Usually: rooms.under = (x >= rooms.x - 9 and x <= rooms.x + rooms.width + 9) and (y >= rooms.y - 6 and y <= rooms.y + rooms.height + 23)
]]
return function(map)
-- This is the incrementation value, 8 is faster enough
local opacity_increment = 8
map:register_event("on_started", function(map)
-- If nothing exists on the rooms table means that there are nothing to display, the scripts stops here.
if map.fade_rooms == nil then
return
end
-- Store additionnal data on the roof table such as surface, color and opacity (default = 255) that can be automatically set by the script
for num, rooms in ipairs(map.fade_rooms) do
rooms.surface = sol.surface.create(rooms.width, rooms.height)
if rooms.color then
rooms.surface:fill_color(rooms.color)
end
if rooms.texture then
local texture_surface = sol.surface.create("roof_texture/" .. rooms.texture.src .. ".png")
local texture_width, texture_height = texture_surface:get_size()
if rooms.texture.opacity then
texture_surface:set_opacity(rooms.texture.opacity)
end
-- Get the total pixels to cover
local total = ((rooms.width * rooms.height) / (texture_width * texture_height))
local x = 0
local y = 0
for i = 1, total do
-- Draw the texture on the right place
texture_surface:draw(rooms.surface, x, y)
-- Increment next value
x = x + texture_width
if x >= rooms.width then
x = 0
y = y + texture_height
end
end
end
rooms.opacity = 255
end
end)
map:register_event("on_draw", function(map, dst_surface)
-- To avoid errors, if there is nothing to draw, abort.
if map.fade_rooms == nil then
return
end
for _, rooms in ipairs(map.fade_rooms) do
local cam_x, cam_y = map:get_camera():get_position()
rooms.surface:draw(dst_surface, rooms.x - cam_x, rooms.y - cam_y)
local opacity = rooms.opacity
-- Get player position, current layer is not a necessary parameter.
local x, y = map:get_hero():get_position()
-- Define the rule of being under a roof for any roof.
rooms.under = (x >= rooms.x - 9 and x <= rooms.x + rooms.width + 9) and (y >= rooms.y - 6 and y <= rooms.y + rooms.height + 23)
-- Define opacity variation for roof
if rooms.under or rooms.force_under then
rooms.opacity = rooms.opacity > (opacity_increment - 1) and rooms.opacity - opacity_increment or 0
else
rooms.opacity = rooms.opacity < (255 - opacity_increment) and rooms.opacity + opacity_increment or 255
end
-- Don't need to update any opacity if the game is paused.
if map:get_game():is_paused() then
rooms.opacity = opacity
end
rooms.surface:set_opacity(rooms.opacity)
end
end)
function map:dim_room_surface(i, bool)
if bool == nil then
bool = self.fade_rooms[i].force_under == nil and true or (not self.fade_rooms[i].force_under)
end
self.fade_rooms[i].force_under = bool
end
end