-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
107 lines (94 loc) · 2.48 KB
/
main.lua
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
speed = 15
function love.load()
width = love.graphics.getWidth()
height = love.graphics.getHeight()
world = love.physics.newWorld(width, height)
world:setGravity(0, speed)
love.graphics.setBackgroundColor(34,34,34)
-- ground = love.physics.newBody(world, 0, 50, 0, 0)
ball = newBall(world, 300,0)
floor = makeFloor(world)
world:setCallbacks(
function(first, second, contact)
puts("COLLISION!",40)
end,
function(first, second, contact)
puts("OAWAKHSAJHSAKHAS", 20)
end,
nil,
nil)
end
function love.update(dt)
world:update(dt)
end
function puts(string, y)
love.graphics.print(tostring(string), 200, 60+y)
end
function love.draw()
love.graphics.print("FPS: " .. love.timer.getFPS(), 50, 50)
-- love.graphics.print("something", body:getX(), body:getY())
ball:draw()
floor:draw()
-- love.graphics.line( body:getPosition(), body:getLinearVelocity())
end
function makeFloor(world)
local floor = {}
floor.body = love.physics.newBody(world, 0, 0, 0, 0)
floor.shape = love.physics.newRectangleShape(floor.body, 0, height-50, width, 50, 0)
function floor:draw()
love.graphics.rectangle("fill", self.body:getX(), self.body:getY(), width, 50)
end
return floor
end
-- x = 300, y = 0
function newBall(world, x, y)
local t = {}
t.body = love.physics.newBody(world, x, y, 10, 0)
t.shape = love.physics.newCircleShape(t.body, x, y, 50)
t.body:setMassFromShapes()
t.color = {255, 0, 0, 87}
function t:draw()
love.graphics.print(self.body:getX(), 400, 350)
love.graphics.print(string.format("%.1f %.1f", self.body:getLinearVelocity()), self.body:getPosition())
local r,g,b,a = love.graphics.getColor()
love.graphics.setColor(unpack(self.color))
love.graphics.circle("fill", self.body:getX(), self.body:getY(), self.shape:getRadius(), 50)
love.graphics.setColor(r,g,b,a)
end
function t:applyImpulse(x, y)
return self.body:applyImpulse(x * speed,y * speed)
end
return t
end
function love.keypressed(key, unicode)
local force = {
w= {0, -1},
a= {-1, 0},
s= {0, 1},
d= {1, 0}--[[,
q= {0, 1, -1, 1},
e= {0, 1, 1, 1}
]]
}
callbacks = {
escape =
function(unicode)
love.event.push('q')
end
}
for index, letter in ipairs({'w', 'a', 's', 'd'}) do
callbacks[letter] =
function(unicode)
-- local x, y = unpack(force[key])
ball:applyImpulse(unpack(force[key]))
end
end
callbacks[' '] =
function(unicode)
-- Kick the ball up
ball:applyImpulse(0, -1)
end
if callbacks[key] then
callbacks[key](unicode)
end
end