-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbox.lua
More file actions
66 lines (50 loc) · 1.62 KB
/
Copy pathbox.lua
File metadata and controls
66 lines (50 loc) · 1.62 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
local Box = {
img = love.graphics.newImage("assets/box.png")
}
Box.__index = Box
Box.width = Box.img:getWidth()
Box.height = Box.img:getHeight()
local ActiveBoxes = {}
function Box.new(x, y)
local instance = setmetatable({}, Box) -- create a new instance of the Box class
instance.x = x
instance.y = y
instance.rotation = 0
instance.scaleX = 1
instance.toBeRemoved = false
instance.physics = {}
instance.physics.body = love.physics.newBody(World, instance.x, instance.y, "dynamic")
instance.physics.shape = love.physics.newRectangleShape(instance.width, instance.height)
instance.physics.fixture = love.physics.newFixture(instance.physics.body, instance.physics.shape)
instance.physics.body:setMass(20)
instance.randomTimeOffset = math.random(0, 100)
table.insert(ActiveBoxes, instance)
end
function Box:syncPhysics()
self.x, self.y = self.physics.body:getPosition()
self.rotation = self.physics.body:getAngle()
end
function Box:update(dt)
self:syncPhysics()
end
function Box:draw()
love.graphics.draw(self.img, self.x, self.y, 0, self.scaleX, 1 , self.width/2, self.height/2)
-- love.graphics.draw(self.img, self.x, self.y, self.rotation, self.scaleX, 1 , self.width/2, self.height/2) -- shows rotation
end
function Box:drawAll()
for i, instance in ipairs(ActiveBoxes) do
instance:draw()
end
end
function Box:updateAll(dt)
for i, instance in ipairs(ActiveBoxes) do
instance:update(dt)
end
end
function Box.removeAll()
for i,v in ipairs(ActiveBoxes) do
v.physics.body:destroy()
end
ActiveBoxes = {}
end
return Box