summaryrefslogtreecommitdiff
path: root/bump/main.lua
diff options
context:
space:
mode:
Diffstat (limited to 'bump/main.lua')
-rw-r--r--bump/main.lua163
1 files changed, 163 insertions, 0 deletions
diff --git a/bump/main.lua b/bump/main.lua
new file mode 100644
index 0000000..973cd61
--- /dev/null
+++ b/bump/main.lua
@@ -0,0 +1,163 @@
+local bump = require 'bump'
+local bump_debug = require 'bump_debug'
+io.stdout:setvbuf("no")
+local instructions = [[
+ bump.lua simple demo
+
+ arrows: move
+ tab: toggle debug info
+ delete: run garbage collector
+]]
+
+local cols_len = 0 -- how many collisions are happening
+
+-- World creation
+local world = bump.newWorld()
+
+
+-- Message/debug functions
+local function drawMessage()
+ local msg = instructions:format(tostring(shouldDrawDebug))
+ jin.graphics.setColor(255, 255, 255)
+ -- jin.graphics.print(msg, 550, 10)
+end
+
+local function drawDebug()
+ bump_debug.draw(world)
+
+ local statistics = ("fps: %d, mem: %dKB, collisions: %d, items: %d"):format(love.timer.getFPS(), collectgarbage("count"), cols_len, world:countItems())
+ jin.graphics.setColor(255, 255, 255)
+ -- jin.graphics.printf(statistics, 0, 580, 790, 'right')
+end
+
+local consoleBuffer = {}
+local consoleBufferSize = 15
+for i=1,consoleBufferSize do consoleBuffer[i] = "" end
+local function consolePrint(msg)
+ table.remove(consoleBuffer,1)
+ consoleBuffer[consoleBufferSize] = msg
+end
+
+local function drawConsole()
+ local str = table.concat(consoleBuffer, "\n")
+ for i=1,consoleBufferSize do
+ jin.graphics.setColor(255,255,255, i*255/consoleBufferSize)
+ -- love.graphics.printf(consoleBuffer[i], 10, 580-(consoleBufferSize - i)*12, 790, "left")
+ end
+end
+
+-- helper function
+local function drawBox(box, r,g,b)
+ jin.graphics.setColor(r,g,b,70)
+ jin.graphics.rect("fill", box.x, box.y, box.w, box.h)
+ jin.graphics.setColor(r,g,b)
+ jin.graphics.rect("line", box.x, box.y, box.w, box.h)
+end
+
+
+
+-- Player functions
+local player = { x=50,y=50,w=20,h=20, speed = 80, onGround = false}
+vx = 0
+vy = 0
+local function updatePlayer(dt)
+ local speed = player.speed
+ if jin.keyboard.isDown('Right') then
+ vx = speed
+ elseif jin.keyboard.isDown('Left') then
+ vx = -speed
+ end
+ if jin.keyboard.isDown('Down') then
+ vy = vy
+ elseif jin.keyboard.isDown('Up') then
+ vy = vy
+ elseif jin.keyboard.isDown('Space') and player.onGround then
+ vy = vy - 12000 * dt
+ print("jump")
+ print("on sky")
+ player.onGround = false
+ end
+ if not player.onGround then
+ vy = vy + 200 * dt
+ end
+ if vx ~= 0 or vy ~= 0 then
+ local cols
+ local py = player.y
+ player.x, player.y, cols, cols_len = world:move(player, player.x + vx * dt, player.y + vy * dt)
+ if py == player.y then
+ player.onGround = true
+ vy = 0
+ print("on ground")
+ end
+ for i=1, cols_len do
+ local col = cols[i]
+ consolePrint(("col.other = %s, col.type = %s, col.normal = %d,%d"):format(col.other, col.type, col.normal.x, col.normal.y))
+ end
+ end
+ vx = 0
+end
+
+local function drawPlayer()
+ drawBox(player, 0, 255, 0)
+end
+
+-- Block functions
+
+local blocks = {}
+
+local function addBlock(x,y,w,h)
+ local block = {x=x,y=y,w=w,h=h}
+ blocks[#blocks+1] = block
+ world:add(block, x,y,w,h)
+end
+
+local function drawBlocks()
+ for _,block in ipairs(blocks) do
+ drawBox(block, 255,0,0)
+ end
+end
+
+function jin.core.onEvent(e)
+ if e.type == "quit" then
+ jin.core.stop()
+ end
+ if e.type == "keydown" then
+ if e.key == "Escape" then
+ jin.core.stop()
+ end
+ end
+end
+
+-- Main LÖVE functions
+
+function jin.core.onLoad()
+ world:add(player, player.x, player.y, player.w, player.h)
+
+ addBlock(0, 0, 800, 32)
+ addBlock(0, 32, 32, 600-32*2)
+ addBlock(800-32, 32, 32, 600-32*2)
+ addBlock(0, 600-32, 800, 32)
+
+ for i=1,30 do
+ addBlock( math.random(100, 600),
+ math.random(100, 400),
+ math.random(10, 100),
+ math.random(10, 100)
+ )
+ end
+end
+
+function jin.core.onUpdate(dt)
+ cols_len = 0
+ updatePlayer(dt)
+end
+
+function jin.core.onDraw()
+ drawBlocks()
+ drawPlayer()
+ if shouldDrawDebug then
+ drawDebug()
+ drawConsole()
+ end
+ drawMessage()
+end