Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.40 KB | None | 0 0
  1. -- Cette ligne permet d'afficher des traces dans la console pendant l'éxécution
  2. io.stdout:setvbuf('no')
  3.  
  4. -- Empèche Love de filtrer les contours des images quand elles sont redimentionnées
  5. -- Indispensable pour du pixel art
  6. love.graphics.setDefaultFilter("nearest")
  7.  
  8. -- Cette ligne permet de déboguer pas à pas dans ZeroBraneStudio
  9. if arg[#arg] == "-debug" then require("mobdebug").start() end
  10.  
  11.  
  12.  
  13.  
  14.  
  15. --Tiles de la map
  16. local imgTiles = {}
  17. imgTiles["1"] = love.graphics.newImage ("pictures/tile1.png")
  18. imgTiles["2"] = love.graphics.newImage ("pictures/tile2.png")
  19. imgTiles["3"] = love.graphics.newImage ("pictures/tile3.png")
  20. imgTiles["4"] = love.graphics.newImage ("pictures/tile4.png")
  21. imgTiles["5"] = love.graphics.newImage ("pictures/tile5.png")
  22. imgTiles["="] = love.graphics.newImage ("pictures/tile=.png")
  23. imgTiles["["] = love.graphics.newImage ("pictures/tile[.png")
  24. imgTiles["]"] = love.graphics.newImage ("pictures/tile].png")
  25. imgTiles["g"] = love.graphics.newImage ("pictures/tileg.png")
  26. imgTiles["H"] = love.graphics.newImage ("pictures/tileH.png")
  27. imgTiles["#"] = love.graphics.newImage ("pictures/tile#.png")
  28. imgTiles["<"] = love.graphics.newImage ("pictures/tile-arrow-left.png")
  29. imgTiles[">"] = love.graphics.newImage ("pictures/tile-arrow-right.png")
  30. imgTiles["#"] = love.graphics.newImage ("pictures/tile#.png")
  31. imgTiles["c"] = love.graphics.newImage ("pictures/coin/coin1.png")
  32. -- Image du héros
  33.  
  34. imgPlayer = love.graphics.newImage ("pictures/player/idle1.png")
  35.  
  36. -- Petite musique pour s'ambiancer
  37.  
  38. OstPlatformer = love.audio.newSource ("Audio/laidback.ogg.ogg","stream")
  39.  
  40. -- La map avec les niveaux et les sprites
  41.  
  42. local map = {}
  43. local levels = {}
  44. local currentLevel = 0
  45. local lstSprites = {}
  46. local player = nil
  47. -- CONSTANTES
  48.  
  49. local TILESIZE = 16 --Dimensions des tuiles
  50. local bJumpReady -- si le joueur est prêt à sauter ou non
  51. local GRAVITY = 1
  52.  
  53.  
  54. function ChargeNiveau (pNum)
  55.   -- sécurité au cas où on dépasse l'index de niveau
  56.  
  57.   if pNum > #levels then
  58.     print ("There is no level "..tostring(pNum)..".txt")
  59.   end
  60.  
  61.   currentLevel = pNum
  62.  map = {}
  63.  local filename = "levels/level"..tostring(pNum)..".txt" -- iterator qui permet de charger ses maps via un fichier txt
  64.  for line in love.filesystem.lines (filename) do
  65.    map [#map + 1 ] = line
  66.  end
  67.  
  68.  -- Look for the sprites in map/ On charge les paramètres des sprites sur la map
  69.     lstSprites = {}
  70.     level = {}
  71.     level.PlayerStart = {}
  72.     level.PlayerStart.col = 5
  73.     level.PlayerStart.lig = 14
  74.    
  75.     for l = 1, #map do
  76.       for c = 1, #map[1] do
  77.         local char = string.sub (map[l], c, c )
  78.         if char == "P" then
  79.           level.PlayerStart.col = c
  80.           level.PlayerStart.lig = l
  81.           player = CreatePlayer (c,l)
  82.         elseif char == "c" then
  83.         CreateCoin (c, l)
  84.    
  85.         end
  86.       end
  87.     end
  88.  end  
  89. function isSolid (pID)
  90.  
  91.   -- sert à déterminer si il y a collision ou non
  92.  
  93.   if pID == "0" then return false
  94.   elseif pID == "1" then return true end
  95.   return false
  96. end
  97.  
  98. function isJumpThrough (pID)
  99.  
  100.   if pID == "2" then return true end
  101.   return false
  102.  
  103.   end
  104.  
  105. function isLadder (pID)
  106.   if pID == "H" then return true
  107.   elseif pID == "#" then return true end
  108.  
  109.   return false
  110.   end
  111.  
  112.  
  113. function CreateSprite (pType,pX, pY)
  114.  
  115.   --factory/usine à sprite pour les créer
  116.  
  117.   mySprite = {}
  118.   mySprite.x = pX
  119.   mySprite.y = pY
  120.   mySprite.vx = 0
  121.   mySprite.vy = 0
  122.   mySprite.type = pType
  123.   mySprite.gravity = 0
  124.   mySprite.isJumping = false
  125.   mySprite.standing = false
  126.   mySprite.flip = false-- pour tourner l'image à gauche sans ajouter d'images
  127.  
  128.   -- Animations des sprites
  129.   mySprite.currentAnimation = ""
  130.   mySprite.frame = 0
  131.   mySprite.animationSpeed = 1/8
  132.   mySprite.animationTimer = mySprite.animationSpeed
  133.   mySprite.Animations = {}
  134.   mySprite.images = {}
  135.  
  136.   mySprite.AddImages = function (psDir,plstImages) -- fonction qui va ajouter les images des sprites
  137.    
  138.     for k,v in pairs (plstImages) do      
  139.       local fileName = psDir.."/"..v..".png"
  140.       mySprite.images [v] = love.graphics.newImage (fileName)
  141.      
  142.     end
  143.   end  
  144.   mySprite.AddAnimation = function(psDir, psName, plstImages)
  145.     mySprite.AddImages(psDir, plstImages)
  146.     mySprite.Animations[psName] = plstImages
  147.   end
  148.  
  149.   mySprite.PlayAnimations = function (psName) -- fonction qui va jouer les animations
  150.    
  151.     if mySprite.currentAnimation ~= psName then
  152.       mySprite.currentAnimation = psName
  153.       mySprite.frame = 1
  154.     end
  155.    
  156.   end
  157.   table.insert (lstSprites, mySprite )
  158.  
  159.   return mySprite
  160.  
  161. end
  162.  
  163. function CreatePlayer(pCol, pLig)
  164.   local myPlayer = CreateSprite("player", (pCol-1) * TILESIZE, (pLig-1) * TILESIZE)
  165.   myPlayer.gravity = 500
  166.   myPlayer.AddAnimation("pictures/player", "idle", { "idle1", "idle2", "idle3", "idle4" })
  167.   myPlayer.AddAnimation("pictures/player", "run", { "run1", "run2", "run3", "run4", "run5", "run6", "run7", "run8", "run9", "run10" })
  168.   myPlayer.AddAnimation("pictures/player", "climb", { "climb1", "climb2" })
  169.   myPlayer.AddAnimation("pictures/player", "climb_idle", { "climb1" })
  170.   myPlayer.PlayAnimation("idle")
  171.   bJumpReady = true
  172.   return myPlayer
  173. end
  174.  
  175. function CreateCoin (pcol, plig )
  176.   local myCoin = CreateSprite ("coin", (pcol -1) *TILESIZE, (plig-1) *TILESIZE )
  177.   myCoin.AddAnimation ("pictures/coin", "idle", { "coin1", "coin2", "coin3", "coin4" } )
  178.   myCoin.PlayAnimations ("idle")
  179.   end
  180. function InitGame(pNiveau)
  181.  
  182.   -- Initialisation du niveau
  183.  
  184.   ChargeNiveau(pNiveau)
  185.  
  186.   end
  187.  
  188.  
  189. function love.load()
  190.   love.window.setTitle("Mini platformer de BladeRED 2019")
  191. love.audio.play (OstPlatformer)
  192. OstPlatformer:isLooping (true)
  193.   Width = love.graphics.getWidth()
  194.   Height = love.graphics.getHeight()
  195.   print ("Chargement du niveau")
  196.   InitGame (1)
  197.   print ("Chargement terminé")
  198. end
  199.  
  200. function AlignOnColumn (pSprite)
  201.   local col = math.floor ((pSprite.x + TILESIZE/2 ) / TILESIZE) + 1  -- determine la position en colonne du sprite lors de la collision
  202.     pSprite.x = (col -1)*TILESIZE  -- position du sprite en x post-collision
  203. end
  204.  
  205. function AlignOnLine (pSprite)
  206.    local lig = math.floor ((pSprite.y + TILESIZE/2 ) / TILESIZE) + 1  -- determine la position en ligne du sprite lors de la collision
  207.       pSprite.y = (lig -1)*TILESIZE -- position du sprite en y post-collision
  208.   end
  209.  
  210. function updatePlayer(pPlayer, dt)
  211.   -- mise à jour du sprite Player
  212.   -- Locales pour la physique du sprite player
  213.   local accel = 350
  214.   local friction = 120
  215.   local maxSpeed = 120
  216.   local jumpSpeed = -225
  217.  
  218.  
  219.   -- Tile under the player / Tuile sous le joueur (pour les échelles)
  220.   local idUnder = getTileAt (pPlayer.x + TILESIZE/2, pPlayer.y + TILESIZE)
  221.   local idOverlap= getTileAt (pPlayer.x + TILESIZE/2, pPlayer.y + TILESIZE - 1 )
  222.  
  223.   -- Stop, Jump ? / Est-ce que le héros peut sauter ou il y a une échelle au-dessus ?
  224.  
  225.   if pPlayer.isJumping and (CollideBelow(pPlayer) or isLadder(idUnder)) then
  226.       pPlayer.isJumping = false
  227.       pPlayer.standing = true
  228.       AlignOnLine (pPlayer)
  229.       end
  230.  
  231.   -- Gestion de Friction
  232.  
  233.   -- Friction en allant à droite
  234.  if pPlayer.vx > 0 then
  235.    pPlayer.vx = pPlayer.vx - friction*dt
  236.    if pPlayer.vx < 0 then
  237.      pPlayer.vx = 0
  238.    end
  239.   end
  240.    -- Friction en allant à gauche
  241.    
  242.    if pPlayer.vx < 0 then
  243.      pPlayer.vx = pPlayer.vx + friction*dt
  244.      if pPlayer.vx > 0 then
  245.        pPlayer.vx = 0
  246.        end
  247.    end
  248.  
  249.   local newAnimation = "idle" -- animation de départ du héros
  250.  
  251.   --Keyboard
  252.  
  253.   if love.keyboard.isDown("right") then
  254.    
  255.     pPlayer.vx = pPlayer.vx + accel*dt
  256.     if pPlayer.vx > maxSpeed then pPlayer.vx = maxSpeed end
  257.     pPlayer.flip = false
  258.     newAnimation = "run"
  259.     end
  260.    
  261.     if love.keyboard.isDown("left") then
  262.      
  263.       pPlayer.vx = pPlayer.vx - accel*dt
  264.     if pPlayer.vx < - maxSpeed then pPlayer.vx = - maxSpeed end
  265.     pPlayer.flip = true
  266.     newAnimation = "run"
  267.    
  268.     end
  269.    
  270.    -- Check if the player overlap a ladder / On vérifie avant de sauter si le héros chevauche une échelle
  271.    
  272.    local isOnLadder = isLadder(idUnder) or isLadder (idOverlap)
  273.    
  274.    if isLadder(idOverlap) == false and isLadder(idUnder) then
  275.        pPlayer.standing = true
  276.        
  277.        end
  278.    -- Climb
  279.    
  280.    if isOnLadder and pPlayer.isJumping == false then
  281.      GRAVITY = 0
  282.      pPlayer.vy = 0
  283.      bJumpReady = false
  284.      
  285.    end
  286.    
  287.    -- si on est au milieu de l'échelle
  288.    
  289.    if isLadder (idUnder) and isLadder (idOverlap) then
  290.      newAnimation ="climb_idle"
  291.      end
  292.     -- si on monte à l'échelle
  293.    
  294.     if love.keyboard.isDown "up" and isOnLadder == true and pPlayer.isJumping == false then
  295.       pPlayer.vy =  -50
  296.       newAnimation ="climb"
  297.       end
  298.    -- Jump
  299.     if love.keyboard.isDown("up") and pPlayer.standing and bJumpReady and isLadder(idOverlap) == false then
  300.       pPlayer.isJumping = true
  301.       pPlayer.vy = jumpSpeed
  302.       pPlayer.standing = false
  303.       bJumpReady = false
  304.     end
  305.    
  306.     --si on descend de l'échelle
  307.    
  308.     if love.keyboard.isDown("down") and isOnLadder == true then
  309.     pPlayer.vy = 50
  310.     newAnimation = "climb"
  311.   end
  312.   -- Not climbing
  313.   if isOnLadder == false and GRAVITY == 0 and pPlayer.isJumping == false then
  314.     GRAVITY = 500
  315.   end
  316.   -- On prépare le prochain saut
  317.     if love.keyboard.isDown("up") == false and bJumpReady == false and pPlayer.standing == true then
  318.       bJumpReady = true
  319.     end
  320.    
  321.     pPlayer.PlayAnimations (newAnimation)
  322.     --Move
  323.    
  324.     pPlayer.x = pPlayer.x + pPlayer.vx* dt
  325.     pPlayer.y = pPlayer.y + pPlayer.vy* dt
  326.    
  327.    
  328.  
  329. end
  330.  
  331. function getTileAt(pX, pY)
  332.  
  333.   -- permet de retourner l'ID de la tile à la coordonnée indiqué
  334.  
  335.   local col = math.floor(pX / TILESIZE) + 1
  336.   local lig = math.floor(pY / TILESIZE) + 1
  337.   if col>0 and col<=#map[1] and lig>0 and lig<=#map then
  338.     local id = string.sub(map[lig],col,col)
  339.     return id
  340.   end
  341.   return 0
  342. end
  343.  
  344.  
  345. -- Fonctions qui détermine les collisions selon le côté du sprite
  346. function CollideRight(pSprite)
  347.   local id1 = getTileAt(pSprite.x + TILESIZE, pSprite.y + 3)
  348.   local id2 = getTileAt(pSprite.x + TILESIZE, pSprite.y + TILESIZE - 2)
  349.   if isSolid(id1) or isSolid(id2) then return true end
  350.   return false
  351. end
  352.  
  353. function CollideLeft(pSprite)
  354.   local id1 = getTileAt(pSprite.x-1, pSprite.y + 3)
  355.   local id2 = getTileAt(pSprite.x-1, pSprite.y + TILESIZE - 2)
  356.   if isSolid(id1) or isSolid(id2) then return true end
  357.   return false
  358. end
  359.  
  360. function CollideBelow(pSprite)
  361.   local id1 = getTileAt(pSprite.x + 1, pSprite.y + TILESIZE)
  362.   local id2 = getTileAt(pSprite.x + TILESIZE - 2, pSprite.y + TILESIZE)
  363.   if isSolid(id1) or isSolid(id2) then return true end
  364.  
  365.  
  366.     if isJumpThrough (id1) or isJumpThrough (id2) then
  367.     local lig = math.floor ((pSprite.y + TILESIZE/2 ) / TILESIZE ) + 1
  368.     local yLine = (lig-1) * TILESIZE
  369.     local distance = pSprite.y - yLine
  370.       if distance > 0 and distance < 10 then
  371.       return true
  372.     end
  373. end
  374. return false
  375. end
  376. function CollideAbove(pSprite)
  377.   local id1 = getTileAt(pSprite.x + 1, pSprite.y-1)
  378.   local id2 = getTileAt(pSprite.x + TILESIZE - 2, pSprite.y-1)
  379. if isSolid(id1) or isSolid(id2) then return true end
  380.   return false
  381. end
  382.  
  383. function updateSprite (pSprite, dt)
  384.   --mise à jour des sprites
  385.  
  386.   -- Locales pour les collisons
  387.  
  388.   oldX = pSprite.X
  389.   oldY = pSprite.Y
  390.  
  391.   -- Si le sprite est Player
  392.  
  393.   if pSprite.type == "player" then
  394.     updatePlayer(pSprite, dt)
  395.   end
  396.   -- Animations
  397.  
  398.   if pSprite.currentAnimation ~= "" then
  399.     pSprite.animationTimer = pSprite.animationTimer - dt
  400.    
  401.     if pSprite.animationTimer <= 0 then
  402.       pSprite.frame = pSprite.frame + 1
  403.       pSprite.animationTimer= pSprite.animationSpeed
  404.      
  405.       if pSprite.frame > #pSprite.Animations[pSprite.currentAnimation] then
  406.         pSprite.frame = 1
  407.         end
  408.     end  
  409.   end
  410.  
  411.   --Collision detection
  412.  
  413.   local collide = false
  414.  
  415.   -- On the right/ A droite
  416.  
  417.   if pSprite.vx > 0 then
  418.     collide = CollideRight (pSprite)
  419.     end
  420.  
  421.   -- On the left/ A gauche
  422.  
  423.   if pSprite.vx < 0 then
  424.     collide = CollideLeft (pSprite)
  425.     end
  426.   -- Stop !
  427.  
  428.   if collide then
  429.    
  430.     pSprite.vx = 0
  431.     AlignOnColumn (pSprite)
  432.    
  433.     end
  434.     collide = false
  435.    
  436.   --Above/ Au-dessus
  437.   if pSprite.vy < 0 then
  438.     collide = CollideAbove (pSprite)
  439.     if collide then
  440.       pSprite.vy = 0
  441.       local lig = math.floor ((pSprite.y + TILESIZE/2 ) / TILESIZE) + 1  -- determine la position en ligne du sprite lors de la collision
  442.       pSprite.y = (lig -1)*TILESIZE -- position du sprite en y post-collision
  443.       end
  444.     end
  445.  
  446.   --Below/ En dessous (chute)
  447.   if pSprite.standing or pSprite.vy > 0 then
  448.     collide = CollideBelow (pSprite)
  449.     if collide then
  450.       pSprite.standing = true
  451.       pSprite.vy = 0
  452.      AlignOnLine (pSprite)
  453.      
  454.     else
  455.       pSprite.standing = false
  456.       end
  457.     end
  458.    
  459.     -- Sprite falling / mouvement de chute du sprite
  460.    
  461.     if pSprite.standing == false then
  462.       pSprite.vy = pSprite.vy + GRAVITY * dt
  463.       end
  464.   end
  465.  
  466. function love.update(dt)
  467.  
  468. for nSprite = #lstSprites, 1,  -1 do
  469.   local sprite = lstSprites [nSprite]
  470.   updateSprite (sprite, dt )
  471.   end
  472. end
  473.  
  474. function drawSprite (pSprite)
  475.  
  476. local imgName = pSprite.Animations [pSprite.currentAnimation][pSprite.frame]
  477. local img = pSprite.images [imgName]
  478. local halfwdth = img:getWidth() / 2
  479. local halfhght = img:getHeight() / 2
  480. local flipcoef = 1
  481. if pSprite.flip then flipcoef = -1 end
  482.  
  483. love.graphics.draw (img , pSprite.x + halfwdth, pSprite.y + halfhght, 0, 1*flipcoef, 1, halfwdth, halfhght )
  484.  
  485. end
  486.  
  487. function love.draw()
  488.  
  489.   -- scale x2 de l'écran
  490.  
  491.   love.graphics.scale(2,2)
  492.   -- On parcourt la map et on la dessine
  493.   local l,c
  494.  
  495.   for l = 1, #map do
  496.     for c = 1, #map[1] do
  497.       local char = string.sub (map[l], c,c)
  498.       if tonumber(char) ~= 0 then
  499.         if imgTiles[char] ~= nil then
  500.           love.graphics.draw(imgTiles[char],(c-1)*TILESIZE,(l-1)*TILESIZE)
  501.         end
  502.       end
  503.   end  
  504.  end
  505. -- On parcourt la liste des sprites et on applique la fonction qui les dessine
  506.  for nSprite = #lstSprites, 1,  -1 do
  507.   local sprite = lstSprites [nSprite]
  508.   drawSprite (sprite )
  509.   end
  510.  --Affiche l'ID de la tuile avec la souris
  511.  local id = getTileAt(love.mouse.getX(), love.mouse.getY())
  512. love.graphics.print(id)
  513.  
  514. end
  515.  
  516. function love.keypressed(key)
  517.  
  518.   print(key)
  519.  
  520. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement