diff --git a/beholder.lua b/beholder.lua index b9f15d2..ada58c6 100755 --- a/beholder.lua +++ b/beholder.lua @@ -1,4 +1,4 @@ --- beholder.lua - v2.0.1 (2011-11) +-- beholder.lua - v2.1.1 (2011-11) -- Copyright (c) 2011 Enrique GarcĂ­a Cota -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: @@ -28,10 +28,6 @@ local function newNode() return { callbacks = {}, children = setmetatable({}, {__mode="k"}) } end -local function initialize() - root = newNode() - nodesById = setmetatable({}, {__mode="k"}) -end local function findNodeById(id) return nodesById[id] @@ -100,7 +96,27 @@ end local beholder = {} --- beholder private functions +-- beholder private functions/vars + +local groups = nil +local currentGroupId = nil + +local function addIdToCurrentGroup(id) + if currentGroupId then + groups[currentGroupId] = groups[currentGroupId] or setmetatable({}, {__mode="k"}) + local group = groups[currentGroupId] + group[#group + 1] = id + end + return id +end + +local function stopObservingGroup(group) + local count = #group + for i=1,count do + beholder.stopObserving(group[i]) + end + return count +end local function falseIfZero(n) return n > 0 and n @@ -118,14 +134,24 @@ end function beholder.observe(...) local event, callback = extractEventAndCallbackFromParams({...}) local node = findOrCreateDescendantNode(root, event) - return addCallbackToNode(node, callback) + return addIdToCurrentGroup(addCallbackToNode(node, callback)) end function beholder.stopObserving(id) local node = findNodeById(id) - if not node then return false end - removeCallbackFromNode(node, id) - return true + if node then removeCallbackFromNode(node, id) end + + local group, count = groups[id], 0 + if group then count = stopObservingGroup(group) end + + return (node or count > 0) and true or false +end + +function beholder.group(groupId, f) + assert(not currentGroupId, "beholder.group can not be nested!") + currentGroupId = groupId + f() + currentGroupId = nil end function beholder.trigger(...) @@ -137,9 +163,12 @@ function beholder.triggerAll(...) end function beholder.reset() - initialize() + root = newNode() + nodesById = setmetatable({}, {__mode="k"}) + groups = {} + currentGroupId = nil end -initialize() +beholder.reset() return beholder diff --git a/main.lua b/main.lua index abf57e3..d7d7813 100755 --- a/main.lua +++ b/main.lua @@ -33,8 +33,8 @@ function move(entity, dt) if not entity.paused then for dir,delta in pairs(directions) do if entity.want[dir] then - entity.x = entity.x + delta.dx * entity.speed - entity.y = entity.y + delta.dy * entity.speed + entity.x = entity.x + delta.dx * entity.speed * dt + entity.y = entity.y + delta.dy * entity.speed * dt end end end @@ -51,7 +51,7 @@ end function draw(entity) love.graphics.setColor(unpack(entity.color)) - love.graphics.rectangle("line", entity.x, entity.y, 16, 16) + love.graphics.rectangle("line", math.floor(entity.x), math.floor(entity.y), 16, 16) end function relativePosition(entity) @@ -103,7 +103,7 @@ function createPlayer() y = 300, want={}, color = {200,200,50}, - speed = 2 + speed = 20 } setmetatable(player, {__tostring = function(t) return 'player' end}) for dir,_ in pairs(directions) do @@ -123,7 +123,7 @@ function createZombie() y = math.random(20,580), want = {}, color = {50,150,50}, - speed = math.max(math.random()/2, 0.4), + speed = 10 + math.random(5), target = player } setmetatable(zombie, {__tostring = function(t) return 'zombie' end}) @@ -163,7 +163,7 @@ end function love.update(dt) all(zombies,chooseDirection) - all(entities,move) + all(entities,move,dt) all(zombies,checkCollision,player) all(zombies,checkCollision,mine) allWithIndex(zombies, removeIfDead)