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.

life.lua 3.2 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. life = Class{}
  2. function life:init( y , x , fat )
  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. end
  15. function life:update(dt)
  16. if not self.dead then
  17. if self.fat - self.width < -self.width*0.3 then
  18. self.dead = true
  19. end
  20. self.fat = self.fat - self.width*0.0001*dt
  21. if not self:checkIfEating() then
  22. self:lookForFood(dt)
  23. end
  24. if self.width > 30 then
  25. table.insert(bugs, life(self.y+self.width, self.x+self.width, self.fat/2))
  26. self.width = self.width/2
  27. self.fat = self.fat/2
  28. end
  29. end
  30. print(self.fat)
  31. end
  32. function life:lookForFood(dt)
  33. self.closest = 9000000
  34. local sx, sy = self.x, self.y
  35. print("looking for food...")
  36. for i, food in ipairs(edibles) do
  37. if math.sqrt((self.x - food.x)^2 + (self.y-food.y)^2) < self.closest then
  38. self.closest = math.sqrt((self.x - food.x)^2 + (self.y-food.y)^2)
  39. sx, sy = food.x, food.y
  40. print("going to " .. i)
  41. end
  42. end
  43. self:goTo(sx, sy, dt)
  44. end
  45. function life:goTo(x, y, dt)
  46. self.speed = (10 + self.fat * 0.5 )
  47. if (x-self.x) ~= 0 then
  48. self.m = (y-self.y)/(x-self.x)
  49. if y-self.y > 0 then
  50. self.m = math.abs(self.m)
  51. elseif y-self.y < 0 and self.m > 0 then
  52. self.m = -self.m
  53. end
  54. else
  55. if y-self.y > 0 then
  56. self.m = 1
  57. elseif y-self.y < 0 then
  58. self.m = -1
  59. else
  60. self.m = 0
  61. end
  62. end
  63. if self.m > 1 or self.m < -1 then
  64. self.speed = math.abs(self.speed / (self.m))
  65. end
  66. if (self.x < x) then
  67. self.x = self.x + self.speed * dt
  68. self.y = self.y + (self.speed* self.m * dt )
  69. print("go right" .. self.fat ..x .. " " ..y )
  70. elseif self.x > x then
  71. self.x = self.x - self.speed* dt
  72. self.y = self.y + (self.speed* self.m * dt )
  73. print("go left" .. self.fat ..x .. " " ..y)
  74. else
  75. self.y = self.y + (self.speed* self.m * dt )
  76. print("go vertical" .. self.fat ..x .. " " ..y)
  77. end
  78. --if (self.y < y) then
  79. -- self.y = self.y + (10 * dt + self.fat * 0.5 * dt)
  80. -- print("go down" .. self.fat)
  81. --elseif self.y > y then
  82. -- print("go up" .. self.fat)
  83. -- self.y = self.y - (10 * dt + self.fat * 0.5 * dt)
  84. --end
  85. self.fat = self.fat - self.fat * self.width * 0.0003 * dt
  86. print(self.m .. " " .. self.speed * self.m)
  87. print((self.speed* self.m * dt ))
  88. end
  89. function life:checkIfEating()
  90. for i, food in ipairs(edibles) do
  91. if math.abs(self.x - food.x) < 10 and math.abs(self.y-food.y) < 10 then
  92. self.fat = self.fat + food.fat
  93. self.width = self.width + food.fat/2
  94. table.remove(edibles, i)
  95. print("yum")
  96. return true
  97. end
  98. end
  99. return false
  100. end
  101. function life:render()
  102. love.graphics.setColor(self.RED, self.GREEN, self.BLUE, self.fat*20/200)
  103. love.graphics.circle('fill', self.x, self.y, self.width)
  104. love.graphics.setColor(255, 255, 255, 255)
  105. end