Tjakka5

Untitled

Aug 26th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.63 KB | None | 0 0
  1. -- Loading the ECS
  2. local Sniper = require("sniper")
  3.  
  4. local Entity    = Sniper.entity
  5. local Component = Sniper.component
  6. local System    = Sniper.system
  7.  
  8. -- Setting up textures
  9. local Textures = {
  10.    love.graphics.newImage("stone.png"),
  11. }
  12.  
  13. -- Setting up components
  14. local Position = Component([[
  15.    float x, y;
  16. ]])
  17.  
  18. local Velocity = Component([[
  19.    float x, y;
  20. ]])
  21.  
  22. local Sprite = Component([[
  23.    int texture;
  24. ]])
  25.  
  26. -- Setting up systems
  27. local Physics = System({Position, Velocity})
  28. function Physics:update(dt)
  29.    for i = 1, Physics.count do
  30.       local id = Physics:get(i)
  31.  
  32.       local position = Position:get(id)
  33.       local velocity = Velocity:get(id)
  34.  
  35.       position.x = position.x + velocity.x * dt
  36.       position.y = position.y + velocity.y * dt
  37.  
  38.       if position.x > love.graphics.getWidth() or
  39.          position.y > love.graphics.getHeight() then
  40.  
  41.          position.x = 0
  42.          position.y = 0
  43.       end
  44.    end
  45. end
  46.  
  47. local SpriteRenderer = System({Position, Sprite})
  48. function SpriteRenderer:draw()
  49.    for i = 1, SpriteRenderer.count do
  50.       local id = Physics:get(i)
  51.  
  52.       local position = Position:get(id)
  53.       local sprite   = Sprite:get(id)
  54.  
  55.       local texture = Textures[sprite.texture]
  56.       love.graphics.draw(texture, position.x, position.y, nil, 0.1, 0.1)
  57.    end
  58. end
  59.  
  60. -- Creating 100 entities
  61. for i = 1, 100 do
  62.    Entity()
  63.    :give(Position, 0, 0)
  64.    :give(Velocity, love.math.random(10, 30), love.math.random(10, 30))
  65.    :give(Sprite, 1)
  66.    :filter()
  67. end
  68.  
  69. -- Callbacks
  70. function love.update(dt)
  71.    Physics:update(dt)
  72. end
  73.  
  74. function love.draw()
  75.    SpriteRenderer:draw()
  76. end
Advertisement
Add Comment
Please, Sign In to add comment