Created
November 14, 2019 01:09
-
-
Save crispinha/6ea044616bf6f5c143f5c0394344eaa1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local lovetoys = require('lovetoys') | |
lovetoys.initialize({globals = true, debug = true}) | |
function love.load() | |
local Position = Component.create("position", {"x", "y"}, {x = 0, y = 0}) | |
local Velocity = Component.create("velocity", {"vx", "vy"}) | |
local player = Entity() | |
player:initialize() | |
player:add(Position(150, 25)) | |
player:add(Velocity(100, 100)) | |
local MixedSystem = class("MixedSystem", System) | |
function MixedSystem:requires() | |
return {"position", "velocity"} | |
end | |
function MixedSystem:update(dt) | |
for _, entity in pairs(self.targets) do | |
local position = entity:get("position") | |
local velocity = entity:get("velocity") | |
position.x = position.x + velocity.vx * dt | |
position.y = position.y + velocity.vy * dt | |
end | |
end | |
function MixedSystem:draw() | |
for _, entity in pairs(self.targets) do | |
love.graphics.rectangle("fill", entity:get("position").x, entity:get("position").y, 10, 10) | |
end | |
end | |
engine = Engine() | |
engine:addEntity(player) | |
engine:addSystem(MixedSystem(), "draw") | |
engine:addSystem(MixedSystem(), "update") | |
end | |
function love.update(dt) | |
engine:update(dt) | |
end | |
function love.draw() | |
engine:draw() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment