aboutsummaryrefslogtreecommitdiff
path: root/utils/map.lua
diff options
context:
space:
mode:
Diffstat (limited to 'utils/map.lua')
-rw-r--r--utils/map.lua76
1 files changed, 53 insertions, 23 deletions
diff --git a/utils/map.lua b/utils/map.lua
index a26168f..1f06487 100644
--- a/utils/map.lua
+++ b/utils/map.lua
@@ -3,23 +3,24 @@ Gamestate = require("libs.hump.gamestate")
STI = require("libs.sti")
Moonshine = require("libs.moonshine")
Camera = require("libs.hump.camera")
+Windfield = require("libs.windfield")
-
-zoomFactor = 3
+zoomFactor = 2
player = {
x=0,
y=0,
sprite=love.graphics.newImage("assets/images/player_demo.png"),
- speed=5
+ speed=100
}
-windowWidth, windowHeight = love.graphics.getDimensions()
-
-w = windowWidth/zoomFactor
-h = windowHeight/zoomFactor
+windowWidth, windowHeight = love.graphics.getDimensions()
+_w = windowWidth/zoomFactor
+_h = windowHeight/zoomFactor
Map = Class {
- init = function(self, mapName) -- TODO pass map instance parameter
+ __include=Gamestate,
+ init = function(self, mapName)
+ _gameWorld = Windfield.newWorld(0,0)
effect = Moonshine(windowWidth, windowHeight, Moonshine.effects.crt)
.chain(Moonshine.effects.vignette)
.chain(Moonshine.effects.scanlines)
@@ -28,6 +29,7 @@ Map = Class {
effect.scanlines.opacity = .5
effect.chromasep.angle = 1
effect.chromasep.radius = 2
+
camera = Camera()
camera:zoom(zoomFactor)
currentMap = STI("assets/maps/"..mapName..".lua")
@@ -35,57 +37,79 @@ Map = Class {
for i,obj in pairs(currentMap.layers["entities"].objects) do
player.x = obj.x
player.y = obj.y
+ player.collider = _gameWorld:newBSGRectangleCollider(player.x, player.y, 8, 8, 0)
+ player.collider:setFixedRotation(true)
end
end
+
+ if currentMap.layers["walls"] then
+ for i,obj in pairs(currentMap.layers["walls"].objects) do
+ local wall = _gameWorld:newRectangleCollider(obj.x, obj.y, obj.width, obj.height)
+ wall:setType("static")
+ end
+ end
end,
entities={}
}
function Map:update(dt)
+
+ local vx = 0
+ local vy = 0
+
if love.keyboard.isDown("up") then
- player.y = player.y - player.speed
+ vy = player.speed * -1
end
if love.keyboard.isDown("down") then
- player.y = player.y + player.speed
+ vy = player.speed
end
if love.keyboard.isDown("left") then
- player.x = player.x - player.speed
+ vx = player.speed * -1
end
if love.keyboard.isDown("right") then
- player.x = player.x + player.speed
+ vx = player.speed
end
+ player.collider:setLinearVelocity(vx, vy)
+
+ _gameWorld:update(dt)
+ player.x = player.collider:getX() - 4
+ player.y = player.collider:getY() - 4
+
camera:lookAt(player.x, player.y)
currentMap:update(dt)
- if camera.x < w/2 then
- camera.x = w/2
+ if camera.x < _w/2 then
+ camera.x = _w/2
end
- if camera.y < h/2 then
- camera.y = h/2
+ if camera.y < _h/2 then
+ camera.y = _h/2
end
local mapWidth = currentMap.width * currentMap.tilewidth
local mapHeight = currentMap.height * currentMap.tileheight
- if camera.x > (mapWidth - w/2) then
- camera.x = (mapWidth - w/2)
+ if camera.x > (mapWidth - _w/2) then
+ camera.x = (mapWidth - _w/2)
end
- if camera.y > (mapHeight - h/2) then
- camera.y = (mapHeight - h/2)
+ if camera.y > (mapHeight - _h/2) then
+ camera.y = (mapHeight - _h/2)
end
end
function Map:draw()
effect(function()
- camera:attach()
- currentMap:drawLayer(currentMap.layers["ground"])
- love.graphics.draw(player.sprite, player.x, player.y)
+ camera:attach()
+ drawMapLayer("ground")
+ drawMapLayer("decorations")
+ love.graphics.draw(player.sprite, player.x, player.y)
+ drawMapLayer("foreground")
+ -- _gameWorld:draw()
camera:detach()
end)
end
@@ -93,4 +117,10 @@ end
function Map:keypressed(key, scancode)
end
+function drawMapLayer(layerName)
+ if currentMap.layers[layerName].visible then
+ currentMap:drawLayer(currentMap.layers[layerName])
+ end
+end
+
return Map \ No newline at end of file