building.lua 3.6 KB

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