2022-03-03 20:24:10 +01:00
|
|
|
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
|
2022-03-04 10:39:02 +01:00
|
|
|
Circle.color = color
|
2022-03-03 20:24:10 +01:00
|
|
|
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)
|
2022-03-04 11:10:37 +01:00
|
|
|
if self.selected then
|
|
|
|
local i
|
|
|
|
for i=1,#self.moves do
|
|
|
|
lg.draw(self.shadowsprite,self.moves[i][1]-self.game.camerax-self.spriteoffx,self.moves[i][2]-self.game.cameray-self.spriteoffy)
|
|
|
|
end
|
|
|
|
end
|
2022-03-03 20:24:10 +01:00
|
|
|
end
|
|
|
|
|
2022-03-04 10:39:02 +01:00
|
|
|
function Circle:MouseCallback(x,y,presses)
|
|
|
|
if self.rect:collidepoint(x,y) then
|
2022-03-04 11:10:37 +01:00
|
|
|
local c = self.game:findName("Circle")
|
2022-03-04 10:39:02 +01:00
|
|
|
local i
|
|
|
|
for i=1,#c do
|
|
|
|
if c[i]~=self then c[i].selected = false end
|
|
|
|
end
|
|
|
|
self.selected = not self.selected
|
2022-03-04 11:10:37 +01:00
|
|
|
self.depth = 1
|
|
|
|
if self.selected then
|
|
|
|
self.moves = self:findAvailableMoves()
|
|
|
|
self.depth = 2
|
|
|
|
end
|
2022-03-04 10:39:02 +01:00
|
|
|
end
|
2022-03-04 11:10:37 +01:00
|
|
|
if self.selected then
|
|
|
|
local i
|
|
|
|
for i=1,#self.moves do
|
|
|
|
rect = self.game.rect:new(self.moves[i][1],self.moves[i][2],self.cellsize,self.cellsize)
|
|
|
|
if rect:collidepoint(x,y) then
|
|
|
|
self.rect:move_ip(self.moves[i][1],self.moves[i][2])
|
|
|
|
self.selected = false
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Circle:findAvailableMoves()
|
|
|
|
local moves = {}
|
|
|
|
local movx
|
|
|
|
local movy
|
|
|
|
local offx
|
|
|
|
local offy
|
|
|
|
obj = self.game:findName("Circle")
|
|
|
|
|
|
|
|
function ValidMove(x,y)
|
|
|
|
local i
|
|
|
|
x = self.rect.x+x+self.cellsize/2
|
|
|
|
y = self.rect.y+y+self.cellsize/2
|
|
|
|
local result = self.grid.rect:collidepoint(x,y)
|
|
|
|
for i=1,#obj do
|
|
|
|
if obj[i].rect:collidepoint(x,y) then
|
|
|
|
result = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
while ValidMove(offx+movx*self.cellsize,offy+movy*self.cellsize) do
|
|
|
|
offx = offx + movx*self.cellsize
|
|
|
|
offy = offy + movy*self.cellsize
|
|
|
|
end
|
|
|
|
if ValidMove(offx,offy) then
|
|
|
|
table.insert(moves,{offx+self.rect.x,offy+self.rect.y})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return moves
|
2022-03-04 10:39:02 +01:00
|
|
|
end
|
|
|
|
|
2022-03-03 20:24:10 +01:00
|
|
|
return Circle
|
|
|
|
end
|
|
|
|
|
|
|
|
return gen
|