building.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. Building = class()
  2. Building.tag = 'building'
  3. Building.size = 60
  4. function Building:activate()
  5. self.image = data.media.graphics.dinoland['hut' .. love.math.random(1, 2)]
  6. self.alpha = 1
  7. self.personTimer = 6 + love.math.random() * 3
  8. self.destroyed = false
  9. self.justDestroyed = false
  10. self.body = love.physics.newBody(ctx.world, self.x, self.y - self.size / 2, 'dynamic')
  11. self.shape = love.physics.newRectangleShape(self.size, self.size)
  12. self.fixture = love.physics.newFixture(self.body, self.shape)
  13. self.body:setUserData(self)
  14. self.body:setFixedRotation(true)
  15. self.fixture:setCategory(ctx.categories.building)
  16. --self.fixture:setMask(ctx.categories.person, ctx.categories.building, ctx.categories.debris)
  17. self.phlerp = PhysicsInterpolator(self, 'alpha')
  18. ctx.event:emit('view.register', {object = self})
  19. end
  20. function Building:deactivate()
  21. self.body:destroy()
  22. ctx.event:emit('view.unregister', {object = self})
  23. end
  24. function Building:update()
  25. self.phlerp:update()
  26. self.personTimer = timer.rot(self.personTimer, function()
  27. --ctx.enemies:add(Caveman, {x = self.x, y = self.y - 20})
  28. return 6 + love.math.random() * 3
  29. end)
  30. if self.justDestroyed then
  31. for i = 1, 6 do
  32. ctx.enemies:add(Caveman, {x = self.x - self.size / 2 + self.size * love.math.random(), y = self.y, invincible = 1, state = Caveman.panic})
  33. end
  34. self.justDestroyed = false
  35. end
  36. if self.destroyed then
  37. local x, y = self.body:getLinearVelocity()
  38. if (math.abs(x) < 4 and math.abs(y) < 4) or (math.abs(x) > 2000 and math.abs(y) > 2000) then
  39. self.alpha = timer.rot(self.alpha)
  40. if self.alpha < 0 then
  41. ctx.buildings:remove(self)
  42. end
  43. end
  44. end
  45. end
  46. function Building:draw()
  47. local g = love.graphics
  48. local scale = self.size / self.image:getWidth()
  49. local lerpd = self.phlerp:lerp()
  50. local x, y = self.body:getPosition()
  51. local angle = self.body:getAngle()
  52. g.setColor(255, 255, 255, 255 * math.clamp(lerpd.alpha, 0, 1))
  53. g.draw(self.image, x, y, angle, scale, scale, self.image:getWidth() / 2, self.image:getHeight() / 2)
  54. self.phlerp:delerp()
  55. end
  56. function Building:collideWith(other)
  57. if other.tag == 'platform' and self.body:getY() > other.body:getY() then
  58. return false
  59. end
  60. if isa(other, Person) and other.state ~= other.dead and self.destroyed and select(2, self.body:getLinearVelocity()) > 250 then
  61. other:changeState('dead')
  62. return false
  63. elseif isa(other, Person) then
  64. return false
  65. end
  66. if other == ctx.pigeon then
  67. return false
  68. end
  69. return true
  70. end
  71. function Building:destroy()
  72. if self.destroyed then return end
  73. self.destroyed = true
  74. self.justDestroyed = true
  75. self.body:setFixedRotation(false)
  76. self.fixture:setCategory(ctx.categories.debris)
  77. self.fixture:setFriction(0.25)
  78. self.body:applyLinearImpulse(-400 + love.math.random() * 600, -2000 + love.math.random() * -2000)
  79. self.body:setAngularVelocity(-20 + love.math.random() * 40)
  80. ctx.hud:addScore(50, 'building')
  81. ctx.sound:play('wood', function(sound)
  82. sound:setVolume(1)
  83. end)
  84. end
  85. function Building:paused()
  86. self.phlerp:update()
  87. end