@@ -0,0 +1,5 @@ | |||||
function love.conf(t) | |||||
t.window.width = 640 | |||||
t.window.height = 480 | |||||
t.window.title = "unnamed" | |||||
end |
@@ -0,0 +1,72 @@ | |||||
-- world module | |||||
local world = require("world") | |||||
-- love's load function | |||||
function love.load() | |||||
-- set the overall map offset | |||||
offset = { | |||||
x = 0, | |||||
y = 0 | |||||
} | |||||
-- generate the world and load the files | |||||
world.new(10, 10) | |||||
world.loadTiles() | |||||
-- store the mouse position | |||||
mouse = { | |||||
x = love.mouse.getX(), | |||||
y = love.mouse.getY() | |||||
} | |||||
end | |||||
-- love's update function | |||||
function love.update(dt) | |||||
-- get the current mouse location | |||||
local mouseNow = { | |||||
x = love.mouse.getX(), | |||||
y = love.mouse.getY() | |||||
} | |||||
-- if mouse is pressed move the offset | |||||
if love.mouse.isDown(1) then | |||||
local mouseDelta = { | |||||
x = mouse.x - mouseNow.x, | |||||
y = mouse.y - mouseNow.y, | |||||
} | |||||
offset.x = offset.x - mouseDelta.x | |||||
offset.y = offset.y - mouseDelta.y | |||||
end | |||||
mouse = mouseNow | |||||
end | |||||
-- love's key press funciton | |||||
function love.keypressed(key, scanCode, isRepeat) | |||||
-- arrow keys for moving offset | |||||
if key == "left" then | |||||
offset.x = offset.x - 16 | |||||
end | |||||
if key == "right" then | |||||
offset.x = offset.x + 16 | |||||
end | |||||
if key == "up" then | |||||
offset.y = offset.y - 16 | |||||
end | |||||
if key == "down" then | |||||
offset.y = offset.y + 16 | |||||
end | |||||
end | |||||
-- love's draw function | |||||
function love.draw() | |||||
-- clear the window | |||||
love.graphics.clear() | |||||
-- draw the map | |||||
world.render(offset) | |||||
end |
@@ -0,0 +1,17 @@ | |||||
local util = {} | |||||
function util.cartToIso(cart) | |||||
local iso = {} | |||||
iso.x = cart.x - cart.y | |||||
iso.y = (cart.x + cart.y) / 2 | |||||
return iso | |||||
end | |||||
function util.isoToCart(iso) | |||||
local cart = {} | |||||
cart.x = (2 * iso.y + iso.x) / 2 | |||||
cart.y = (2 * iso.y - iso.x) / 2 | |||||
return cart | |||||
end | |||||
return util |
@@ -0,0 +1,50 @@ | |||||
-- bring in the util module | |||||
local util = require("util") | |||||
-- world stuffs | |||||
local world = {} | |||||
-- create a new world with given height and width | |||||
function world.new(width, height) | |||||
world.width = width | |||||
world.height = height | |||||
for x = 1, world.width do | |||||
world[x] = {} | |||||
for y = 1, world.height do | |||||
world[x][y] = "nothing" | |||||
end | |||||
end | |||||
end | |||||
-- load the tile files | |||||
function world.loadTiles() | |||||
world.grass = love.graphics.newImage("img/tiles/grass.png") | |||||
world.dirt = love.graphics.newImage("img/tiles/dirt.png") | |||||
world.water = love.graphics.newImage("img/tiles/water.png") | |||||
world.nothing = love.graphics.newImage("img/tiles/nothing.png") | |||||
world.select = love.graphics.newImage("img/tiles/select.png") | |||||
end | |||||
-- render the world | |||||
function world.render(offset) | |||||
for x = 1, world.width do | |||||
for y = 1, world.height do | |||||
local tile = world[x][y] | |||||
local loc = util.cartToIso({x = x, y = y}) | |||||
if tile == "grass" then | |||||
image = world.grass | |||||
elseif tile == "dirt" then | |||||
image = world.dirt | |||||
elseif tile == "water" then | |||||
image = world.water | |||||
else | |||||
image = world.nothing | |||||
end | |||||
love.graphics.draw(image, loc.x * 16 + offset.x, loc.y * 16 + offset.y) | |||||
end | |||||
end | |||||
end | |||||
return world |