NeutronLoved/objects/grid.lua

141 lines
3.8 KiB
Lua
Raw Normal View History

2022-03-03 20:24:10 +01:00
local gridgen = {}
function gridgen:new(game,x,y,cellsize,tut)
2022-03-03 20:24:10 +01:00
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")}
2022-03-04 14:03:06 +01:00
Grid.pinkturn = game:newImage("text/turn/pink.png")
Grid.blueturn = game:newImage("text/turn/blue.png")
Grid.bluearrows = game:Timer(1)
Grid.pinkarrows = game:Timer(1)
Grid.bluearrows.loops = -1
Grid.pinkarrows.loops = 1
Grid.tut = tut
Grid.bluearrowsprite = game:newImage("arrows/blue.png")
Grid.pinkarrowsprite = game:newImage("arrows/pink.png")
2022-03-03 20:24:10 +01:00
Grid:register("Grid")
Grid.depth = -1
2022-03-04 20:50:31 +01:00
Grid.turn= 3
2022-03-03 20:24:10 +01:00
-- Spawn circles
local i
for i=1,5 do
local xspawn = x+(i-1)*cellsize
2022-03-04 10:39:02 +01:00
local pink = game.objects.circle:new(game,xspawn,y,"pink",Grid)
2022-03-03 20:24:10 +01:00
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)
2022-03-04 14:03:06 +01:00
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].drawy<=self.rect.y+self.cellsize/2 then winner = "pink" end
if obj[i].drawy+obj[i].rect.h>=self.rect.y+self.rect.h-self.cellsize/2 then winner = "blue" end
2022-03-04 14:03:06 +01:00
end
if winner~="None" then
self:endgame(winner)
end
if self.pinkarrows.loops<1 then self.pinkarrows:tick(dt) end
if self.bluearrows.loops<1 then self.bluearrows:tick(dt) end
2022-03-04 14:03:06 +01:00
end
2022-03-03 20:24:10 +01:00
2022-03-04 14:03:06 +01:00
function Grid:addturn()
local pastturn = self.turn
2022-03-04 14:03:06 +01:00
self.turn = self.turn + 1
self.turn = self.turn%4
-- Check for arrow animation
if math.floor(pastturn/2)%2~=math.floor(self.turn/2)%2 then
if self.turn<2 then
-- Trigger pink arrows
self.pinkarrows.loops = 0
else
-- Trigger blue arrows
self.bluearrows.loops = 0
end
end
2022-03-04 14:03:06 +01:00
-- 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
2022-03-07 15:29:27 +01:00
count = count + #obj[i]:findAvailableMoves()
2022-03-04 14:03:06 +01:00
end
if count==0 then
2022-03-07 15:29:27 +01:00
local win = "pink"
if self.turn < 2 then win = "blue" end
2022-03-04 14:03:06 +01:00
self:endgame(win)
end
end
function Grid:endgame(winner)
2022-03-09 11:04:28 +01:00
local args = {}
args.winner = winner
2022-03-10 09:39:17 +01:00
local t = self.game.objects.transition:new(game,0.5,"ending",args)
2022-03-09 11:04:28 +01:00
self.game:summon(t)
2022-03-04 14:03:06 +01:00
end
2022-03-03 20:24:10 +01:00
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
2022-03-04 14:03:06 +01:00
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
-- Draw arrows
local i=0
local margin = 50
for i=0,1 do
local offx = margin
if i==1 then offx = game.WIDTH-margin-self.pinkarrowsprite:getWidth() end
-- Draw pink arrows
if self.pinkarrows.loops==0 then
local offy = -margin
local movement = (game.HEIGHT+margin*2)*self:easing(self.pinkarrows:getratio())
lg.draw(self.pinkarrowsprite,offx,offy+movement)
end
if self.bluearrows.loops==0 then
local offy = game.HEIGHT+margin
local movement = -(game.HEIGHT+margin*2)*self:easing(self.bluearrows:getratio())
lg.draw(self.bluearrowsprite,offx,offy+movement)
end
end
end
function Grid:easing(x)
return 1-(1-x)*(1-x)
2022-03-03 20:24:10 +01:00
end
return Grid
end
return gridgen