Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function loadPlayer(world)
- player = {}
- player.speed = 170
- player.body = {}
- player.fixture = {}
- player.shape = {}
- player.sounds = {}
- crosshair = {}
- crosshair.x = 0
- crosshair.y = 0
- player.image = love.graphics.newImage("assets/img/player.bmp")
- player.width = player.image:getWidth()
- player.height = player.image:getHeight()
- player.body = love.physics.newBody(world, 0, 0, "dynamic")
- player.shape = love.physics.newRectangleShape( player.width, player.height )
- player.fixture = love.physics.newFixture( player.body, player.shape, 1 )
- player.body:setMass(0)
- crosshair.image = love.graphics.newImage("assets/img/crosshair.png")
- player.sounds.shot = love.audio.newSource("assets/sound/gun-gunshot-01.mp3")
- player.sounds.shot:setVolume(0.5)
- player.sounds.shellFall = love.audio.newSource("assets/sound/bullet-fall.mp3")
- player.sounds.shellFall:setVolume(0.9)
- end
- function updatePlayer(dt)
- x,y = love.mouse.getPosition()
- crosshair.x = x
- crosshair.y = y
- --player.angle = math.atan2(x-player.x-w/2,y-player.y-h/2)-math.pi/2
- angle = -(math.atan2((x-player.body:getX()),(y-player.body:getY())))
- player.body:setAngle(angle)
- if love.keyboard.isDown("w") then
- player.body:setY(player.body:getY()-dt*player.speed )
- end
- if love.keyboard.isDown("a") then
- player.body:setX(player.body:getX()-dt*player.speed )
- end
- if love.keyboard.isDown("s") then
- player.body:setY(player.body:getY()+dt*player.speed )
- end
- if love.keyboard.isDown("d") then
- player.body:setX(player.body:getX()+dt*player.speed )
- end
- if love.mouse.isDown("l") then
- love.audio.play(player.sounds.shot)
- end
- if player.sounds.shot:tell("seconds") > 0.3 then
- love.audio.play(player.sounds.shellFall)
- end
- end
- function drawPlayer()
- love.graphics.draw(player.image, player.body:getX(), player.body:getY(), player.body:getAngle(),1,1,player.width/2,player.height/2)
- love.graphics.draw(crosshair.image, crosshair.x, crosshair.y,0,1,1,12.5,12.5)
- love.graphics.circle( "fill",player.body:getX(),player.body:getX(),5) -- different from the image position!
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement