-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevelsMenu.lua
198 lines (167 loc) · 6.86 KB
/
levelsMenu.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
local composer = require( "composer" )
local scene = composer.newScene()
local tracker = require("_externalModules.googleAnalitcs")
local widget = require( "widget" )
-- Require "global" data table (https://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/)
-- This will contain relevant data like the current level, max levels, number of stars earned, etc.
local myData = require( "mydata" )
local userLevels = require( "_controls.levels-score-manager" )
-- Declare vertices for vector stars (an image is probably preferable for an actual game).
local starVertices = { 0,-8,1.763,-2.427,7.608,-2.472,2.853,0.927,4.702,6.472,0.0,3.0,-4.702,6.472,-2.853,0.927,-7.608,-2.472,-1.763,-2.427 }
-- Button handler to cancel the level selection and return to the menu
local function handleCancelButtonEvent( event )
if ( "ended" == event.phase ) then
composer.gotoScene( "menu", { effect="crossFade", time=333 } )
end
end
-- Button handler to go to the selected level
local function handleLevelSelect( event )
if ( "ended" == event.phase ) then
-- 'event.target' is the button and '.id' is a number indicating which level to go to.
-- The 'game' scene will use this setting to determine which level to load.
-- This could be done via passed parameters as well.
userLevels.currentLevel = event.target.id
-- Purge the game scene so we have a fresh start
composer.removeScene( "levelsMenu", false )
-- Go to the game scene
composer.gotoScene( "level1", { effect="crossFade", time=333 } )
end
end
-- Declare the Composer event handlers
-- On scene create...
function scene:create( event )
local sceneGroup = self.view
tracker.EnterScene("Levels selector");
-- userLevels.loadLevels()
print(myData.settings.unlockedLevels)
-- Create background
local background = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
background:setFillColor( 1 )
background.x = display.contentCenterX
background.y = display.contentCenterY
sceneGroup:insert( background )
-- Use a scrollView to contain the level buttons (for support of more than one full screen).
-- Since this will only scroll vertically, lock horizontal scrolling.
local levelSelectGroup = widget.newScrollView({
-- width = 460,
-- height = 260,
width = 600,
height = 400,
scrollWidth = 460,
scrollHeight = 800,
horizontalScrollDisabled = true
})
-- 'xOffset', 'yOffset' and 'cellCount' are used to position the buttons in the grid.
local xOffset = 64
local yOffset = 24
local cellCount = 1
-- Define the array to hold the buttons
local buttons = {}
-- Read 'maxLevels' from the 'myData' table. Loop over them and generating one button for each.
for i = 1, myData.maxLevels do
-- Create a button
buttons[i] = widget.newButton({
label = tostring( i ),
id = tostring( i ),
onEvent = handleLevelSelect,
emboss = false,
shape="roundedRect",
width = 48,
height = 32,
font = native.systemFontBold,
fontSize = 18,
labelColor = { default = { 1, 1, 1 }, over = { 0.5, 0.5, 0.5 } },
cornerRadius = 8,
labelYOffset = -6,
fillColor = { default={ 0, 0.5, 1, 1 }, over={ 0.5, 0.75, 1, 1 } },
strokeColor = { default={ 0, 0, 1, 1 }, over={ 0.333, 0.667, 1, 1 } },
strokeWidth = 2
})
-- Position the button in the grid and add it to the scrollView
buttons[i].x = xOffset
buttons[i].y = yOffset
levelSelectGroup:insert( buttons[i] )
-- Check to see if the player has achieved (completed) this level.
-- The '.unlockedLevels' value tracks the maximum unlocked level.
-- First, however, check to make sure that this value has been set.
-- If not set (new user), this value should be 1.
-- If the level is locked, disable the button and fade it out.
print(myData.settings.unlockedLevels)
if ( myData.settings.unlockedLevels == nil ) then
myData.settings.unlockedLevels = 1
end
if ( i <= myData.settings.unlockedLevels ) then
buttons[i]:setEnabled( true )
buttons[i].alpha = 1.0
else
buttons[i]:setEnabled( false )
buttons[i].alpha = 0.5
end
-- Generate stars earned for each level, but only if:
-- a. The 'levels' table exists
-- b. There is a 'stars' value inside of the 'levels' table
-- c. The number of stars is greater than 0 (no need to draw zero stars).
local star = {}
print(myData.settings.levels)
if ( myData.settings.levels[i] and myData.settings.levels[i].stars and myData.settings.levels[i].stars > 0 ) then
for j = 1, myData.settings.levels[i].stars do
star[j] = display.newPolygon( 0, 0, starVertices )
star[j]:setFillColor( 1, 0.9, 0 )
star[j].strokeWidth = 1
star[j]:setStrokeColor( 1, 0.8, 0 )
star[j].x = buttons[i].x + (j * 16) - 32
star[j].y = buttons[i].y + 8
levelSelectGroup:insert( star[j] )
end
end
-- Compute the position of the next button.
-- This tutorial draws 5 buttons across.
-- It also spaces based on the button width and height + initial offset from the left.
xOffset = xOffset + 75
cellCount = cellCount + 1
if ( cellCount > 5 ) then
cellCount = 1
xOffset = 64
yOffset = yOffset + 45
end
end
-- Place the scrollView into the scene and center it.
sceneGroup:insert( levelSelectGroup )
levelSelectGroup.x = display.contentCenterX
levelSelectGroup.y = display.contentCenterY
-- Create a cancel button for return to the menu scene.
local doneButton = widget.newButton({
id = "button1",
label = "Cancel",
onEvent = handleCancelButtonEvent
})
doneButton.x = display.contentCenterX
doneButton.y = display.contentHeight - 20
sceneGroup:insert( doneButton )
end
-- On scene show...
function scene:show( event )
local sceneGroup = self.view
if ( event.phase == "did" ) then
end
end
-- On scene hide...
function scene:hide( event )
local sceneGroup = self.view
if ( event.phase == "will" ) then
end
end
-- On scene destroy...
function scene:destroy( event )
local sceneGroup = self.view
sceneGroup:removeSelf()
for i = 1, sceneGroup.numChildren do
sceneGroup[1]:removeSelf()
end
end
-- Composer scene listeners
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
return scene