From 2bebf7f259b4655c3f63fdca955145c832530b08 Mon Sep 17 00:00:00 2001 From: Arslaan Pathan Date: Thu, 3 Jul 2025 16:05:32 +1200 Subject: Add debug console --- assets/scripts/local2P.lua | 92 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) (limited to 'assets/scripts/local2P.lua') diff --git a/assets/scripts/local2P.lua b/assets/scripts/local2P.lua index 54bb4ae..48bce2c 100644 --- a/assets/scripts/local2P.lua +++ b/assets/scripts/local2P.lua @@ -14,6 +14,18 @@ isNewRound = false isFight = false isGameOver = false +debugConsoleActive = false +debugInput = "" +debugOutput = "" + +consoleKeycodes = {} +for _, name in ipairs({"a","b","c","d","e","f","g","h","i","j","k","l","m", + "n","o","p","q","r","s","t","u","v","w","x","y","z", + "space","return","backspace","backquote","minus","0","1","2","3","4","5","6","7","8","9", + "(", ")", "%", "$", "."}) do + consoleKeycodes[name] = getKeycodeByName(name) +end + winner = nil math.randomseed(os.time()) @@ -29,6 +41,62 @@ knockbackMultiplierX = 22.4 knockbackMultiplierY = 7.73 minimalKnockbackMultiplierY = 40.3 +function EvaluateDebugCode(code) + local func, syntaxError = load(code, "debug", "t", _G) + if not func then + debugOutput = "Syntax error: " .. syntaxError + return + end + + local success, result = pcall(func) + if not success then + debugOutput = "Runtime error: " .. result + else + if result == nil then + debugOutput = "Code executed successfully." + else + debugOutput = tostring(result) + end + end +end + +function HandleDebugConsoleInput() + if not debugConsoleActive then return end + + local shiftPressed = Input.isKeyDown("Left Shift") + + for keyName, keycode in pairs(consoleKeycodes) do + if Input.isKeyPressedOnce(keyName) then + if keyName == "backspace" then + debugInput = debugInput:sub(1, -2) + elseif keyName == "return" then + EvaluateDebugCode(debugInput) + debugInput = "" + elseif #keyName == 1 then + if shiftPressed then + debugInput = debugInput .. keyName:upper() + else + debugInput = debugInput .. keyName + end + elseif keyName == "space" then + debugInput = debugInput .. " " + elseif keyName == "minus" then + debugInput = debugInput .. "-" + elseif keyName == "backquote" then + debugInput = debugInput .. "`" + end + end + end +end + +function HandleDebugToggle() + if Input.isKeyPressedOnce("`") then + debugConsoleActive = not debugConsoleActive + debugInput = "" + debugOutput = "" + gamePaused = debugConsoleActive -- optional: pause game when console is open + end +end function SafeInitCharacter(character, default_x, default_y) character.x = default_x @@ -561,6 +629,23 @@ function DrawGameOverGraphic() queueTextForRender(winner.name, fontFile, nameX, nameY, nameFontSize, 255, 255, 255, 255) end +function DrawDebugConsole() + if not debugConsoleActive then return end + + local fontFile = "assets/fonts/OpenSans-Regular.ttf" + local fontSize = 20 + + -- Draw background rectangle for console input area + queueRectForRender(10, HEIGHT - 60, WIDTH - 20, 50, 0, 0, 0, 180) + + -- Draw input text + queueTextForRender("> " .. debugInput, fontFile, 20, HEIGHT - 50, fontSize, 255, 255, 255, 255) + + -- Draw output text above input + queueTextForRender(debugOutput, fontFile, 20, HEIGHT - 80, fontSize, 255, 255, 0, 255) +end + + function Update() local maxDelta = 0.05 -- max 50 ms per frame if deltaTime > maxDelta then @@ -569,6 +654,13 @@ function Update() Timer.update(deltaTime) + HandleDebugToggle() + + if debugConsoleActive then + HandleDebugConsoleInput() + DrawDebugConsole() + end + if not gamePaused then ApplyPhysics(player1Character) ApplyPhysics(player2Character) -- cgit v1.2.3