Relevant code from map.lua:
Code: Select all
Level = Class{}
function mapLoad()
map = sti("assets/maps/samplemap.lua")
-- create level class for map loading
function Level:init(image, mapWidth, mapHeight)
self.image = image
self.mapWidth = mapWidth
self.mapHeight = mapHeight
end
-- create map object for samplemap
map = Level(love.graphics.newImage("assets/maps/samplemap.png"), image:getWidth(), image:getHeight())
Code: Select all
Entity = Class{}
-- create entity class to include player, enemies, and NPC objects
function Entity:init(sprite, speed, posX, posY, health)
self.sprite = sprite
self.speed = speed
self.posX, self.posY = posX, posY
self.health = health
end
-- create player object
player = Entity(love.graphics.newImage("assets/player.png"), 5, nil, nil, 10)
-- create movement function
function playerUpdate()
local delta = love.timer.getDelta()
-- move up
if love.keyboard.isDown("w") then
player.posY = player.posY + (player.speed * delta)
-- move left
elseif love.keyboard.isDown("a") then
player.posX = player.posX - (player.speed * delta)
-- move down
elseif love.keyboard.isDown("s") then
player.posY = player.posY - (player.speed * delta)
-- move right
elseif love.keyboard.isDown("d") then
player.posX = player.posX + (player.speed * delta)
end
end
...
function playerDraw()
love.graphics.draw(player.sprite, player.posX, player.posY)
end