diff --git a/assets/text/turn/blue.png b/assets/text/turn/blue.png new file mode 100644 index 0000000..7bdd636 Binary files /dev/null and b/assets/text/turn/blue.png differ diff --git a/assets/text/turn/pink.png b/assets/text/turn/pink.png new file mode 100644 index 0000000..2f6e181 Binary files /dev/null and b/assets/text/turn/pink.png differ diff --git a/game.lua b/game.lua index 62e3df1..94edad8 100644 --- a/game.lua +++ b/game.lua @@ -5,6 +5,7 @@ local insert = table.insert function Game:reinit() self.gameloop = {} self.objects = {} + self.scenes = require "scenes" self.objects.base = require "objects/base" self.objects.grid = require "objects/grid" self.objects.circle = require "objects/circle" diff --git a/main.lua b/main.lua index 5864f25..86578d1 100644 --- a/main.lua +++ b/main.lua @@ -10,7 +10,7 @@ if game.OS == "Android" or game.OS == "iOS" then game.OSTYPE = "Mobile" end lg = love.graphics -if game.OSTYPE=="PC" then push = require "lib/push" end +if game.OS~="Horizon" then push = require "lib/push" end function love.load() lg.setBackgroundColor(31/255,14/255,28/255) @@ -29,11 +29,8 @@ function love.load() push:setupScreen(game.WIDTH, game.HEIGHT, w, h, {fullscreen = true,resizable = res, pixelperfect = true}) push:setBorderColor(0.161,0.157, 0.192,1) end - local cellsize = 44 - local x = (game.WIDTH-cellsize*5)/2 - local y = (game.HEIGHT-cellsize*5)/2 - local grid = game.objects.grid:new(game,x,y,cellsize) - game:summon(grid) + + game.scenes:main(game) end function love.draw(screen) diff --git a/objects/circle.lua b/objects/circle.lua index 5e01c92..4112b8f 100644 --- a/objects/circle.lua +++ b/objects/circle.lua @@ -12,7 +12,18 @@ function gen:new(game,x,y,color,grid) Circle:register(color) Circle.selected = false - function Circle:step(dt) end + function Circle:step(dt) + if self.color=="neutron" and self:isMyTurn()then + if not self.selected then + self.moves = self:findAvailableMoves() + self.selected = true + end + end + end + + function Circle:isMyTurn() + return self.color=="pink" and self.grid.turn==1 or self.color=="blue" and self.grid.turn==3 or self.color=="neutron" and self.grid.turn%2==0 + end function Circle:draw() local spriteindex = 1 @@ -27,7 +38,7 @@ function gen:new(game,x,y,color,grid) end function Circle:MouseCallback(x,y,presses) - if self.rect:collidepoint(x,y) then + if self.rect:collidepoint(x,y) and self:isMyTurn() then local c = self.game:findName("Circle") local i for i=1,#c do @@ -47,6 +58,7 @@ function gen:new(game,x,y,color,grid) if rect:collidepoint(x,y) then self.rect:move_ip(self.moves[i][1],self.moves[i][2]) self.selected = false + self.grid:addturn() break end end @@ -77,8 +89,8 @@ function gen:new(game,x,y,color,grid) for movx=-1,1 do for movy=-1,1 do if not (movx==0 and movy==0) then - offx = movx*self.cellsize - offy = movy*self.cellsize + offx = 0 + offy = 0 while ValidMove(offx+movx*self.cellsize,offy+movy*self.cellsize) do offx = offx + movx*self.cellsize offy = offy + movy*self.cellsize diff --git a/objects/grid.lua b/objects/grid.lua index 5c4afc8..71dfca3 100644 --- a/objects/grid.lua +++ b/objects/grid.lua @@ -5,8 +5,11 @@ 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 @@ -20,8 +23,47 @@ function gridgen:new(game,x,y,cellsize) local neutron = game.objects.circle:new(game,x+2*cellsize,y+2*cellsize,"neutron",Grid) game:summon(neutron) - function Grid:step(dt) end + 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 @@ -34,6 +76,14 @@ function gridgen:new(game,x,y,cellsize) 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 diff --git a/scenes.lua b/scenes.lua new file mode 100644 index 0000000..eaaa169 --- /dev/null +++ b/scenes.lua @@ -0,0 +1,12 @@ +local scenes = {} + +function scenes:main(game) + game.gameloop = {} + local cellsize = 44 + local x = (game.WIDTH-cellsize*5)/2 + local y = (game.HEIGHT-cellsize*5)/2 + local grid = game.objects.grid:new(game,x,y,cellsize) + game:summon(grid) +end + +return scenes diff --git a/ù b/ù deleted file mode 100644 index 5db633c..0000000 --- a/ù +++ /dev/null @@ -1,29 +0,0 @@ -local gen= {} - -function gen:new(game,x,y,color,grid) - - local Circle = game.objects.base:new(game,x,y,grid.cellsize,grid.cellsize) - Circle.cellsize = grid.cellsize - Circle.grid = grid - Circle.shadowsprite = game:newImage("circles/shadow.png") - Circle.sprites = {game:newImage("circles/regular/"..color..".png"),game:newImage("circles/selected/"..color..".png")} - Circle:register("Circle") - Circle:register(color) - Circle.selected = false - - function Circle:step(dt) end - - function Circle:draw() - local spriteindex = 1 - if self.selected then spriteindex = 2 end - lg.draw(self.sprites[spriteindex],self.rect[1]-self.game.camerax-self.spriteoffx,self.rect[2]-self.game.cameray-self.spriteoffy) - end - - function Circle:MouseCallback(x,y,presses) - - end - - return Circle -end - -return gen