Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update main.lua #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 70 additions & 62 deletions utbs-lua-v1/scripts/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ local boxH = 0.0
local boxX = settings.boxX
local boxY = settings.boxY

-- Current alpha of the battle box
local boxA = 0.0

--
-- SOUL STATE VARIABLES
--
Expand Down Expand Up @@ -112,7 +115,7 @@ function initializeBattleSystem()

-- Register built-in soul modes.
registerSoulMode('default', require('mods/utbs-lua-v1/scripts/modes/default'))

-- Set soul mode and color.
setSoulMode('default')
end
Expand Down Expand Up @@ -148,7 +151,7 @@ function openBox(speed)

-- Clear any attacks that might be in progress.
clearAttacks()

if soulMode ~= nil and soulMode.onBoxStartOpen ~= nil then
soulMode.onBoxStartOpen(self)
end
Expand Down Expand Up @@ -202,7 +205,7 @@ function useAttack(name, data)
debugPrint("UTBS: WARNING Cannot use attack '" .. name .. "' because it does not exist. Make sure to register it!")
return
end

debugPrint("UTBS: Using attack '" .. name .. "' with data: {" .. data .. "}")

attack.spawnAttack(self, data)
Expand Down Expand Up @@ -295,7 +298,7 @@ function setSoulMode(modeName, forceColor)

soulMode = mode

if soulMode.onSwitchToMode ~= nil then
if soulMode.onSwitchToMode ~= nil then
soulMode.onSwitchToMode(self)
end

Expand Down Expand Up @@ -391,12 +394,12 @@ end
--

function onCreate()
-- Called when the Lua file is loaded.
-- Called when the Lua file is loaded.
-- Some variables are not properly initialized yet.
end

function onCreatePost()
-- Called after the Lua file is loaded and all variables are properly initialized.
-- Called after the Lua file is loaded and all variables are properly initialized.
-- NOTE: This gets called multiple times for some reason -_-
end

Expand Down Expand Up @@ -433,7 +436,7 @@ function onUpdateScore(miss)
end

function onDestroy()
-- triggered when the lua file is ended (Song fade out finished)
-- triggered when the lua file is ended (Song fade out finished)
end

--
Expand Down Expand Up @@ -483,46 +486,42 @@ function handleUpdate_BoxOpenClose(elapsed)
return
end

local function handleBoxProperties(deltaX, deltaY, deltaWidth, deltaHeight, deltaAlpha)
boxX = boxX + deltaX
boxY = boxY + deltaY
boxW = boxW + deltaWidth
boxH = boxH + deltaHeight
boxA = boxH + deltaAlpha
end

boxX = getProperty('utbsBattleBox.x')
boxY = getProperty('utbsBattleBox.y')
local boxA = getProperty('utbsBattleBox.alpha')
boxA = getProperty('utbsBattleBox.alpha')

if isBoxOpening then
local deltaX = (settings.boxX - boxX) / elapsed * settings.boxOpenSpeed * boxRelativeOpenSpeed
local deltaY = (settings.boxY - boxY) / elapsed * settings.boxOpenSpeed * boxRelativeOpenSpeed
local deltaWidth = (targetBoxWidth - boxW) / elapsed * settings.boxOpenSpeed * boxRelativeOpenSpeed
local deltaHeight = (targetBoxHeight - boxH) / elapsed * settings.boxOpenSpeed * boxRelativeOpenSpeed
local deltaAlpha = (1.0 - boxA) / elapsed * settings.boxOpenSpeed * boxRelativeOpenSpeed

boxX = boxX + deltaX
boxY = boxY + deltaY
boxW = boxW + deltaWidth
boxH = boxH + deltaHeight
boxA = boxA + deltaAlpha

handleBoxProperties(deltaX, deltaY, deltaWidth, deltaHeight, deltaAlpha)
elseif isBoxClosing then
local deltaX = (settings.boxX - boxX) / elapsed * settings.boxCloseSpeed
local deltaY = (settings.boxY - boxY) / elapsed * settings.boxCloseSpeed
local deltaWidth = (0 - boxW) / elapsed * settings.boxCloseSpeed
local deltaHeight = (0 - boxH) / elapsed * settings.boxCloseSpeed
local deltaAlpha = (0 - boxA) / elapsed * settings.boxCloseSpeed

boxX = boxX + deltaX
boxY = boxY + deltaY
boxW = boxW + deltaWidth
boxH = boxH + deltaHeight
boxA = boxA + deltaAlpha

handleBoxProperties(deltaX, deltaY, deltaWidth, deltaHeight, deltaAlpha)
elseif isBoxLerping then
local deltaX = (settings.boxX - boxX) / elapsed * settings.boxOpenSpeed * boxRelativeOpenSpeed
local deltaY = (settings.boxY - boxY) / elapsed * settings.boxOpenSpeed * boxRelativeOpenSpeed
local deltaWidth = (targetBoxWidth - boxW) / elapsed * settings.boxOpenSpeed * boxRelativeOpenSpeed
local deltaHeight = (targetBoxHeight - boxH) / elapsed * settings.boxOpenSpeed * boxRelativeOpenSpeed
local deltaAlpha = (1.0 - boxA) / elapsed * settings.boxOpenSpeed * boxRelativeOpenSpeed

boxX = boxX + deltaX
boxY = boxY + deltaY
boxW = boxW + deltaWidth
boxH = boxH + deltaHeight
boxA = boxA + deltaAlpha

handleBoxProperties(deltaX, deltaY, deltaWidth, deltaHeight, deltaAlpha)
else
if isBoxOpen then
boxA = 1.0
Expand All @@ -541,17 +540,17 @@ function handleUpdate_BoxOpenClose(elapsed)

-- Apply the box size before we apply the position.
setProperty('utbsBattleBox.scale.x', boxW / settings.boxImageSize)
setProperty('utbsBattleBox.scale.y', boxH / settings.boxImageSize)
setProperty('utbsBattleBox.scale.y', boxH / settings.boxImageSize)
setProperty('utbsBattleBox.x', boxX)
setProperty('utbsBattleBox.y', boxY)
setProperty('utbsBattleBox.alpha', boxA)
setProperty('utbsBattleBox.y', boxY)
setProperty('utbsBattleBox.alpha', boxA)

-- Ensure the border is visible.
setProperty('utbsBattleBoxBorder.scale.x', (boxW + settings.boxBorderWidth) / settings.boxImageSize)
setProperty('utbsBattleBoxBorder.scale.y', (boxH + settings.boxBorderWidth) / settings.boxImageSize)
setProperty('utbsBattleBoxBorder.x', boxX)
setProperty('utbsBattleBoxBorder.y', boxY)
setProperty('utbsBattleBoxBorder.alpha', boxA)
setProperty('utbsBattleBoxBorder.scale.x', (boxW + settings.boxBorderWidth) / settings.boxImageSize)
setProperty('utbsBattleBoxBorder.scale.y', (boxH + settings.boxBorderWidth) / settings.boxImageSize)
setProperty('utbsBattleBoxBorder.x', boxX)
setProperty('utbsBattleBoxBorder.y', boxY)
setProperty('utbsBattleBoxBorder.alpha', boxA)

-- Set internal bounding box to the displayed box.
boxLeftBound = -boxW / 2 + settings.boxBorderWidth
Expand Down Expand Up @@ -621,7 +620,7 @@ function handleUpdate_Soul(elapsed)
local targetColor = soulFlashParams['targetColor']
local targetAlpha = soulFlashParams['targetAlpha']
local baseAlpha = getProperty('utbsBattleBox.alpha')

local useTargetColor = math.floor(frequency * soulFlashTimer) % 2 == 0

local finalColor = useTargetColor and targetColor or soulColor
Expand All @@ -634,7 +633,7 @@ function handleUpdate_Soul(elapsed)
-- Flash white while invulnerable due to taking damage.
local frequency = 20
local targetColor = settings.soulColors['white']

local useTargetColor = math.floor(frequency * soulInvulnTimer) % 2 == 0

local finalColor = useTargetColor and targetColor or soulColor
Expand All @@ -653,25 +652,25 @@ function handleUpdate_Soul(elapsed)
soulMode.onSoulInput(self, elapsed, left, down, up, right, action)

-- Constrain soul position to bounds.
if soulXPos <= boxLeftBound then
if soulXPos <= boxLeftBound then
soulXPos = boxLeftBound
isSoulTouchingLeft = true
else
isSoulTouchingLeft = false
end
if soulXPos >= boxRightBound then
if soulXPos >= boxRightBound then
soulXPos = boxRightBound
isSoulTouchingRight = true
else
isSoulTouchingRight = false
end
if soulYPos <= boxTopBound then
if soulYPos <= boxTopBound then
soulYPos = boxTopBound
isSoulTouchingTop = true
else
isSoulTouchingTop = false
end
if soulYPos >= boxBottomBound then
if soulYPos >= boxBottomBound then
soulYPos = boxBottomBound
isSoulTouchingBottom = true
else
Expand Down Expand Up @@ -707,7 +706,7 @@ function handleUpdate_Attacks(elapsed)
bullet.x = bullet.x + bullet.velocityX * elapsed
bullet.y = bullet.y + bullet.velocityY * elapsed
bullet.angle = bullet.angle + bullet.angularVelocity * elapsed

local fadeAlpha = 1.0

if bullet.fadeInDuration > 0 then
Expand Down Expand Up @@ -808,36 +807,45 @@ function handleUpdate_Score(wasTextReset)
if wasTextReset then
baseScoreText = getProperty('scoreTxt.text')
-- Trim the last 2 characters.
baseScoreText = string.sub(baseScoreText, 1, string.len(baseScoreText) - 2)
baseScoreText = string.sub(baseScoreText, 1, #baseScoreText - 2)
end

-- Add the damage
text = baseScoreText .. " Times Hit: " .. timesHit .. "\n"

setProperty("scoreTxt.text", text, false)
end

function accessControls()
-- TODO: Is there a nicer method?
local function getControl(keyboardControlOptions, gamepadControlOption, useGamepad)
local lastActiveOne = 'gamepads.lastActive.pressed.DPAD_'
local lastActiveTwo = 'gamepads.lastActive.pressed.LEFT_STICK_DIGITAL_'
if gamepadControlOption ~= 'LEFT' and gamepadControlOption ~= 'DOWN' and gamepadControlOption ~= 'UP' and gamepadControlOption ~= 'RIGHT' then
-- Support for buttons
lastActiveOne = 'gamepads.lastActive.pressed.'
lastActiveTwo = 'gamepads.lastActive.pressed.'
end

-- Support two controls for the action input
if #keyboardControlOptions == 2 then
return getPropertyFromClass('flixel.FlxG', 'keys.pressed.' .. keyboardControlOptions[1]) or getPropertyFromClass('flixel.FlxG', 'keys.pressed.' .. keyboardControlOptions[2]) or
(useGamepad and (getPropertyFromClass('flixel.FlxG', lastActiveOne .. gamepadControlOption) or getPropertyFromClass('flixel.FlxG', lastActiveTwo .. gamepadControlOption)))
end

return getPropertyFromClass('flixel.FlxG', 'keys.pressed.' .. keyboardControlOptions[1]) or getPropertyFromClass('flixel.FlxG', 'keys.pressed.' .. keyboardControlOptions[2]) or getPropertyFromClass('flixel.FlxG', 'keys.pressed.' .. keyboardControlOptions[3]) or
(useGamepad and (getPropertyFromClass('flixel.FlxG', lastActiveOne .. gamepadControlOption) or getPropertyFromClass('flixel.FlxG', lastActiveTwo .. gamepadControlOption)))
end

local useGamepad = false
if (getPropertyFromClass('flixel.FlxG', 'gamepads.numActiveGamepads') > 0) then
useGamepad = true
end

local right = getPropertyFromClass('flixel.FlxG', 'keys.pressed.RIGHT') or getPropertyFromClass('flixel.FlxG', 'keys.pressed.D') or getPropertyFromClass('flixel.FlxG', 'keys.pressed.L') or
(useGamepad and (getPropertyFromClass('flixel.FlxG', 'gamepads.lastActive.pressed.DPAD_RIGHT') or getPropertyFromClass('flixel.FlxG', 'gamepads.lastActive.pressed.LEFT_STICK_DIGITAL_RIGHT')))

local left = getPropertyFromClass('flixel.FlxG', 'keys.pressed.LEFT') or getPropertyFromClass('flixel.FlxG', 'keys.pressed.A') or getPropertyFromClass('flixel.FlxG', 'keys.pressed.J') or
(useGamepad and (getPropertyFromClass('flixel.FlxG', 'gamepads.lastActive.pressed.DPAD_LEFT') or getPropertyFromClass('flixel.FlxG', 'gamepads.lastActive.pressed.LEFT_STICK_DIGITAL_LEFT')))

local up = getPropertyFromClass('flixel.FlxG', 'keys.pressed.UP') or getPropertyFromClass('flixel.FlxG', 'keys.pressed.W') or getPropertyFromClass('flixel.FlxG', 'keys.pressed.I') or
(useGamepad and (getPropertyFromClass('flixel.FlxG', 'gamepads.lastActive.pressed.DPAD_UP') or getPropertyFromClass('flixel.FlxG', 'gamepads.lastActive.pressed.LEFT_STICK_DIGITAL_UP')))

local down = getPropertyFromClass('flixel.FlxG', 'keys.pressed.DOWN') or getPropertyFromClass('flixel.FlxG', 'keys.pressed.S') or getPropertyFromClass('flixel.FlxG', 'keys.pressed.K') or
(useGamepad and (getPropertyFromClass('flixel.FlxG', 'gamepads.lastActive.pressed.DPAD_DOWN') or getPropertyFromClass('flixel.FlxG', 'gamepads.lastActive.pressed.LEFT_STICK_DIGITAL_DOWN')))

local action = getPropertyFromClass('flixel.FlxG', 'keys.pressed.SPACE') or getPropertyFromClass('flixel.FlxG', 'keys.pressed.Z') or
(useGamepad and (getPropertyFromClass('flixel.FlxG', 'gamepads.lastActive.pressed.A') or getPropertyFromClass('flixel.FlxG', 'gamepads.lastActive.pressed.A')))
local left = getControl({'LEFT', 'A', 'J'}, 'LEFT', useGamepad)
local down = getControl({'DOWN', 'S', 'K'}, 'DOWN', useGamepad)
local up = getControl({'UP', 'W', 'I'}, 'UP', useGamepad)
local right = getControl({'RIGHT', 'D', 'L'}, 'RIGHT', useGamepad)
local action = getControl({'SPACE', 'Z'}, 'A', useGamepad)

return left, down, up, right, action
end
Expand All @@ -852,10 +860,10 @@ function damagePlayer(damage, applyInvuln)
damage = soulMode.onPlayerHit(self, damage)
end


-- Deal damage to the player.
setProperty('health', getProperty('health') - (damage / 100.0));

if (applyInvuln) then
soulInvulnTimer = settings.soulInvulnDuration

Expand Down Expand Up @@ -928,7 +936,7 @@ function buildState()

getSoulPosition = function() return soulXPos, soulYPos end,
setSoulPosition = setSoulPosition,

getSoulAngle = function() return soulAngle end,
setSoulAngle = function(angle) soulAngle = angle end,

Expand Down