NeutronLoved/objects/grid.lua

93 lines
2.4 KiB
Lua

local gridgen = {}
function gridgen:new(game,x,y,cellsize)
local Grid = game.objects.base:new(game,x,y,cellsize*5,cellsize*5)
Grid.cellsize = cellsize
Grid.sprites = {game:newImage("grid/tile.png"),game:newImage("grid/pink.png"),game:newImage("grid/blue.png")}
Grid.pinkturn = game:newImage("text/turn/pink.png")
Grid.blueturn = game:newImage("text/turn/blue.png")
Grid:register("Grid")
Grid.depth = -1
Grid.turn= 2
-- Spawn circles
local i
for i=1,5 do
local xspawn = x+(i-1)*cellsize
local pink = game.objects.circle:new(game,xspawn,y,"pink",Grid)
local blue = game.objects.circle:new(game,xspawn,y+4*cellsize,"blue",Grid)
game:summon(pink)
game:summon(blue)
end
local neutron = game.objects.circle:new(game,x+2*cellsize,y+2*cellsize,"neutron",Grid)
game:summon(neutron)
function Grid:step(dt)
-- Check if the neutron is on one side or the other
local obj = self.game:findName("neutron")
local i
local winner = "None"
for i=1,#obj do
if obj[i].rect.y==self.rect.y then winner = "pink" end
if obj[i].rect.y+obj[i].rect.h==self.rect.y+self.rect.h then winner = "blue" end
end
if winner~="None" then
self:endgame(winner)
end
end
function Grid:addturn()
self.turn = self.turn + 1
self.turn = self.turn%4
-- Check if a move can be made
local color = "blue"
if self.turn%2==1 then
if self.turn==1 then color = "pink" end
else
color = "neutron"
end
local count = 0
local i
local obj = self.game:findName(color)
for i=1,#obj do
count = count + #obj[i]:findAvailableMoves()
end
if count==0 then
local win = "blue"
if self.turn < 2 then win = "pink" end
self:endgame(win)
end
end
function Grid:endgame(winner)
print(winner.." won !")
self.game.scenes:main(self.game)
end
function Grid:draw()
local y
local x
for y=1,5 do
for x=1,5 do
local spriteindex = 1
if y==1 then spriteindex = 2 end
if y==5 then spriteindex = 3 end
lg.draw(self.sprites[spriteindex],self.rect[1]-self.game.camerax-self.spriteoffx+(x-1)*self.cellsize,self.rect[2]-self.game.cameray-self.spriteoffy+(y-1)*self.cellsize)
end
end
if self.turn<2 then
-- Draw "pink's turn"
lg.draw(self.pinkturn,(self.game.WIDTH-self.pinkturn:getWidth())/2,0)
else
-- Draw "Blue's turn"
lg.draw(self.blueturn,(self.game.WIDTH-self.blueturn:getWidth())/2,self.game.HEIGHT-self.blueturn:getHeight())
end
end
return Grid
end
return gridgen