mirror of
https://github.com/kikito/beholder.lua.git
synced 2024-12-16 00:34:21 +00:00
fixed non-usage of dt. updated to 2.1.1
This commit is contained in:
parent
c36ed687bc
commit
1b4ece5fcd
53
beholder.lua
53
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
|
-- 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:
|
-- 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"}) }
|
return { callbacks = {}, children = setmetatable({}, {__mode="k"}) }
|
||||||
end
|
end
|
||||||
|
|
||||||
local function initialize()
|
|
||||||
root = newNode()
|
|
||||||
nodesById = setmetatable({}, {__mode="k"})
|
|
||||||
end
|
|
||||||
|
|
||||||
local function findNodeById(id)
|
local function findNodeById(id)
|
||||||
return nodesById[id]
|
return nodesById[id]
|
||||||
@ -100,7 +96,27 @@ end
|
|||||||
local beholder = {}
|
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)
|
local function falseIfZero(n)
|
||||||
return n > 0 and n
|
return n > 0 and n
|
||||||
@ -118,14 +134,24 @@ end
|
|||||||
function beholder.observe(...)
|
function beholder.observe(...)
|
||||||
local event, callback = extractEventAndCallbackFromParams({...})
|
local event, callback = extractEventAndCallbackFromParams({...})
|
||||||
local node = findOrCreateDescendantNode(root, event)
|
local node = findOrCreateDescendantNode(root, event)
|
||||||
return addCallbackToNode(node, callback)
|
return addIdToCurrentGroup(addCallbackToNode(node, callback))
|
||||||
end
|
end
|
||||||
|
|
||||||
function beholder.stopObserving(id)
|
function beholder.stopObserving(id)
|
||||||
local node = findNodeById(id)
|
local node = findNodeById(id)
|
||||||
if not node then return false end
|
if node then removeCallbackFromNode(node, id) end
|
||||||
removeCallbackFromNode(node, id)
|
|
||||||
return true
|
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
|
end
|
||||||
|
|
||||||
function beholder.trigger(...)
|
function beholder.trigger(...)
|
||||||
@ -137,9 +163,12 @@ function beholder.triggerAll(...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function beholder.reset()
|
function beholder.reset()
|
||||||
initialize()
|
root = newNode()
|
||||||
|
nodesById = setmetatable({}, {__mode="k"})
|
||||||
|
groups = {}
|
||||||
|
currentGroupId = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
initialize()
|
beholder.reset()
|
||||||
|
|
||||||
return beholder
|
return beholder
|
||||||
|
12
main.lua
12
main.lua
@ -33,8 +33,8 @@ function move(entity, dt)
|
|||||||
if not entity.paused then
|
if not entity.paused then
|
||||||
for dir,delta in pairs(directions) do
|
for dir,delta in pairs(directions) do
|
||||||
if entity.want[dir] then
|
if entity.want[dir] then
|
||||||
entity.x = entity.x + delta.dx * entity.speed
|
entity.x = entity.x + delta.dx * entity.speed * dt
|
||||||
entity.y = entity.y + delta.dy * entity.speed
|
entity.y = entity.y + delta.dy * entity.speed * dt
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -51,7 +51,7 @@ end
|
|||||||
|
|
||||||
function draw(entity)
|
function draw(entity)
|
||||||
love.graphics.setColor(unpack(entity.color))
|
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
|
end
|
||||||
|
|
||||||
function relativePosition(entity)
|
function relativePosition(entity)
|
||||||
@ -103,7 +103,7 @@ function createPlayer()
|
|||||||
y = 300,
|
y = 300,
|
||||||
want={},
|
want={},
|
||||||
color = {200,200,50},
|
color = {200,200,50},
|
||||||
speed = 2
|
speed = 20
|
||||||
}
|
}
|
||||||
setmetatable(player, {__tostring = function(t) return 'player' end})
|
setmetatable(player, {__tostring = function(t) return 'player' end})
|
||||||
for dir,_ in pairs(directions) do
|
for dir,_ in pairs(directions) do
|
||||||
@ -123,7 +123,7 @@ function createZombie()
|
|||||||
y = math.random(20,580),
|
y = math.random(20,580),
|
||||||
want = {},
|
want = {},
|
||||||
color = {50,150,50},
|
color = {50,150,50},
|
||||||
speed = math.max(math.random()/2, 0.4),
|
speed = 10 + math.random(5),
|
||||||
target = player
|
target = player
|
||||||
}
|
}
|
||||||
setmetatable(zombie, {__tostring = function(t) return 'zombie' end})
|
setmetatable(zombie, {__tostring = function(t) return 'zombie' end})
|
||||||
@ -163,7 +163,7 @@ end
|
|||||||
|
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
all(zombies,chooseDirection)
|
all(zombies,chooseDirection)
|
||||||
all(entities,move)
|
all(entities,move,dt)
|
||||||
all(zombies,checkCollision,player)
|
all(zombies,checkCollision,player)
|
||||||
all(zombies,checkCollision,mine)
|
all(zombies,checkCollision,mine)
|
||||||
allWithIndex(zombies, removeIfDead)
|
allWithIndex(zombies, removeIfDead)
|
||||||
|
Loading…
Reference in New Issue
Block a user