Sei sulla pagina 1di 20

-- Notes:

-- To do:
-- 1) tile matching
-- 2) tile animation
-- 3) friction for different floor types
-- 4) Need to move shadows and other elements into their own layer groups
-- 5) New level Buttons
-- 6) Cancel button
-- 7) Assign each tile a type based on the number.
-To allow tiles of different numbers to have the same physical ef
fects
-- 8) Adjust friction to prevent marble from getting stuck in space
------

Name Ideas
Marble Quest
Marble Madness
Moxyd
Hydroxyd

-- ----------------------------------------------------------------- Hide the Status bar


-- ---------------------------------------------------------------display.setStatusBar( display.HiddenStatusBar )
-- ----------------------------------------------------------------

-- ----------------------------------------------------------------- Load the physics and sprite module


-- ---------------------------------------------------------------local physics = require("physics")
-- define a variable to hold the physics
system
-- ----------------------------------------------------------------- Set up physics
-- ----------------------------------------------------------------- physics.setDrawMode( "hybrid" )
-- Set the draw mode for testing and deb
ugging
physics.start()
-- Must start physics be
fore calling any of it's methods!

physics.setGravity(0,0)
n view so vertical and

-- Set gravity. I want a top dow


-- horiz

ontal gravity are both 0.


-- ---------------------------------------------------------------require "sprite"
-- Define variables used by program
local tile_size = 32
local tiles_in_sheet = 80
local tile_sheet = sprite.newSpriteSheet( "images/marble_tiles.png", tile_size,
tile_size )
local tile_set = sprite.newSpriteSet( tile_sheet, 1, tiles_in_sheet )
local tile_cols = 10
local tile_rows = 12
local tile_array = {}
-- Make groups to hold layers of sprites
local tile_group = display.newGroup()
local floor_group = display.newGroup()
local shadow_group = display.newGroup()
local solid_group = display.newGroup()
tile_group:insert( floor_group )
tile_group:insert( shadow_group )
tile_group:insert( solid_group )
-- These arrays define the
local level_array = {}
local floor_array = {} -local shadow_array = {} -local solid_array = {} --

tiles used in each of the layer groups


Used for the floor layer
Used for shadows
Used for solid objects like walls and things

local matches_array = {}
local matched_tiles = 0
local level = 1
local start_x
local start_y
local
local
local
local

floor_friction = 0.99
floor_friction_space = 0.0
floor_friction_ice = 0.11
floor_friction_normal = 0.99

local
local
local
local

control_force = 1.0
control_force_normal = 1.0
control_force_space = 0
control_force_ice = 0.22

