flung.lua - See enemies tumbling down!

Share and discuss custom LunaLua code and content packs for SMBX2.

Moderator: Userbase Moderators

Cloud Devil
Koopa
Koopa
Posts: 17
Joined: Sun Nov 07, 2021 8:17 am
Pronouns: he/him

flung.lua - See enemies tumbling down!

Postby Cloud Devil » Mon Jun 03, 2024 2:05 am

A silly library made to make NPCs tumble down when thrown, or launched through a cannon.

Download:
https://drive.google.com/file/d/1LzdV4x ... drive_link

EDIT: Seems that there's a problem in my drive. Anyway, here's the code. Just copy these into a lua file and then name it flung.lua
Spoiler: show
--A silly library if you want to see npcs tumbling down
--Rotation code based from Saturnyoshi & Emral's Grrrol

local flung = {}
local npcDrawn = nil

local npcManager = require("npcManager")
local npcutils = require("npcs/npcutils")

function flung.onInitAPI()
registerEvent(flung, "onDraw")
registerEvent(flung, "onTick")
end

function drawNPCFrame(id, frame, x, y, angle, direction)
local settings = npcManager.getNpcSettings(id)
local priority = -45

local myWidth = settings.gfxwidth
local myHeight = settings.gfxheight

if settings.gfxwidth == 0 then
myWidth = settings.width
end
if settings.gfxwidth == 0 then
myHeight = settings.height
end

if settings.foreground then
priority = -15
end

local img = Graphics.sprites.npc[id].img
imgHeight = img.height
imgWidth = img.width

local npcDrawn = Sprite.box({
texture = img,
width = myWidth,
height = myHeight,
x = x + settings.gfxoffsetx + (myWidth * 0.5),
y = y + settings.gfxoffsety + (myHeight * 0.5),
rotation = angle,
pivot = Sprite.align.CENTER,
frames = imgHeight / myHeight,
frame = frame,
})

npcDrawn.x = x + settings.gfxoffsetx + (myWidth * 0.5)
npcDrawn.y = y + settings.gfxoffsety + (myHeight * 0.5)

if direction > 0 then
npcDrawn.scale = vector.v2(-1, 1)
end

npcDrawn:draw({
align = Sprite.align.CENTER,
frame = frame,
sceneCoords = true,
priority = priority,
})
end

function flung.onDraw()
for _,fallingNPC in ipairs(NPC.get()) do
if fallingNPC.isProjectile then
local config = npcManager.getNpcSettings(fallingNPC.id)
local data = fallingNPC.data
local tid = fallingNPC.id

local tdir = fallingNPC.data._basegame.angle or 0

if tdir == nil then return end

local myWidth = config.gfxwidth
local myHeight = config.gfxheight

if config.gfxwidth == 0 then
myWidth = config.width
end
if config.gfxwidth == 0 then
myHeight = config.height
end

local drawX = fallingNPC.x + 0.5 * fallingNPC.width - 0.5 * myWidth
local drawY = fallingNPC.y + fallingNPC.height - myHeight

npcutils.hideNPC(fallingNPC)

drawNPCFrame(tid, fallingNPC.frame, drawX, drawY, tdir, fallingNPC.direction)
end
end
end

function flung.onTick()
for _,fallingNPC in ipairs(NPC.get()) do
if fallingNPC.isProjectile then
local data = fallingNPC.data._basegame
-- Rotation
local fallMagnitude = fallingNPC.speedX
if fallingNPC.speedX == 0 then
fallMagnitude = fallingNPC.direction
end
if fallingNPC:mem(0x12A, FIELD_WORD) > 0 and not fallingNPC.isHidden and fallingNPC:mem(0x124,FIELD_WORD) ~= 0 and fallingNPC:mem(0x12C,FIELD_WORD) == 0 then
if data.angle == nil then
data.angle = 0
data.timer = 0
else
data.angle = data.angle + (fallMagnitude / (0.5 * fallingNPC.height)) * 0.5 * 180 / math.pi
if fallingNPC:mem(0x12C, FIELD_WORD) > 0 then
data.angle = data.angle + 15
end
end
if fallingNPC:mem(0x12C, FIELD_WORD) ~= 0 then return end
data.timer = data.timer + fallingNPC.direction * fallingNPC.speedX
end
end
end
end

return flung
Last edited by Cloud Devil on Wed Jun 05, 2024 8:02 am, edited 1 time in total.

mariobrigade2018
Rocky Wrench
Rocky Wrench
Posts: 677
Joined: Wed May 24, 2023 7:00 pm
Flair: OK in coding who dreams of making a Mario game
Pronouns: he/him

Re: flung.lua - See enemies tumbling down!

Postby mariobrigade2018 » Mon Jun 03, 2024 1:03 pm

Gif pls.

MarioChallengerX2
Buster Beetle
Buster Beetle
Posts: 89
Joined: Sat Dec 31, 2022 4:34 pm
Pronouns: he/him

Re: flung.lua - See enemies tumbling down!

Postby MarioChallengerX2 » Tue Jun 04, 2024 10:03 pm

Hey. Why does the download link say it's in violation of Google's ToS?

mariobrigade2018
Rocky Wrench
Rocky Wrench
Posts: 677
Joined: Wed May 24, 2023 7:00 pm
Flair: OK in coding who dreams of making a Mario game
Pronouns: he/him

Re: flung.lua - See enemies tumbling down!

Postby mariobrigade2018 » Tue Jun 04, 2024 10:10 pm

MarioChallengerX2 wrote:
Tue Jun 04, 2024 10:03 pm
Hey. Why does the download link say it's in violation of Google's ToS?
WHAT?

Added in 10 hours 47 minutes 5 seconds:
Cloud Devil wrote:
Mon Jun 03, 2024 2:05 am
Ping
Still need GIF pls.

Spudly
Shy Guy
Shy Guy
Posts: 9
Joined: Sun Mar 17, 2024 3:37 am
Pronouns: He/him they/them

Re: flung.lua - See enemies tumbling down!

Postby Spudly » Tue Jun 11, 2024 4:08 pm

How do you exclude npcs from the rotating, and how do you also make rotating effects?

mariobrigade2018
Rocky Wrench
Rocky Wrench
Posts: 677
Joined: Wed May 24, 2023 7:00 pm
Flair: OK in coding who dreams of making a Mario game
Pronouns: he/him

Re: flung.lua - See enemies tumbling down!

Postby mariobrigade2018 » Tue Jun 11, 2024 8:42 pm

Spudly wrote:
Tue Jun 11, 2024 4:08 pm
How do you exclude npcs from the rotating, and how do you also make rotating effects?
You could add a table that excludes certain npc and then add another check that searches for the NPCs in the table and send them out of the library.
Cloud Devil wrote: Ping
GIF pls.

Spudly
Shy Guy
Shy Guy
Posts: 9
Joined: Sun Mar 17, 2024 3:37 am
Pronouns: He/him they/them

Re: flung.lua - See enemies tumbling down!

Postby Spudly » Wed Jun 12, 2024 11:31 pm

mariobrigade2018 wrote:
Tue Jun 11, 2024 8:42 pm
Spudly wrote:
Tue Jun 11, 2024 4:08 pm
How do you exclude npcs from the rotating, and how do you also make rotating effects?
You could add a table that excludes certain npc and then add another check that searches for the NPCs in the table and send them out of the library.
Cloud Devil wrote: Ping
GIF pls.
That solves the blacklist issue, but what about the effects?


Return to “LunaLua”

Who is online

Users browsing this forum: Amazon [Bot], RoadHogStudios and 3 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari