Created
November 9, 2011 23:35
-
-
Save juaxix/1353548 to your computer and use it in GitHub Desktop.
Mágica Gems by juaxix for Codea - freesource - iPad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Bar class | |
-- LGPL Juan Belón Pérez | |
-- videojuegos.ser3d.es | |
-- 2011 | |
Bar = class() | |
function Bar:init() | |
self.bgems = {} | |
self.time = 0 | |
for i=1,maxGemsRow do | |
local gem = Gem((i*79)+32, 666, curGemColor,true) | |
gem.state = GEM_BAR | |
table.insert(self.bgems,gem) | |
end | |
end | |
function Bar:draw() | |
for k=1,maxGemsCol do | |
if board.gems[maxGemsRow][k].state == GEM_LIVE then | |
self.bgems[k].state = GEM_WAIT | |
self.bgems[k].y = 666 | |
elseif self.bgems[k].state == GEM_WAIT then | |
self.bgems[k].state = GEM_FALL | |
elseif self.bgems[k].state == GEM_BAR then | |
local best = self:findNextTileDown(k) | |
self.bgems[k].state = GEM_REGEN | |
board.gems[best][k].state = GEM_LIVE | |
board.gems[best][k].time = 1 | |
board.gems[best][k].gcolor= self.bgems[k].gcolor | |
board.gems[best][k].color = self.bgems[k].color | |
board.gems[best][k].tint = self.bgems[k].tint | |
--board.gems[best][k].x = self.bgems[k].x | |
--board.gems[best][k].y = self.bgems[k].y | |
board.gems[best][k].model = self.bgems[k].model | |
self.bgems[k].time = 1.6 | |
elseif self.bgems[k].state == GEM_REGEN then | |
self.bgems[k].time = self.bgems[k].time - 0.3 | |
if self.bgems[k].time <= 0 then | |
-- regen | |
local xy = vec2(self.bgems[k].x,self.bgems[k].y) | |
--self.bgems[k]:init(g.x,g.y,curGemColor,true) | |
self.bgems[k] = Gem(xy.x,xy.y,curGemColor,true) | |
self.bgems[k].state = GEM_FALL | |
end | |
elseif self.bgems[k].state == GEM_FALL then | |
self.bgems[k].y = self.bgems[k].y - math.random(11,33) | |
-- best tile | |
local best = board.gems[self:findNextTileDown(k)][k] | |
local bg = self.bgems[k] | |
if bg.y < best.y then | |
-- end | |
self.bgems[k].y = best.y | |
self.bgems[k].yDown = best.y | |
self.bgems[k].state = GEM_BAR | |
end | |
end | |
end | |
for i,g in ipairs(self.bgems) do | |
g:draw() | |
end | |
end | |
function Bar:findNextTileDown(j) | |
local i = maxGemsRow | |
local b = maxGemsRow | |
local d = false | |
while i > 0 and not d do | |
if board.gems[i][j].state == GEM_DIE or board.gems[i][j].state == GEM_EXPLODE | |
then | |
if i<=b then | |
b = i | |
end | |
else -- cant go down,obstacle fall or live or born | |
d = true | |
--print("i:"..i..","..j) | |
-- print(board.gems[i][j].state) | |
end | |
i = i - 1 | |
end | |
return b | |
end | |
function Bar:touched(touch) | |
-- Codify does not automatically call this method | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Board class | |
-- LGPL Juan Belón Pérez | |
-- videojuegos.ser3d.es | |
-- 2011 | |
Board = class() | |
function Board:init() | |
self.gems = {} | |
self.texts = {} | |
self.sel1 = nil | |
self.sel2 = nil | |
self.gemsEx= {} | |
-- board init: | |
for k=1,maxGemsRow do | |
self.gems[k] = {} | |
for i=1,maxGemsCol do | |
local gem = Gem((i*79)+33, 55+(70*k), curGemColor,(k>3)) | |
--table.insert(self.gems, gem) | |
self.gems[k][i] = gem | |
end | |
end | |
end | |
-- O(n^2) | |
function Board:update() | |
-- set level | |
if gemsCleared > currentLvl*33 and gemsCleared<currentLvl*66 then | |
currentLvl = currentLvl + 1 | |
if currentLvl > 5 and currentLvl < 12 then | |
curGemColor = 5 | |
elseif currentLvl > 11 and currentLvl < 18 then | |
curGemColor = 6 | |
elseif currentLvl > 17 and currentLvl < 23 then | |
curGemColor = 7 | |
elseif currentLvl > 23 then | |
curGemColor = 8 | |
end | |
end | |
self:checkSolutions() | |
for i=1 , maxGemsRow do | |
for j=1, maxGemsCol do | |
if self.gems[i][j].state == GEM_EXPLODE then | |
self.gems[i][j].time = self.gems[i][j].time - 0.23 | |
-- print(self.gems[i][j].time) | |
if self.gems[i][j].time<=0 then | |
self.gems[i][j].state = GEM_DIE | |
end | |
elseif self.gems[i][j].state == GEM_LIVE | |
--or self.gems[i][j].state == GEM_BORN | |
then | |
if (i>1 and ( | |
self.gems[i-1][j].state == GEM_DIE or | |
self.gems[i-1][j].state == GEM_EXPLODE | |
)) | |
then | |
self.gems[i][j].state = GEM_FALL | |
self.gems[i][j].time = 13 | |
if not (self:nextGemDown(i,j,self.gems[i][j])) then | |
self.gems[i][j].state = GEM_LIVE | |
end | |
else | |
self.gems[i][j].state = GEM_LIVE | |
end | |
elseif self.gems[i][j].state == GEM_FALL then | |
self.gems[i][j].time = self.gems[i][j].time - 0.1 | |
self.gems[i][j].y = self.gems[i][j].y - 2.3 | |
if self.gems[i][j].y <= self.gems[i][j].yDown or self.gems[i][j].time <=0 then | |
self.gems[i][j].y = self.gems[i][j].yDown | |
self.gems[i][j].state = GEM_LIVE | |
end | |
end | |
end | |
end | |
--self:stopAnimations() | |
end | |
-- O(n) | |
function Board:nextGemDown(i,j,gem) | |
local k =i - 1 | |
local b =i -- best tile | |
while k > 0 do | |
if self.gems[k][j].state == GEM_DIE or self.gems[k][j].state == GEM_EXPLODE then | |
if k<b then | |
b = k | |
end | |
else -- cant go down,obstacle fall or live or born | |
break | |
end | |
k = k - 1 | |
end | |
if b<i then | |
local y = self.gems[b][j].y | |
self.gems[b][j]:mirror(gem) | |
gem.yDown = gem.y | |
gem.y = y | |
self.gems[i][j] = self.gems[b][j] | |
self.gems[b][j] = gem | |
return true | |
end | |
return false | |
end | |
-- dont use | |
function Board:stopAnimations() | |
for i=1,maxGemsRow do | |
for j=1, maxGemsCol do | |
if self.gems[i][j].state == GEM_EXPLODE then | |
self.gems[i][j].state = GEM_DIE | |
elseif self.gems[i][j].state == GEM_FALL then | |
self.gems[i][j].y = self.gems[i][j].yDown | |
self.gems[i][j].state = GEM_LIVE | |
end | |
end | |
end | |
end | |
function Board:draw() | |
self:update() | |
-- some cool random backgrounds here :) | |
-- box background | |
pushMatrix() | |
translate(341,233) | |
rotate(180) | |
sprite("Planet Cute:Wood Block",0,0,630,881) | |
popMatrix() | |
-- lines | |
fill(204, 134, 70, 255) | |
stroke(204, 134, 70, 255) | |
strokeWidth(32) | |
smooth() | |
lineCapMode(0) | |
-- right line | |
line(666,70,666,669) | |
-- bottom line | |
--line(55,77,669,77) | |
fill(204, 134, 70, 121) | |
stroke(204, 134, 70, 121) | |
-- bar line (is part of the board) | |
line(55,600,666,600) | |
-- left line | |
line(60,77,60,666) | |
-- top line | |
-- line(55,669,666,669) | |
-- floor | |
for i=0, 7 do | |
sprite("Planet Cute:Brown Block",(79*i)+77,44,101,87) | |
end | |
-- board gems | |
local i,j | |
for i=1, maxGemsRow do | |
for j=1,maxGemsCol do | |
(self.gems[i][j]):draw() | |
end | |
end | |
noSmooth() | |
stroke(255, 255, 255, 255) | |
strokeWidth(2) | |
for i,t in ipairs(self.texts) do | |
if t.time<=0 then | |
table.remove(self.texts,i) | |
else | |
t:draw() | |
end | |
end | |
end | |
function Board:isVequal(v1,v2) | |
if v1==nil and v2 == nil then | |
return true | |
elseif v1==nil or v2 == nil then | |
return false | |
end | |
return v1[0]==v2[0] and v2[1]==v1[1] | |
end | |
function Board:floodFillSelection(i,j,gcolor,count,start) | |
if not (start) then | |
if self.gems[i][j].exploredSel then -- done case | |
return 0 | |
end | |
if self.gems[i][j].gcolor==gcolor and not (self.gems[i][j].exploredSel) and | |
self.gems[i][j].state==GEM_LIVE then | |
-- single match base case | |
count = count + 1 | |
end | |
end | |
-- selection flag flood fill | |
self.gems[i][j].exploredSel = true | |
if self.gems[i][j].state==GEM_DIE then -- empty case | |
return 0 | |
end | |
-- adyacent cells | |
-- up | |
if i<maxGemsRow and not (self.gems[i+1][j].exploredSel) and | |
self.gems[i+1][j].gcolor==gcolor | |
then | |
count = count + self:floodFillSelection(i+1,j,gcolor,count,false) | |
end | |
-- left | |
if j>1 and not (self.gems[i][j-1].exploredSel) and self.gems[i][j-1].gcolor==gcolor then | |
count = count + self:floodFillSelection(i,j-1,gcolor,count,false) | |
end | |
-- right | |
if j<maxGemsCol and not (self.gems[i][j+1].exploredSel) and | |
self.gems[i][j+1].gcolor==gcolor | |
then | |
count = count + self:floodFillSelection(i,j+1,gcolor,count,false) | |
end | |
-- down | |
if i>1 and not (self.gems[i-1][j].exploredSel) and | |
self.gems[i-1][j].gcolor==gcolor | |
then | |
count = count + self:floodFillSelection(i-1,j,gcolor,count,false) | |
end | |
return count | |
end | |
--O(log(n)) | |
function Board:floodFill(i,j,gcolor) | |
if self.gems[i][j].explored then -- done case | |
return 0 | |
end | |
if self.gems[i][j].gcolor==gcolor and not (self.gems[i][j].explored) and | |
self.gems[i][j].state==GEM_LIVE then | |
-- single match base case | |
local index = {} | |
index[0] = i | |
index[1] = j | |
table.insert(self.gemsEx,index) | |
else | |
return | |
end | |
-- flag flood fill | |
self.gems[i][j].explored = true | |
if self.gems[i][j].state==GEM_DIE then -- empty case | |
return | |
end | |
-- adyacent cells | |
-- up | |
if i<maxGemsRow and not (self.gems[i+1][j].explored) and self.gems[i+1][j].gcolor==gcolor then | |
self:floodFill(i+1,j,gcolor) | |
end | |
-- left | |
if j>1 and not (self.gems[i][j-1].explored) and self.gems[i][j-1].gcolor==gcolor then | |
self:floodFill(i,j-1,gcolor) | |
end | |
-- right | |
if j<maxGemsCol and not (self.gems[i][j+1].explored) and self.gems[i][j+1].gcolor==gcolor then | |
self:floodFill(i,j+1,gcolor) | |
end | |
-- down | |
if i>1 and not (self.gems[i-1][j].explored) and self.gems[i-1][j].gcolor==gcolor then | |
self:floodFill(i-1,j,gcolor) | |
end | |
end | |
-- O(8*n^2*m)*O(log2(n)) -- worst case | |
function Board:checkSolutions() | |
local colors = {"blue","green","orange","red","pink","purple","yellow","violet"} | |
for k,c in ipairs(colors) do | |
if k > curGemColor then | |
return | |
end | |
for i=1 ,maxGemsRow do | |
for j=1, maxGemsCol do | |
if self.gems[i][j].state == GEM_LIVE and self.gems[i][j].gcolor==c then | |
self:floodFill(i,j,c) | |
local n = table.maxn(self.gemsEx) | |
if n>2 then -- explode gems!! | |
sound(SOUND_PICKUP) | |
local points = math.pow(2,n) | |
score = score + points | |
gemsCleared = gemsCleared + n | |
for t,gem in ipairs(self.gemsEx) do | |
self.gems[gem[0]][gem[1]].state = GEM_EXPLODE | |
self.gems[gem[0]][gem[1]].time = 1 | |
end | |
local a = math.floor(n/2) + 1 | |
table.insert(self.texts, FontAnim(points, | |
self.gems[self.gemsEx[a][0]][self.gemsEx[a][1]].x, | |
self.gems[self.gemsEx[a][0]][self.gemsEx[a][1]].y | |
)) | |
end | |
self.gemsEx = {} -- reset aux values for solution computations | |
self:clearExplorations() | |
end | |
end | |
end | |
end | |
end | |
-- O(n^2) | |
function Board:clearExplorations() | |
for i=1 ,maxGemsRow do | |
for j=1, maxGemsCol do | |
if self.gems[i][j].state == GEM_LIVE then | |
self.gems[i][j].explored =false | |
end | |
end | |
end | |
end | |
function Board:clearExploredSelection() | |
for i=1,maxGemsRow do | |
for j=1,maxGemsCol do | |
self.gems[i][j].exploredSel = false | |
end | |
end | |
end | |
function Board:checkBrothers(origin,destiny, io,jo,id,jd) | |
local r = {} | |
local c1 = origin.gcolor | |
local c2 = destiny.gcolor | |
self:clearExploredSelection() | |
destiny.exploredSel = true | |
r = self:floodFillSelection(io,jo,c2,0,true) | |
--print("c2:"..r) | |
if r>2 then return true end | |
self:clearExploredSelection() | |
origin.exploredSel = true | |
r = self:floodFillSelection(id,jd,c1,0,true) | |
--print("c1:"..r) | |
if r>2 then return true end | |
--self:checkBrothersSides(origin,destiny,io,jo,id,jd) | |
return false | |
end | |
function Board:checkBrothersSides(origin,destiny,io,jo,id,jd) | |
-- left | |
if jo<jd then | |
if jo>1 and self.gems[io][jo-1].gcolor == c2 then return true end | |
else | |
if jd>1 and self.gems[id][jd-1].gcolor == c1 then return true end | |
end | |
-- right | |
if jo<jd then | |
if jo<maxGemsCol and self.gems[io][jo+1].gcolor == c2 then return true end | |
else | |
if jd<maxGemsCol and self.gems[id][jd+1].gcolor == c1 then return true end | |
end | |
-- bottom | |
if io<id then | |
if io>1 and self.gems[io-1][jo].gcolor == c2 then return true end | |
else | |
if id>1 and self.gems[id-1][jd].gcolor == c1 then return true end | |
end | |
-- top | |
if io<id then | |
if io<maxGemsRow and self.gems[io+1][jo].gcolor == c2 then return true end | |
else | |
if id<maxGemsRow and self.gems[id+1][jd].gcolor == c1 then return true end | |
end | |
return false | |
end | |
-- O(1) | |
function Board:checkSelections(index) | |
if self:isVequal(index,self.sel1) | |
--or self:isVequal(index,self.sel2) | |
then | |
self.gems[self.sel1[0]][self.sel1[1]].selected = false | |
return nil | |
end | |
if self.sel1 == nil then | |
self.sel1 = index | |
elseif self.sel2 == nil then | |
self.sel2 = index | |
elseif not self:isVequal(index,self.sel1) then | |
if self.sel1 ~= nil then | |
self.gems[self.sel1[0]][self.sel1[1]].selected = false | |
end | |
self.sel1 = index | |
else | |
if self.sel2 ~= nil then | |
self.gems[self.sel2[0]][self.sel2[1]].selected = false | |
end | |
self.sel2 = index | |
end | |
if self:isVequal(self.sel1,self.sel2) then | |
self.sel2 = nil | |
end | |
if self.sel1 ~= nil and self.sel2 ~= nil then | |
local g1 = self.gems[self.sel1[0]][self.sel1[1]] | |
local g2 = self.gems[self.sel2[0]][self.sel2[1]] | |
local i1,i2,j1,j2 = self.sel1[0],self.sel2[0],self.sel1[1],self.sel2[1] | |
if math.abs(i1-i2)>1 or math.abs(j1-j2)>1 or | |
g1.state~=GEM_LIVE or g2.state~=GEM_LIVE or | |
(i1~=i2 and j1~=j2) | |
then | |
g1.selected = false | |
g2.selected = false | |
self.sel1 = nil | |
self.sel2 = nil | |
return | |
end | |
if not(self:checkBrothers(g1,g2,i1,j1,i2,j2)) then | |
g2.selected = false | |
self.sel2 = nil | |
return | |
end | |
-- do change | |
g1:mirror(g2) | |
self.gems[self.sel1[0]][self.sel1[1]] = g2 | |
self.gems[self.sel2[0]][self.sel2[1]] = g1 | |
self.sel1 = nil | |
self.sel2 = nil | |
sound(SOUND_SHOOT) | |
end | |
end | |
-- O(n2) | |
function Board:touched(touch) | |
-- if self.state ~= BOARD_PLAY then return end | |
local i,j | |
magic:touched(touch) | |
for i=1,maxGemsRow do | |
for j=1,maxGemsCol do | |
if self.gems[i][j]:touched(touch) then | |
self.gems[i][j].selected = true | |
-- magic.color = self.gems[i][j].color -- optional tuning | |
--print(i,j) | |
local k = {} | |
k[0] = i | |
k[1] = j | |
self:checkSelections(k) | |
return | |
else | |
--print(i,j,maxGemsCol) | |
end | |
end | |
end | |
end | |
FontAnim = class() | |
function FontAnim:init(text,x,y) | |
self.text = text | |
self.x = x | |
self.y = y | |
self.time = 3 | |
end | |
function FontAnim:draw() | |
self.time = self.time - 1/11 | |
if self.time > 0 then | |
number(self.x, self.y, self.text, 10) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Gem class | |
-- LGPL Juan Belón Pérez | |
-- videojuegos.ser3d.es | |
-- 2011 | |
Gem = class() | |
GEM_BAR = 0 | |
GEM_LIVE = 1 | |
GEM_DIE = 2 | |
GEM_EXPLODE= 3 | |
GEM_FALL = 4 | |
GEM_BORN = 5 | |
GEM_WAIT = 6 | |
GEM_REGEN = 7 | |
function Gem:init(x,y,seedGen,empty) | |
self.x = x | |
self.y = y | |
self.yo= 0 -- destiny y for falling animation | |
self.color = color(99,99,99,255) | |
self.tint = false | |
if empty then | |
self.state= GEM_DIE | |
else | |
self.state= GEM_LIVE | |
end | |
self.selected = false | |
local r = math.random(1, seedGen) | |
if r == 1 then | |
self.gcolor= "blue" | |
self.model = "Planet Cute:Gem Blue" | |
elseif r == 2 then | |
self.gcolor= "green" | |
self.model = "Planet Cute:Gem Green" | |
elseif r == 3 then | |
self.gcolor= "orange" | |
self.model = "Planet Cute:Gem Orange" | |
elseif r == 4 then | |
self.gcolor= "red" | |
self.model = "Planet Cute:Gem Orange" | |
self.color.r = 255 | |
self.tint = true | |
elseif r == 5 then | |
self.gcolor= "pink" | |
self.model = "Planet Cute:Gem Orange" | |
self.color.r = 255 | |
self.color.g = 0 | |
self.color.b = 255 | |
--tint(252, 0, 255, 255) | |
self.tint = true | |
elseif r == 6 then | |
self.gcolor= "purple" | |
self.model = "Planet Cute:Gem Blue" | |
self.color.r = 255 | |
self.tint = true | |
elseif r == 7 then | |
self.gcolor= "yellow" | |
self.model = "Planet Cute:Gem Orange" | |
self.color.r = 237 | |
self.color.g = 255 | |
self.color.b = 0 | |
self.tint = true | |
else | |
self.gcolor= "violet" | |
self.color.r = 255 | |
self.color.g = 0 | |
self.color.b = 255 | |
self.tint = true | |
self.model = "Planet Cute:Gem Green" | |
end | |
self.explored = false -- flood fill flag | |
self.time = 0 -- anims | |
self.exploredSel = false -- selection check flood fill flag | |
end | |
function Gem:mirror(gem) | |
local aux = self.x | |
self.x = gem.x | |
gem.x = aux | |
aux = self.y | |
self.y = gem.y | |
gem.y = aux | |
gem.selected = false | |
self.selected = false | |
end | |
function Gem:draw() | |
if self.state == GEM_DIE then return nil end | |
if self.state == GEM_EXPLODE then | |
sprite("Small World:Explosion", self.x,self.y,66,111) | |
elseif self.state == GEM_LIVE or self.state == GEM_FALL or self.state == GEM_WAIT | |
or self.state == GEM_REGEN | |
then | |
if self.state == GEM_REGEN then | |
sprite("Small World:Glow",self.x,self.y+33,66,333) | |
end | |
if self.selected then | |
sprite("Planet Cute:Selector", self.x,self.y,69,123) | |
end | |
if self.tint then | |
tint(self.color) | |
end | |
sprite(self.model, self.x,self.y,66,111) | |
noTint() | |
end | |
end | |
function Gem:touched(touch) | |
local xOk,yOk = false,false | |
local xL1 = 13 | |
local xL2 = 16 | |
local yL = 55 | |
--print(touch.x,touch.y,self.x,self.y) | |
if touch.x == self.x then | |
xOk = true | |
else | |
if touch.x < self.x then | |
xOk = (self.x - touch.x) < xL1 | |
else | |
xOk = (touch.x - self.x) < xL2 | |
end | |
end | |
if touch.y == self.y then | |
yOk = true | |
elseif self.y>touch.y then | |
yOk = ( self.y - touch.y ) < yL | |
end | |
-- if xOk and yOk then | |
-- print(self.gcolor..","..self.color.r..","..self.color.g..","..self.color.b) | |
-- print(self.tint) | |
-- end | |
return xOk and yOk | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Magic class | |
-- LGPL Juan Belón Pérez | |
-- videojuegos.ser3d.es | |
-- 2011 | |
Magic = class() | |
function Magic:init(ps) | |
self.ps = ps | |
self.p = {} | |
for i=0,ps do | |
self.p[i]= {x=math.random(WIDTH*10)/10, | |
y=math.random(HEIGHT*10)/10, ox=0.0, oy=0.0, vx=math.random(20)-10, | |
vy=math.random(20)-10} | |
end | |
self.color = color(223, 255, 0, 255) | |
end | |
function Magic:draw() | |
local p = self.p | |
noSmooth() | |
--background(10,10,20) | |
fill(255,0,0) | |
stroke(self.color) | |
strokeWidth(3) | |
for i=0,self.ps do | |
p[i].ox= p[i].x | |
p[i].oy= p[i].y | |
p[i].x = p[i].x + p[i].vx | |
p[i].y = p[i].y + p[i].vy | |
if p[i].x<0 then | |
p[i].x=0 | |
p[i].vx= -p[i].vx | |
end | |
if p[i].y<0 then | |
p[i].y=0 | |
p[i].vy= -p[i].vy | |
end | |
if p[i].x>WIDTH then | |
p[i].x=WIDTH | |
p[i].vx= -p[i].vx | |
end | |
if p[i].y>HEIGHT then | |
p[i].y=HEIGHT | |
p[i].vy= -p[i].vy | |
end | |
p[i].vx = p[i].vx*0.98 | |
p[i].vy = p[i].vy*0.98 | |
line(p[i].ox, p[i].oy, p[i].x, p[i].y) | |
end | |
end | |
function Magic:touched(t) | |
local a = 5 | |
local p = self.p | |
for i=0,self.ps do | |
local d= (p[i].x-t.x)*(p[i].x-t.x) + (p[i].y-t.y)*(p[i].y-t.y) | |
d= math.sqrt(d) | |
p[i].vx = p[i].vx - a/d*(p[i].x-t.x) | |
p[i].vy = p[i].vy - a/d*(p[i].y-t.y) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Magic Gems by @juaxix | |
-- LGPL Juan Belón Pérez | |
-- http://videojuegos.ser3d.es | |
-- 11/2011 | |
-- | |
function setup() | |
maxGemsRow = 7 | |
maxGemsCol = 7 | |
currentLvl = 1 | |
score = 0 | |
curGemColor= 4 | |
maxGemColor= 8 | |
gemsCleared= 0 | |
board = Board() | |
bar = Bar() | |
magic = Magic(66) | |
casting = false | |
end | |
function draw() | |
background(47, 145, 216, 255) | |
board:draw() | |
bar:draw() | |
-- level and score numbers | |
noSmooth() | |
stroke(255, 255, 255, 255) | |
strokeWidth(2) | |
sprite("Small World:Treasure",16, HEIGHT - 16) | |
number(42, HEIGHT - 6, score, 10) | |
sprite("Small World:Sign",WIDTH-33,HEIGHT - 29,60) | |
number(WIDTH - 47, HEIGHT - 10,"L"..currentLvl, 8) | |
if casting then | |
magic:draw() | |
end | |
end | |
function touched(touch) | |
if touch.state == ENDED then | |
if touch.x<99 or touch.x>696 then | |
casting = false | |
return nil | |
end | |
if touch.y>610 then | |
bar:touched(touch) | |
casting = false | |
elseif touch.x>75 and touch.y>53 and touch.x<696 and touch.y<555 then | |
casting = true | |
board:touched(touch) | |
end | |
end | |
end | |
----------------------------------- | |
-- Functions for drawing numbers -- | |
----------------------------------- | |
-- Draw a number. x, y is top left | |
function number(x, y, n, w) | |
l = string.len(n) | |
for i = 1, l do | |
drawDigit(x + ((i - 1) * (w * 1.5)), y, string.sub(n, i, i), w) | |
end | |
end | |
-- Draw a single digit | |
function drawDigit(x, y, n, w) | |
h = 2 * w | |
if string.match(n, "1") then | |
line(x + (w / 2), y, x + (w / 2), y - h) | |
elseif string.match(n, "2") then | |
line(x, y, x + w, y) | |
line(x + w, y, x + w, y - (h / 2)) | |
line(x + w, y - (h / 2), x, y - (h / 2)) | |
line(x, y - (h / 2), x, y - h) | |
line(x, y - h, x + w, y - h) | |
elseif string.match(n, "3") then | |
line(x, y, x + w, y) | |
line(x + w, y, x + w, y - h) | |
line(x + w, y - h, x, y - h) | |
line(x, y - (h / 2), x + w, y - (h / 2)) | |
elseif string.match(n, "4") then | |
line(x, y, x, y - (h / 2)) | |
line(x, y - (h / 2), x + w, y - (h / 2)) | |
line(x + w, y, x + w, y - h) | |
elseif string.match(n, "5") then | |
line(x + w, y, x, y) | |
line(x, y, x, y - (h / 2)) | |
line(x, y - (h / 2), x + w, y - (h / 2)) | |
line(x + w, y - (h / 2), x + w, y - h) | |
line(x + w, y - h, x, y - h) | |
elseif string.match(n, "6") then | |
line(x + w, y, x, y) | |
line(x, y, x, y - h) | |
line(x, y - h, x + w, y - h) | |
line(x + w, y - h, x + w, y - (h / 2)) | |
line(x + w, y - (h / 2), x, y - (h / 2)) | |
elseif string.match(n, "7") then | |
line(x, y, x + w, y) | |
line(x + w, y, x + w, y - h) | |
elseif string.match(n, "8") then | |
line(x, y, x + w, y) | |
line(x + w, y, x + w, y - h) | |
line(x + w, y - h, x, y - h) | |
line(x, y - h, x, y) | |
line(x, y - (h / 2), x + w, y - (h / 2)) | |
elseif string.match(n, "9") then | |
line(x + w, y - (h / 2), x, y - (h / 2)) | |
line(x, y - (h / 2), x, y) | |
line(x, y, x + w, y) | |
line(x + w, y, x + w, y - h) | |
line(x + w, y - h, x, y - h) | |
elseif string.match(n, "0") then | |
line(x, y, x + w, y) | |
line(x + w, y, x + w, y - h) | |
line(x + w, y - h, x, y - h) | |
line(x, y - h, x, y) | |
elseif string.match(n, "x") then | |
line(x, y - (w / 3), x + w, y - (h + 1)) | |
line(x + w, y - (w / 3), x, y - (h + 1)) | |
elseif string.match(n,"L") then | |
line(x - (w/6), y, x - (w/6), y - h) | |
line(x + w, y - h, x, y - h) | |
elseif string.match(n, "-") then | |
line(x, y - (h/2), x+w, y -(h/2)) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment