Difference between revisions of "Lovetoys"

(Example source updated)
Line 7: Line 7:
 
If you don't have any idea what this entity component stuff is all about, click that link and give it a read! It's totally worth it!
 
If you don't have any idea what this entity component stuff is all about, click that link and give it a read! It's totally worth it!
  
A basic structure of a main loop:
+
A simple example for implementation:
  
 
<source lang="lua">
 
<source lang="lua">
 
require(lovetoys/engine)
 
require(lovetoys/engine)
-- A new instance of an engine is beeing created
+
 
engine = Engine()
+
function love.load()
 +
 
 +
    -- Creation of a new world
 +
    love.physics.setMeter(64)
 +
    world = love.physics.newWorld(0, 9.81*80, true)
 +
    -- Enabling the collision functions
 +
    world:setCallbacks(beginContact, endContact)
 +
 
 +
    love.graphics.setMode(1000, 600, false, true, 0)
 +
 
 +
    -- A new instance of an engine is beeing created
 +
    engine = Engine()
 
     -- A new instance of an eventmanager is beeing created
 
     -- A new instance of an eventmanager is beeing created
 
     eventmanager = EventManager()
 
     eventmanager = EventManager()
 
 
     -- A new instance of a collisionmanager is beeing created
 
     -- A new instance of a collisionmanager is beeing created
 
     collisionmanager = CollisionManager()
 
     collisionmanager = CollisionManager()
  
     -- The collisionmanager is beeing registered as a listener for the  
+
     -- The collisionmanager is beeing registered as a listener for the
 
     -- "BeginContact" event.
 
     -- "BeginContact" event.
 
     eventmanager:addListener("BeginContact", {collisionmanager, collisionmanager.fireEvent})
 
     eventmanager:addListener("BeginContact", {collisionmanager, collisionmanager.fireEvent})
 +
 +
    -- Logic (update) systems are beeing added to the engine
 +
    engine:addSystem(ExampleSystem(), "logic", 1)
 +
 +
    -- Drawing systems are beeing added to the engine
 +
    engine:addSystem(ExampleDrawSystem(), "draw")
 +
end
 +
  
 
function love.update(dt)
 
function love.update(dt)
Line 31: Line 49:
 
     -- Engine draw function
 
     -- Engine draw function
 
     engine:draw()
 
     engine:draw()
end
 
 
function love:keypressed(key, u)
 
    eventmanager:fireEvent(KeyPressed(key, u))
 
 
end
 
end
 
+
--Collision function
function love:mousepressed(x, y, button)
+
function beginContact(a, b, coll)
     eventmanager:fireEvent(MousePressed(x, y, button))
+
     -- Dynamic creation of a new instance of BeginContact and firing it to the Eventmanager
 +
    stack:current().eventmanager:fireEvent(BeginContact(a, b, coll))
 
end
 
end
  

Revision as of 03:50, 2 December 2013



Lovetoys is a small bundle of helper classes and libraries consisting of 3 packages.
The most important one is the Entity Component System which is based on Richard Lords Introduction to ECS's.
If you don't have any idea what this entity component stuff is all about, click that link and give it a read! It's totally worth it!

A simple example for implementation:

require(lovetoys/engine)

function love.load()

    -- Creation of a new world
    love.physics.setMeter(64)
    world = love.physics.newWorld(0, 9.81*80, true)
    -- Enabling the collision functions
    world:setCallbacks(beginContact, endContact)

    love.graphics.setMode(1000, 600, false, true, 0)

    -- A new instance of an engine is beeing created
    engine = Engine()
    -- A new instance of an eventmanager is beeing created
    eventmanager = EventManager()
    -- A new instance of a collisionmanager is beeing created
    collisionmanager = CollisionManager()

    -- The collisionmanager is beeing registered as a listener for the
    -- "BeginContact" event.
    eventmanager:addListener("BeginContact", {collisionmanager, collisionmanager.fireEvent})

    -- Logic (update) systems are beeing added to the engine
    engine:addSystem(ExampleSystem(), "logic", 1)

    -- Drawing systems are beeing added to the engine
    engine:addSystem(ExampleDrawSystem(), "draw")
end


function love.update(dt)
    -- Engine update function
    engine:update(dt)
end

function love.draw()
    -- Engine draw function
    engine:draw()
end
--Collision function
function beginContact(a, b, coll)
    -- Dynamic creation of a new instance of BeginContact and firing it to the Eventmanager
    stack:current().eventmanager:fireEvent(BeginContact(a, b, coll))
end



Grab it or give it a look on Github.

If you want to see a proper way of implementation or some nice examples for events and collisions, have a look at our Example-project or our Game.