I've just released a new lib - it's called beholder.lua.
Github page:
https://github.com/kikito/beholder.lua
This lib basically allows you to handle "events" - it's mostly useful when you have "something important for an entity in your game happenning outside the code of that entity".
The most obvious example is in LÖVE's callbacks. By default, these callbacks force you to put potentially different kinds of code in the same place. The code for handling the player movement is next to the code for showing the exit menu.
With beholder, you do something similar to this:
Code: Select all
beholder = require 'beholder'
...
function love.keypress(key)
beholder.trigger("KEYPRESS", key)
end
Once the triggers are in place, you can "register" to the keyboard events anywhere you want; in your player code, you can move to the left when the left key is pressed like this:
Code: Select all
function movePlayerLeft()
...
end
function createPlayer()
...
beholder.observe("KEYPRESS", "left", movePlayerLeft)
...
end
Code: Select all
function love.load()
...
beholder.observe("KEYPRESS", "escape", function() love.event.push('q') end)
beholder.observe("KEYPRESS", "pause", pauseEverything)
end
It's also worth noting that the events are inclusive. For example, now it's possible to log all the key presses with this:
Code: Select all
function activateKeyLogger()
beholder.observe("KEYPRESS", function(key) log("key pressed: ", key) end)
end
Input handling is however just one of the possible examples. Others could be resource loading, network signals, or collisions in the physics system.
I've made a very stupid demo showing some of the uses this lib can have. It shows:
- An extended version of the "distributed" keyboard handling shown on this post. Includes player movement, esc for exiting and pause key for pausing the action.
- Collision-related actions are also near their respective objects - the collision detection function just triggers an event.
- A logic-related event (KILLED) is triggered each time an enemy is killed. That info is used by the love.load function to know when the game is won
Beholder is one of the parts of middleclass-extras that I'm re-releasing after releasing middleclass 2.0. However, beholder has now become middleclass-independant. I'm considering creating a "mixing" for tighter middleclass integration, but I'm not sure about that yet. In any case, you can use beholder without touching middleclass (beholder doesn't reference/require middleclass in any way)You are being chased by a group of zombies.
You can't place another mine on the ground until the previous one has been exploded by a zombie.
- Use the movement keys for moving around.
- Space plants a mine on the ground.
Zombies kill you. Mines kill them. If they touch you, you are dead. If they touch a mine, they are dead. Make them all touch a mine to win.
The zombies initial position is random. If you die instantaneously after launching the demo, launch it again; I did not check that zombies didn't materialize on top of you right at the start.
This demo prints a lot of info on the console - launching it from the console is recommended.
I have not used OOP, several files, etc on the demo. This was on purpose (and very difficult for me!). I wanted to make this as accessible to everyone as possible. When (if) I do the middleclass integration, I'll go full OOP on you.
Regards!
Edit: In version 2.0.x you must use the dot instead of colons to invoke beholder methods: beholder.trigger() instead of beholder:trigger() . I've updated this post to show that, and also updated the demo