-
Notifications
You must be signed in to change notification settings - Fork 0
/
paddle.lua
72 lines (53 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
local Config = require('config')
local Paddle = {}
function Paddle.isNotTouchTopLimit(targetPosY)
return targetPosY > Config.Paddle.topLimit
end
function Paddle.isNotTouchBottomLimit(targetPosY)
return targetPosY <= Config.Paddle.bottomLimit
end
function Paddle.defaultPos()
local paddingX = 15
Config.Player.posX = paddingX
Config.Player.posY = Config.Window.height /2 - Config.Paddle.height/2
Config.IA.posX = (Config.Window.width - paddingX) - Config.Paddle.width
Config.IA.posY = Config.Window.height /2 - Config.Paddle.height /2
end
function Paddle.onCollisionEnter()
local ballHeight = Config.Ball.posY + Config.Ball.radius
-- Check player CollisionEnter :
if Config.Ball.posX <= Config.Player.posX + Config.Paddle.width then
local paddleHeight = Config.Player.posY + Config.Paddle.height
-- Check if the ball touches the player's paddle :
if ballHeight > Config.Player.posY and Config.Ball.posY < paddleHeight then
Config.Ball.dirX = -Config.Ball.dirX
Config.Ball.dirY = math.random() * 2 - 1 -- Random value between -1 and 1
end
end
-- Check IA CollisionEnter :
if Config.Ball.posX + Config.Ball.radius >= Config.IA.posX then
local paddleHeight = Config.IA.posY + Config.Paddle.height
if ballHeight > Config.IA.posY and Config.Ball.posY < paddleHeight then
Config.Ball.dirX = -Config.Ball.dirX
Config.Ball.dirY = math.random() * 2 - 1 -- Random value between -1 and 1
end
end
end
function Paddle.move(dt)
local ballHeight = Config.Ball.posY + Config.Ball.radius
-- Move IA vertically based on the ball position:
if Config.Ball.posX > Config.Window.width / 2 and Config.Ball.dirX > 0 then
if ballHeight > Config.IA.posY + Config.Paddle.height then
-- Move down
if Paddle.isNotTouchBottomLimit(Config.IA.posY) then
Config.IA.posY = Config.IA.posY + Config.Paddle.speedVel / 1.2 * dt
end
elseif ballHeight < Config.IA.posY + Config.Paddle.height / 2 then
-- Move up
if Paddle.isNotTouchTopLimit(Config.IA.posY) then
Config.IA.posY = Config.IA.posY - Config.Paddle.speedVel / 1.2 * dt
end
end
end
end
return Paddle;