Advertisement
Guest User

Love2d player movment

a guest
Jun 20th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.13 KB | None | 0 0
  1.  
  2. function loadPlayer(world)
  3.     player = {}
  4.     player.speed = 170
  5.     player.body = {}
  6.     player.fixture = {}
  7.     player.shape = {}
  8.     player.sounds = {}
  9.  
  10.     crosshair = {}
  11.     crosshair.x = 0
  12.     crosshair.y = 0
  13.  
  14.     player.image = love.graphics.newImage("assets/img/player.bmp")
  15.     player.width = player.image:getWidth()
  16.     player.height = player.image:getHeight()
  17.     player.body = love.physics.newBody(world, 0, 0, "dynamic")
  18.     player.shape = love.physics.newRectangleShape( player.width, player.height )
  19.     player.fixture = love.physics.newFixture( player.body, player.shape, 1 )
  20.     player.body:setMass(0)
  21.  
  22.     crosshair.image = love.graphics.newImage("assets/img/crosshair.png")
  23.  
  24.     player.sounds.shot = love.audio.newSource("assets/sound/gun-gunshot-01.mp3")
  25.     player.sounds.shot:setVolume(0.5)
  26.     player.sounds.shellFall = love.audio.newSource("assets/sound/bullet-fall.mp3")
  27.     player.sounds.shellFall:setVolume(0.9)
  28. end
  29.  
  30. function updatePlayer(dt)
  31.     x,y = love.mouse.getPosition()
  32.     crosshair.x = x
  33.     crosshair.y = y
  34.  
  35.     --player.angle = math.atan2(x-player.x-w/2,y-player.y-h/2)-math.pi/2
  36.     angle = -(math.atan2((x-player.body:getX()),(y-player.body:getY())))
  37.     player.body:setAngle(angle)
  38.     if love.keyboard.isDown("w") then
  39.         player.body:setY(player.body:getY()-dt*player.speed )
  40.     end
  41.     if love.keyboard.isDown("a") then
  42.         player.body:setX(player.body:getX()-dt*player.speed )
  43.     end
  44.     if love.keyboard.isDown("s") then
  45.         player.body:setY(player.body:getY()+dt*player.speed )
  46.     end
  47.     if love.keyboard.isDown("d") then
  48.         player.body:setX(player.body:getX()+dt*player.speed )
  49.     end
  50.  
  51.     if love.mouse.isDown("l") then
  52.         love.audio.play(player.sounds.shot)
  53.     end
  54.  
  55.     if player.sounds.shot:tell("seconds") > 0.3  then
  56.         love.audio.play(player.sounds.shellFall)
  57.     end
  58. end
  59.  
  60. function drawPlayer()
  61.     love.graphics.draw(player.image, player.body:getX(), player.body:getY(), player.body:getAngle(),1,1,player.width/2,player.height/2)
  62.     love.graphics.draw(crosshair.image, crosshair.x, crosshair.y,0,1,1,12.5,12.5)
  63.     love.graphics.circle( "fill",player.body:getX(),player.body:getX(),5) -- different from the image position!
  64. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement