Drawing player object in relation to STI maps?
Posted: Sat Oct 17, 2020 6:14 pm
I found it hard to understand some tutorials I've found for using STI so hoping I'm able to learn more here. Some background: I'm just implementing loading maps with STI, I've created a level class with hump.class and a map object with some basic parameters for finding width and height for defining map boundaries. The map itself has a single object layer called "spawn point" with a "player" object, following along with this tutorial, but I haven't done anything with it yet.
Relevant code from map.lua:
In player.lua, I have a player object that has a few basic properties as well, obviously some properties need to be changed because the movement controls don't fit within a tiled map. X and Y coords are set to nil right now because I'm not sure how to draw the player in relation to the map.
How would I 1) create a reusable function that draws the player at the map's player spawn object, and 2) implement movement within the map? Not looking for complete solutions, just tips on where to start. Attached are the game's LOVE file and a screenshot of the map's layers in Tiled. Thanks!
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