-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource.lua
73 lines (55 loc) · 1.8 KB
/
resource.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
physics = require "physics"
sprite = require "sprite"
resource = {
-- Class constants
spriteName = 'img/resource.png',
spriteWidth = 82,
spriteHeight = 74,
spriteIdleFrameBegin = 1,
spriteIdleFrameCount = 1,
spriteIdleFrameRate = 1,
spriteDepleteFrameBegin = 2,
spriteDepleteFrameCount = 1,
spriteDepleteFrameRate = 1,
group = display.newGroup(),
list = {}
}
resource.spriteSheet = sprite.newSpriteSheet(resource.spriteName, resource.spriteWidth, resource.spriteHeight)
resource.spriteSet = sprite.newSpriteSet(resource.spriteSheet, resource.spriteIdleFrameBegin, resource.spriteIdleFrameCount + resource.spriteDepleteFrameCount)
sprite.add(resource.spriteSet, 'idle', resource.spriteIdleFrameBegin, resource.spriteIdleFrameCount, resource.spriteIdleFrameRate, 0)
sprite.add(resource.spriteSet, 'deplete', resource.spriteDepleteFrameBegin, resource.spriteDepleteFrameCount, resource.spriteDepleteFrameRate, 0)
function resource:new(x, y)
local object = {
image = sprite.newSprite(resource.spriteSet),
remaining = 30,
destroyed = false
}
setmetatable(object, { __index = resource })
resource.group:insert(object.image)
physics.addBody(object.image, 'static', {friction = 0.0, bounce = 0.0})
object.image.isFixedRotation = true
object.image:prepare('idle')
object.image:play()
object.image.x = x + resource.spriteWidth / 2
object.image.y = y + resource.spriteHeight / 2
table.insert(resource.list, object)
return object
end
function resource:extract()
if self.remaining > 0 then
self.remaining = self.remaining - 1
if self.remaining == 0 then
self.image:prepare('deplete')
self.image:play()
end
return 1
end
return 0
end
function resource:destroy()
if not self.destroyed then
self.image:removeSelf()
-- Remove from resource.list
self.destroyed = true
end
end