-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopponent.lua
More file actions
126 lines (99 loc) · 3.4 KB
/
Copy pathopponent.lua
File metadata and controls
126 lines (99 loc) · 3.4 KB
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
local opponent = {
isEnabled = false,
hasRolledForTurn = false,
rollResult = nil
}
function opponent:run(gameState)
if self.isEnabled == false then
return
end
local ballUpperBound = gameState.ball.y
local ballLowerBound = gameState.ball.y + gameState.ball.height
local paddleUpperBound = gameState.rightPaddle.y
local paddleLowerBound = gameState.rightPaddle.y + gameState.paddleHeight
local isBallAbovePaddle = ballLowerBound < paddleUpperBound
local isBallUnderPaddle = ballUpperBound > paddleLowerBound
if isBallAbovePaddle then
self:handleBallAbovePaddle(gameState)
elseif isBallUnderPaddle then
self:handleBallUnderPaddle(gameState)
else
self:handleBallInsideOfBounds(gameState)
end
end
function opponent:handleBallAbovePaddle(gameState)
local newPaddleY = gameState.rightPaddle.y - gameState.paddleVelocity
if newPaddleY < 8 then
gameState.rightPaddle.y = gameState.offsetGap
else
gameState.rightPaddle.y = newPaddleY
end
end
function opponent:handleBallUnderPaddle(gameState)
local computed = gameState.rightPaddle.y + gameState.paddleVelocity
if computed > gameState.maxVerticalOffset then
gameState.rightPaddle.y = gameState.maxVerticalOffset
else
gameState.rightPaddle.y = computed
end
end
function opponent:handleBallInsideOfBounds(gameState)
if gameState:ballDidCollide() then
hasRolledForTurn = false
end
if hasRolledForTurn == false then
self:determineVolleyTarget()
hasRolledForTurn = true
end
self:handleVolleyTarget(gameState)
end
function opponent:determineVolleyTarget()
math.randomseed(os.time())
local randomNumber = math.random(1, 9)
if randomNumber <= 3 then
self.rollResult = 'upper_portion'
elseif randomNumber >=4 and randomNumber <= 6 then
self.rollResult = 'middle_portion'
else
self.rollResult = 'lower_portion'
end
end
function opponent:handleVolleyTarget(gameState)
local ballUpperBound = gameState.ball.y
local ballLowerBound = gameState.ball.y + gameState.ball.height
if self.rollResult == 'upper_portion' then
local paddleUpperBound = gameState.rightPaddle.y
local paddleLowerBound = gameState.rightPaddle.y + (gameState.paddleHeight / 3)
local isBallAboveSegment = ballLowerBound < paddleUpperBound
local isBallUnderSegment = ballUpperBound > paddleLowerBound
if isBallAboveSegment then
self:handleBallAbovePaddle(gameState)
elseif isBallUnderSegment then
self:handleBallUnderPaddle(gameState)
end
elseif self.rollResult == 'middle_portion' then
local paddleUpperBound = gameState.rightPaddle.y + (gameState.paddleHeight / 3)
local paddleLowerBound = gameState.rightPaddle.y + ((gameState.paddleHeight / 3) * 2)
local isBallAboveSegment = ballLowerBound < paddleUpperBound
local isBallUnderSegment = ballUpperBound > paddleLowerBound
if isBallAboveSegment then
self:handleBallAbovePaddle(gameState)
elseif isBallUnderSegment then
self:handleBallUnderPaddle(gameState)
end
else
local paddleUpperBound = gameState.rightPaddle.y + ((gameState.paddleHeight / 3) * 2)
local paddleLowerBound = gameState.rightPaddle.y + gameState.paddleHeight
local isBallAboveSegment = ballLowerBound < paddleUpperBound
local isBallUnderSegment = ballUpperBound > paddleLowerBound
if isBallAboveSegment then
self:handleBallAbovePaddle(gameState)
elseif isBallUnderSegment then
self:handleBallUnderPaddle(gameState)
end
end
end
function opponent:setStatus(isEnabled)
self.isEnabled = isEnabled
end
return opponent