-
Notifications
You must be signed in to change notification settings - Fork 0
/
shipBuilder.lua
318 lines (216 loc) · 8.68 KB
/
shipBuilder.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
-----------------------------------------------------------------------------------------
--
-- shipBuilder.lua
--
-----------------------------------------------------------------------------------------
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
-- include Corona's "physics" library
local physics = require "physics"
physics.start(); physics.pause()
typeObject = ""
selectedStartX = 140
selectedStartY = 80
itemsAddedText = display.newText( "Added items - 0", 0, 0, "Helvetica", 30 )
newObjectText = display.newText( "New object", 0, 0, "Helvetica", 30 )
typeText = display.newText( "Type", 0, 0, "Helvetica", 30 )
widthText = display.newText( "Width", 0, 0, "Helvetica", 30 )
heightText = display.newText( "Height", 0, 0, "Helvetica", 30 )
--------------------------------------------
-- forward declarations and other locals
local screenW, screenH, halfW, halfH = display.contentWidth, display.contentHeight, display.contentWidth*0.5, display.contentHeight*0.5
-----------------------------------------------------------------------------------------
-- BEGINNING OF IMPLEMENTATION
--
-- NOTE: Code outside of listener functions (below) will only be executed once,
-- unless storyboard.removeScene() is called.
--
-----------------------------------------------------------------------------------------
--timer.performWithDelay( 60, controlLoop, 0 )
-- Called when the scene's view does not exist:
function scene:createScene( event )
local group = self.view
interfaceGroup = display.newGroup()
shipGroup = display.newGroup()
menuShapeGroup = display.newGroup()
menuWeaponGroup = display.newGroup()
menuGroup = display.newGroup()
menuShapeGroup.x, menuShapeGroup.y = 360,60
menuShapeGroup.isVisible = false
menuGroup.x, menuGroup.y = 180, 60
local widget = require "widget"
--Declare objects
--typeText.x, typeText.y = 260, display.contentHeight - 90
typeText.x, typeText.y = 250, display.contentHeight - 180
itemsAddedText.x, itemsAddedText.y = display.contentWidth - 100, 50
newObjectText.x, newObjectText.y = 0, display.contentHeight - 180
typeObject = display.newRect(0, 0, 100, 100)
typeObject.x, typeObject.y = 250, display.contentHeight - 80
local myObject = display.newRect( 0, 0, 100, 100 )
myObject.x, myObject.y = 0, display.contentHeight - 80
heightText.x, heightText.y = 430, display.contentHeight - 180
widthText.x, widthText.y = 700, display.contentHeight - 180
local function closeMenu()
menuGroup.isVisible = false
menuShapeGroup.isVisible = false
menuWeaponGroup.isVisible = false
end
local function addShapeBtnRelease()
if menuShapeGroup.isVisible == true then
menuShapeGroup.isVisible = false
else
menuShapeGroup.isVisible = true
end
end
local addShapeBtn = widget.newButton{
label="Add Shape",
labelColor = { default={255}, over={128} },
default="button.png",
over="button-over.png",
width=180, height=60,
onRelease = addShapeBtnRelease -- event listener function
}
addShapeBtn.x,addShapeBtn.y = 0,0
local function addSquareBtnRelease()
print("add square")
closeMenu()
end
local addSquareBtn = widget.newButton{
label="Add Square",
labelColor = { default={255}, over={128} },
default="button.png",
over="button-over.png",
width=180, height=60,
onRelease = addSquareBtnRelease -- event listener function
}
addSquareBtn.x,addSquareBtn.y = 0,0
local function addCircleBtnRelease()
print("add circle")
closeMenu()
end
local addCircleBtn = widget.newButton{
label="Add Circle",
labelColor = { default={255}, over={128} },
default="button.png",
over="button-over.png",
width=180, height=60,
onRelease = addCircleBtnRelease -- event listener function
}
addCircleBtn.x,addCircleBtn.y = 0,60
local function addWeaponBtnRelease()
print("add weapon")
end
local addWeaponBtn = widget.newButton{
label="Add Weapon",
labelColor = { default={255}, over={128} },
default="button.png",
over="button-over.png",
width=180, height=60,
onRelease = addWeaponBtnRelease -- event listener function
}
addWeaponBtn.x,addWeaponBtn.y = 0,60
local function onMenuBtnRelease()
if menuGroup.isVisible == true then
menuGroup.isVisible = false
if menuShapeGroup.isVisible == true then
menuShapeGroup.isVisible = false
end
else
menuGroup.isVisible = true
end
end
local menuBtn = widget.newButton{
label="Menu",
labelColor = { default={255}, over={128} },
default="button.png",
over="button-over.png",
width=180, height=60,
onRelease = onMenuBtnRelease -- event listener function
}
menuBtn.x,menuBtn.y = 0, 60
-- slider listener function
local function heightSliderListener( event )
if selectedObject ~= nil then
selectedObject.height = (event.value + 1) * 3
end
end
local heightSlider = widget.newSlider{
top = 700,
left = 350,
listener = heightSliderListener
}
local function widthSliderListener( event )
if selectedObject ~= nil then
selectedObject.width = (event.value + 1) * 3
end
end
local widthSlider = widget.newSlider{
top = 700,
left = 600,
listener = widthSliderListener
}
--Hide menu groups
menuGroup.isVisible = false
menuShapeGroup:insert(addCircleBtn)
menuShapeGroup:insert(addSquareBtn)
--Insert menu group items
menuGroup:insert(addShapeBtn)
menuGroup:insert(addWeaponBtn)
--Insert menu groups
interfaceGroup:insert(menuGroup)
interfaceGroup:insert(menuShapeGroup)
interfaceGroup:insert(menuWeaponGroup)
interfaceGroup:insert(heightText)
interfaceGroup:insert(widthText)
interfaceGroup:insert(widthSlider)
interfaceGroup:insert(heightSlider)
group:insert(shipGroup)
group:insert(interfaceGroup)
group:insert(itemsAddedText)
group:insert(myObject)
group:insert(newObjectText)
-- touch listener function
function myObject:touch( event )
selectedObject = myObject
if event.phase == "began" then
self.markX = self.x -- store x location of object
self.markY = self.y -- store y location of object
elseif event.phase == "moved" then
local x = (event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY
self.x, self.y = x, y -- move object based on calculations above
end
return true
end
-- make 'myObject' listen for touch events
myObject:addEventListener( "touch", myObject )
end
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
end
-- Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
end
-- If scene's view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group = self.view
package.loaded[physics] = nil
physics = nil
end
-----------------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
-----------------------------------------------------------------------------------------
-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )
-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )
-- "exitScene" event is dispatched whenever before next scene's transition begins
scene:addEventListener( "exitScene", scene )
-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )
-----------------------------------------------------------------------------------------
return scene