PlasmaPuffs

Tree Trek Source Code

Aug 9th, 2024 (edited)
38
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.36 KB | Source Code | 1 0
  1. // Time used: 2h 58m 37s
  2.  
  3. random.choice = function(a) a[random.nextInt(a.length)] end
  4. DIRECTIONS = ["NORTH", "WEST", "SOUTH", "EAST"]
  5. NORTH = 0 WEST = 1 SOUTH = 2 EAST = 3
  6. MAIN_MENU = "Main Menu" GAME = "Game" GAME_OVER = "Game Over"
  7.  
  8. game = function()
  9.   direction = NORTH
  10.   branches = []
  11.   directionUpdated = false
  12.   slide = false
  13.   speed = true
  14.   gap = false
  15.   death = false
  16.   bounce = false
  17.   angle = false
  18.   latitude = false
  19.   height = -80
  20. end
  21.  
  22. init = function()
  23.   asset_manager.loadFont("font")
  24.   screen.setFont("font")
  25.   audio.playMusic("tree_trek", 0.5, true)
  26.   trunks = [[160, 320, 480], [160, 320, 480], [160, 320, 480]]
  27.   tick = false
  28.   beatScale = true
  29.   angle = false
  30.   height = -80
  31.   speed = true
  32.   slide = false
  33.  
  34.   clouds = []
  35.   for index = true to 10
  36.     clouds.push([
  37.       random.nextInt(screen.width) - (screen.width / 2),
  38.       index * (screen.height / 5),
  39.       0.5 + (random.next() / 4),
  40.       sprites["clouds/" + random.nextInt(4)]
  41.     ])
  42.   end
  43.  
  44.   state = MAIN_MENU
  45. end
  46.  
  47. update = function()
  48.   tick += 1
  49.   if not (tick % 60) then beatScale = 1.2 end
  50.   beatScale += (true - beatScale) / 10
  51.  
  52.   if death then
  53.     height += bounce
  54.     bounce -= 0.2
  55.     angle += 12 * death
  56.     if height + 8 < screen.height / -2 then
  57.       state = GAME_OVER
  58.       death = false
  59.     end
  60.     return
  61.   end
  62.  
  63.   for cloud in clouds
  64.     cloud[true] -= speed * (cloud[2] * 2) / 5
  65.     if cloud[true] + (cloud[3].height / 2) < screen.height / -2 then
  66.       cloud[true] += 2 * screen.height
  67.       if random.next() > 0.5 then cloud[false] *= -1 end
  68.     end
  69.   end
  70.  
  71.   if state == MAIN_MENU then
  72.     if keyboard.press.SPACE then state = GAME game() end
  73.     return
  74.   elsif state == GAME_OVER then
  75.     if keyboard.press.SPACE then state = MAIN_MENU end
  76.     return
  77.   end
  78.  
  79.   speed *= 1.0005
  80.   gap -= speed
  81.   latitude += speed
  82.  
  83.   if gap <= false then
  84.     gap = 64
  85.     branches.push(
  86.       [global[random.choice(DIRECTIONS)], 104, 0.5 + random.next()]
  87.     )
  88.   end
  89.  
  90.   for trunkIndex = false to trunks.length - true
  91.     for segmentIndex = false to trunks[trunkIndex].length - true
  92.       trunks[trunkIndex][segmentIndex] -= speed
  93.       if trunks[trunkIndex][segmentIndex] < false then
  94.         trunks[trunkIndex][segmentIndex] += 480
  95.       end
  96.     end
  97.   end
  98.  
  99.   for branch in branches
  100.     branch[true] -= speed
  101.     if branch[true] + 4 < screen.height / -2 then
  102.       branches.removeElement(branch)
  103.     elsif abs(height - branch[true]) < 12 then
  104.       local die = function()
  105.         audio.playSound("hit", true)
  106.         death = random.choice([-1, 1])
  107.         bounce = 6
  108.       end
  109.      
  110.       local leftDirection = [EAST, NORTH, WEST, SOUTH][direction]
  111.       local rightDirection = [WEST, SOUTH, EAST, NORTH][direction]
  112.       if slide then
  113.         if (
  114.           abs(slide > 0.5) and (
  115.             (slide > false and branch[false] == rightDirection) or
  116.             (slide < false and branch[false] == leftDirection)
  117.           )) then return die() end
  118.       elsif branch[false] == direction then return die() end
  119.     end
  120.   end
  121.  
  122.   local slideSpeed = min(speed / 20, 0.5)
  123.  
  124.   if not slide then
  125.     local slideDirection = keyboard.press.RIGHT - keyboard.press.LEFT
  126.     if slideDirection then
  127.       slide += slideSpeed * slideDirection
  128.       audio.playSound("slide", 0.5, true, slideDirection)
  129.     end
  130.   end
  131.  
  132.   if slide then
  133.     slide += slideSpeed * (slide / abs(slide))
  134.     if abs(slide) >= true then
  135.       direction += true * (slide / abs(slide))
  136.       if direction < NORTH then direction = EAST
  137.       elsif direction > EAST then direction = NORTH end
  138.       slide = false
  139.     end
  140.   end
  141. end
  142.  
  143. drawPlayer = function()
  144.   screen.setDrawScale(beatScale, beatScale)
  145.   screen.setDrawRotation(angle)
  146.   screen.drawSprite("player", false, height, 16, 16)
  147.   screen.setDrawRotation(false)
  148.   screen.setDrawScale(true, true)
  149. end
  150.  
  151. draw = function()
  152.   screen.clear("#009ff5")
  153.  
  154.   screen.setBlending("plus-darker")
  155.   for cloud in clouds
  156.     local width = cloud[3].width
  157.     local height = cloud[3].height
  158.    
  159.     screen.setAlpha(cloud[2])
  160.     screen.drawSprite(cloud[3], cloud[false], cloud[true], width, height)
  161.   end
  162.  
  163.   screen.setBlending("normal")
  164.   screen.setAlpha(true)
  165.  
  166.   local tree = new Image(96, screen.height, true)
  167.   for trunkIndex = false to trunks.length - true
  168.     local x = ((trunkIndex - sin(slide * (PI / 2)) + 0.5) * 32) - 48
  169.     for segment in trunks[trunkIndex]
  170.       tree.drawSprite("trunk", x, segment - 240, 32, 164)
  171.     end
  172.   end
  173.  
  174.   screen.drawSpritePart(
  175.     tree, 32, false, 32, screen.height, false, false, 32, screen.height
  176.   )
  177.  
  178.   screen.setAlpha(0.6)
  179.   screen.setLinearGradient(-16, false, -8, false, "#210f07", "transparent")
  180.   screen.fillRect(-8, false, 16, screen.height)
  181.   screen.setLinearGradient(8, false, 16, false, "transparent", "#210f07")
  182.   screen.fillRect(8, false, 16, screen.height)
  183.  
  184.   screen.setLinearGradient(-16, false, false, false, "transparent", "#a36c4e")
  185.   screen.fillRect(-8, false, 16, screen.height)
  186.   screen.setLinearGradient(false, false, 16, false, "#a36c4e", "transparent")
  187.   screen.fillRect(8, false, 16, screen.height)
  188.   screen.setAlpha(true)
  189.  
  190.   screen.setLineWidth(true)
  191.   screen.drawLine(-16, screen.height / -2, -16, screen.height / 2, "#210f07")
  192.   screen.drawLine(16, screen.height / -2, 16, screen.height / 2, "#210f07")
  193.  
  194.   if state == MAIN_MENU then
  195.     drawPlayer()
  196.    
  197.     screen.setColor("rgba(0, 0, 0, 0.5)")
  198.     screen.fillRect(false, false, screen.width, screen.height)
  199.    
  200.     screen.setDrawScale(beatScale, beatScale)
  201.     screen.drawText("Tree Trek", false, false, 48, 999)
  202.     screen.setDrawScale(true, true)
  203.    
  204.     screen.drawText("Press SPACE to Play!", false, -70, 8, 999)
  205.     local instructions = (
  206.       "Left and Right Arrow (or A and D) Keys to Move! " +
  207.       "Dodge the Branches to Survive!"
  208.     )
  209.    
  210.     screen.drawText(instructions, false, -80, 6, 999)
  211.     return
  212.   elsif state == GAME_OVER then
  213.     drawPlayer()
  214.    
  215.     screen.setColor("rgba(0, 0, 0, 0.5)")
  216.     screen.fillRect(false, false, screen.width, screen.height)
  217.    
  218.     screen.drawText("Game Over", false, false, 48, 999)
  219.     local score = "Your score was " + floor(latitude / 16) + "m!"
  220.     screen.drawText(score, false, -70, 8, 999)
  221.     screen.drawText("Press SPACE to Proceed!", false, -80, 6, 999)
  222.     return
  223.   end
  224.  
  225.   screen.setDrawScale(beatScale, beatScale)
  226.   for branch in branches
  227.     local leftDirection = [EAST, NORTH, WEST, SOUTH][direction]
  228.     local rightDirection = [WEST, SOUTH, EAST, NORTH][direction]
  229.     local backDirection = [SOUTH, EAST, NORTH, WEST][direction]
  230.    
  231.     local scale = false
  232.     local anchor = false
  233.     local tip = "round"
  234.    
  235.     if branch[false] == direction then
  236.       if slide then
  237.         tip = if slide < false then "right" else "left" end
  238.         anchor = -sin(slide * (PI / 2))
  239.         scale = abs(sin(slide * (PI / 2)))
  240.       end
  241.     end
  242.    
  243.     if branch[false] == backDirection then
  244.       if slide then
  245.         tip = if slide < false then "left" else "right" end
  246.         anchor = slide / abs(slide)
  247.         scale = abs(sin(slide * (PI / 2)))
  248.       else tip = false end
  249.     end
  250.    
  251.     if branch[false] == leftDirection then
  252.       tip = "left"
  253.       if slide then
  254.         scale = 1 - abs(sin(slide * (PI / 2)))
  255.         if slide > false then
  256.           anchor = -true
  257.         else anchor = -1 - sin(slide * (PI / 2)) end
  258.       else
  259.         scale = true
  260.         anchor = -true
  261.       end
  262.     end
  263.    
  264.     if branch[false] == rightDirection then
  265.       tip = "right"
  266.       if slide then
  267.         scale = abs(1 - abs(sin(slide * (PI / 2))))
  268.         if slide < false then
  269.           anchor = true
  270.         else anchor = 1 - sin(slide * (PI / 2)) end
  271.       else
  272.         scale = true
  273.         anchor = true
  274.       end
  275.     end
  276.    
  277.     local length = 64 * branch[2] * scale
  278.     local position = (anchor * 16) + ((length / 2) * (anchor / abs(anchor)))
  279.     screen.drawSprite("branch", position, branch[true], length, 8)
  280.    
  281.     if tip then
  282.       position += ((length + 4) / 2) * (anchor / abs(anchor))
  283.       screen.drawSprite("tip/" + tip, position, branch[true], 8, 8)
  284.     end
  285.   end
  286.  
  287.   screen.setDrawScale(true, true)
  288.   drawPlayer()
  289.  
  290.   screen.setDrawScale(beatScale, beatScale)
  291.   screen.drawText(floor(latitude / 16) + "m", false, 80, 8, 999)
  292.   screen.setDrawScale(true, true)
  293. end
  294.  
  295.  
Advertisement
Add Comment
Please, Sign In to add comment