Commit a27845e2 authored by Delisa Mason's avatar Delisa Mason

feat: Add systems/state for level loading, topdown gameplay

parent 3af5ec2a
return function ()
local system = {}
function system.update(game, dt)
function system.update(levelstate, dt)
end
-- function system.preUpdate(game, dt)
-- function system.preUpdate(levelstate, dt)
-- end
-- function system.postUpdate(game, dt)
-- function system.postUpdate(levelstate, dt)
-- end
-- function system.draw(game, dt)
-- function system.draw(levelstate, dt)
-- end
-- function system.keyreleased(game, key, code)
-- function system.keyreleased(levelstate, key, code)
-- end
return system
......
<?xml version="1.0" encoding="UTF-8"?>
<tileset name="default" tilewidth="16" tileheight="16" tilecount="4" columns="4">
<image source="../images/tileset.png" width="64" height="16"/>
</tileset>
This diff is collapsed.
This diff is collapsed.
local VelocityGenerator = require 'game.components.velocity'
local PlayerInputGenerator = require 'game.components.player_input'
return function(layer, index)
local player_data = layer.objects[index]
local image = love.graphics.newImage('game/assets/images/player.png')
local imgH = image:getHeight()
local imgW = image:getWidth()
return {
x = player_data.x,
y = player_data.y,
w = player_data.width,
h = player_data.height,
sprite = {
image = image,
quads = {
down = love.graphics.newQuad(0, 0, 16, 16, imgW, imgH),
left = love.graphics.newQuad(16, 0, 16, 16, imgW, imgH),
up = love.graphics.newQuad(32, 0, 16, 16, imgW, imgH),
right = love.graphics.newQuad(48, 0, 16, 16, imgW, imgH),
}
},
facing = 'down',
velocity = VelocityGenerator(layer.properties.speed),
input = PlayerInputGenerator({
up = 'up',
down = 'down',
left = 'left',
right = 'right',
pause = 'p',
action = 'space',
}),
}
end
local sti = require 'lib.sti'
local RenderMapSystem = require 'game.systems.render_map'
local PlayerInputSystem = require 'game.systems.player_input'
local MovementSystem = require 'game.systems.movement'
local Player = require 'game.objects.player'
local function load_map_objects(map, player)
map:addCustomLayer("sprites", 3)
local sprite_layer = map.layers["sprites"]
sprite_layer.player = player
function sprite_layer:draw()
local obj = self.player
local x = math.floor(obj.x)
local y = math.floor(obj.y)
love.graphics.draw(obj.sprite.image, obj.sprite.quads[obj.facing], x, y)
end
end
local systems
local state = {}
local levelstate = {}
function state:enter(_, map_path)
levelstate.map = sti(map_path)
levelstate.player = Player(levelstate.map.layers["player"], 1)
load_map_objects(levelstate.map, levelstate.player)
systems = require 'game.systems'
systems.add(PlayerInputSystem())
systems.add(RenderMapSystem())
systems.add(MovementSystem(levelstate.player, levelstate.map))
end
function state:draw()
local dt = love.timer.getDelta()
systems.draw(levelstate, dt)
end
function state:update(dt)
systems.update(levelstate, dt)
end
function state:keydown(key, code)
end
function state:keyreleased(key, code)
end
return state
local Gamestate = require 'lib.gamestate'
local LevelState = require 'game.states.level'
local state = {}
local font = love.graphics.newFont('game/assets/fonts/shattered-v1.ttf', 24)
......@@ -19,13 +20,9 @@ function state:update(dt)
elapsed = elapsed + math.floor(dt * 100)
end
function state:keydown(key, code)
end
function state:keyreleased(key, code)
if key == 'space' then
-- TODO: Create nextstate
-- Gamestate.push(nextstate)
Gamestate.push(LevelState, 'game/assets/maps/level01.lua')
end
end
......
......@@ -31,20 +31,20 @@ function systems.add(systemToAdd)
end
-- Run update loops on each system
function systems.update(game, dt)
system_func('preUpdate', game, dt)
system_func('update', game, dt)
system_func('postUpdate', game, dt)
function systems.update(levelstate, dt)
system_func('preUpdate', levelstate, dt)
system_func('update', levelstate, dt)
system_func('postUpdate', levelstate, dt)
end
-- Run keyreleased loop on each system
function systems.keyreleased(game, key, code)
system_func('keyreleased', game, key, code)
function systems.keyreleased(levelstate, key, code)
system_func('keyreleased', levelstate, key, code)
end
-- Run draw loops on each system
function systems.draw(game, dt)
system_func('draw', game, dt)
function systems.draw(levelstate, dt)
system_func('draw', levelstate, dt)
end
......
local bump = require 'lib.bump'
return function (player, map)
local system = {}
local world = bump.newWorld(map.tilewidth)
-- Add player to physics world
world:add(player, player.x, player.y, player.w, player.h)
-- Add all rectangles on the "collideables" layer of `map`
for _, obj in ipairs(map.layers["collideables"].objects) do
if obj.shape == "rectangle" then
world:add(obj, obj.x, obj.y, obj.width, obj.height)
end
end
function system.update(levelstate, dt)
if levelstate.player.velocity.dx ~= 0 or levelstate.player.velocity.dy ~= 0 then
local entity = levelstate.player
local goalX = entity.x + entity.velocity.dx
local goalY = entity.y + entity.velocity.dy
local actualX, actualY, _, _ = world:move(entity, goalX, goalY)
entity.x = actualX
entity.y = actualY
end
end
system.bumpWorld = world
return system
end
return function()
local system = {}
function system.keyreleased(game, key, code)
local entity = game.player
function system.keyreleased(levelstate, key, code)
local entity = levelstate.player
if key == entity.input.keymap.pause then
game.paused = not game.paused
levelstate.paused = not levelstate.paused
return
end
end
function system.update(game, dt)
local entity = game.player
if game.paused then
function system.update(levelstate, dt)
local entity = levelstate.player
if levelstate.paused then
return
end
if entity.input.moveUp() then
entity.facing = "up"
entity.velocity.dy = -entity.velocity.speed * dt
else if entity.input.moveDown() then
elseif entity.input.moveDown() then
entity.facing = "down"
entity.velocity.dy = entity.velocity.speed * dt
end
......@@ -26,7 +26,7 @@ return function()
if entity.input.moveLeft() then
entity.facing = "left"
entity.velocity.dx = -entity.velocity.speed * dt
else if entity.input.moveRight() then
elseif entity.input.moveRight() then
entity.facing = "right"
entity.velocity.dx = entity.velocity.speed * dt
end
......@@ -36,5 +36,10 @@ return function()
end
end
function system.postUpdate(levelstate, dt)
levelstate.player.velocity.dx = 0
levelstate.player.velocity.dy = 0
end
return system
end
return function ()
local system = {}
function system.update(levelstate, dt)
levelstate.map:update(dt)
end
-- Renders map centering on player. Avoids going over the map edges.
function system.draw(levelstate, dt)
local windowWidth = love.graphics.getWidth()
local windowHeight = love.graphics.getHeight()
local mapMaxWidth = levelstate.map.width * levelstate.map.tilewidth
local mapMaxHeight = levelstate.map.height * levelstate.map.tileheight
local x = math.min(math.max(0, levelstate.player.x - windowWidth/2), mapMaxWidth - windowWidth)
local y = math.min(math.max(0, levelstate.player.y - windowHeight/2), mapMaxHeight - windowHeight)
levelstate.map:draw(-x, -y, 1, 1)
end
return system
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment