Skip to content

Commit d6d7c09

Browse files
committed
Merge branch 'master' of git@github.com:Wohlhabend-Networks/LunaDLL.git
2 parents 96d091e + 48685ed commit d6d7c09

28 files changed

+2026
-641
lines changed

LuaScriptsLibExt/HealthPoint.lua

Lines changed: 241 additions & 241 deletions
Large diffs are not rendered by default.

LuaScriptsLibExt/bgofix.lua

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
--***************************************************************************--
2+
--*██████╗ ██████╗ ██████╗ ███████╗██╗██╗ ██╗ ██╗ ██╗ ██╗ █████╗ *--
3+
--*██╔══██╗██╔════╝ ██╔═══██╗██╔════╝██║╚██╗██╔╝ ██║ ██║ ██║██╔══██╗*--
4+
--*██████╔╝██║ ███╗██║ ██║█████╗ ██║ ╚███╔╝ ██║ ██║ ██║███████║*--
5+
--*██╔══██╗██║ ██║██║ ██║██╔══╝ ██║ ██╔██╗ ██║ ██║ ██║██╔══██║*--
6+
--*██████╔╝╚██████╔╝╚██████╔╝██║ ██║██╔╝ ██╗██╗███████╗╚██████╔╝██║ ██║*--
7+
--*╚═════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝╚══════╝ ╚═════╝ ╚═╝ ╚═╝*--
8+
--***************************************************************************--
9+
--* A small library for rendering BGOs of custom sizes and changing render order
10+
--* Coded by Sambo, Oct. 2016.
11+
--* NOTE: You must use PNG CGFX for this to work. LunaLua currently doesn't support drawing of masked GIFs.
12+
--***************************************************************************--
13+
14+
local bgofix = {}
15+
16+
local defaults = {}
17+
defaults.foreground = {23,24,25,36,45,46,49,50,51,68,69,187,188,189,190}
18+
defaults.veryBackBgos = {11,12,75,76,77,78,79}
19+
defaults.specialBGOs = {98,160}
20+
21+
local bgos = {} -- Table of unique BGO IDs and their custom properties
22+
local blank = Graphics.loadImage(Misc.resolveFile("bgofix/blank.png"))
23+
local animData = {}
24+
25+
function bgofix.onInitAPI()
26+
registerEvent(bgofix, "onStart", "initialize")
27+
registerEvent(bgofix, "onDraw", "draw")
28+
end
29+
30+
--[[********************************************
31+
* Load the custom image for each BGO if it exists in .PNG format
32+
* This will rather redundantly reload images that are already in the memory of SMBX; however, there is no workaround for this issue right now.
33+
********************************************]]
34+
local function loadImg(id)
35+
local imageName = Misc.resolveFile("background-" .. id .. ".png") -- Search level and episode directorties
36+
local image;
37+
if imageName then
38+
image = Graphics.loadImage(imageName)
39+
end
40+
if image then return image;
41+
else return nil; end;
42+
end
43+
44+
--[[********************************************
45+
* Set the given BGO to be drawn over if a valid custom image exists
46+
* and it is larger than the original in either dimension.
47+
********************************************]]
48+
local function setShouldDraw(bgo, image)
49+
if image then
50+
if (image.width ~= bgo.width) or (image.height ~= image.height) then
51+
return true;
52+
else
53+
return false;
54+
end
55+
end
56+
end
57+
58+
--[[********************************************
59+
* Get the default render priority for the BGO with the given ID
60+
********************************************]]
61+
local function getDefault(id) -- Test sets from most likely to least likely, except for the largest set, to avoid as many tests as possible.
62+
for _,v in pairs(defaults.foreground) do
63+
if id == v then
64+
return -20;
65+
end
66+
end
67+
for _,v in pairs(defaults.veryBackBgos) do
68+
if id == v then
69+
return -95;
70+
end
71+
end
72+
for _,v in pairs(defaults.specialBGOs) do
73+
if id == v then
74+
return -80;
75+
end
76+
end
77+
return -85;
78+
end
79+
80+
--[[********************************************
81+
* Set the render priority to the default value or to a custom value inputted by the user
82+
* Note: This uses LunaLua render priorities, which are not equivalent to SMBX sort-order priorities!
83+
* This could change the render order from how it is normally. If this occurs, use setPriority to fix it.
84+
* This will only work if called in onStart!
85+
********************************************]]
86+
function bgofix.setPriority(id, priority)
87+
assert(bgos[id].image, "Cannot change the render priority of BGO-" .. id .. " because it doesn't have a .PNG custom image.") -- Make sure the user didn't break the rules.
88+
if (priority) and not (bgos[id].shouldDraw) then
89+
bgos[id].shouldDraw = true -- Handling for the user changing the render priority of BGOs which are the same size as the original
90+
end
91+
92+
bgos[id].priority = priority or getDefault(id) -- fetch default setting if custom setting is not given.
93+
Graphics.sprites.background[id].img = blank -- Make the actual BGO invisible. This is to allow the user to make the BGO's render priority lower.
94+
end
95+
96+
--[[********************************************
97+
* Resize the given BGO to have the same dimensions as its image
98+
********************************************]]
99+
local function resize(bgo, image)
100+
if ((image) and (bgo.width ~= image.width or bgo.height ~= image.height)) then
101+
bgo.width = image.width
102+
bgo.height = image.height
103+
end
104+
end
105+
106+
--[[********************************************
107+
* Initialization
108+
********************************************]]
109+
function bgofix.initialize()
110+
111+
for _,bgo in ipairs(BGO.get()) do
112+
if not bgos[bgo.id] then -- Do this only once for each BGO with a unique ID.
113+
bgos[bgo.id] = {}
114+
bgos[bgo.id].image = loadImg(bgo.id) -- custom image (nil if no valid image)
115+
bgos[bgo.id].shouldDraw = setShouldDraw(bgo, bgos[bgo.id].image); -- Does the BGO need to be rendered over?
116+
117+
118+
if bgos[bgo.id].shouldDraw then
119+
bgofix.setPriority(bgo.id) -- LunaLua render priority
120+
end
121+
122+
end
123+
124+
if bgos[bgo.id] then
125+
resize(bgo, bgos[bgo.id].image);
126+
end
127+
128+
end
129+
end
130+
131+
--[[********************************************
132+
* Get all BGOs on the screen
133+
* This is why I bothered to resize any BGOs so they were bigger.
134+
* If I hadn't done this, BGOs that still appeared to be onscreen would disappear because the object isn't on the screen.
135+
********************************************]]
136+
local function getBgosOnScreen()
137+
local bgosOnScreen = {}
138+
for _,cam in pairs(Camera.get()) do -- Check this way so it works in 2P mode
139+
for __,bgo in pairs(BGO.getIntersecting(cam.x, cam.y, cam.x + cam.width, cam.y + cam.height)) do
140+
if not bgo.isHidden then -- Don't insert BGOs that are on hidden layers!
141+
table.insert(bgosOnScreen, bgo)
142+
end
143+
end
144+
end
145+
return bgosOnScreen;
146+
end
147+
148+
--[[********************************************
149+
* Set a custom animation
150+
* Animation image is a separate image from the BGO image with the naming style "bgo-anim-*.png," where '*' is the ID of the BGO.
151+
* This may seem like a weird way to do it, but it is so they will display better in the PGE editor.
152+
********************************************]]
153+
function bgofix.setAnimation(id, numFrames, frameSpeed)
154+
155+
if not bgos[id].shouldDraw then
156+
bgos[id].shouldDraw = true
157+
bgos[id].priority = getDefault(id)
158+
Graphics.sprites.background[id].img = blank
159+
end
160+
161+
animData[id] = {}
162+
163+
local imageName = Misc.resolveFile("bgo-anim-" .. id .. ".png")
164+
if imageName then
165+
animData[id].image = Graphics.loadImage(imageName)
166+
else
167+
error("Animation image for background-" .. id .. " not found.")
168+
end
169+
170+
animData[id].frames = numFrames or 4
171+
animData[id].frameSpeed = frameSpeed or 8 -- The number of ticks per frame for the BGO. Default = 8
172+
animData[id].frame = 0 -- Current frame. Reset to 0 when this == numFrames - 1
173+
animData[id].tick = 0 -- Tick counter. Reset to 0 and move to the next frame when this == frameSpeed
174+
175+
assert(animData[id].frames > 0, "Invalid number of frames for BGO-" .. id .. ". Value must be greater than 0.")
176+
assert(animData[id].frameSpeed > 0, "Invalid frame speed value for BGO-" .. id .. ". Value must be greater than 0.")
177+
end
178+
179+
--[[********************************************
180+
* Update the frame numbers of the BGOs with custom animations
181+
********************************************]]
182+
local function updateAnimations()
183+
for id,_ in pairs(animData) do
184+
-- Compact frame controller
185+
-- Why doesn't anyone use the modulus operator??
186+
animData[id].frame = math.floor(animData[id].tick / animData[id].frameSpeed)
187+
animData[id].tick = (animData[id].tick + 1) % (animData[id].frames * animData[id].frameSpeed)
188+
end
189+
end
190+
191+
--[[********************************************
192+
* Draw over the BGOs that are on the screen
193+
********************************************]]
194+
function bgofix.draw()
195+
-- Draw all BGOs on the screen that are listed for drawing
196+
for _,bgo in pairs(getBgosOnScreen()) do
197+
if bgos[bgo.id].shouldDraw then
198+
if animData[bgo.id] then -- Draw animations for BGOs that have them
199+
Graphics.drawImageToSceneWP(animData[bgo.id].image, bgo.x, bgo.y, 0, bgo.height * animData[bgo.id].frame, bgo.width, bgo.height, bgos[bgo.id].priority)
200+
-- image position source image position dimensions priority
201+
else
202+
Graphics.drawImageToSceneWP(bgos[bgo.id].image, bgo.x, bgo.y, bgos[bgo.id].priority)
203+
end
204+
end
205+
end
206+
updateAnimations();
207+
end
208+
209+
return bgofix

LuaScriptsLibExt/bgofix/blank.png

104 Bytes
Loading

0 commit comments

Comments
 (0)