-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.lua
More file actions
123 lines (105 loc) · 3.69 KB
/
objects.lua
File metadata and controls
123 lines (105 loc) · 3.69 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
local objects = {}
local objectsAvailables = {}
MARGIN = 180
for i = 1, 15 do
objectsAvailables[i] = love.graphics.newImage("Images/Tiles/Object_" .. i .. ".png")
end
function createObject()
local object = {}
object.x = math.random(MARGIN, SCREEN_WIDTH - MARGIN)
object.y = math.random(MARGIN, SCREEN_HEIGHT - MARGIN)
object.life = 15
local rand = math.random(1, #objectsAvailables)
object.image = objectsAvailables[rand]
object.imageWidth = object.image:getWidth()
object.imageHeight = object.image:getHeight()
object.rebound = false
object.offsetX = object.x - object.imageWidth / 2
object.offsetY = object.y - object.imageHeight / 2
object.offsetX2 = object.offsetX + object.imageWidth
object.offsetY2 = object.offsetY + object.imageHeight
object.radius = object.imageWidth / 2
if object.imageHeight / 2 > object.radius then
object.radius = object.imageHeight / 2
end
if rand == 3 or rand == 4 or rand == 7 then
object.rebound = true
end
object.draw = function()
love.graphics.draw(object.image, object.x, object.y, 0, 1, 1, object.imageWidth / 2, object.imageHeight / 2)
--love.graphics.rectangle("line", object.offsetX, object.offsetY, object.imageWidth, object.imageHeight)
--love.graphics.circle("line", object.x, object.y, object.radius)
--love.graphics.print(tostring(object.life), object.x, object.y)
end
return object
end
function initObjects()
for i = #objects, 1, -1 do
table.remove(objects, i)
end
for i = 1, 15 do
local object = createObject()
while checkObjectCollision(object) do
object = createObject()
end
table.insert(objects, object)
end
end
function updateObjects(dt)
for i = #objects, 1, -1 do
if objects[i].life == 0 then
table.remove(objects, i)
end
end
end
function drawObjects()
for _, object in ipairs(objects) do
object.draw()
end
end
function checkRebound(bullet)
for i = 1, #objects do
local object = objects[i]
if object.rebound == true then
-- si ma bullet touche un objet "rebondissant"
if
(bullet.x + bullet.radius >= object.offsetX and bullet.x - bullet.radius <= object.offsetX2) and
(bullet.y + bullet.radius >= object.offsetY and bullet.y - bullet.radius <= object.offsetY2)
then
-- On se base sur le radius de la balle et de sa position "interne" dans la boite pour déterminer la vélocité à appliquer
local dx = math.abs(bullet.x - object.x)
local dy = math.abs(bullet.y - object.y)
-- les objets sont carrés on teste juste la distance en X ou Y
if dx > dy then
bullet.vx = bullet.vx * -1
elseif dx < dy then
bullet.vy = bullet.vy * -1
else
bullet.vx = bullet.vx * -1
bullet.vy = bullet.vy * -1
end
object.life = object.life - 1
end
end
end
end
function checkVehicleCollision(vehicle)
local collision = false
for _, object in ipairs(objects) do
if object.rebound == true then
if isIntersecting(object.x, object.y, object.radius, vehicle.x, vehicle.y, vehicle.radius) then
collision = true
end
end
end
return collision
end
function checkObjectCollision(newObject)
local collision = false
for _, object in ipairs(objects) do
if collide(object, newObject) then
collision = true
end
end
return collision
end