You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 rivejä
6.8 KiB

  1. life = Class{}
  2. function life:init( y , x , fat, undead)
  3. self.RED = 255
  4. self.GREEN = 255
  5. self.BLUE = 255
  6. self.x = x
  7. self.y = y
  8. self.width = fat
  9. self.height = fat
  10. self.fat = fat
  11. self.dead = false
  12. self.multiplycounter = 1
  13. self.closest = 999999
  14. self.childrenCount = 0
  15. self.childThreshold = math.random(30, 100)
  16. self.childMaxThreshold = math.random(1, 5)
  17. self.hasRandomTarget = false
  18. self.sx = x
  19. self.sy = y
  20. self.hasPrey = nil
  21. self.lifeSpan = math.random(50, 100)
  22. if undead then
  23. self.lifeSpan = undead
  24. end
  25. end
  26. function life:update(dt)
  27. if not self.dead then
  28. self.lifeSpan = self.lifeSpan - dt*0.7
  29. print("LIFE: " .. self.lifeSpan)
  30. if self.fat <= 0 or self.lifeSpan <= 0 then
  31. self.dead = true
  32. end
  33. self.fat = self.fat - self.width*0.004*dt
  34. if not self:checkIfEating() then
  35. self:lookForFood(dt)
  36. end
  37. if self.width > self.childThreshold and self.childrenCount < self.childMaxThreshold then
  38. table.insert(bugs, life(self.y+self.width, self.x+self.width, self.fat/2+5))
  39. self.width = self.width/2
  40. self.fat = self.fat/2-5
  41. self.childrenCount = self.childrenCount + 1
  42. if self.childrenCount == 1 then
  43. self.RED = 0.2
  44. end
  45. if self.childrenCount == 2 then
  46. self.RED = 0.4
  47. self.GREEN = 0.1
  48. end
  49. if self.childrenCount == 3 then
  50. self.RED = 0.6
  51. self.GREEN = 0.2
  52. end
  53. if self.childrenCount == 4 then
  54. self.RED = 0.8
  55. self.GREEN = 0.3
  56. self.BLUE = 0.1
  57. end
  58. if self.childrenCount == 5 then
  59. self.RED = 1
  60. self.GREEN = 0.7
  61. self.BLUE = 0.5
  62. end
  63. end
  64. end
  65. print(self.fat)
  66. end
  67. function life:starving()
  68. return self.fat < self.width * 0.5
  69. end
  70. function life:findPrey()
  71. if self.hasPrey == nil then
  72. closestPrey = 999999
  73. for i, prey in ipairs(bugs) do
  74. dist = math.sqrt((self.x - prey.x)^2 + (self.y-prey.y)^2)
  75. if prey.width < self.width and prey ~= self and dist < closestPrey then
  76. self.sx, self.sy = prey.x, prey.y
  77. closestPrey = dist
  78. print("going to " .. i)
  79. self.hasPrey = prey
  80. end
  81. end
  82. else
  83. if self.hasPrey.dead then
  84. self.hasPrey = nil
  85. return
  86. end
  87. self.sx, self.sy = self.hasPrey.x, self.hasPrey.y
  88. print("going to " .. self.sx .. self.sy)
  89. if math.abs(self.x-self.hasPrey.x) < self.width/2 and math.abs(self.y-self.hasPrey.y) < self.width/2 then
  90. if (math.random(0, 30) + self.width > math.random(0, 30) + self.hasPrey.width) then
  91. self.fat = self.hasPrey.fat + self.fat - 5
  92. self.hasPrey.fat = 5
  93. self.hasPrey.dead = true
  94. self.hasPrey = nil
  95. else
  96. self.dead = true
  97. end
  98. end
  99. end
  100. end
  101. function life:lookForFood(dt)
  102. self.closest = 9000000
  103. print("looking for food...")
  104. for i, food in ipairs(edibles) do
  105. dist = math.sqrt((self.x - food.x)^2 + (self.y-food.y)^2)
  106. if dist < self.closest and dist < self.fat*20 and self.hasPrey == nil then
  107. self.closest = math.sqrt((self.x - food.x)^2 + (self.y-food.y)^2)
  108. self.sx, self.sy = food.x, food.y
  109. print("going to " .. i)
  110. self.hasRandomTarget = true
  111. elseif not self.hasRandomTarget then
  112. if (self:starving()) then
  113. self:findPrey()
  114. else
  115. self.sx, self.sy = math.floor(math.random(0, VIRTUAL_WIDTH)), math.floor(math.random(0, VIRTUAL_HEIGHT))
  116. print(self.sx)
  117. self.hasRandomTarget = true
  118. end
  119. end
  120. end
  121. if self.hasRandomTarget and math.abs(self.x - self.sx) < 5 and math.abs(self.y-self.sy) < 5 then
  122. print("going random" .. self.sx.. " " .. self.sy)
  123. self.sx, self.sy = math.floor(math.random(0, VIRTUAL_WIDTH)), math.floor(math.random(0, VIRTUAL_HEIGHT))
  124. self.hasRandomTarget = false
  125. end
  126. self:goTo(self.sx, self.sy, dt)
  127. end
  128. function life:goTo(x, y, dt)
  129. local factor = 1
  130. if self.hasPrey ~= nil then
  131. factor = 3
  132. end
  133. self.speed = (10 + self.fat * 0.5 ) * factor
  134. if (x-self.x) ~= 0 then
  135. self.m = (y-self.y)/(x-self.x)
  136. if y-self.y > 0 then
  137. self.m = math.abs(self.m)
  138. elseif y-self.y < 0 and self.m > 0 then
  139. self.m = -self.m
  140. end
  141. else
  142. if y-self.y > 0 then
  143. self.m = 1
  144. elseif y-self.y < 0 then
  145. self.m = -1
  146. else
  147. self.m = 0
  148. end
  149. end
  150. if self.m > 1 or self.m < -1 then
  151. self.speed = math.abs(self.speed / (self.m))
  152. end
  153. if (self.x < x) then
  154. self.x = self.x + self.speed * dt
  155. self.y = self.y + (self.speed* self.m * dt )
  156. print("go right" .. self.fat ..x .. " " ..y )
  157. elseif self.x > x then
  158. self.x = self.x - self.speed* dt
  159. self.y = self.y + (self.speed* self.m * dt )
  160. print("go left" .. self.fat ..x .. " " ..y)
  161. else
  162. self.y = self.y + (self.speed* self.m * dt )
  163. print("go vertical" .. self.fat ..x .. " " ..y)
  164. end
  165. --if (self.y < y) then
  166. -- self.y = self.y + (10 * dt + self.fat * 0.5 * dt)
  167. -- print("go down" .. self.fat)
  168. --elseif self.y > y then
  169. -- print("go up" .. self.fat)
  170. -- self.y = self.y - (10 * dt + self.fat * 0.5 * dt)
  171. --end
  172. self.fat = self.fat - self.fat * self.width * 0.0003 * dt
  173. print(self.m .. " " .. self.speed * self.m)
  174. print((self.speed* self.m * dt ))
  175. end
  176. function life:checkIfEating()
  177. for i, food in ipairs(edibles) do
  178. if math.abs(self.x - food.x) < 10 and math.abs(self.y-food.y) < 10 then
  179. self.fat = self.fat + food.fat
  180. self.width = self.width + food.fat/2
  181. table.remove(edibles, i)
  182. print("yum")
  183. return true
  184. end
  185. end
  186. return false
  187. end
  188. function life:render()
  189. love.graphics.setColor(self.RED, self.GREEN, self.BLUE, self.fat*25/255)
  190. if self.hasPrey ~= nil then
  191. love.graphics.setColor(255, 0, 0, self.fat*3/255)
  192. end
  193. love.graphics.circle('fill', self.x, self.y, self.width)
  194. love.graphics.setColor(255,255,255,255)
  195. love.graphics.printf(math.floor(self.lifeSpan), smallfont, self.x+self.width, self.y, self.width*10, 'left')
  196. --draw line from self to self.sx, self.sy
  197. love.graphics.setColor(255, 0, 0, 255)
  198. love.graphics.line(self.x, self.y, self.sx, self.sy)
  199. love.graphics.setColor(255, 255, 255, 255)
  200. end