NeutronLoved/objects/circle.lua

149 lines
3.9 KiB
Lua

local gen= {}
function gen:new(game,x,y,color,grid)
local Circle = game.objects.base:new(game,x,y,grid.cellsize,grid.cellsize)
Circle.drawx = Circle.rect.x
Circle.drawy = Circle.rect.y
Circle.cellsize = grid.cellsize
Circle.grid = grid
Circle.color = color
Circle.shadowsprite = game:newImage("circles/shadow.png")
Circle.shadowpos = {}
Circle.moves = {}
Circle.sprites = {game:newImage("circles/regular/"..color..".png"),game:newImage("circles/selected/"..color..".png")}
Circle:register("Circle")
Circle:register(color) Circle.selected = false
Circle.highlight = false
Circle.highlightTimer = game:Timer(0.08)
function Circle:step(dt)
self.drawx = self.drawx + (self.rect.x-self.drawx)*dt*6
self.drawy = self.drawy + (self.rect.y-self.drawy)*dt*6
local i = 1
for i=1,#self.moves do
self.shadowpos[i][1]= self.shadowpos[i][1] + (self.moves[i][1]-self.shadowpos[i][1])*dt*6
self.shadowpos[i][2]= self.shadowpos[i][2] + (self.moves[i][2]-self.shadowpos[i][2])*dt*6
end
if self.highlight then
self.highlightTimer:tick(dt)
if self.highlightTimer.loops > 3 then
self.highlight = false
self.highlightTimer.loops = 0
end
end
end
function Circle:isMyTurn()
return self:getTurn()==self.color
end
function Circle:getTurn()
local turns = {"neutron","pink","neutron","blue"}
return turns[self.grid.turn+1]
end
function Circle:draw()
local spriteindex = 1
if self.selected or self.highlight and self.highlightTimer.loops%2==0 then spriteindex = 2 end
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)
lg.draw(self.shadowsprite,self.shadowpos[i][1]-self.game.camerax-self.spriteoffx,self.shadowpos[i][2]-self.game.cameray-self.spriteoffy)
end
end
lg.draw(self.sprites[spriteindex],self.drawx-self.game.camerax-self.spriteoffx,self.drawy-self.game.cameray-self.spriteoffy)
end
function Circle:MouseCallback(x,y,presses)
if not self.grid.tut.showing and self.rect:collidepoint(x,y) then
if self:isMyTurn() then
local c = self.game:findName("Circle")
local i
for i=1,#c do
if c[i]~=self then c[i].selected = false end
end
local pastselected = self.selected
self.selected = not self.selected
self.depth = 1
if self.selected then
if not pastselected then
self.moves = self:findAvailableMoves()
self.shadowpos = {}
local i = 1
self.shadowpos = {}
for i=1,#self.moves do
table.insert(self.shadowpos,{self.rect.x,self.rect.y})
end
self.depth = 2
end
end
else
objs = self.game:findName(self:getTurn())
local i
for i=1,#objs do
objs[i].highlight = true
end
end
end
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
self.grid:addturn()
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 = 0
offy = 0
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
end
return Circle
end
return gen