From 8f7b5fff0a29c65844d232301d6c32c77ceae811 Mon Sep 17 00:00:00 2001 From: "theo@manjaro" Date: Fri, 4 Mar 2022 10:39:02 +0100 Subject: [PATCH] Can select circles --- game.lua | 8 ++++++++ main.lua | 9 +++++++++ objects/base.lua | 2 ++ objects/circle.lua | 12 ++++++++++++ objects/grid.lua | 2 +- ù | 29 +++++++++++++++++++++++++++++ 6 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 ù diff --git a/game.lua b/game.lua index 5882543..62e3df1 100644 --- a/game.lua +++ b/game.lua @@ -216,6 +216,13 @@ function Game:draw(screen) end end +function Game:MouseCallback(x,y,presses) + local i + for i=1,#self.gameloop do + self.gameloop[i]:MouseCallback(x,y,presses) + end +end + function Game:bint(bool) -- Convert Boolean to Integer local result = 0 if bool then result = 1 end @@ -251,6 +258,7 @@ function Game:Timer(time) end return Timer + end Game:reinit() diff --git a/main.lua b/main.lua index cf1e2b9..5864f25 100644 --- a/main.lua +++ b/main.lua @@ -70,3 +70,12 @@ end function love.resize(w, h) if game.OSTYPE~="3DS" then push:resize(w, h) end end + +function love.mousepressed(x,y,button,istouch,presses) + -- Format positions according to screen + + if game.OS ~= "Horizon" then + x,y = push:toGame(x,y) + end + game:MouseCallback(x,y,presses) +end diff --git a/objects/base.lua b/objects/base.lua index bc1ca29..66fbdd8 100644 --- a/objects/base.lua +++ b/objects/base.lua @@ -13,6 +13,8 @@ function basegen:new(game,x,y,w,h) function Base:step(dt) end + function Base:MouseCallback(x,y,presses) end + function Base:register(name) table.insert(self.classes,name) end diff --git a/objects/circle.lua b/objects/circle.lua index 7ed326e..5eb3ddb 100644 --- a/objects/circle.lua +++ b/objects/circle.lua @@ -5,6 +5,7 @@ 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.color = color Circle.shadowsprite = game:newImage("circles/shadow.png") Circle.sprites = {game:newImage("circles/regular/"..color..".png"),game:newImage("circles/selected/"..color..".png")} Circle:register("Circle") @@ -19,6 +20,17 @@ function gen:new(game,x,y,color,grid) 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) + if self.rect:collidepoint(x,y) then + local c = self.game:findName(self.color) + local i + for i=1,#c do + if c[i]~=self then c[i].selected = false end + end + self.selected = not self.selected + end + end + return Circle end diff --git a/objects/grid.lua b/objects/grid.lua index 7ce3ac2..5c4afc8 100644 --- a/objects/grid.lua +++ b/objects/grid.lua @@ -12,7 +12,7 @@ function gridgen:new(game,x,y,cellsize) local i for i=1,5 do local xspawn = x+(i-1)*cellsize - local pink = game.objects.circle:new(game,xspawn,y,"pink",self) + local pink = game.objects.circle:new(game,xspawn,y,"pink",Grid) local blue = game.objects.circle:new(game,xspawn,y+4*cellsize,"blue",Grid) game:summon(pink) game:summon(blue) diff --git a/ù b/ù new file mode 100644 index 0000000..5db633c --- /dev/null +++ b/ù @@ -0,0 +1,29 @@ +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