Advertisement
Snusmumriken

love2d camera 0.3.5

Oct 28th, 2016
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.97 KB | None | 0 0
  1. --by Alex Wordly
  2. --http://pastebin.com/uVYpUtzF
  3. --[[
  4.     Reference:
  5. function love.load()
  6.     cam = require'amcam'()
  7.     player = {
  8.         x = 10,
  9.         y = 20,
  10.         vx = 0,
  11.         vy = 0,
  12.         angle = 0,
  13.         img = love.graphics.newImage('player.png')
  14.     }
  15.     scale = 1
  16. end
  17.  
  18. function love.update(dt)
  19.     if love.keyboard.isDown('w') then player.vy = player.vy - 10*dt end
  20.     if love.keyboard.isDown('a') then player.vx = player.vx - 10*dt end
  21.     if love.keyboard.isDown('s') then player.vy = player.vy + 10*dt end
  22.     if love.keyboard.isDown('d') then player.vx = player.vx + 10*dt end
  23.     local oldx, oldy = player.x, player.y
  24.     player.x, player.y = player.x + player.vx, player.y + player.vy
  25.     player.angle = math.atan2(oldy - player.y, oldx - player.x)
  26.     cam:setPosition(player.x, player.y)
  27.     if love.keyboard.isDown('-') then scale = scale - 0.2 end
  28.     if love.keyboard.isDown('=') then scale = scale + 0.2 end
  29.     cam:setScale(scale)
  30. end
  31.  
  32. function love.draw()
  33.     cam:motor()
  34.         love.graphics.rectangle('line', 0, 0, 800, 600)
  35.         local width, height = player.img:getDimensions()
  36.         love.graphics.draw(player.img, player.x, player.y, player.angle, _, _, width/2, height/2)
  37.         local msx, msy = love.mouse.getPosition()
  38.         local camx, camy = cam:toWorld(msx, msy)
  39.         love.graphics.print('Mouse on screen: '..msx..':'..msy, camx, camy)
  40.     cam:stop()
  41.     love.graphics.print('This is demo')
  42. end
  43. ]]
  44. local camera = {}
  45. local function isNum(v) return type(v) == 'number' end
  46. local function err(v, ...) error(v:format(...), 3) end
  47. local graphics = love.graphics or error'Graphics module required!'
  48. setmetatable(camera,
  49.     {__call =
  50.         function(self)
  51.             local o = {}
  52.             o.x = 400
  53.             o.y = 300
  54.             o.gw, o.gh = graphics.getDimensions()
  55.             o.scale = 1
  56.             o.markers = {}  -- additional elements, draw inside cam or on cam bounds
  57.             self.__index = self
  58.             return setmetatable(o, self)
  59.         end,
  60.     }
  61. )
  62.  
  63. function camera:moveTo(x, y)        -- move from current position to vector
  64.     self.x = isNum(x) and self.x + x or err('Cam: arg#1 error: number expected, got '..type(x))
  65.     self.y = isNum(y) and self.y + y or err('Cam: arg#2 error: number expected, got '..type(y))
  66. end
  67.  
  68. function camera:setPosition(x, y)   --set absolute position
  69.     self.x = isNum(x) and x or err('Cam: arg#1 error: number expected, got '..type(x))
  70.     self.y = isNum(y) and y or err('Cam: arg#2 error: number expected, got '..type(y))
  71. end
  72.  
  73. function camera:addMarker(id, x, y, img)
  74.     local o = {}
  75.     o.x = isNum(x) and x or err('Cam: arg#1 error: number expected, got '..type(x))
  76.     o.y = isNum(y) and y or err('Cam: arg#2 error: number expected, got '..type(y))
  77.     o.img = img
  78.     self.markers[id] = o
  79. end
  80.  
  81. local function trim(x, min, max) return x < min and min or x > max and max or x end
  82.  
  83. function camera:drawMarker(id)      -- marker api
  84.     local v = self.markers[id]
  85.     local ax, ay, bx, by = self:getBounds()
  86.     local bw, bh = v.img:getDimensions()
  87.     bx = bx - bw
  88.     by = by - bh
  89.     graphics.draw(v.img, trim(v.x, ax, bx), trim(v.y, ay, by))
  90. end
  91.  
  92. function camera:getBounds()     -- get bounding box of camers
  93.     local w, h = self.gw/(2*self.scale), self.gh/(2*self.scale)
  94.     return self.x - w, self.y - h, self.x + w, self.y + h
  95. end
  96.  
  97. function camera:getPosition()
  98.     return self.x, self.y
  99. end
  100.  
  101. function camera:toWorld(x, y)   -- translate screen position to camera
  102.     local w, h = (x - self.gw/2) / self.scale, (y - self.gh/2) / self.scale
  103.     return w + self.x, h + self.y
  104. end
  105.  
  106. function camera:toCam(x, y)     -- translate camera position to world
  107.     local x, y = x - self.x, y - self.y
  108.     return x * self.scale + w/2, y * self.scale + h/2
  109. end
  110.  
  111. function camera:setScale(s)
  112.     self.scale = isNum(s) and s or err('Cam: arg#1 error: number expected, got '..type(s))
  113. end
  114.  
  115. function camera:getScale()
  116.     return self.scale
  117. end
  118.  
  119. function camera:motor()
  120.     self.gw, self.gh = graphics.getDimensions()
  121.     graphics.push()
  122.     graphics.scale(self.scale)
  123.     graphics.translate(-self.x + (graphics.getWidth()/2)/self.scale, -self.y + (graphics.getHeight()/2)/self.scale)
  124. end
  125.  
  126. function camera:stop()
  127.     love.graphics.pop()
  128. end
  129. return camera
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement