-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai.lua
More file actions
40 lines (34 loc) · 826 Bytes
/
ai.lua
File metadata and controls
40 lines (34 loc) · 826 Bytes
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
AI = {}
function AI:load()
self.width = 20
self.height = 100
self.x = love.graphics.getWidth() - self.width - 50
self.y = love.graphics.getHeight() / 2
self.yVel = 0
self.speed = 500
self.timer = 0
self.rate = 0.5
end
function AI:update(dt)
self:move(dt)
self.timer = self.timer + dt
if self.timer > self.rate then
self.timer = 0
self:acquireTarget()
end
end
function AI:move(dt)
self.y = self.y + self.yVel * dt
end
function AI:acquireTarget()
if Ball.y + Ball.height < self.y then
self.yVel = - self.speed
elseif Ball.y > self.y + self.height then
self.yVel = self.speed
else
self.yVel = 0
end
end
function AI:draw()
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
end