-
Notifications
You must be signed in to change notification settings - Fork 0
/
paddle.lua
77 lines (66 loc) · 2.29 KB
/
paddle.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
local Paddle = Base:extend({
image = nil,
body = nil,
shape = nil,
fixture = nil,
move = {left = 0, right = 0, up = 0, down = 0},
--move = 'nowhere', -- TODO: Improve that movement system with history support
speed = 400,
})
function Paddle:constructor(x, y)
self.image = love.graphics.newImage("assets/paddle.png")
x = x or (love.graphics.getWidth() / 2)
y = y or love.graphics.getHeight() -
(love.graphics.getHeight() / 100 * 10)
self.body = love.physics.newBody(world, x, y, "static")
self.shape = love.physics.newRectangleShape(self.image:getWidth(),
self.image:getHeight())
self.fixture = love.physics.newFixture(self.body, self.shape)
self.fixture:setUserData("Paddle")
self.fixture:setFriction(0.0)
end
function Paddle:update(dt)
if self.move.left > 0 and self.body:getX() - (self.image:getWidth()/2) > 0 then
self.body:setX(self.body:getX() - (self.speed * dt))
end
if self.move.right > 0
and self.body:getX() + (self.image:getWidth()/2) < love.graphics.getWidth() then
self.body:setX(self.body:getX() + (self.speed * dt))
end
if self.move.up > 0 then
self.body:setY(self.body:getY() - (self.speed * dt))
end
if self.move.down > 0 then
self.body:setY(self.body:getY() + (self.speed * dt))
end
end
function Paddle:keypressed(key, scancode, isrepeat)
if key == 'left' then
self.move.left = 1
elseif key == 'right' then
self.move.right = 1
elseif key == 'up' then
self.move.up = 1
elseif key == 'down' then
self.move.down = 1
end
end
function Paddle:keyreleased(key)
if key == 'left' then
self.move.left = 0
elseif key == 'right' then
self.move.right = 0
elseif key == 'up' then
self.move.up = 0
elseif key == 'down' then
self.move.down = 0
end
end
function Paddle:draw(dt)
--love.graphics.polygon("fill", self.body:getWorldPoints(self.shape:getPoints()))
love.graphics.draw(self.image, self.body:getX() - (self.image:getWidth() / 2),
self.body:getY() - (self.image:getHeight() / 2))
--x, y = self.fixture:getBoundingBox()
--love.graphics.draw(self.image, x, y)
end
return Paddle