world.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. World = class()
  2. function World:init()
  3. self.player = Player(6, 6)
  4. self.level = 0
  5. self.message = ""
  6. self:nextLevel()
  7. end
  8. function World:nextLevel()
  9. self.grid = Grid()
  10. self.grid:generateLevel(self.player)
  11. self.player:light(self)
  12. self.bullets = {}
  13. self.npcs = {}
  14. self.patterns = {}
  15. self.level = self.level + 1
  16. self.message = "Welcome to level " .. self.level .. "!"
  17. if self.level == 1 then
  18. self.message = self.message .. " Press ? for controls."
  19. end
  20. end
  21. function World:update(key)
  22. self.grid:update(world, key)
  23. self.player:update(world, key)
  24. -- Iterate over things backwards, so that it's easier to remove them
  25. -- without messing with the iterator.
  26. for i = #self.bullets, 1, -1 do
  27. self.bullets[i]:update(world)
  28. if self.bullets[i].dead then
  29. table.remove(self.bullets, i)
  30. end
  31. end
  32. for i = #self.npcs, 1, -1 do
  33. self.npcs[i]:update(world)
  34. if self.npcs[i].dead then
  35. table.remove(self.npcs, i)
  36. end
  37. end
  38. for i = #self.patterns, 1, -1 do
  39. self.patterns[i].pattern:update(world)
  40. if self.patterns[i].pattern.done then
  41. -- breaks between patterns
  42. if self.patterns[i].undead and self.patterns[i].undead > 20 then
  43. table.remove(self.patterns, i)
  44. elseif self.patterns[i].undead then
  45. self.patterns[i].undead = self.patterns[i].undead + 1
  46. else
  47. self.patterns[i].undead = 0
  48. end
  49. end
  50. end
  51. for i = #self.npcs, 1, -1 do
  52. self.npcs[i]:testDead(self)
  53. if self.npcs[i].dead then
  54. table.remove(self.npcs, i)
  55. end
  56. end
  57. self.player:checkBullet(world)
  58. self.player:checkNPC(world)
  59. -- TODO: difficulty based on level?
  60. if #self.patterns < 2 and (love.math.random(1, 50) == 1 or #self.patterns == 0) then
  61. self:addRandomEdgePattern()
  62. end
  63. if #self.npcs < math.min(4, self.level / 2 + 1) then
  64. self:spawnNPC()
  65. end
  66. end
  67. function World:draw(text)
  68. self.grid:draw(text)
  69. for k, v in pairs(self.bullets) do
  70. v:draw(text)
  71. end
  72. for k, v in pairs(self.npcs) do
  73. if self.grid[v.y][v.x].lit then v:draw(text)
  74. end
  75. end
  76. self.player:draw(text)
  77. end
  78. function World:addRandomEdgePattern()
  79. -- Adds a bullet pattern to a random edge that does not already have
  80. -- a pattern.
  81. local edges = {'top', 'bottom', 'left', 'right'}
  82. local pickedEdge = nil
  83. while #edges do
  84. local i = love.math.random(1, #edges)
  85. local edge = edges[i]
  86. local thisEdgeUsed = false
  87. for k, pattern in pairs(self.patterns) do
  88. if pattern.edge == edge then
  89. thisEdgeUsed = true
  90. break
  91. end
  92. end
  93. if thisEdgeUsed then
  94. table.remove(edges, i)
  95. else
  96. pickedEdge = edge
  97. break
  98. end
  99. end
  100. if pickedEdge then
  101. local factory = EDGE_PATTERNS[love.math.random(1, math.min(self.level + 2, #EDGE_PATTERNS))]
  102. self.patterns[#self.patterns + 1] = factory(pickedEdge)
  103. end
  104. end
  105. function World:spawnNPC()
  106. local npc
  107. while true do
  108. npc = Goblin(love.math.random(WIDTH), love.math.random(HEIGHT))
  109. local a = distanceBetweenPoints(npc, self.player)
  110. local b = self.grid[npc.y][npc.x]
  111. if not (b.lit or b.solid) and a > 10 then
  112. break
  113. end
  114. end
  115. self.npcs[#self.npcs + 1] = npc
  116. end
  117. function World:getFullMessage()
  118. local text = ''
  119. if self.player.dead then
  120. text = text .. self.player.deathMessage
  121. text = text .. ' You got to level ' .. self.level
  122. text = text .. ' with ' .. self.player.treasure .. ' treasure.'
  123. text = text .. ' Press R to play again.'
  124. return text
  125. end
  126. if self.player.treasure > 0 then
  127. if self.player.treasure == 69 then
  128. text = text .. '[Treasure: ' .. self.player.treasure .. '. nice] '
  129. else
  130. text = text .. '[Treasure: ' .. self.player.treasure .. '] '
  131. end
  132. end
  133. text = text .. self.message
  134. return text
  135. end