From 60a94167109c47d4622782b75f7c3a1c2bc55f94 Mon Sep 17 00:00:00 2001 From: Jack Zhang <954294627@qq.com> Date: Thu, 26 May 2022 23:08:57 +0800 Subject: [PATCH] [1.0.0b] Fixed tick rate error by using https://github.com/bjornbytes/tick --- Entities.lua | 4 +-- conf.lua | 3 --- main.lua | 4 +++ tick.lua | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 tick.lua diff --git a/Entities.lua b/Entities.lua index 4cc049d..760874e 100644 --- a/Entities.lua +++ b/Entities.lua @@ -68,7 +68,7 @@ entityConfig = { ['type'] = 'MoveAround', ['mass'] = 1.5, ['bonus'] = 2, - ['speed'] = 0.25, + ['speed'] = 1, ['width'] = MOLE_WIDTH, ['height'] = MOLE_HEIGHT, ['moveRange'] = 135, @@ -82,7 +82,7 @@ entityConfig = { ['type'] = 'MoveAround', ['mass'] = 1.5, ['bonus'] = 602, - ['speed'] = 0.25, + ['speed'] = 1, ['width'] = MOLE_WIDTH, ['height'] = MOLE_HEIGHT, ['moveRange'] = 135, diff --git a/conf.lua b/conf.lua index 0bb1639..4f4608b 100644 --- a/conf.lua +++ b/conf.lua @@ -34,8 +34,6 @@ COLOR_ORANGE = {239/255, 108/255, 0/255} COLOR_DEEP_ORANGE = {194/255, 136/255, 4/255} COLOR_GREEN = {67/255, 160/255, 71/255} -TOTAL_LEVEL_COUNT = 3 - DEBUG_MODE = false SHOW_BOUNDING_VOLUME = false TEST_LEVEL = false @@ -49,5 +47,4 @@ function love.conf(t) t.window.borderless = true -- Remove all border visuals from the window (boolean) t.window.resizable = false -- Let the window be user-resizable (boolean) t.window.fullscreen = false -- Enable fullscreen (boolean) - t.window.vsync = 1 -- Vertical sync mode (number) end \ No newline at end of file diff --git a/main.lua b/main.lua index d0ad337..5dd25a0 100644 --- a/main.lua +++ b/main.lua @@ -2,11 +2,15 @@ Gamestate = require 'hump.gamestate' Class = require 'hump.class' vector = require 'hump.vector' lume = require 'lume' +tick = require 'tick' require 'Util' require 'GameStates' require 'Animation' function love.load() + -- Set FPS + tick.framerate = 60 + -- Init fonts defaultFont = love.graphics.getFont() infoFont = love.graphics.newFont('fonts/Pixel-Square-10-1.ttf', 10) diff --git a/tick.lua b/tick.lua new file mode 100644 index 0000000..d659e0c --- /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 + + if love.update then love.update(0) end + + 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