you can now collect the coins

master
Pascal Batty 2019-04-28 15:16:37 +02:00
parent 12365663e2
commit 1a92ebe2aa
1 changed files with 22 additions and 1 deletions

View File

@ -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