-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tetra.lua
141 lines (112 loc) · 3.65 KB
/
Tetra.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
-- MyNet.lua
local Dim = require 'Dim'
local Grid = require 'Grid'
local GameState = require 'GameState'
local physics = require 'physics'
physics.start()
physics.setGravity(0, 0) -- 9.8
print(physics.engineVersion)
local composer = require('composer')
local scene = composer.newScene()
local widget = require('widget')
widget.setTheme('widget_theme_android_holo_dark')
local asteroidsTable = {}
-- local scrollView = nil
local gridGroup = nil
local backGroup = nil
local astroLoopTimer = nil
local grid = nil
local function createAsteroid2(x, y, color)
local newAsteroid = display.newCircle(backGroup, x, y, math.random(6))
table.insert(asteroidsTable, newAsteroid)
physics.addBody(newAsteroid, 'dynamic', { density=0.3, radius=10, bounce=0.9 } )
newAsteroid:setLinearVelocity( math.random( -25,25 ), math.random( -25,25 ) )
newAsteroid:setFillColor(unpack(color))
end
local function astroLoop()
grid:iterator(function(c)
if c.bitCount == 1 then
createAsteroid2(c.center.x, c.center.y, c.color)
end
end)
-- Remove asteroids which have drifted off screen
for i = #asteroidsTable, 1, -1 do
local thisAsteroid = asteroidsTable[i]
if ( thisAsteroid.x < 0 or
thisAsteroid.x > display.contentWidth or
thisAsteroid.y < 0 or
thisAsteroid.y > display.contentHeight )
then
display.remove( thisAsteroid )
table.remove( asteroidsTable, i )
end
end
end
function scene:create(event)
local sceneGroup = self.view
gridGroup = display.newGroup()
sceneGroup:insert(gridGroup)
backGroup = display.newGroup()
sceneGroup:insert(backGroup)
--[[
scrollView = widget.newScrollView({
top = 0,
left = 0,
width = display.actualContentWidth,
height = display.actualContentHeight,
-- isBounceEnabled = false,
backgroundColor = {0,0,0},
})
-- scrollView.x = 0
-- scrollView.y = 0
-- scrollView.anchorX = 0
-- scrollView.anchorY = 0
sceneGroup:insert(scrollView)
]]
if system.getInfo('platform') == 'win32' then
_G.dimensions = Dim:new(100)
else
_G.dimensions = Dim:new(200)
end
-- for debugging the gaps between cells problem
-- display.setDefault('background', 0.5,0.5,0.5)
grid = Grid:new(gridGroup, _G.dimensions.numX, _G.dimensions.numY)
grid.gameState = GameState:new()
grid:newLevel()
end
function scene:show(event)
local sceneGroup = self.view
local phase = event.phase
if phase == 'will' then
-- Code here runs when the scene is still off screen (but is about to come on screen)
astroLoop()
elseif phase == 'did' then
-- Code here runs when the scene is entirely on screen
astroLoopTimer = timer.performWithDelay(10000, astroLoop, 0)
end
end
function scene:hide(event)
local sceneGroup = self.view
local phase = event.phase
if phase == 'will' then
-- Code here runs when the scene is on screen (but is about to go off screen)
if astroLoopTimer then timer.cancel(astroLoopTimer) end
elseif phase == 'did' then
-- Code here runs immediately after the scene goes entirely off screen
composer.removeScene('Tetra')
end
end
function scene:destroy(event)
local sceneGroup = self.view
grid:destroy()
-- Code here runs prior to the removal of scene's view
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener('create', scene)
scene:addEventListener('show', scene)
scene:addEventListener('hide', scene)
scene:addEventListener('destroy', scene)
-- -----------------------------------------------------------------------------------
return scene