Player.lua 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. class "Player"
  2. function Player:Player(screen, playerEntity)
  3. self.screen = screen
  4. self.playerEntity = playerEntity
  5. self.playerPhysics = screen:trackPhysicsChild(playerEntity, PhysicsScreenEntity.ENTITY_TRIPLE_CIRCLE, false, 0.0, 1, 0, false, true, -1)
  6. self.playerSprite = safe_cast(playerEntity:getEntityById("playerSprite", true), ScreenSprite)
  7. self.groundSensor = safe_cast(playerEntity:getEntityById("groundSensor", true), ScreenShape)
  8. self.playerVelocity = 0.0
  9. self.jumping = true
  10. self.numCoins = 0
  11. self.dead = false
  12. self.deadCounter = 0
  13. self.jumpSound = Sound("Resources/sounds/jump.wav")
  14. self.dieSound = Sound("Resources/sounds/die.wav")
  15. end
  16. function Player:Jump()
  17. if self.jumping == false and self.dead == false then
  18. self.playerPhysics:applyImpulse(0.0, -80.0)
  19. self.jumpSound:Play()
  20. end
  21. end
  22. function Player:Die()
  23. if self.dead == true then return end
  24. self.playerSprite.rotation.roll = -90
  25. self.playerSprite.position.y = 10
  26. self.playerPhysics:applyImpulse(0.0, -80.0)
  27. self.dead = true
  28. self.dieSound:Play()
  29. end
  30. function Player:Update(elapsed)
  31. if self.playerEntity.position.y > 400 then
  32. self:Die()
  33. end
  34. if self.dead then
  35. self.deadCounter = self.deadCounter + elapsed
  36. self.playerSprite:playAnimation("idle", 0, false)
  37. self.playerPhysics:setVelocityX(0)
  38. return
  39. end
  40. if self.screen:isEntityColliding(self.groundSensor) then
  41. self.jumping = false
  42. else
  43. self.jumping = true
  44. end
  45. if Services.Input:getKeyState(KEY_LEFT) then
  46. self.playerVelocity = -7.2
  47. self.playerSprite.scale.x = -1
  48. elseif Services.Input:getKeyState(KEY_RIGHT) then
  49. self.playerVelocity = 7.2
  50. self.playerSprite.scale.x = 1
  51. else
  52. self.playerVelocity = 0.0
  53. end
  54. self.playerPhysics:setVelocityX(self.playerVelocity)
  55. if self.jumping == true then
  56. self.playerSprite:playAnimation("jump", 0, false)
  57. else
  58. if self.playerVelocity == 0.0 then
  59. self.playerSprite:playAnimation("idle", 0, false)
  60. else
  61. self.playerSprite:playAnimation("walk", 0, false)
  62. end
  63. end
  64. end