1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
|