-
Notifications
You must be signed in to change notification settings - Fork 0
/
marsLanding.lua
284 lines (257 loc) · 7.87 KB
/
marsLanding.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
-----------------------------------------------------------------------------------------
--
-- blankAct.lua
--
-- An empty (template) activity
-----------------------------------------------------------------------------------------
-- Get local reference to the game globals
local game = globalGame
-- Create the act object
local act = game.newAct()
------------------------- Start of Activity --------------------------------
local widget = require("widget")
-------------------------- Local Variables ---------------------------------
local height = 100000
--velocity is given in x(left/Right), y(Forward/Reverse), z(Up/Down). All together,
--These forces can be used to calculate total magnitude
local velocity = {0, 0, 0}
local group
local group1
local group2
local escapeVelocity = 1200
local thrusting = false
local thrustingUp = false
local thrustingDown = false
local thrustingRight = false
local thrustingLeft = false
local autopilot1 = false
local autopilot2 = false
local autopilot3 = false
-- Init the act
local heightText
local images =
{
"/media/marsLanding/Image1_1.png",
"/media/marsLanding/Image1_2.png",
"/media/marsLanding/Image1_3.png",
"/media/marsLanding/Image2_1.png",
"/media/marsLanding/Image2_2.png",
"/media/marsLanding/Image2_3.png",
"/media/marsLanding/Image3_1.png",
"/media/marsLanding/Image3_2.png",
"/media/marsLanding/Image3_3.png",
}
-------------------------- Local Functions ---------------------------------
local function onBack()
game.gotoAct( "mainAct", { effect = "zoomOutIn", time = 500 } )
end
local function flyTo( shipHeight )
if height < shipHeight and velocity[3] < 50 then
velocity[3] = velocity[3] + 3.711/5
elseif height < shipHeight then
velocity[3] = 50
else
autopilot1 = false
autopilot2 = false
autopilot3 = false
end
end
local function autoPilot()
if height < 10000 and velocity[3] < -50 then
autopilot1 = true
print("autopilot1 " .. autopilot1)
elseif height < 20000 and velocity[3] < -100 then
autopilot2 = true
print("autopilot2 " .. autopilot2)
elseif height < 50000 and velocity[3] < -190 then
autopilot3 = true
print("autopilot3 " .. autopilot3)
end
end
local function thrust( event )
if event.phase == "began" then
thrusting = true
elseif event.phase == "ended" then
thrusting = false
end
end
local function createSideThrustButton( scene, x, y, vertices, listener )
local b = widget.newButton {
x = x, y = y,
shape = "polygon",
vertices = vertices,
fillColor = { default = { game.themeColor.r, game.themeColor.g, game.themeColor.b },
over = { game.themeHighlightColor.r, game.themeHighlightColor.g, game.themeHighlightColor.b } },
labelColor = { default={ 1, 1, 1 } },
onEvent = listener
}
scene:insert(b)
return b
end
local function thrustUp( event )
if event.phase == "began" then
thrustingUp = true
elseif event.phase == "ended" then
thrustingUp = false
end
end
local function thrustDown( event )
if event.phase == "began" then
thrustingDown = true
elseif event.phase == "ended" then
thrustingDown = false
end
end
local function thrustLeft( event )
if event.phase == "began" then
thrustingLeft = true
elseif event.phase == "ended" then
thrustingLeft = false
end
end
local function thrustRight( event )
if event.phase == "began" then
thrustingRight = true
elseif event.phase == "ended" then
thrustingRight = false
end
end
local function gravity()
--print (velocity[1], velocity[2], velocity[3])
if (not thrusting) then
velocity[3] = velocity[3] - 3.711/5
if (velocity[3] < -200) then
velocity[3] = -200
end
else
velocity[3] = velocity [3] + 3.711/5
end
height = height + velocity[3]
--heightText.text = "Height: " .. math.floor(height) .. "\nX Velocity: " .. math.floor(velocity[1]) .. "\nY Velocity: " .. math.floor(velocity[2]) .. "\nZ Velocity: " .. math.floor(velocity[3])
heightText.text = "Height: " .. math.floor(height) .. "\nZ Velocity: " .. math.floor(velocity[3])
end
local function moving()
if (thrustingUp) then
velocity[2] = velocity[2] + 2/10
elseif(thrustingDown) then
velocity[2] = velocity[2] - 2/10
end
if (thrustingLeft) then
velocity[1] = velocity[1] - 2/10
elseif (thrustingRight) then
velocity[1] = velocity[1] + 2/10
end
end
local function friction()
if (velocity[1] > 0) then
velocity[1] = velocity[1] - 2/45
elseif(velocity[1] < 0) then
velocity[1] = velocity[1] + 2/45
end
if (velocity[2] > 0) then
velocity[2] = velocity[2] - 2/45
elseif(velocity[2] < 0) then
velocity[2] = velocity[2] + 2/45
end
end
local function moveShip()
group1.x = group1.x + velocity[1]
group1.y = group1.y + velocity[2]
--print(group1.x, group1.y)
if height > 100 then
group1.xScale = 100 / (500 * (height/100000))
group1.yScale = 100 / (500 * (height/100000))
--print(group1.xScale, group1.yScale)
end
end
------------------------ EnterFrame Loop ---------------------------------------
function act:enterFrame()
gravity()
moving()
friction()
autoPilot()
if autopilot1 == true then
flyTo(15000)
elseif autopilot2 == true then
flyTo(25000)
elseif autopilot3 == true then
flyTo(75000)
end
moveShip()
end
--------------------------- Init Game ------------------------------------------
function act:init()
-- Remember to put all display objects in act.group
group = display.newGroup( )
act.group:insert(group)
group1 = display.newGroup( )
group1.x = act.xCenter
group1.y = act.yCenter
act.group:insert(group1)
local k = 1
for i = -1, 1 do
for j = -1, 1 do
local l = display.newImage(group1, images[k], 750, 1000)
l.x = 750*j
l.y = 1000*i
k = k + 1
end
end
group2 = display.newGroup( )
act.group:insert(group)
local background = {}
background[1] = display.newRect(act.group, act.xCenter, act.yMin + 50, act.width, 100)
background[2] = display.newRect(act.group, act.xMin + 20, act.yCenter, 40, act.height)
background[3] = display.newRect(act.group, act.xCenter, act.yMax - 50, act.width, 100)
background[4] = display.newRect(act.group, act.xMax - 20, act.yCenter, 40, act.height)
for i = 1, #background do
background[i]:setFillColor(.5)
end
local heightTextOptions =
{
text = "Height: " .. height,
x = act.xMin + 5,
y = act.yMin + 50,
fontSize = 20,
font = native.systemFontBold,
}
heightText = display.newText(heightTextOptions)
heightText.anchorX = 0
heightText.anchorY = 0
act.group:insert(heightText)
local backBtn = widget.newButton {
label = "Back",
x = act.xMin + 40, y = act.yMin + 30,
shape = "roundedRect",
width = 60, height = 40,
fillColor = { default = { game.themeColor.r, game.themeColor.g, game.themeColor.b },
over = { game.themeHighlightColor.r, game.themeHighlightColor.g, game.themeHighlightColor.b } },
labelColor = { default={ 1, 1, 1 } },
onRelease = onBack
}
act.group:insert( backBtn )
--Creating the buttons
local thrustRightBtn = createSideThrustButton( act.group, act.xMax - 110, act.yMax - 65,
{ 0, -20, -25, 0, 0, 20 }, thrustRight )
local thrustLeftBtn = createSideThrustButton( act.group, act.xMax - 30, act.yMax - 65,
{ 0,-20, 25,0, 0,20 }, thrustLeft )
local thrustUpBtn = createSideThrustButton( act.group, act.xMax - 70, act.yMax - 105,
{ 0,-25, -20,0, 20,0 }, thrustUp )
local thrustDownBtn = createSideThrustButton( act.group, act.xMax - 70, act.yMax - 25,
{ 0,25, -20,0, 20,0 }, thrustDown )
local thrustBtn = widget.newButton {
label = "Thrust",
x = act.xMin + 55, y = act.yMax - 50,
shape = "circle",
radius = 35,
fillColor = { default = { game.themeColor.r, game.themeColor.g, game.themeColor.b },
over = { game.themeHighlightColor.r, game.themeHighlightColor.g, game.themeHighlightColor.b } },
labelColor = { default={ 1, 1, 1 } },
onEvent = thrust
}
act.group:insert( thrustBtn )
end
---------------------------- Listeners -----------------------------------
------------------------- End of Activity --------------------------------
-- Corona needs the scene object returned from the act file
return act.scene