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.

135 rivejä
4.5 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.lifeSpan = math.random(50, 100)
  21. if undead then
  22. self.lifeSpan = undead
  23. end
  24. end
  25. function life:update(dt)
  26. if not self.dead then
  27. self.lifeSpan = self.lifeSpan - dt*0.7
  28. print("LIFE: " .. self.lifeSpan)
  29. if self.fat <= 0 or self.lifeSpan <= 0 then
  30. self.dead = true
  31. end
  32. self.fat = self.fat - self.width*0.001*dt
  33. if not self:checkIfEating() then
  34. self:lookForFood(dt)
  35. end
  36. if self.width > self.childThreshold and self.childrenCount < self.childMaxThreshold then
  37. table.insert(bugs, life(self.y+self.width, self.x+self.width, self.fat/2+5))
  38. self.width = self.width/2
  39. self.fat = self.fat/2-5
  40. self.childrenCount = self.childrenCount + 1
  41. end
  42. end
  43. print(self.fat)
  44. end
  45. function life:lookForFood(dt)
  46. self.closest = 9000000
  47. print("looking for food...")
  48. for i, food in ipairs(edibles) do
  49. dist = math.sqrt((self.x - food.x)^2 + (self.y-food.y)^2)
  50. if dist < self.closest and dist < self.fat*20 then
  51. self.closest = math.sqrt((self.x - food.x)^2 + (self.y-food.y)^2)
  52. self.sx, self.sy = food.x, food.y
  53. print("going to " .. i)
  54. self.hasRandomTarget = true
  55. elseif not self.hasRandomTarget then
  56. self.sx, self.sy = math.floor(math.random(0, VIRTUAL_WIDTH)), math.floor(math.random(0, VIRTUAL_HEIGHT))
  57. print(self.sx)
  58. self.hasRandomTarget = true
  59. end
  60. end
  61. if self.hasRandomTarget and math.abs(self.x - self.sx) < 5 and math.abs(self.y-self.sy) < 5 then
  62. print("going random" .. self.sx.. " " .. self.sy)
  63. self.sx, self.sy = math.floor(math.random(0, VIRTUAL_WIDTH)), math.floor(math.random(0, VIRTUAL_HEIGHT))
  64. self.hasRandomTarget = false
  65. end
  66. self:goTo(self.sx, self.sy, dt)
  67. end
  68. function life:goTo(x, y, dt)
  69. self.speed = (10 + self.fat * 0.5 )
  70. if (x-self.x) ~= 0 then
  71. self.m = (y-self.y)/(x-self.x)
  72. if y-self.y > 0 then
  73. self.m = math.abs(self.m)
  74. elseif y-self.y < 0 and self.m > 0 then
  75. self.m = -self.m
  76. end
  77. else
  78. if y-self.y > 0 then
  79. self.m = 1
  80. elseif y-self.y < 0 then
  81. self.m = -1
  82. else
  83. self.m = 0
  84. end
  85. end
  86. if self.m > 1 or self.m < -1 then
  87. self.speed = math.abs(self.speed / (self.m))
  88. end
  89. if (self.x < x) then
  90. self.x = self.x + self.speed * dt
  91. self.y = self.y + (self.speed* self.m * dt )
  92. print("go right" .. self.fat ..x .. " " ..y )
  93. elseif self.x > x then
  94. self.x = self.x - self.speed* dt
  95. self.y = self.y + (self.speed* self.m * dt )
  96. print("go left" .. self.fat ..x .. " " ..y)
  97. else
  98. self.y = self.y + (self.speed* self.m * dt )
  99. print("go vertical" .. self.fat ..x .. " " ..y)
  100. end
  101. --if (self.y < y) then
  102. -- self.y = self.y + (10 * dt + self.fat * 0.5 * dt)
  103. -- print("go down" .. self.fat)
  104. --elseif self.y > y then
  105. -- print("go up" .. self.fat)
  106. -- self.y = self.y - (10 * dt + self.fat * 0.5 * dt)
  107. --end
  108. self.fat = self.fat - self.fat * self.width * 0.0003 * dt
  109. print(self.m .. " " .. self.speed * self.m)
  110. print((self.speed* self.m * dt ))
  111. end
  112. function life:checkIfEating()
  113. for i, food in ipairs(edibles) do
  114. if math.abs(self.x - food.x) < 10 and math.abs(self.y-food.y) < 10 then
  115. self.fat = self.fat + food.fat
  116. self.width = self.width + food.fat/2
  117. table.remove(edibles, i)
  118. print("yum")
  119. return true
  120. end
  121. end
  122. return false
  123. end
  124. function life:render()
  125. love.graphics.setColor(self.RED, self.GREEN, self.BLUE, self.fat*20/200)
  126. love.graphics.circle('fill', self.x, self.y, self.width)
  127. love.graphics.setColor(255,255,255,255)
  128. love.graphics.printf(math.floor(self.lifeSpan), smallfont, self.x+self.width, self.y, self.width*10, 'left')
  129. love.graphics.setColor(255, 255, 255, 255)
  130. end