Added game's ending check

This commit is contained in:
theo@manjaro 2022-03-04 14:03:06 +01:00
parent 4208f706fd
commit cc83d13b38
8 changed files with 83 additions and 40 deletions

BIN
assets/text/turn/blue.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

BIN
assets/text/turn/pink.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

View File

@ -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"

View File

@ -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)

View File

@ -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

View File

@ -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

12
scenes.lua Normal file
View File

@ -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

29
ù
View File

@ -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