Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Loading the ECS
- local Sniper = require("sniper")
- local Entity = Sniper.entity
- local Component = Sniper.component
- local System = Sniper.system
- -- Setting up textures
- local Textures = {
- love.graphics.newImage("stone.png"),
- }
- -- Setting up components
- local Position = Component([[
- float x, y;
- ]])
- local Velocity = Component([[
- float x, y;
- ]])
- local Sprite = Component([[
- int texture;
- ]])
- -- Setting up systems
- local Physics = System({Position, Velocity})
- function Physics:update(dt)
- for i = 1, Physics.count do
- local id = Physics:get(i)
- local position = Position:get(id)
- local velocity = Velocity:get(id)
- position.x = position.x + velocity.x * dt
- position.y = position.y + velocity.y * dt
- if position.x > love.graphics.getWidth() or
- position.y > love.graphics.getHeight() then
- position.x = 0
- position.y = 0
- end
- end
- end
- local SpriteRenderer = System({Position, Sprite})
- function SpriteRenderer:draw()
- for i = 1, SpriteRenderer.count do
- local id = Physics:get(i)
- local position = Position:get(id)
- local sprite = Sprite:get(id)
- local texture = Textures[sprite.texture]
- love.graphics.draw(texture, position.x, position.y, nil, 0.1, 0.1)
- end
- end
- -- Creating 100 entities
- for i = 1, 100 do
- Entity()
- :give(Position, 0, 0)
- :give(Velocity, love.math.random(10, 30), love.math.random(10, 30))
- :give(Sprite, 1)
- :filter()
- end
- -- Callbacks
- function love.update(dt)
- Physics:update(dt)
- end
- function love.draw()
- SpriteRenderer:draw()
- end
Advertisement
Add Comment
Please, Sign In to add comment