-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocks.lua
63 lines (48 loc) · 1.13 KB
/
blocks.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
--
-- blocks.lua
-- defines the Block class
--
Block = {}
Block.__index = Block
function Block.new(type, x, y)
local self = {}
setmetatable(self, Block)
local b = blocks[type]
-- position
self.x = x
self.y = y
self.width = b.width
self.height = b.height
self.offset = b.offset
self.rect = HC.rectangle(x+self.offset.x,y+self.offset.y,self.width,self.height)
-- descriptors
self.type = 'block'
self.name = b.name
self.description = b.description
self.typ = b.typ
self.img = b.image
self.health = b.health
self.active = true
-- moving
self.start = x
self.range = b.range
self.fin = self.start + self.range
self.step_x = b.step_x
self.vel = { x = b.step_x, y = 0 }
return self
end
function Block:update(dt, bgPos)
-- walk back and forth
if self.step_x ~= 0 then
if self.x >= self.fin + bgPos then
self.vel.x = -self.step_x
elseif self.x < self.start + bgPos then
self.vel.x = self.step_x
end
self.x = self.x + self.vel.x
self.rect:moveTo(self.x + self.width/2 + self.offset.x, self.y + self.height/2 + self.offset.y)
end
end
function Block:draw()
love.graphics.draw(self.img, self.x, self.y)
end