Advertisement
Guest User

love2d code

a guest
Jun 11th, 2024
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. function love.load()
  2. sounds = {}
  3. sounds.grass = love.audio.newSource("sounds/Walking-Grass.mp3", "static")
  4. sounds.music = love.audio.newSource("sounds/BackgroundMusic1.wav", "stream")
  5. sounds.music:setLooping(true)
  6.  
  7. sounds.music:play()
  8.  
  9. wf = require 'scripts/windfield'
  10. world = wf.newWorld(0, 0)
  11.  
  12. camera = require 'scripts/camera'
  13. cam = camera()
  14.  
  15. anim8 = require 'scripts/anim8'
  16. love.graphics.setDefaultFilter("nearest", "nearest")
  17.  
  18. sti = require 'scripts/sti'
  19. gameMap = sti('maps/testMap.lua')
  20.  
  21. player = {}
  22. player.collider = world:newBSGRectangleCollider(390, 240, 20, 40, 0)
  23. player.collider:setFixedRotation(true)
  24. player.x = 400
  25. player.y = 200
  26. player.speed = 250
  27. player.spriteSheet = love.graphics.newImage('sprites/player-sheet.png')
  28. player.grid = anim8.newGrid( 12, 18, player.spriteSheet:getWidth(), player.spriteSheet:getHeight() )
  29.  
  30. player.animations = {}
  31. player.animations.down = anim8.newAnimation( player.grid('1-4', 1), 0.2 )
  32. player.animations.left = anim8.newAnimation( player.grid('1-4', 2), 0.2 )
  33. player.animations.right = anim8.newAnimation( player.grid('1-4', 3), 0.2 )
  34. player.animations.up = anim8.newAnimation( player.grid('1-4', 4), 0.2 )
  35.  
  36. player.anim = player.animations.left
  37.  
  38. background = love.graphics.newImage('sprites/background.png')
  39.  
  40. walls = {}
  41. if gameMap.layers["Walls"] then
  42. for i, obj in pairs(gameMap.layers["Walls"].objects) do
  43. local wall = world:newRectangleCollider(obj.x, obj.y, obj.width, obj.height)
  44. wall: setType('static')
  45. table.insert(walls, wall)
  46. end
  47. end
  48.  
  49. end
  50.  
  51. function love.update(dt)
  52. local isMoving = false
  53.  
  54. local vx = 0
  55. local vy = 0
  56.  
  57. if love.keyboard.isDown("d") then
  58. vx = player.speed
  59. player.anim = player.animations.right
  60. isMoving = true
  61. end
  62.  
  63. if love.keyboard.isDown("a") then
  64. vx = player.speed * -1
  65. player.anim = player.animations.left
  66. isMoving = true
  67. end
  68.  
  69. if love.keyboard.isDown("s") then
  70. vy = player.speed
  71. player.anim = player.animations.down
  72. isMoving = true
  73. end
  74.  
  75. if love.keyboard.isDown("w") then
  76. vy = player.speed * -1
  77. player.anim = player.animations.up
  78. isMoving = true
  79. end
  80.  
  81. player.collider:setLinearVelocity(vx, vy)
  82.  
  83. if isMoving == false then
  84. player.anim:gotoFrame(2)
  85. end
  86.  
  87. world:update(dt)
  88. player.x = player.collider:getX()
  89. player.y = player.collider:getY()
  90.  
  91. player.anim:update(dt)
  92.  
  93. -- Update camera position
  94. cam:lookAt(player.x, player.y)
  95.  
  96. -- This section prevents the camera from viewing outside the background
  97. -- First, get width/height of the game window
  98. local w = love.graphics.getWidth()
  99. local h = love.graphics.getHeight()
  100.  
  101. -- Left border
  102. if cam.x < w/2 then
  103. cam.x = w/2
  104. end
  105.  
  106. -- Right border
  107. if cam.y < h/2 then
  108. cam.y = h/2
  109. end
  110.  
  111. -- Get width/height of background
  112. local mapW = gameMap.width * gameMap.tilewidth
  113. local mapH = gameMap.height * gameMap.tileheight
  114.  
  115. -- Right border
  116. if cam.x > (mapW - w/2) then
  117. cam.x = (mapW - w/2)
  118. end
  119. -- Bottom border
  120. if cam.y > (mapH - h/2) then
  121. cam.y = (mapH - h/2)
  122. end
  123.  
  124.  
  125. end
  126.  
  127. function love.draw()
  128. cam:attach()
  129. gameMap:drawLayer(gameMap.layers["Floor"])
  130. gameMap:drawLayer(gameMap.layers["Path"])
  131. gameMap:drawLayer(gameMap.layers["trees"])
  132. player.anim:draw(player.spriteSheet, player.x, player.y, nil, 2, nil, 6, 9)
  133. cam:detach()
  134. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement