-
Notifications
You must be signed in to change notification settings - Fork 1
/
platform.lua
193 lines (162 loc) · 5.66 KB
/
platform.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
physics = require "physics"
sprite = require "sprite"
--require 'ground'
platform = {
-- Class constants
spriteName = 'img/platform.png',
spriteWidth = 199,
spriteHeight = 103,
spriteIdleFrameBegin = 1,
spriteIdleFrameCount = 1,
spriteIdleFrameRate = 500,
spriteFireFrameBegin = 2,
spriteFireFrameCount = 3,
spriteFireFrameRate = 500,
velocityMax = 500,
rotationMax = 10,
resourcesMax = 150,
charge = 0,
chargeFull = 30,
-- Make the class singleton-ish so it can handle global events gracefully
instance = nil
}
platform.spriteSheet = sprite.newSpriteSheet(platform.spriteName, platform.spriteWidth, platform.spriteHeight)
platform.spriteSet = sprite.newSpriteSet(platform.spriteSheet, platform.spriteIdleFrameBegin, platform.spriteIdleFrameCount + platform.spriteFireFrameCount)
sprite.add(platform.spriteSet, 'idle', platform.spriteIdleFrameBegin, platform.spriteIdleFrameCount, platform.spriteIdleFrameRate, 0)
sprite.add(platform.spriteSet, 'fire', platform.spriteFireFrameBegin, platform.spriteFireFrameCount, platform.spriteFireFrameRate, 0)
function platform:new(center_x, center_y)
if not platform.instance then
platform.instance = {
resources = 30,
laser = nil,
image = sprite.newSprite(platform.spriteSet),
destroyed = false
}
setmetatable(platform.instance, { __index = platform })
physics.addBody(platform.instance.image, 'kinematic', {friction = 0.5, bounce = 0.2, filter = { categoryBits = 16, maskBits = 68 }})
platform.instance.image.isFixedRotation = true
Runtime:addEventListener('accelerometer', platform.onAccelerometer)
Runtime:addEventListener('touch', platform.onTouch)
platform.instance.image:prepare('idle')
platform.instance.image:play()
end
platform.instance.image.x = center_x
platform.instance.image.y = center_y
return platform.instance
end
function platform:destroy()
if not self.destroyed then
self.image:removeSelf()
Runtime:removeEventListener('accelerometer', onAccelerometer)
Runtime:removeEventListener('touch', onTouch)
platform.instance = nil
self.destroyed = true
end
end
accel_readout = nil
function platform.onAccelerometer(event)
if accel_readout then
accel_readout:removeSelf()
end
if platform.instance then
--platform.instance.image:setLinearVelocity(-platform.instance.velocityMax, 0)
--accel_readout = display.newText('x/y/z: ' .. event.xGravity .. '/' .. event.yGravity .. '/' .. event.zGravity, viewx + 500, 300, native.systemFont, 20)
--print('x/y/z: ' .. event.xGravity .. '/' .. event.yGravity .. '/' .. event.zGravity)
local normalize = math.max(math.min(0.5, -event.yGravity), -0.5) * 2
if math.abs(normalize) < 0.0 then
normalize = 0
end
platform.instance.image:setLinearVelocity(platform.instance.velocityMax * normalize, 0)
platform.instance.image.rotation = normalize * platform.rotationMax
end
end
function platform.onTouch(event)
-- Can't test accelerometer on simulator, so touch the sides of the screen for testing
if platform.instance then
if event.phase == 'began' then
if event.x < 100 then
platform.instance.image:setLinearVelocity(-platform.instance.velocityMax, 0)
platform.instance.image.rotation = -platform.rotationMax
elseif event.x > 960 - 100 then
platform.instance.image:setLinearVelocity(platform.instance.velocityMax, 0)
platform.instance.image.rotation = platform.rotationMax
end
if event.y > 400 then
platform.instance.image:setLinearVelocity(0, 0)
if platform.instance.charge == 0 then
platform.instance.charge = 1
media.playEventSound(sound.laser_charge)
--audio.play(sound.laser_charge)
platform.instance.image:prepare('fire')
platform.instance.image:play()
end
end
elseif event.phase == 'ended' or event.phase == 'cancelled' then
platform.instance.image:setLinearVelocity(0, 0)
platform.instance.image.rotation = 0
platform.instance.charge = 0
if platform.instance.laser ~= nil then
platform.instance.laser:destroy()
platform.instance.laser = nil
end
platform.instance.image:prepare('idle')
platform.instance.image:play()
end
end
end
function platform:update(time)
if self.charge == self.chargeFull then
if not self.laser then
self.laser = laser:new(self.image.x, self.image.y + laser.spriteHeight/2 + 44)
end
elseif self.charge > 0 then
self.charge = self.charge + 1
end
end
-- Laser/extraction-thingy class
laser = {
-- Class constants
--velocity = 400,
spriteName = 'img/laser_stretch.png',
spriteWidth = 26,
spriteHeight = 540,
spriteIdleFrameBegin = 1,
spriteIdleFrameCount = 1,
spriteIdleFrameRate = 1,
group = display.newGroup(),
list = {}
}
laser.spriteSheet = sprite.newSpriteSheet(laser.spriteName, laser.spriteWidth, laser.spriteHeight)
laser.spriteSet = sprite.newSpriteSet(laser.spriteSheet, laser.spriteIdleFrameBegin, laser.spriteIdleFrameCount)
sprite.add(laser.spriteSet, 'idle', laser.spriteIdleFrameBegin, laser.spriteIdleFrameCount, laser.spriteIdleFrameRate, 0)
function laser:new(center_x, center_y)
local object = {
image = sprite.newSprite(laser.spriteSet),
destroyed = false
}
setmetatable(object, { __index = laser })
media.playSound(sound.laser, true)
--audio.play(sound.laser)
laser.group:insert(object.image)
object.image.x = center_x
object.image.y = center_y
return object
end
function laser:update(time)
for _, i in ipairs(ground.list) do
if self.image.x >= i:x() and self.image.x < i:x() + i.w then
i:carve(self.image.x, 48, 1)
break
end
end
platform.instance.image:setLinearVelocity(0, 0)
platform.instance.image.rotation = 0
end
function laser:destroy()
if not destroyed then
media.stopSound()
--audio.stop(0)
self.image:removeSelf()
self.destroyed = true
end
end