-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathball.lua
105 lines (91 loc) · 2.61 KB
/
ball.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
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
local ballClass = { }
ballClass.__index = ballClass
local state = require('state')
local Vector = require('vector')
local function Ball()
local width, height = love.graphics.getDimensions()
local instance = {
position = Vector( width/2, height/2 ),
velocity = Vector(100, 200),
radius = 5
}
setmetatable(instance, ballClass)
return instance
end
function ballClass:draw()
love.graphics.setColor( 183, 3, 3 )
love.graphics.circle( 'fill', self.position.x, self.position.y, self.radius )
end
function ballClass:update(dt)
local edge = self:edge()
if (edge ~= "") then
self:reflection(edge, dt)
end
self.position = self.position:move(self.velocity, dt)
end
function ballClass:edge()
local width, height = love.graphics.getDimensions()
local edge = ""
if self.position.x <= 0 then
edge = "left"
end
if (self.position.x + self.radius) >= width then
edge = "right"
end
if self.position.y <= 0 then
edge = "top"
end
if (self.position.y + self.radius) >= height then
edge = "bottom"
end
return edge
end
function ballClass:reflection(edge, dt)
local width, height = love.graphics.getDimensions()
if edge == "top" or edge == "bottom" then
self.velocity = self.velocity:reverse("y")
if self.position.x < 10 or self.position.x > width - 10 then
self.velocity = self.velocity:reverse("x")
end
end
if edge == "left" or edge == "right" then
self.velocity = self.velocity:reverse("x")
if self.position.y < 10 or self.position.y > height - 10 then
self.velocity = self.velocity:reverse("y")
end
self:collisionCheck(edge, dt)
end
end
function ballClass:collisionCheck(edge, dt)
local save = false
for _, entity in ipairs(state.entities) do
if (entity.num == 1 and edge == "right")
or (entity.num == 2 and edge == "left") then
save = self:touching(entity, dt)
end
end
if save == false then
state:addPoint(edge)
end
end
function ballClass:touching(player, dt)
if self.position.y > player.y - 5 and self.position.y < player.y + player.w + 5 then
self:hitangle(player.y - 5, player.y + player.h + 5, dt)
return true
else
return false
end
end
function ballClass:hitangle(top, bottom, dt)
local span = bottom - top
if self.y > top and self.y < top + span/3 then
self.velocity = self.velocity:move(Vector(100, 200), dt)
end
if self.y > top + span/3 and self.y < top + (2 * span/3) then
self.velocity = self.velocity:move(Vector(100, 0), dt)
end
if self.y > top + (2 * span/3) and self.y < bottom then
self.velocity = self.velocity:move(Vector(100, -200), dt)
end
end
return Ball