local colors_array = {"color_a", "color_b", "color_c", "color_d", "color_e", "co


lor_f" }
local panel_group

local last_open_tile = nil


local ball
local ball_shadow
-- ----------------------------------------------------------------

-- ----------------------------------------------------------------- Make tile sets


-- ----------------------------------------------------------------- A helper function makes an array with frame numbers for all tiles
local function get_all_frames()
local t = {}
for i = 1, tiles_in_sheet, 1 do
t[i] = i
end
return t
end
local tile_set = sprite.newSpriteMultiSet(
{
{ sheet = tile_sheet, frames
{ sheet = tile_sheet, frames
open
1 to 6
{ sheet = tile_sheet, frames
close 7 to 12
{ sheet = tile_sheet, frames
reen
13 to 18
{ sheet = tile_sheet, frames
19 to 24
{ sheet = tile_sheet, frames
yan
25 to 30
{ sheet = tile_sheet, frames
-31 to 36
{ sheet = tile_sheet, frames
-- red
37 to 42
{ sheet = tile_sheet, frames
43 to 48
{ sheet = tile_sheet, frames
-- orange
49 to 54
{ sheet = tile_sheet, frames
55 to 60
{ sheet = tile_sheet, frames

= get_all_frames() },
= { 4, 6, 7, 8, 9, 10 } }, -- blue
= { 10, 9, 8, 7, 6, 4 } }, -- blue
= { 4, 11, 12, 12, 14, 15 } }, -- g
= { 15, 14, 13, 12, 11, 4 } }, -= { 4, 16, 17, 18, 19, 20 } }, -- c
= { 20, 19, 18, 17, 16, 4 } },
= { 4, 21, 22, 23, 24, 25 } },
= { 25, 24, 23, 22, 21, 4 } }, -= { 4, 26, 27, 28, 29, 30 } },
= { 30, 29, 28, 27, 26, 4 } }, -= { 4, 31, 32, 33, 34, 35 } },

-- yellow

61 to 66
{ sheet = tile_sheet, frames = { 35, 34, 33, 32, 31, 4 } }, --

67 to 72
2 } }

{ sheet = tile_sheet, frames = { 2, 5, 36, 37, 38, 39, 40, 41, 4


-- 73 to 81
}

)
-- Animated
sprite.add(
eyes
sprite.add(
sprite.add(
sprite.add(
sprite.add(
sprite.add(

sequences defined at the end set


tile_set, "color_a", 1 + tiles_in_sheet, 6, 200, 1 )

sprite.add(
eyes
sprite.add(
sprite.add(
sprite.add(
sprite.add(
sprite.add(

tile_set, "color_a_close", 7 + tiles_in_sheet, 6, 200, 1 ) -- Color

tile_set,
tile_set,
tile_set,
tile_set,
tile_set,

tile_set,
tile_set,
tile_set,
tile_set,
tile_set,

"color_b",
"color_c",
"color_d",
"color_e",
"color_f",

13
25
37
49
61

+
+
+
+
+

"color_b_close",
"color_c_close",
"color_d_close",
"color_e_close",
"color_f_close",

tiles_in_sheet,
tiles_in_sheet,
tiles_in_sheet,
tiles_in_sheet,
tiles_in_sheet,

19
31
43
55
67

+
+
+
+
+

6,
6,
6,
6,
6,

200,
200,
200,
200,
200,

tiles_in_sheet,
tiles_in_sheet,
tiles_in_sheet,
tiles_in_sheet,
tiles_in_sheet,

6,
6,
6,
6,
6,

1
1
1
1
1

)
)
)
)
)

200,
200,
200,
200,
200,

sprite.add( tile_set, "all", 1, tiles_in_sheet, 1000, 1 )


-- ----------------------------------------------------------------

--------

---------------------------------------------------------------Define Game Levels


Three arrays define tile maps describing three layers:
solid
shadow
floor
----------------------------------------------------------------

-- --------------- level 1

-- Color

1
1
1
1
1

)
)
)
)
)

-- -------------floor_array[1] = { 2, 2, 4, 2, 2, 2, 2, 4, 2, 2,
40,37,37,37,37,37,37,37,37,37,
38,36,36,36,36,36,36,36,36,36,
38,36,36,36,36,36,36,36,36,36,
38,36,36,36,36,36,36,36,36,36,
38,36,36,36,36,36,36,36,36,36,
38,36,36,36,36,36,36,36,36,36,
38,36,36,36,36,36,36,36,36,36,
38,36,36,36,36,36,36,36,36,36,
38,36,36,36,36,36,36,36,36,36,
38,36,36,36,36,36,36,36,36,36,
2,2,4,2,2,2,2,4,2,2}
shadow_array[1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67,67,67,67,67,67,67,67,67,67,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0}
solid_array[1] = {2,2,4,2,2,2,2,4,2,2,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
2,2,4,2,2,2,2,4,2,2}

-- --------------- level 2
-- -------------floor_array[2] = {2,2,4,2,2,2,2,4,2,2,
40,37,37,37,37,37,37,37,37,37,
38,36,36,36,36,36,36,36,36,36,
38,36,36,2,41,36,2,41,36,36,
38,36,36,39,42,36,39,42,36,36,
38,36,36,36,36,36,36,36,36,36,
38,36,36,36,36,36,36,36,36,36,
38,36,36,36,36,36,36,36,36,36,
38,36,36,2,41,36,2,41,36,36,
38,36,36,39,42,36,39,42,36,36,
38,36,36,36,36,36,36,36,36,36,
2,2,4,2,2,2,2,4,2,2}
shadow_array[2] = {0,0,0,0,0,0,0,0,0,0,
67,67,67,67,67,67,67,67,67,67,

0,0,0,0,0,0,0,0,0,0,
0,0,0,0,69,0,0,69,0,0,
0,0,0,66,70,0,66,70,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,69,0,0,69,0,0,
0,0,0,66,70,0,66,70,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0}
solid_array[2] = {2,2,4,2,2,2,2,4,2,2,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,2,0,0,2,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,2,0,0,2,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
2,2,4,2,2,2,2,4,2,2}

-- --------------- level 3
-- -------------floor_array[3] = {2,4,2,4,2,2,4,2,4,2,
44,44,44,44,44,44,44,44,44,44,
45,2,45,43,43,45,44,43,2,43,
45,46,45,43,45,43,43,43,46,44,
45,43,43,2,43,43,2,46,43,43,
45,43,43,46,44,43,43,43,44,43,
45,45,43,43,44,43,43,43,43,43,
44,43,43,2,44,43,2,45,43,43,
45,43,43,46,43,43,46,45,43,43,
45,2,43,43,44,43,43,43,2,43,
45,46,45,43,43,43,43,43,46,46,
2,4,2,4,2,2,4,2,4,2}
shadow_array[3] = {0,0,0,0,0,0,0,0,0,0,
67,67,67,67,67,67,67,67,67,67,
0, 0,69, 0, 0, 0, 0, 0, 0,69,
0,66,70, 0, 0, 0, 0, 0,66,70,
0, 0, 0, 0,69, 0, 0,69, 0, 0,
0, 0, 0,66,70, 0,66,70, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0,69, 0, 0,69, 0, 0,
0, 0, 0,66,70, 0,66,70, 0, 0,
0, 0,69, 0, 0, 0, 0, 0, 0,69,
0,66,70, 0, 0, 0, 0, 0,66,70,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
solid_array[3] = {2,4,2,4,2,2,4,2,4,2,
0,0,0,0,0,0,0,0,0,0,
0,2,0,0,0,0,0,0,2,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,2,0,0,2,0,0,0,

0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,2,0,0,2,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,2,0,0,0,0,0,0,2,0,
0,0,0,0,0,0,0,0,0,0,
2,4,2,4,2,2,4,2,4,2}

-- --------------- level 4
-- -------------floor_array[4] = {2,2,2,2,2,2,2,2,2,2,
40,37,37,37,37,37,37,37,37,37,
38,52,4,41,52,36,52,4,41,52,
38,52,39,42,52,36,36,39,42,52,
38,36,52,36,52,36,52,36,36,36,
38,36,52,52,36,36,36,36,52,52,
38,52,36,36,36,36,36,36,36,52,
38,36,52,52,36,52,52,52,36,36,
38,52,52,36,36,52,52,52,52,36,
38,52, 4,41,36,52,52, 4,41,36,
38,52,39,42,52,52,52,39,42,52,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
shadow_array[4] = {0,0,0,0,0,0,0,0,0,0,
67,67,67,67,67,67,67,67,67,67,
0, 0, 0,69, 0, 0, 0, 0,69, 0,
0, 0,66,70, 0, 0, 0,66,70, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,69, 0, 0, 0, 0,69, 0,
0, 0,66,70, 0, 0, 0,66,70, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
solid_array[4] = {2,2,2,2,2,2,2,2,2,2,
0,0,0,0,0,0,0,0,0,0,
0,0,4,0,0,0,0,4,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,4,0,0,0,0,4,0,0,
0,0,0,0,0,0,0,0,0,0,
2,2,2,2,2,2,2,2,2,2}

-- --------------- level 5
-- --------------

floor_array[5] = {4,2,2,2,2,2,2,2,2,2,
37,2,40,37,37,37,2,40,37,37,
36,2,38,2,41,36,2,38,2,41,
36,2,38,2,38,36,2,38,2,38,
36,2,38,2,38,36,2,38,2,38,
36,2,38,2,38,36,2,38,2,38,
36,2,38,2,38,36,2,38,2,38,
36,2,38,2,38,36,2,38,2,38,
36,2,38,2,38,36,2,38,2,38,
36,2,38,2,38,36,2,38,2,38,
36,39,42,2,38,36,39,42,2,38,
2,2,2,2,2,2,2,2,2,4}
shadow_array[5] = {0,0,0,0,0,0,0,0,0,0,
67,0,71,67,67,67,0,71,67,67,
0,0,68,0,69,0,0,68,0,69,
0,0,68,0,68,0,0,68,0,68,
0,0,68,0,68,0,0,68,0,68,
0,0,68,0,68,0,0,68,0,68,
0,0,68,0,68,0,0,68,0,68,
0,0,68,0,68,0,0,68,0,68,
0,0,68,0,68,0,0,68,0,68,
0,0,68,0,68,0,0,68,0,68,
0,66,70,0,68,0,66,70,0,68,
0,0,0,0,0,0,0,0,0,0
}
solid_array[5] = {4,2,2,2,2,2,2,2,2,2,
0,2,0,0,0,0,2,0,0,0,
0,2,0,2,0,0,2,0,2,0,
0,2,0,2,0,0,2,0,2,0,
0,2,0,2,0,0,2,0,2,0,
0,2,0,2,0,0,2,0,2,0,
0,2,0,2,0,0,2,0,2,0,
0,2,0,2,0,0,2,0,2,0,
0,2,0,2,0,0,2,0,2,0,
0,2,0,2,0,0,2,0,2,0,
0,0,0,2,0,0,0,0,2,0,
2,2,2,2,2,2,2,2,2,4}

-- --------------- level 6
-- -------------floor_array[6] = {2,2,2,2,2,2,2,2,2,2,
63,63,63,63,37,37,63,63,63,63,
63,4,63,63,36,36,63,63,4,63,
63,63,63,63,36,36,63,63,63,63,
63,63,63,63,36,36,63,63,63,63,
63,63,63,63,36,36,63,63,63,63,
63,63,63,63,36,36,63,63,63,63,
63,63,63,63,36,36,63,63,63,63,
63,63,63,63,36,36,63,63,63,63,
63,4,63,63,36,36,63,63,4,63,
63,63,63,63,36,36,63,63,63,63,
2,2,2,2,2,2,2,2,2,2}
shadow_array[6] = {0,0,0,0,0,0,0,0,0,0,

0,0,0,0,67,67,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0}
solid_array[6] = {2,2,2,2,2,2,2,2,2,2,
0,0,0,0,0,0,0,0,0,0,
0,4,0,0,0,0,0,0,4,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,4,0,0,0,0,0,0,4,0,
0,0,0,0,0,0,0,0,0,0,
2,2,2,2,2,2,2,2,2,2}

-- --------------- level 7
-- -------------floor_array[7] = {2,2,2,2,2,2,2,2,2,2,
64,64,64,64,64,64,64,64,64,64,
64,5,64,64,64,64,64,64,5,64,
64,64,64,64,64,64,64,64,64,64,
64,64,64,64,64,64,64,64,64,64,
64,64,64,64,64,64,64,64,64,64,
64,64,64,64,64,64,64,64,64,64,
64,64,64,64,64,64,64,64,64,64,
64,64,64,64,64,64,64,64,64,64,
64,5,64,64,64,64,64,64,5,64,
64,64,64,64,64,64,64,64,64,64,
2,2,2,2,2,2,2,2,2,2}
shadow_array[7] = {0,0,0,0,0,0,0,0,0,0,
67,67,67,67,67,67,67,67,67,67,
0,0,69,0,0,0,0,0,0,69,
0,66,70,0,0,0,0,0,66,70,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,69,0,0,0,0,0,0,69,
0,66,70,0,0,0,0,0,66,70,
0,0,0,0,0,0,0,0,0,0}
solid_array[7] = {2,2,2,2,2,2,2,2,2,2,
0,0,0,0,0,0,0,0,0,0,
0,4,0,0,0,0,0,0,4,0,
0,0,0,0,0,0,0,0,0,0,

0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,4,0,0,0,0,0,0,4,0,
0,0,0,0,0,0,0,0,0,0,
2,2,2,2,2,2,2,2,2,2}

-- --------------- level 8
-- -------------floor_array[8] = {2,2,2,2,2,2,2,2,2,2,
37,37,37,37,65,65,37,38,37,37,
36,62,41,38,65,65,36,36,62,41,
36,39,42,36,65,65,36,37,39,42,
36,36,37,36,65,65,36,36,36,36,
65,65,36,36,36,36,36,36,65,65,
65,65,36,37,36,36,37,36,65,65,
36,36,36,36,65,65,36,36,36,36,
36,38,36,36,65,65,36,38,36,36,
36,62,41,36,65,65,36,36,62,38,
36,39,42,36,65,65,36,36,39,42,
2,2,2,2,2,2,2,2,2,2}
shadow_array[8] = {0,0,0,0,0,0,0,0,0,0,
67,67,67,67,0,0,67,67,67,67,
0,0,69,0,0,0,0,0,0,69,
0,66,70,0,0,0,0,0,66,70,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,69,0,0,0,0,0,0,69,
0,66,70,0,0,0,0,0,66,70,
0,0,0,0,0,0,0,0,0,0}
solid_array[8] = {2,2,2,2,2,2,2,2,2,2,
0,0,0,0,0,0,0,0,0,0,
0,4,0,0,0,0,0,0,4,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,4,0,0,0,0,0,0,4,0,
0,0,0,0,0,0,0,0,0,0,
2,2,2,2,2,2,2,2,2,2}

-- Find the number of matches for each level


for i = 1, #solid_array, 1 do

local c = 0
for j = 1, #solid_array[i], 1 do
if solid_array[i][j] == 4 then
c = c + 1
end
end
matches_array[i] = c
-- print( "level:", i, "matches:", c )
end

-- ----------------------------------------------------------------

-- ---------------------------------------------------------------- create a random array of matching tile color sets


-- --------------------------------------------------------------local function get_random_colors( number_of_colors )
local random_colors = {}
for i = 1, number_of_colors / 2, 1 do
random_colors[ #random_colors + 1 ] = colors_array[i]
random_colors[ #random_colors + 1 ] = colors_array[i]
end
-- Randomize the colors array
local n = #random_colors
for i = n, 2, -1 do
local r = math.random(i)
random_colors[i], random_colors[r] = random_colors[r], random_co
lors[i]
end
return random_colors
end
-- ---------------------------------------------------------------

-- -------------------------------------------------------------- Clear last level


-- ------------------------------------------------------------local function clear_level()
for i = 1, #tile_array, 1 do
local tile = tile_array[i].solid
physics.removeBody( tile )
end
end
-- -------------------------------------------------------------

-- -------------------------------------------------------------- Load a Level


-- ------------------------------------------------------------local function load_level( n )
level = n
matched_tiles = matches_array[level]
clear_level()
local c_array = get_random_colors( matches_array[n] )
local c_index = 1
for i = 1, #tile_array, 1
local floor_tile
local shadow_tile
local solid_tile

do
= tile_array[i].floor
= tile_array[i].shadow
= tile_array[i].solid

local floor_id = floor_array[n][i]


local shadow_id = shadow_array[n][i]
local solid_id = solid_array[n][i]
floor_tile.tile_id = floor_id
shadow_tile.tile_id = shadow_id
solid_tile.tile_id = solid_id
floor_tile.currentFrame = floor_id
shadow_tile.currentFrame = shadow_id
solid_tile.currentFrame = solid_id

if shadow_id == 0 then
shadow_tile.isVisible = false
else
shadow_tile.isVisible = true
end
if solid_id == 0 then
solid_tile.isVisible = false
else
solid_tile.isVisible = true
end
floor_group.isVisible = true
shadow_group.isVisible = true
solid_group.isVisible = true
if solid_id == 2 then
physics.addBody( solid_tile, "static", {density=1.0, fri
ction=0.0 } )
solid_tile:prepare( "all" )
solid_tile.currentFrame = solid_id
elseif solid_id == 4 then
physics.addBody( solid_tile, "static", {density=1.0, fri
ction=0.0 } )
solid_tile.color = c_array[c_index]
c_index = c_index + 1
solid_tile:prepare( solid_tile.color )
solid_tile.isLocked = false
solid_tile.isOpen = false
end
end
ball.x = tile_size * tile_cols / 2
ball.y = tile_size * tile_rows / 2
last_open_tile = nil
end
-- -------------------------------------------------------------

-- -------------------------------------------------------------- Make Walls


-- ------------------------------------------------------------local function make_walls()
local screen_width = tile_cols * tile_size
local screen_height = tile_rows * tile_size
-- Create a solid wall along the floor
local floor = display.newRect(0, 0, screen_width, 30 ) -- Define a rect
floor.x = screen_width / 2
-- Position the rectangle
floor.y = screen_height + 15
floor:setFillColor( 0, 0, 0 )
-- Set the color to red
physics.addBody( floor, "static", {density=1.0, friction=0.0 } ) -- Crea
te a physics body
tile_group:insert( floor )
floor.myName = "wall"
-- Create a ceiling
local ceiling = display.newRect(0, 0, screen_width, 30)
ceiling.x = screen_width / 2
ceiling.y = -15
ceiling:setFillColor( 0, 0, 0 )
physics.addBody( ceiling, "static", {density=1.0, friction=0.0 } )
tile_group:insert( ceiling )
ceiling.myName = "wall"
-- Creat the left wall
local left = display.newRect(0, 0, 30, screen_height )
left.x = -15
left.y = screen_height / 2
left:setFillColor( 0, 0, 0 )
physics.addBody( left, "static", {density=1.0, friction=0.0 } )
tile_group:insert( left )
left.myName = "wall"
-- Create a right wall
local right = display.newRect(0, 0, 30, screen_height )
right.x = screen_width + 15
right.y = screen_height / 2
right:setFillColor( 0, 0, 0 )
physics.addBody( right, "static", {density=1.0, friction=0.0 } )
tile_group:insert( right )
right.myName = "wall"
-- Create the ball
ball = display.newImage("images/ball.png")
ball.x = 160
ball.y = 240
physics.addBody( ball, "dynamic",{ density = 1.0, friction = 0.0, bounce
= 0.65, radius=10 } )
ball.isFixedRotation = true
tile_group:insert( ball )
ball.myName = "ball"

ball.linearDamping = 0.5
-- Create Ball Shadow
ball_shadow = display.newImage( "images/ball_shadow.png" )
shadow_group:insert( ball_shadow )
end
-- -------------------------------------------------------------

-- ------------------------------------------------------------- Handle swipes on the screen and move the ball


-- -----------------------------------------------------------local function on_touch( event )
local phase = event.phase
if "began" == phase then
-- Store initial position
start_x = event.x
start_y = event.y
elseif "moved" == phase then
local force_x = ( event.x - start_x ) * control_force
local force_y = ( event.y - start_y ) * control_force
start_x = event.x
start_y = event.y
ball:applyForce( force_x, force_y, ball.x, ball.y )
elseif "ended" == phase or "cancelled" == phase then
end
-- Important to return true. This tells the system that the event
-- should not be propagated to listeners of any objects underneath.
return true
end
Runtime:addEventListener( "touch", on_touch )
-- -------------------------------------------------------------

-- -------------------------------------------------------------- Make level panel


-- ------------------------------------------------------------local function open_panel()
transition.to( panel_group, {time=500, x=0} )
end
local function close_panel()
transition.to( panel_group, {time=500, x=display.contentWidth} )
end
local function tap_level( event )
load_level( event.target.level )
close_panel()
end
local function make_button( n )
local button_start_frame = 4
local button_group = display.newGroup()
local button_label = display.newText( n, 0, 0, "04b03", 24 )
local button_sprite = sprite.newSprite( tile_set )
button_sprite.currentFrame = button_start_frame
button_group:insert( button_sprite )
button_group:insert( button_label )
button_label.x = 2
button_label.y = -2
return button_group
end
local function make_panel()
local buttons_per_row = 6
local button_margin = 18
panel_group = display.newGroup()
local panel_back = display.newRect( 0, 0, display.contentWidth, display.
contentHeight )
panel_group:insert( panel_back )
panel_back:setFillColor( 0, 0, 0, 128 )
-- Make a button for each level
for i = 1, #floor_array, 1 do
local button = make_button( i )
panel_group:insert( button )

button.x = tile_size + ( ( ( i - 1 ) % buttons_per_row ) * ( til


e_size + button_margin ) )
button.y = tile_size + ( math.floor( ( i - 1 ) / buttons_per_row
) * ( tile_size + button_margin ) )
button.level = i
button:addEventListener( "tap", tap_level )
end
end
-- -------------------------------------------------------------

-- ----------------------------------------------------------------- Make tiles *************************


-- ----------------------------------------------------------------- tile properties
-- 1) id - unique index for each tile
local function make_tiles()
-- **** Todo: make floor tiles
-- add all to floor_group
for i = 1, tile_rows, 1 do
for j = 1, tile_cols, 1 do
local tile_x = ( ( j - 1 ) * tile_size ) + ( tile_size /
2 )
local tile_y = ( ( i - 1 ) * tile_size ) + ( tile_size /
2 )
local floor_tile = sprite.newSprite( tile_set )
local shadow_tile = sprite.newSprite( tile_set )
local solid_tile = sprite.newSprite( tile_set )
floor_tile:prepare( "all" )
shadow_tile:prepare( "all" )
solid_tile:prepare( "all" )
floor_group:insert( floor_tile )
shadow_group:insert( shadow_tile )
solid_group:insert( solid_tile )
floor_tile.currentFrame = 4
floor_tile.tile_type = 4
shadow_tile.isVisible = false
solid_tile.isVisible = false
local tile_obj = {}
tile_obj.floor = floor_tile
tile_obj.shadow = shadow_tile
tile_obj.solid = solid_tile

tile_array[#tile_array+1] = tile_obj
floor_tile.x = tile_x
floor_tile.y = tile_y
shadow_tile.x = tile_x
shadow_tile.y = tile_y
solid_tile.x = tile_x
solid_tile.y = tile_y
end
end
end
-- ---------------------------------------------------------------

-- -------------------------------------------------------------- Initialize the game


-- ------------------------------------------------------------local function init()
make_tiles()
make_walls()
make_panel()
end
init()
-- ------------------------------------------------------------local show_panel = display.newRect(30, 430, 30, 30)
show_panel:addEventListener( "tap", open_panel )

-- --------------------------------------------------------------- Handle opening an closing color tiles and finding matches


-- -------------------------------------------------------------local function check_game_complete()
-- print( tile_array, #tile_array )
for i = 1, #tile_array, 1 do
if tile_array[i].tile_id == 4 and tile_array[i].isLocked == fals
e then
return false
end
end
return true
end

local function close( tile )


tile.isOpen = false
tile:prepare( tile.color .. "_close" )
tile:play()
end

-- --------------------------------------------------------------------------- Check for matching tiles


-- -------------------------------------------------------------------------local function check_tile_match( tile )
if last_open_tile ~= nil then
if last_open_tile.color == tile.color then
-- print( "match", matched_tiles )
last_open_tile.isLocked = true
tile.isLocked = true
matched_tiles = matched_tiles - 2
if matched_tiles == 0 then
open_panel()
end
else
-- print( "no match" )
close( last_open_tile )
close( tile )
end
last_open_tile = nil
else
last_open_tile = tile
end
end
-- ---------------------------------------------------------------------------- Open
-- --------------------------------------------------------------------------local function open( tile )
tile.isOpen = true
tile:prepare( tile.color )
tile:play()
check_tile_match( tile )
end
-- ---------------------------------------------------------------------------

-- ---------------------------------------------------------------------------- physics collisions


-- --------------------------------------------------------------------------local function onPostCollision( event )
-- print( "began: " .. event.object1.myName .. " & " .. event.object2.my
Name )
local tile = event.object1 -- Get the tile Object
local tile_id = tile.tile_id
-- Check this is an eye, is not locked and is not open
if tile_id == 4 and tile.isLocked == false and tile.isOpen == false then

open( tile ) -- Open this eye


end
-- print( "Force:" .. event.force )
end
Runtime:addEventListener( "postCollision", onPostCollision )
-- ----------------------------------------------------------------------------

-- ----------------------------------------------------------------------------- Check the type of floor tile we're over


-- ---------------------------------------------------------------------------function check_floor_tile()
local tile_x = math.floor( ball.x / tile_size )
local tile_y = math.floor( ball.y / tile_size )
local tile_index = ( tile_y * 10 ) + tile_x + 1
local tile_id = floor_array[level][tile_index]
-- print( tile_x, tile_y, tile_index, tile_id, level )
if tile_id == 65 then -- Void tile
-- You are over a void tile need to:
-- stop physics
-- stop marble
-- scale marble down
elseif tile_id == 63 then -- Space Tile
-- No mouse input
control_force = control_force_space
-- no friction
ball.linearDamping = floor_friction_space
elseif tile_id == 64 then -- Ice Tile
-- Change in friction and mouse input
control_force = control_force_ice
ball.linearDamping = floor_friction_ice
elseif tile_id == 36 then -- Regular floor tile
-control_force = control_force_normal
-- *** still have friction on collision -- remove friction from
objects
ball.linearDamping = floor_friction_normal
end
end
function position_ball_shadow()
ball_shadow.x = ball.x
ball_shadow.y = ball.y
end
function on_frame( event )
check_floor_tile()
position_ball_shadow()
end
Runtime:addEventListener( "enterFrame", on_frame )
-- ----------------------------------------------------------------------------

Potrebbero piacerti anche