Drawing player object in relation to STI maps?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
nosuchfile
Prole
Posts: 1
Joined: Sat Oct 17, 2020 5:45 pm

Drawing player object in relation to STI maps?

Post by nosuchfile »

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:

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())
  
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.

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
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!
Attachments
project.love
(495.77 KiB) Downloaded 247 times
Capture.PNG
Capture.PNG (47.37 KiB) Viewed 6104 times
RockEt__2580__
Prole
Posts: 12
Joined: Mon Sep 14, 2020 8:36 am

Re: Drawing player object in relation to STI maps?

Post by RockEt__2580__ »

Sorry for the wait.

For your first question well I will give tips on how I did my enemies spawning with sti

Code: Select all

spawnEnemyLoc ={}
function spawnEnemy(map)
local kn= map.objects
for i,v in pairs(kn) do
   if v.name=="spawnEnemy1" then
       spawnEnemyLoc[1]=v
   end

   if v.name=="spawnEnemy2" then
       spawnEnemyLoc[2]=v
   end

   if v.name=="spawnEnemy3" then
       spawnEnemyLoc[3]=v
   end

   if v.name=="spawnEnemy4" then
       spawnEnemyLoc[4]=v
   end
end
end
I don't know how to use oop well so the function only takes the the map argument, the iterates over the map.objects(which are the objects I think), then if the name of the object is me enemy spawn points it assigns it to my table call spawnEnemyLoc, note that I have 4 enemy spawn location





As for the second question

Code: Select all


--you need to create a custom for your dynamic objects
layer =map:addCustomLayer( "Sprites" , 3)

local player
for k, object in pairs(map.objects) do
    if object.name =="Player" then
        player = object
        break
    end
end

--layer.player.oy
-- Create player object
--blob.sprite =love.graphics.newImage( "imgs/blob1.png" )
layer.player = {
sprite = blob.sprite,
x      = blob.x ,
y      = blob.y,
sx=190/blob.sprite:getWidth(),
sy=195/blob.sprite:getHeight(),
r=0,
ox     =300,
oy     =380
}


--Actually Draw player
layer.draw = function(self)
love.graphics.draw(self.player.sprite,
math.floor(self.player.x),
math.floor(self.player.y),
--math.floor(blob.x),
--math.floor(blob.y),
layer.player.r,layer.player.sy,flip,
--0, layer.player.sx , layer.player.sy,
self.player.ox,
self.player.oy)

end
All the code should be done in love.load()
As for the moving of the player it can be done in love.update(), cuz it will now respect layers from sti

If you are unclear about what I have written pls ask
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests