Skip to content

Instantly share code, notes, and snippets.

@systemsoverload
Created March 12, 2014 02:56
Show Gist options
  • Save systemsoverload/9499938 to your computer and use it in GitHub Desktop.
Save systemsoverload/9499938 to your computer and use it in GitHub Desktop.
Minimal Gamera Bound Love2d World
-- print program output to ST console
io.stdout:setvbuf("no")
local gamera = require 'gamera'
local min, max = math.min, math.max
-- game variables (entities)
local world, player, target, cam1
-- auxiliary functions
local isDown = love.keyboard.isDown
local floor = math.floor
-- world functions
local function drawBackground(cl,ct,cw,ch)
local w = world.w / world.columns
local h = world.h / world.rows
local minX = max(floor(cl/w), 0)
local maxX = min(floor((cl+cw)/w), world.columns-1)
local minY = max(floor(ct/h), 0)
local maxY = min(floor((ct+ch)/h), world.rows-1)
for y=minY, maxY do
for x=minX, maxX do
if (x + y) % 2 == 0 then
love.graphics.setColor(155,155,155)
else
love.graphics.setColor(200,200,200)
end
love.graphics.rectangle("fill", x*w, y*h, w, h)
end
end
end
-- player functions
local function updatePlayer(dt)
charSpeed = (550 * dt)
-- player controls
if isDown('left') and player.x > player.w / 2 then
player.x = player.x - charSpeed
elseif isDown('right') and player.x < (world.w - player.w / 2) then
player.x = player.x + charSpeed
end
if isDown('up') and player.y > (player.h / 2) then
player.y = player.y - charSpeed
elseif isDown('down') and player.y < (world.h - player.h / 2) then
player.y = player.y + charSpeed
end
end
local function drawPlayer()
love.graphics.setColor(0,255,0)
love.graphics.rectangle('fill',
player.x - player.w / 2,
player.y - player.h / 2,
player.w,
player.h)
end
-- main love functions
function love.load()
world = { w = 5000, h = 3000, rows = 10, columns = 20 }
target = { x = 500, y = 500 }
player = { x = 200, y = 200, w = 100, h = 100 }
cam1 = gamera.new(0, 0, world.w, world.h)
cam1:setWindow(10,10,780,580)
end
function love.update(dt)
updatePlayer(dt)
-- Lock the camera to the player pos
cam1:setPosition(player.x, player.y)
end
function love.draw()
cam1:draw(function(l,t,w,h)
drawBackground(l,t,w,h)
drawPlayer()
end)
love.graphics.setColor(255,255,255)
end
-- exit with esc
function love.keypressed(key)
if key == 'escape' then love.event.quit() end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment