From 1a92ebe2aa06ec223ac1db9d57b9aa972177ed76 Mon Sep 17 00:00:00 2001 From: Pascal Batty Date: Sun, 28 Apr 2019 15:16:37 +0200 Subject: [PATCH] you can now collect the coins --- right_coin.lua | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/right_coin.lua b/right_coin.lua index 3ee1837..ebf5a8e 100644 --- a/right_coin.lua +++ b/right_coin.lua @@ -50,7 +50,14 @@ function drawActor(self) self.spr=self.ani[(((t)//3)% #self.ani)+1] local flip if self.isFlipped then flip = 1 else flip = 0 end - spr(self.spr, self.x, self.y, 1, 1, flip) + spr(self.spr, self.x, self.y, 0, 1, flip) +end + +function hitTest(rect1, rect2) + return rect1.x < rect2.x + rect2.w + and rect1.x + rect1.w > rect2.x + and rect1.y < rect2.y + rect2.h + and rect1.y + rect1.h > rect2.y end colors={"blue","red","yellow"} @@ -121,6 +128,7 @@ function addRandomCoin() newCoin.y=loc.y coins[#coins + 1]=newCoin end + -- MOCK addRandomCoin() addRandomCoin() @@ -152,6 +160,7 @@ do -- Player self:resolve() self.x = self.x + self.vx self.y = self.y + self.vy + self:collectCoin() end function Player:resolve() @@ -198,6 +207,18 @@ do -- Player return y end + function Player:collectCoin() + for i,v in pairs(coins) do + if hitTest(self, v) then + table.remove(coins,i) + if v.color == "blue" then coinBlue=coinBlue+1 + elseif v.color == "red" then coinRed=coinRed+1 + elseif v.color == "yellow" then coinYellow=coinYellow+1 + end + end + end + end + function Player:draw() drawActor(self) end