Skip to content
Browse files

New mixin for handlers, goal blocker now fades out when all gems are …

…taken.
  • Loading branch information...
1 parent fbfd17e commit 880399af3f994fc2c7b65654a2f2004d6b3f1686 Ross Andrews committed
Showing with 91 additions and 30 deletions.
  1. +17 −13 drawing.lua
  2. +23 −1 effect_manager.lua
  3. +4 −2 entity_manager.lua
  4. +45 −0 handlers.lua
  5. +2 −14 level.lua
View
30 drawing.lua
@@ -56,19 +56,23 @@ function drawEntity(e)
end
function drawGoal(self)
- local g = love.graphics
+ local goal = self.manager:find('goal_blocker')[1]
- g.push()
- g.translate(self.goal.body:getX(), self.goal.body:getY())
- g.setColor(10, 70, 10)
- g.circle('fill', 0, 0, self.goal.shape:getRadius())
-
- if self.gem_count > 0 then
- g.rotate(-math.pi/2)
- g.setColor(40, 230, 40)
- local a = math.pi * 2 * (self.player.gem_count / self.gem_count)
- g.arc('fill', 0, 0, self.goal.shape:getRadius(), 0, a)
- end
+ if goal then
+ local g = love.graphics
- g.pop()
+ g.push()
+ g.translate(goal.body:getX(), goal.body:getY())
+ g.setColor(10, 70, 10)
+ g.circle('fill', 0, 0, goal.shape:getRadius())
+
+ if self.gem_count > 0 then
+ g.rotate(-math.pi/2)
+ g.setColor(40, 230, 40)
+ local a = math.pi * 2 * (self.player.gem_count / self.gem_count)
+ g.arc('fill', 0, 0, goal.shape:getRadius(), 0, a)
+ end
+
+ g.pop()
+ end
end
View
24 effect_manager.lua
@@ -70,4 +70,26 @@ function Puff:draw()
g.setColor(190, 190, 30, 255 * (self.duration - self.t))
g.rectangle('fill', -s/2, -s/2, s, s)
g.pop()
-end
+end
+
+----------------------------------------
+
+local Fade = {
+ duration = 0.5
+}
+
+function fade(x, y)
+ return setmetatable({x=x,y=y,t=0}, {__index=Fade})
+end
+
+Fade.update = Puff.update
+
+function Fade:draw()
+ local g = love.graphics
+
+ g.push()
+ g.translate(self.x, self.y)
+ g.setColor(40, 230, 40, 255 * (self.duration - self.t))
+ g.circle('fill', 0, 0, 16)
+ g.pop()
+end
View
6 entity_manager.lua
@@ -34,8 +34,10 @@ function methods:add(body, shape, type)
end
function methods:remove(entity)
- entity.remove = true
- self.needs_cull = true
+ if entity then
+ entity.remove = true
+ self.needs_cull = true
+ end
end
function methods:cull()
View
45 handlers.lua
@@ -0,0 +1,45 @@
+module(..., package.seeall)
+
+local EffectManager = require('effect_manager')
+
+local SIZE = 32
+local methods = _M
+
+local entity_manager = nil
+local effect_manager = nil
+local level = nil
+
+function methods:addHandlers()
+ entity_manager = self.manager
+ effect_manager = self.effect_manager
+ level = self
+
+ self.manager:handler('gem', 'player', take_gem)
+ self.manager:handler('gem', 'crate', pushback_crate)
+end
+
+------------------------------------------------------------
+
+function pushback_crate(gem, crate)
+ local f = 5
+ local dx = crate.body:getX() - gem.body:getX()
+ local dy = crate.body:getY() - gem.body:getY()
+ crate.body:applyLinearImpulse(f*dx, f*dy)
+end
+
+function take_gem(gem, player)
+ player.gem_count = player.gem_count + 1
+ entity_manager:remove(gem)
+ effect_manager:add(EffectManager.puff(gem.body:getX(), gem.body:getY(), gem.body:getAngle()))
+
+ if player.gem_count == level.gem_count then open_goal() end
+end
+
+function open_goal()
+ local goal = entity_manager:find('goal_blocker')[1]
+
+ if goal then
+ entity_manager:remove(goal)
+ effect_manager:add(EffectManager.fade(goal.body:getX(), goal.body:getY()))
+ end
+end
View
16 level.lua
@@ -21,6 +21,7 @@ methods = setmetatable({}, {__index=Map.methods})
mixin(methods, 'create_entities')
mixin(methods, 'drawing')
mixin(methods, 'physics')
+mixin(methods, 'handlers')
function new(strs)
local tbl = Map.new_from_strings(strs)
@@ -42,18 +43,5 @@ function methods:init()
self.goal = self:makeGoal()
self.player = self:makePlayer()
- self.manager:handler('gem', 'player',
- function(gem, player)
- self.player.gem_count = self.player.gem_count + 1
- self.manager:remove(gem)
- self.effect_manager:add(EffectManager.puff(gem.body:getX(), gem.body:getY(), gem.body:getAngle()))
- end)
-
- self.manager:handler('gem', 'crate',
- function(gem, crate)
- local f = SIZE * 15
- local dx = crate.body:getX() - gem.body:getX()
- local dy = crate.body:getY() - gem.body:getY()
- crate.body:applyForce(f*dx, f*dy)
- end)
+ self:addHandlers()
end

0 comments on commit 880399a

Please sign in to comment.
Something went wrong with that request. Please try again.