Guest User

mapRenderer

a guest
Aug 29th, 2020
69
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Create a dynamic layer (data can be changed)
  2. mapRenderer = {}
  3.  
  4. function mapRenderer(map)
  5.     love.physics.setMeter(32)
  6.     world = love.physics.newWorld(0,0)
  7.     map:box2d_init(world)
  8.     local layer = map:addCustomLayer("Sprites",6)
  9.  
  10.     -- Get the spawnpoint object
  11.     local player
  12.     for k, object in pairs(map.objects) do
  13.         if object.name == "Player" then
  14.             player = object
  15.             break
  16.         end
  17.     end
  18.  
  19.     -- Create the player object (positioned on the spawnpoint initially)
  20.     local sprite = love.graphics.newImage("graphics/sprites/malePlayer.png")
  21.     layer.player = {
  22.         sprite = sprite,
  23.         x = player.x,
  24.         y = player.y,
  25.         ox = sprite:getWidth() / 32,
  26.         oy = sprite:getHeight() / 2.16
  27.     }
  28.  
  29.     -- Player controls
  30.     layer.update = function(self, dt)
  31.         -- 96 pixels per second
  32.         local speed = 96 * dt
  33.  
  34.         -- Move player
  35.         -- Run
  36.         if love.keyboard.isDown("z") then
  37.             speed = speed * 1.5
  38.         end
  39.    
  40.         -- Move left
  41.         if love.keyboard.isDown("left") then
  42.             self.player.x = self.player.x - speed
  43.         end
  44.  
  45.         -- Move
  46.         if love.keyboard.isDown("right") then
  47.             self.player.x = self.player.x + speed
  48.         end
  49.     end
  50.  
  51.     -- Draw player to screen
  52.     layer.draw = function(self)
  53.         love.graphics.draw(
  54.             self.player.sprite,
  55.             math.floor(self.player.x),
  56.             math.floor(self.player.y),
  57.             0,
  58.             1,
  59.             1,
  60.             self.player.ox,
  61.             self.player.oy
  62.         )
  63.  
  64.     end
  65.  
  66.     -- Remove original spawnpoint layer
  67.     map:removeLayer("spawnObj")
  68. end
  69.  
  70. return mapRenderer
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×