beholder.lua/main.lua

234 lines
5.0 KiB
Lua
Raw Normal View History

2012-01-29 12:03:33 +00:00
beholder = require 'beholder'
local directions = {
up = {dx = 0, dy = -1},
down = {dx = 0, dy = 1},
left = {dx = -1, dy = 0},
right = {dx = 1, dy = 0}
}
function startMoving(entity, direction)
entity.want[direction] = true
end
function stopMoving(entity, direction)
if not direction then
entity.want = {}
else
entity.want[direction] = nil
end
end
function pause(entity)
entity.paused = true
end
function unpause(entity)
entity.paused = false
end
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
end
end
end
end
function checkCollision(a,b)
local ax1, ay1, ax2, ay2 = a.x, a.y, a.x+16, a.y+16
local bx1, by1, bx2, by2 = b.x, b.y, b.x+16, b.y+16
if ax1 < bx2 and ax2 > bx1 and ay1 < by2 and ay2 > by1 then
2012-01-29 13:21:59 +00:00
beholder.trigger("COLLISION", a, b)
beholder.trigger("COLLISION", b, a)
2012-01-29 12:03:33 +00:00
end
end
function draw(entity)
love.graphics.setColor(unpack(entity.color))
love.graphics.rectangle("line", entity.x, entity.y, 16, 16)
end
function relativePosition(entity)
local position = {}
local dx = entity.target.x - entity.x
local dy = entity.target.y - entity.y
table.insert(position, dy > 0 and 'down' or dy < 0 and 'up')
table.insert(position, dx > 0 and 'right' or dx < 0 and 'left')
return position
end
function chooseDirection(entity)
if entity.target then
stopMoving(entity)
for _,dir in ipairs(relativePosition(entity)) do
startMoving(entity, dir)
end
end
end
function all(collection, f,...)
for i=#collection,1,-1 do
f(collection[i], ...)
end
end
function allWithIndex(collection, f)
for i=#collection,1,-1 do
f(collection[i], i, collection)
end
end
function removeIfDead(entity, id, collection)
if entity.dead then table.remove(collection, id) end
end
function gameOver()
print("braains")
2012-01-29 13:21:59 +00:00
beholder.reset()
2012-01-29 12:03:33 +00:00
love.event.push('q')
end
entities, zombies = {},{}
function createPlayer()
player = {
x = 400,
y = 300,
want={},
color = {200,200,50},
speed = 2
}
setmetatable(player, {__tostring = function(t) return 'player' end})
for dir,_ in pairs(directions) do
2012-01-29 13:21:59 +00:00
beholder.observe("KEYPRESSED", dir, function() startMoving(player, dir) end)
beholder.observe("KEYRELEASED", dir, function() stopMoving(player, dir) end)
2012-01-29 12:03:33 +00:00
end
2012-01-29 13:21:59 +00:00
beholder.observe("KEYPRESSED", " ", activateMine)
2012-01-29 12:03:33 +00:00
2012-01-29 13:21:59 +00:00
beholder.observe("COLLISION", player, gameOver)
2012-01-29 12:03:33 +00:00
table.insert(entities, player)
end
function createZombie()
local zombie = {
x = math.random(20,780),
y = math.random(20,580),
want = {},
color = {50,150,50},
speed = math.max(math.random()/2, 0.4),
target = player
}
setmetatable(zombie, {__tostring = function(t) return 'zombie' end})
table.insert(zombies, zombie)
table.insert(entities, zombie)
end
function createMine()
mine = {
x = player.x,
y = player.y,
want = {},
color = {100,100,200},
speed = 0
}
setmetatable(mine, {__tostring = function(t) return 'mine' end})
2012-01-29 13:21:59 +00:00
beholder.observe("COLLISION", mine, function(zombie)
2012-01-29 12:03:33 +00:00
if not mine.exploded then
mine.exploded = true
mine.color = {50,50,50}
zombie.dead = true
2012-01-29 13:21:59 +00:00
beholder.trigger("KILLED", zombie)
2012-01-29 12:03:33 +00:00
end
end)
table.insert(entities, mine)
end
function activateMine()
if mine.exploded then
mine.exploded = false
mine.color = {100,100,200}
mine.x,mine.y = player.x, player.y
end
end
function love.update(dt)
all(zombies,chooseDirection)
all(entities,move)
all(zombies,checkCollision,player)
all(zombies,checkCollision,mine)
allWithIndex(zombies, removeIfDead)
allWithIndex(entities, removeIfDead)
end
function love.draw()
all(entities,draw)
love.graphics.setColor(255,255,255)
love.graphics.print("Last pressed key: " .. lastPressedKey, 0, 580)
end
function love.keypressed(key)
2012-01-29 13:21:59 +00:00
beholder.trigger("KEYPRESSED", key)
2012-01-29 12:03:33 +00:00
end
function love.keyreleased(key)
2012-01-29 13:21:59 +00:00
beholder.trigger("KEYRELEASED", key)
2012-01-29 12:03:33 +00:00
end
function love.load()
math.randomseed(os.time())
local zombieCount = 20
local killCount = 0
createPlayer()
createMine()
for i=1,zombieCount do
createZombie()
end
2012-01-29 13:21:59 +00:00
beholder.observe(print) -- print every event on the terminal
2012-01-29 12:03:33 +00:00
-- prints the last pressed key on the screen
lastPressedKey = "<none yet>"
2012-01-29 13:21:59 +00:00
beholder.observe("KEYPRESSED", function(key)
2012-01-29 12:03:33 +00:00
lastPressedKey = key
end)
-- binds escape to the gameOver function (quit game)
2012-01-29 13:21:59 +00:00
beholder.observe("KEYPRESSED", "escape", gameOver)
2012-01-29 12:03:33 +00:00
-- handle pause
2012-01-29 13:21:59 +00:00
beholder.observe("KEYPRESSED", "pause", function()
2012-01-29 12:03:33 +00:00
all(entities, pause)
local id
2012-01-29 13:21:59 +00:00
id = beholder.observe("KEYPRESSED", function()
2012-01-29 12:03:33 +00:00
all(entities, unpause)
2012-01-29 13:21:59 +00:00
beholder.stopObserving(id)
2012-01-29 12:03:33 +00:00
end)
end)
-- victor is triggered if enough kills are done
2012-01-29 13:21:59 +00:00
beholder.observe("KILLED", function()
2012-01-29 12:03:33 +00:00
killCount = killCount + 1
if killCount == zombieCount then
print("You win!")
2012-01-29 13:21:59 +00:00
beholder.reset()
2012-01-29 12:03:33 +00:00
love.event.push('q')
end
end)
end