From d049391009d9124c8c8db00a1a652d4c389e8780 Mon Sep 17 00:00:00 2001 From: Madiwka3 Date: Thu, 22 Oct 2020 09:04:23 +0600 Subject: [PATCH] Framerate limited --- animator.lua | 6 +++- explosion.lua | 27 +++++++++++++++++ main.lua | 11 +++++-- src/AI.lua | 1 - src/baseGame.lua | 39 +++++++++++++++++++----- src/dependencies.lua | 2 ++ tick.lua | 70 ++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 143 insertions(+), 13 deletions(-) create mode 100644 explosion.lua create mode 100644 tick.lua diff --git a/animator.lua b/animator.lua index 8818b0e..acf4958 100644 --- a/animator.lua +++ b/animator.lua @@ -94,4 +94,8 @@ function staticanimator() love.graphics.setColor(40/255,40/255,40/255,1) love.graphics.circle("fill", player2.x, player2.y , diseffectRange[1]*100, 100) end -end \ No newline at end of file +end +function animateExplosion(x, y, v, color) + love.graphics.setColor(color) + love.graphics.circle("fill", x, y, explosionRange * v, 100) +end \ No newline at end of file diff --git a/explosion.lua b/explosion.lua new file mode 100644 index 0000000..edee6fd --- /dev/null +++ b/explosion.lua @@ -0,0 +1,27 @@ +explosion = Class{} + +function explosion:init(x, y, v, color) + self.color = color + self.x = x + self.y = y + self.v = v + self.range = 0 + self.killed = false + print(self.i) +end + +function explosion:update(dt) + self.range = self.range + dt * 24 + if self.range * self.v > VIRTUAL_WIDTH * 2 then + print("killing myself with range" .. self.range) + self.killed = true + end +end + + + +function explosion:render() + print("rendering myself" .. self.x .. " " .. self.y .. " " .. self.range .. " " .. self.v) + love.graphics.setColor(color) + love.graphics.circle("fill", self.x, self.y, self.range * self.v, 100) +end \ No newline at end of file diff --git a/main.lua b/main.lua index b6a0d11..d3aeeb0 100644 --- a/main.lua +++ b/main.lua @@ -15,7 +15,7 @@ doubleclick1 = false doubleclick2 = false hold1 = false hold2 = false -debug = false +debug = true paused = false androidButtons = {} pauseButtons = {} @@ -170,6 +170,8 @@ function controlChanger() end function love.load() love.keyboard.setKeyRepeat(true) + + tick.framerate = 60 simpleScale.setWindow(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT) configfile = io.open("config.lua", "r") configsave = io.open("config.lua", "w") @@ -871,6 +873,7 @@ function love.load() player3 = paddle(5000, 5000, 10, 100) player4 = paddle(5000, 5000, 10, 100) ball = {} + explosions = {} ball[1] = eball(VIRTUAL_WIDTH / 2, VIRTUAL_HEIGHT / 2 - 2, 16, 16) ball[2] = eball(VIRTUAL_WIDTH / 1.9, VIRTUAL_HEIGHT / 2 - 2, 16, 16) ball[3] = eball(VIRTUAL_WIDTH / 1.8, VIRTUAL_HEIGHT / 2 - 2, 16, 16) @@ -900,7 +903,7 @@ function startShake(duration, magnitude) t, shakeDuration, shakeMagnitude = 0, duration or 1, magnitude or 5 end function displayFPS() - --love.window.setTitle(love.timer.getFPS()) + love.window.setTitle(love.timer.getFPS()) --love.window.setTitle(globalState .. " " .. gameState .. " " .. paddle_SPEED .. " " .. p1bonus .. " " .. player1.dy) if love.keyboard.isDown("space") then player1nukescore = 200 @@ -918,7 +921,9 @@ end function love.update(dt) --print("IMPORTANT!!!!!" .. globalState .. gameState) - + for i, explosion in ipairs(explosions) do + explosion:update(dt) + end staticanimatorcounter(dt) player1.goal = -1 player2.goal = -1 diff --git a/src/AI.lua b/src/AI.lua index e7bbe9b..bb72fde 100644 --- a/src/AI.lua +++ b/src/AI.lua @@ -1,5 +1,4 @@ function AI(target, ballCnt, diff) - print("AI RUNNING") currentTarget = evaluateClosestBall(target); --print("CLOSEST TARGET IS " .. currentTarget) if diff < 1200 then diff --git a/src/baseGame.lua b/src/baseGame.lua index f041275..afb84c7 100644 --- a/src/baseGame.lua +++ b/src/baseGame.lua @@ -11,7 +11,7 @@ function basegame(dt) end speedControl() balancer() - + effectControl() if t < shakeDuration then t = t + dt end @@ -40,7 +40,7 @@ function basegame(dt) --print(areanuclear .. striken .. player1score .. player2score) for i = 1, maxBalls do if rules("p1hit", i) then - + print("inserted") if (areanuclear == 0 and striken == 1 and (player1score > ptw*0.8 or player2score > ptw*0.8)) then --print("Calling animation") --print("AREA NUCLEAR?" .. areanuclear) @@ -370,7 +370,7 @@ end function goalManager() for i = 1, maxBalls do - if (rules("p1miss", i)) then + if (rules("p1miss", i)) then ball[i].disabled = true ball[i].x = 2000 @@ -587,6 +587,8 @@ function nuclearDraw() nuclearanimation / 3, nuclearanimation / 3, nuclearanimation / 3 + + for i = 1, maxBalls do love.graphics.setColor(nuclearanimation / 3, nuclearanimation / 3, nuclearanimation / 3, 1) ball[i]:render("controlled") @@ -630,6 +632,10 @@ function normalDraw() else love.graphics.clear(40 / 255, 40 / 255, 40 / 255, 1) end + for i, explosion in ipairs(explosions) do + explosion:render() + print("exploding") + end staticanimator() if MAP_TYPE == 1 then @@ -747,7 +753,6 @@ function menuDraw() ) ) end - if not isAndroid then table.insert( IPselect, newButton( @@ -761,7 +766,6 @@ function menuDraw() end ) ) - end table.insert( IPselect, newButton( @@ -848,6 +852,7 @@ function menuDraw() end function baseDraw() love.graphics.clear(40 / 255, 40 / 255, 40 / 255, 1) + if shakeDuration > t then local dx = love.math.random(-shakeMagnitude, shakeMagnitude) @@ -867,6 +872,7 @@ function baseDraw() menuDraw() end end + if globalState == 'base' or globalState == 'reverse' or globalState == 'nettest' or globalState == 'clienttest' then love.graphics.setFont(smallfont) @@ -892,6 +898,7 @@ function baseDraw() mymenu:butt(gameState, VIRTUAL_WIDTH, VIRTUAL_HEIGHT, doneButtons, sounds, "middle") love.keyboard.mouseisReleased = false end + end function androidDraw() --HOME BUTTON HERE @@ -1107,6 +1114,7 @@ function rules(query, i) end if query == "p1miss" then if gameMode == "reversegame" then + return ball[i]:collides(player1) elseif gameMode == "normal" then return ball[i].x < -10 and ball[i].disabled == false @@ -1132,7 +1140,7 @@ function clientsBaseGame(dt) end speedControl() balancer() - + effectControl() if t < shakeDuration then t = t + dt end @@ -1163,7 +1171,7 @@ function clientsBaseGame(dt) --print(areanuclear .. striken .. player1score .. player2score) for i = 1, maxBalls do if rules("p1hit", i) then - + if (areanuclear == 0 and striken == 1 and (player1score > ptw*0.8 or player2score > ptw*0.8)) then --print("Calling animation") superanimator("tensehit", 1) @@ -1471,7 +1479,7 @@ function menuDemo(dt) AI(player1, maxBalls, 1300) player2.goal = 360 end - print(neededTarget, neededTarget1) + --print(neededTarget, neededTarget1) --print("menu demo active") ball[1]:update(dt) player1:update(dt) @@ -1568,4 +1576,19 @@ function menuDemo(dt) ball[1].dy = -ball[1].dy end +end +function effectControl() + if player1score > 0.8 * ptw or player2score > 0.8 * ptw then + for i = 1, maxBalls do + if math.abs(ball[i].x - VIRTUAL_WIDTH/2) < 10 and #explosions < 1 then + table.insert(explosions, explosion(love.math.random(100, VIRTUAL_WIDTH-100), love.math.random(player1.y, player2.y), 100, {player1.y/2.81/255,player2.y/2.81/255,ball[1].y/2.81/255,0.4})) + end + end + end + for i, explosion in ipairs(explosions) do + if explosion.killed then + table.remove(explosions, i) + print("buried the body") + end + end end \ No newline at end of file diff --git a/src/dependencies.lua b/src/dependencies.lua index e0aad71..9354dcd 100644 --- a/src/dependencies.lua +++ b/src/dependencies.lua @@ -13,5 +13,7 @@ require 'src/constantvars' require 'src/menus' require 'src/AI' require 'src/reverseGame' +require 'explosion' +tick = require 'tick' utf8 = require("utf8") serialize = require 'ser' \ No newline at end of file diff --git a/tick.lua b/tick.lua new file mode 100644 index 0000000..5d00c7a --- /dev/null +++ b/tick.lua @@ -0,0 +1,70 @@ +-- tick +-- https://github.com/bjornbytes/tick +-- MIT License + +local tick = { + framerate = nil, + rate = .03, + timescale = 1, + sleep = .001, + dt = 0, + accum = 0, + tick = 1, + frame = 1 + } + + local timer = love.timer + local graphics = love.graphics + + love.run = function() + if not timer then + error('love.timer is required for tick') + end + + if love.load then love.load(love.arg.parseGameArguments(arg), arg) end + timer.step() + local lastframe = 0 + + love.update(0) + + return function() + tick.dt = timer.step() * tick.timescale + tick.accum = tick.accum + tick.dt + while tick.accum >= tick.rate do + tick.accum = tick.accum - tick.rate + + if love.event then + love.event.pump() + for name, a, b, c, d, e, f in love.event.poll() do + if name == 'quit' then + if not love.quit or not love.quit() then + return a or 0 + end + end + + love.handlers[name](a, b, c, d, e, f) + end + end + + tick.tick = tick.tick + 1 + if love.update then love.update(tick.rate) end + end + + while tick.framerate and timer.getTime() - lastframe < 1 / tick.framerate do + timer.sleep(.0005) + end + + lastframe = timer.getTime() + if graphics and graphics.isActive() then + graphics.origin() + graphics.clear(graphics.getBackgroundColor()) + tick.frame = tick.frame + 1 + if love.draw then love.draw() end + graphics.present() + end + + timer.sleep(tick.sleep) + end + end + + return tick \ No newline at end of file