Step 1: Go through the list of balls and find the ones travelling towards my AI paddle
Step 2: Put them in a potentialTargets table
Step 3: Find out which potentialTargets will reach the bottom first by applying their current Y value (y) + their Vertical Velocity (v) over an undetermined period of time.
Step 4: Flag the ball to be a target
Step 5: move the ball towards the target
Here is what I have:
Code: Select all
function findTargetBall()
--table to store our potential balls
local potentialTargets = {}
--index for our table above
local j = 1
for i = 1, #ballList do
if ballList[i].v > 0 then
--store all the balls moving down in our table
potentialTargets[j] = ballList[i]
j = j + 1
end
end
for i = 1, #potentialTargets do
--store our potentialTargets Y so we don't change it
local y = potentialTargets[i].y
--compare the y to our paddles
while y <= paddleList[2].y do
--as long as its not passed our paddle we add some velocity
y = y + potentialTargets[i].v
--and count the steps
potentialTargets[i].stepsToPaddle = potentialTargets[i].stepsToPaddle + 1
end
--now we sort by the steps putting the smallest in the front
table.sort(potentialTargets, function(a,b) return a.stepsToPaddle < b.stepsToPaddle end)
end
for i = 1, #ballList do
if ballList[i] == potentialTargets[1] then
ballList[i].isTarget = true
else
ballList[i].isTarget = false
end
end
end
Code: Select all
--AI movement
for i = 1, #ballList do
local x = paddleList[2].x + .5 * paddleList[2].w
if ballList[i].isTarget then
if ballList[i].x >= x then
--if its further than 5
if ballList[i].x - x >= 5 then
paddleList[2]:update(4.5) --Our max ball speed is 5, we want to be able to beat it
else
paddleList[2]:update(ballList[i].x - x)
end
elseif ballList[i].x < x then
--if its further than 5
if ballList[i].x - x >= -5 then
paddleList[2]:update(-4.5)
else
paddleList[2]:update(ballList[i].x - x)
end
end
end
end
I want to get this all set before I re-write my program to include/learn physics so I can implement my magnet and repel abilities!