Skip to content

Instantly share code, notes, and snippets.

@albinotonnina
Last active August 29, 2015 13:58
Show Gist options
  • Save albinotonnina/10419955 to your computer and use it in GitHub Desktop.
Save albinotonnina/10419955 to your computer and use it in GitHub Desktop.
module(..., package.seeall)
local GameRand = require "GameRand"
-- create a GameRand object
gr1=GameRand()
-- seed with "test"
gr1:seed('test')
local physics = require("physics")
physics.start()
physics.setGravity( 0, 70 )
local ballBody = { density=0.8, friction=1.0, bounce=.7, radius=15 }
local movieclip = require("movieclip")
-- Audio
local swordSound = audio.loadSound("sounds/sword.mp3")
local moveSound = audio.loadSound("sounds/move.mp3")
local fail1Sound = audio.loadSound("sounds/fail1.mp3")
--local puckHitWall = audio.loadSound("puckHitWall.wav")
--local buzzer = audio.loadSound("buzzer.wav")
--local winner = audio.loadSound("winner.wav")
--local goalScore = audio.loadSound("goalScore.wav")
local ballsInGame
local balls = {}
local buche = {}
local currentScheme
local xballs
local yballs
local w
local h
local emptySpots
function in_table ( e, t )
for _,v in pairs(t) do
if (v==e) then return true end
end
return false
end
function countBallsInGame (balls)
local count = 0
for key,value in pairs(balls) do
if(value.isVisible == true)then
count = count +1
end
end
return count
end
function checkCanPlay (buche)
local countPlayable = 0
local buchePiene = 0
local numBuche = 0
for key,value in pairs(buche) do
numBuche = numBuche + 1
if(balls[key].isVisible == true)then
buchePiene = buchePiene +1
if(canPlay(value.posX,value.posY))then
countPlayable= countPlayable+1
end
end
-- if(value.canPlay == true)then
-- countPlayable= countPlayable+1
-- end
end
if(countPlayable<1)then
return false
else
return true
end
end
function canPlay(posX,posY)
n = (yballs*(posX-1)) + posY
sup = (yballs*(posX-1)) + posY-2
dwn = (yballs*(posX-1)) + posY+2
rgt = (yballs*(posX-1+2)) + posY
lft = (yballs*(posX-1-2)) + posY
supDel = (yballs*(posX-1)) + posY-1
dwnDel = (yballs*(posX-1)) + posY+1
rgtDel = (yballs*(posX-1+1)) + posY
lftDel = (yballs*(posX-1-1)) + posY
if(buche[sup]~=nil and buche[sup].empty == 1 and buche[supDel] ~= nil and buche[sup].x == buche[n].x and buche[supDel].empty == 0) then
return true
end
if(buche[dwn]~=nil and buche[dwn].empty == 1 and buche[dwnDel] ~= nil and buche[dwn].x == buche[n].x and buche[dwnDel].empty == 0) then
return true
end
if(buche[rgt]~=nil and buche[rgt].empty == 1 and buche[rgtDel] ~= nil and buche[rgtDel].empty == 0) then
return true
end
if(buche[lft]~=nil and buche[lft].empty == 1 and buche[lftDel] ~= nil and buche[lftDel].empty == 0) then
return true
end
return false
end
function nuovaGriglia(params)
params = params[1]
currentScheme = params.currentScheme or 'usa'
currentDifficulty = params.difficulty or 0
levelName = params.name or 'level'
xballs = 7
yballs =7
w = params.width or 36
h = params.height or 36
emptySpots = params.emptySpots or {24}
local fillCol = params.fillCol or {128, 0, 0}
local borderCol = params.borderCol or {96, 96, 96}
local borderW = params.borderW or 1
local onCol = {0, 255, 0}
local grid = display.newGroup()
local ballGroup = display.newGroup()
local debugNumbers = display.newGroup()
local container = display.newGroup()
grid:insert(debugNumbers)
container:insert(grid)
container:insert(ballGroup)
play.setMessage(levelName)
debugNumbers.isVisible = false
local bar = {}
local actionStarted = false
local startPosition = {0,0}
local startX = startPosition[1]
local startY = startPosition[2]
local n
local sup
local dwn
local rgt
local lft
local supDel
local dwnDel
local rgtDel
local lftDel
local katana
local imageTable = {}
for i = 1,7 do
table.insert( imageTable, "images/blood" .. i .. ".png" )
end
local bloodyPatch = movieclip.newAnim( imageTable )
bloodyPatch.isVisible= false
--grid:insert(bloodyPatch)
local function katanaspazz()
katana = display.newImageRect("images/katana.png",251,470)
katana.y = 120
transition.to( katana, { time=200, alpha=0, x=display.contentWidth+500, y=200 } )
end
local function spazza(xball,yball)
--gr1:randInt(1,10)
-- timer.performWithDelay(100, katanaspazz() )
katanaspazz()
for i=1,ballGroup.numChildren do
local child = ballGroup[i]
transition.to( child, { time=200, alpha=0, x=500, y=10*i } )
end
print(xball,yball)
bloodyPatch.x = xball
bloodyPatch.y = yball
local function spruzz()
bloodyPatch.isVisible= true
bloodyPatch:play{ startFrame=3, endFrame=7, loop=1, remove=true }
audio.play(swordSound)
audio.play(fail1Sound)
end
spruzz()
--print(ballGroup.numChildren)
end
----------------|||||||||||||||||-------------------
local function playBall(x, y)
return function(event)
local t = event.target
local phase = event.phase
startPosition = {x,y}
startX = startPosition[1]
startY = startPosition[2]
if event.phase == "began" then
n = (yballs*(startX-1)) + startY
sup = (yballs*(startX-1)) + startY-2
dwn = (yballs*(startX-1)) + startY+2
rgt = (yballs*(startX-1+2)) + startY
lft = (yballs*(startX-1-2)) + startY
supDel = (yballs*(startX-1)) + startY-1
dwnDel = (yballs*(startX-1)) + startY+1
rgtDel = (yballs*(startX-1+1)) + startY
lftDel = (yballs*(startX-1-1)) + startY
if(buche[sup]~=nil and buche[sup].empty == 1 and buche[supDel] ~= nil and buche[sup].x == buche[n].x and buche[supDel].empty == 0) then
--buche[sup]:setStrokeColor(onCol[1], onCol[2], onCol[3])
end
if(buche[dwn]~=nil and buche[dwn].empty == 1 and buche[dwnDel] ~= nil and buche[dwn].x == buche[n].x and buche[dwnDel].empty == 0) then
--buche[dwn]:setStrokeColor(onCol[1], onCol[2], onCol[3])
end
if(buche[rgt]~=nil and buche[rgt].empty == 1 and buche[rgtDel] ~= nil and buche[rgtDel].empty == 0) then
--buche[rgt]:setStrokeColor(onCol[1], onCol[2], onCol[3])
end
if(buche[lft]~=nil and buche[lft].empty == 1 and buche[lftDel] ~= nil and buche[lftDel].empty == 0) then
--buche[lft]:setStrokeColor(onCol[1], onCol[2], onCol[3])
end
display.getCurrentStage():setFocus(t)
t.isFocus = true
t.active = true
-- Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
t.x00 = t.x
t.y00 = t.y
-- Stop current motion, if any
--event.target:setLinearVelocity( 0, 0 )
--event.target.angularVelocity = 0
--t.width = t.width+15
--t.height = t.width+15
elseif t.isFocus then
balls[n].chosen = false
if "moved" == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif "ended" == phase or "cancelled" == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
balls[n].chosen = false
--if(buche[sup]~=nil)then buche[sup]:setStrokeColor(fillCol[1], fillCol[2], fillCol[3]) end
--if(buche[dwn]~=nil)then buche[dwn]:setStrokeColor(fillCol[1], fillCol[2], fillCol[3]) end
--if(buche[rgt]~=nil)then buche[rgt]:setStrokeColor(fillCol[1], fillCol[2], fillCol[3]) end
--if(buche[lft]~=nil)then buche[lft]:setStrokeColor(fillCol[1], fillCol[2], fillCol[3]) end
if(buche[sup]~=nil and buche[sup].empty == 1 and buche[sup].x == buche[n].x and buche[supDel]~= nil and buche[supDel].empty == 0 and balls[n].chosen == false) then
local diffSup = math.abs(buche[sup].x-t.x) + math.abs(buche[sup].y-t.y)
buche[n].canPlay = true
if(diffSup<=26)then
audio.play(moveSound)
play.addMove()
balls[supDel].isVisible = false
balls[sup].isVisible = true
balls[n].isVisible = false
balls[n].chosen = true
buche[supDel].empty = 1
buche[sup].empty = 0
buche[n].empty = 1
end
end
if(buche[dwn]~=nil and buche[dwn].empty == 1 and buche[dwn].x == buche[n].x and buche[dwnDel]~= nil and buche[dwnDel].empty == 0 and balls[n].chosen == false) then
local diffDwn = math.abs(buche[dwn].x-t.x) + math.abs(buche[dwn].y-t.y)
buche[n].canPlay = true
if(diffDwn<=26)then
audio.play(moveSound)
play.addMove()
balls[dwnDel].isVisible = false
balls[dwn].isVisible = true
balls[n].isVisible = false
balls[n].chosen = true
buche[dwnDel].empty = 1
buche[dwn].empty = 0
buche[n].empty = 1
end
end
if(buche[rgt]~=nil and buche[rgt].empty == 1 and buche[rgtDel]~= nil and buche[rgtDel].empty == 0 and balls[n].chosen == false) then
local diffRgt = math.abs(buche[rgt].x-t.x) + math.abs(buche[rgt].y-t.y)
buche[n].canPlay = true
if(diffRgt<=26)then
audio.play(moveSound)
play.addMove()
balls[rgtDel].isVisible = false
balls[rgt].isVisible = true
balls[n].isVisible = false
balls[n].chosen = true
buche[rgtDel].empty = 1
buche[rgt].empty = 0
buche[n].empty = 1
end
end
if(buche[lft]~=nil and buche[lft].empty == 1 and buche[lftDel]~= nil and buche[lftDel].empty == 0 and balls[n].chosen == false) then
local diffLft = math.abs(buche[lft].x-t.x) + math.abs(buche[lft].y-t.y)
buche[n].canPlay = true
if(diffLft<=26)then
audio.play(moveSound)
play.addMove()
balls[lftDel].isVisible = false
balls[lft].isVisible = true
balls[n].isVisible = false
balls[n].chosen = true
buche[lftDel].empty = 1
buche[lft].empty = 0
buche[n].empty = 1
end
end
t.x = buche[n].x
t.y = buche[n].y
ballsInGame = countBallsInGame(balls)
if(ballsInGame == 1) then
play.stopTimer()
--ballGroup.isVisible = false
--grid:removeSelf()
--grid = nil
play.setMessage('YOU WIN!!')
play.addPoints()
elseif(checkCanPlay(buche) == false) then
play.setMessage('YOU LOSE...')
play.stopTimer()
spazza(event.x,event.y)
-- ballGroup.isVisible = false
-- grid:removeSelf()
--grid = nil
end
balls[n].chosen = false;
end
end
end
end
-------------------|||||||||||||||||||-----------------------
local function getThump(event)
if event.isShake == true then
-- local alert = native.showAlert( "Thump!", "Location: 0,0\r\nMessage: Cadono i pezzi", {"OK"} )
system.vibrate()
for i=1,grid.numChildren do
local child = grid[i]
--local description = (child.isVisible and "mm") or "not visible"
--print( "child["..i.."] is " .. description )
local fall = function()
child.bodyType = "dynamic"
end
timer.performWithDelay(i*20, fall )
end
end
end
Runtime:addEventListener("accelerometer", getThump)
--Sets up pockets
function inPocket( self, event )
local fallDown = transition.to( event.other, { alpha=0, xScale=0.3, yScale=0.3, time=200 } )
local object = event.other
event.other:setLinearVelocity( 0, 0 )
print('in buca')
end
--[[
if(scheme == 'lvl1') then
currentScheme = {9,16,23,24,25,32,38,39,40}
emptySpots = {23,25,39,40}
elseif(scheme == 'usa') then
currentScheme = {3,4,5,10,11,12,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,38,39,40,45,46,47}
emptySpots = {25}
elseif(scheme == 'europa') then
currentScheme = {2,3,4,8,9,10,11,12,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,36,37,38,39,40,44,45,46}
emptySpots = {24}
end
--]]
function showNumbers()
if(debugNumbers.isVisible)then
debugNumbers.isVisible = false
else
debugNumbers.isVisible = true
end
end
local num = 0
local n = 1
for i = 1, xballs do
local slot = {}
for j = 1, yballs do
local ballContainer = display.newGroup()
local ballImg
local debugNumber = display.newText( "", (i-1) * w, (j-1) * h, "Helvetica", 14 )
debugNumber:setTextColor(255, 255, 255)
debugNumber.text = n
debugNumbers:insert(debugNumber)
if (in_table(n,currentScheme) ) then
ballImg = display.newImageRect("images/ball.png",w,h)
ballContainer:insert(ballImg)
balls[n] = ballContainer
balls[n]:setReferencePoint(display.CenterReferencePoint);
--balls[n].isVisible = false
ballGroup:insert(balls[n])
physics.addBody(balls[n], 'static',ballBody)
ballContainer.x = (i-1) * w
ballContainer.y = (j-1) * h
balls[n].linearDamping = 0.3 -- simulates friction of felt
balls[n].angularDamping = 2 -- stops balls from spinning forever
--balls[n].isBullet = true -- If true physics body always awake
balls[n].active = false
balls[n].isVisible = true
--balls[n].bullet = false -- force continuous collision detection, to stop really fast shots from passing through other balls
--balls[n].id = "spriteBall"
balls[n]:addEventListener("touch", playBall(i, j))
--balls[n].postCollision = ballCollisionAudio
balls[n]:addEventListener( "postCollision", balls[n] )
if (in_table(n,emptySpots) ) then balls[n].isVisible = false end
--local sensorRadius = 20
buche[n] = display.newImageRect("images/bgBall.png",w,h)
if (in_table(n,emptySpots) ) then
buche[n].empty = 1
else
buche[n].empty = 0
end
--buche[n]:setStrokeColor(borderCol[1], borderCol[2], borderCol[3])
--buche[n].strokeWidth = borderW
buche[n]:setReferencePoint(display.CenterReferencePoint);
--physics.addBody( buche[n], "kinematic",{ radius=sensorRadius, isSensor=true } )
--buche[n].collision = inPocket
--buche[n]:addEventListener( "collision", buche[n] ) -- add table listener to each pocket sensor
grid:insert(buche[n])
buche[n].canPlay = false
buche[n].x = (i-1) * w
buche[n].y = (j-1) * h
buche[n].posX = i
buche[n].posY = j
buche[n].name = num
physics.addBody(buche[n], "static", { friction=0.5, bounce=0.3 })
buche[n].isPlatform = true
end
slot[#slot+1] = 0
num = num+1
n = n+1
end
bar[#bar+1] = slot
end
grid.bar = bar
return container
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment