Class = require("libs.hump.class") Gamestate = require("libs.hump.gamestate") STI = require("libs.sti") local Moonshine = require("libs.moonshine") Camera = require("libs.hump.camera") Windfield = require("libs.windfield") require("libs.tserial") require("core.notifications") require("entities.enemy") local zoomFactor = 3 local player = { x=0, y=0, sprite=love.graphics.newImage("assets/images/player_demo.png"), speed=100, dir="down" } local fullscreen = true local windowWidth, windowHeight = love.graphics.getDimensions() local _w = windowWidth/zoomFactor local _h = windowHeight/zoomFactor Map = Class { __include=Gamestate, init = function(self, mapName) _gameWorld = Windfield.newWorld(0,0) _gameWorld:setQueryDebugDrawing(true) -- Remove when deploy _gameWorld:addCollisionClass('player') _gameWorld:addCollisionClass('interactive') _gameWorld:addCollisionClass('enemy') effect = Moonshine(windowWidth, windowHeight, Moonshine.effects.crt) .chain(Moonshine.effects.vignette) .chain(Moonshine.effects.scanlines) .chain(Moonshine.effects.chromasep) effect.scanlines.thickness = .2 effect.scanlines.opacity = .5 effect.chromasep.angle = 1 effect.chromasep.radius = 3 notifications = Notifications(zoomFactor) camera = Camera() camera:zoom(zoomFactor) currentMap = STI("assets/maps/"..mapName..".lua") if currentMap.layers["entities"] then for i,obj in pairs(currentMap.layers["entities"].objects) do if obj.name=="player" and obj.type=="player" then player.x = obj.x player.y = obj.y player.collider = _gameWorld:newBSGRectangleCollider(player.x, player.y, 8, 8, 0) player.collider:setFixedRotation(true) player.collider:setCollisionClass('player') player.collider:setObject({ x = player.x, y = player.y, dir = player.dir }) end if obj.type=="interactive" then local collider = _gameWorld:newRectangleCollider(obj.x, obj.y, obj.width, obj.height) local interactiveData = { type = obj.type, interactive_type = obj.properties.interactive_type, message = obj.properties.message and obj.properties.message or nil, talker_data = obj.properties.talker_data and obj.properties.talker_data or nil } collider:setObject(interactiveData) collider:setCollisionClass('interactive') collider:setType("static") end if obj.type=="enemy" then enemy = Enemy(obj.x, obj.y, _gameWorld) enemy:setCollider() end 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("w") then vy = player.speed * -1 player.dir = "up" end if love.keyboard.isDown("a") then vx = player.speed * -1 player.dir = "left" end if love.keyboard.isDown("s") then vy = player.speed player.dir = "down" end if love.keyboard.isDown("d") then vx = player.speed player.dir = "right" end player.collider:setLinearVelocity(vx, vy) _gameWorld:update(dt) player.x = player.collider:getX() - 4 player.y = player.collider:getY() - 4 player.collider:setObject({ x = player.x, y = player.y, dir = player.dir }) camera:lookAt(player.x, player.y) currentMap:update(dt) if camera.x < _w/2 then camera.x = _w/2 end 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) end if camera.y > (mapHeight - _h/2) then camera.y = (mapHeight - _h/2) end if player.collider:enter('interactive') then local _interColliderData = player.collider:getEnterCollisionData('interactive') local interactiveCollider = _interColliderData.collider local interactiveData = interactiveCollider:getObject() if interactiveData.message then notifications:send(interactiveData.message) end end notifications:update(dt) enemy:update(dt) end function Map:draw() effect(function() camera:attach() drawMapLayer("ground") drawMapLayer("decorations") love.graphics.draw(player.sprite, player.x, player.y) drawMapLayer("foreground") _gameWorld:draw() -- Debug Collision Draw enemy:draw() camera:detach() notifications:draw() end) end function Map:keypressed(key, scancode) if(scancode=="f4") then fullscreen = not fullscreen love.window.setFullscreen(fullscreen) resize() end end function drawMapLayer(layerName) if currentMap.layers[layerName].visible then currentMap:drawLayer(currentMap.layers[layerName]) end end function resize() _width, _height = love.graphics.getDimensions() if not(fullscreen) then _width = 1280 _height = 720 end effect.resize(_width,_height) notifications.calculateWindowDimensions() end return Map