|
| 1 | +bump = require "bump" |
| 2 | + |
| 3 | +function with_color(color, f, ...) |
| 4 | + local r, g, b, a = love.graphics.getColor() |
| 5 | + love.graphics.setColor(color) |
| 6 | + f(...) |
| 7 | + love.graphics.setColor(r, g, b, a) |
| 8 | +end |
| 9 | + |
| 10 | +function newSquare() |
| 11 | + local square = {} |
| 12 | + square.width = love.math.random(10, 40) |
| 13 | + square.height = square.width |
| 14 | + |
| 15 | + square.color = { |
| 16 | + love.math.random(), |
| 17 | + love.math.random(), |
| 18 | + love.math.random(), |
| 19 | + 1 |
| 20 | + } |
| 21 | + local width, height = love.graphics.getDimensions() |
| 22 | + |
| 23 | + square.x = love.math.random(0, width) |
| 24 | + square.y = love.math.random(0, height) |
| 25 | + |
| 26 | + return square |
| 27 | +end |
| 28 | + |
| 29 | +function love.load() |
| 30 | + world = bump.newWorld() |
| 31 | + squares = {} |
| 32 | + circle = {x=0, y=0, dx=0, dy=0, speed=100, r=10, color={1, 0, 1, 1}} |
| 33 | + world:add(circle, circle.x, circle.y, circle.r * 2, circle.r * 2) |
| 34 | + |
| 35 | + for i = 1, 100 do |
| 36 | + local square = newSquare() |
| 37 | + table.insert(squares, square) |
| 38 | + world:add(square, square.x, square.y, square.width, square.height) |
| 39 | + end |
| 40 | +end |
| 41 | + |
| 42 | +function love.draw() |
| 43 | + for _, sq in pairs(squares) do |
| 44 | + with_color(sq.color, love.graphics.rectangle, |
| 45 | + "fill", sq.x, sq.y, sq.width, sq.height) |
| 46 | + end |
| 47 | + |
| 48 | + -- draw the circle to the right/bottom of x, y, |
| 49 | + -- since bump uses these coordinates for collision |
| 50 | + -- checks |
| 51 | + with_color(circle.color, love.graphics.arc, "fill", |
| 52 | + circle.x + circle.r, circle.y + circle.r, circle.r, 0, math.pi*2) |
| 53 | +end |
| 54 | + |
| 55 | +function love.update(dt) |
| 56 | + -- handle keyboard input |
| 57 | + if love.keyboard.isDown("a", "left") then |
| 58 | + circle.dx = - circle.speed |
| 59 | + elseif love.keyboard.isDown("d", "right") then |
| 60 | + circle.dx = circle.speed |
| 61 | + else |
| 62 | + circle.dx = 0 |
| 63 | + end |
| 64 | + |
| 65 | + if love.keyboard.isDown("w", "up") then |
| 66 | + circle.dy = - circle.speed |
| 67 | + elseif love.keyboard.isDown("s", "down") then |
| 68 | + circle.dy = circle.speed |
| 69 | + else |
| 70 | + circle.dy = 0 |
| 71 | + end |
| 72 | + |
| 73 | + local x = circle.x + circle.dx * dt |
| 74 | + local y = circle.y + circle.dy * dt |
| 75 | + -- circle.x, circle.y = world:move(circle, x - circle.r, y - circle.r) |
| 76 | + circle.x, circle.y = world:move(circle, x, y) |
| 77 | +end |
0 commit comments