Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. local self = {}
  2.  
  3.  
  4. self.image = love.graphics.newImage("assets/imgs/ice.png")
  5. self.reflectionImage = love.graphics.newImage("assets/imgs/iceReflection.png")
  6.  
  7. self.angle = 0
  8. self.canvas = love.graphics.newCanvas()
  9.  
  10. self.x = 75
  11. self.xv = 0
  12. self.xv_acceleration = 3000
  13. self.xvMax = 200
  14. self.friction = 0.5
  15.  
  16. self.y = 300
  17. self.oldY = self.y
  18. self.yv = 0
  19.  
  20. self.direction = "left"
  21. self.isDead = false
  22. self.isFalling = false
  23.  
  24.  
  25. function self:draw()
  26. love.graphics.draw(self.canvas, 0, 0, 0, scale, scale)
  27. end
  28.  
  29. function self:update(dt)
  30.  
  31. self:rotationUpdate(dt)
  32.  
  33. -- coords / scale so that enlarging the canvas doesn't displace the self
  34. love.graphics.setCanvas(self.canvas)
  35. love.graphics.clear()
  36. love.graphics.draw(self.image, (round(self.x / scale) * scale) / scale, (round(self.y / scale) * scale) / scale,
  37. self.angle, 1, 1, self.image:getWidth() / 2, self.image:getHeight() / 2)
  38. love.graphics.draw(self.reflectionImage, (round(self.x / scale) * scale) / scale, (round(self.y / scale) * scale) / scale,
  39. self.angle / 4, 1, 1, self.image:getWidth() / 2, self.image:getHeight() / 2)
  40. love.graphics.setCanvas()
  41.  
  42. if love.keyboard.isDown("a") then self.xv = self.xv - self.xv_acceleration * dt end
  43. if love.keyboard.isDown("d") then self.xv = self.xv + self.xv_acceleration * dt end
  44.  
  45. if not love.keyboard.isDown("a") and not love.keyboard.isDown("d") then
  46. self.xv = self.xv * math.pow(self.friction, dt)
  47. end
  48.  
  49. if self.xv > self.xvMax then self.xv = self.xvMax end
  50. if self.xv < -self.xvMax then self.xv = -self.xvMax end
  51.  
  52. self.oldX = self.x
  53. self.oldY = self.y
  54. self.x = self.x + self.xv * dt
  55. self.y = self.y + self.yv * dt
  56.  
  57. self.isFalling = true
  58. for k, platform in ipairs(platforms) do
  59. if platform.y < WH then
  60. if platform:isPlayerOn() then
  61. if self.oldY + self.image:getHeight() * scale / 2 <= platform.oldY then
  62. self.y = platform.y - self.image:getWidth() / 2 * scale
  63. self.yv = 0
  64. self.angle = 0
  65. self.isFalling = false
  66. sounds.jump:play()
  67. end
  68. end
  69. if platform:isPlayerOn() then
  70. if self.oldX + self.image:getWidth() / 2 * scale <= platform.x or
  71. self.oldX - self.image:getWidth() / 2 * scale >= platform.x + platform.image:getWidth() * scale then
  72. self.x = self.oldX
  73. self.xv = 0
  74. end
  75. end
  76. end
  77. end
  78. self.yv = self.yv + 600 * dt
  79.  
  80. if (self.y > WH or self.y + self.image:getHeight() < 0) and not self.isDead then
  81. -- move self off screen | Also removes sound effect when on platform
  82. self.x = -100
  83.  
  84. self.isDead = true
  85. sounds.death:play()
  86. score.updateHighscore()
  87. end
  88.  
  89. if snowman:hasHit() and not snowman.isHit and not self.isDead then
  90. snowman:dwindle()
  91. bonusPopup.poppingOut = true
  92. score.timer = score.timer + 10
  93. sounds.bonus:play()
  94. end
  95. end
  96.  
  97. function self:rotationUpdate(dt)
  98. if love.keyboard.isDown("d") then self.direction = "right" end
  99. if love.keyboard.isDown("a") then self.direction = "left" end
  100.  
  101. if self.isFalling then
  102. if self.direction == "right" then
  103. self.angle = (self.angle + dt * 5) % (math.pi * 2)
  104. else
  105. self.angle = (self.angle - dt * 5) % (math.pi * 2)
  106. end
  107. end
  108. end
  109.  
  110. function self:restart()
  111. self.isDead = false
  112. self.x = 75
  113. self.y = 150
  114. self.xv = 0
  115. self.yv = 0
  116. end
  117.  
  118. return self
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement