Browse Source

Improved eating, balanced fat waste

master
Madiwka3 1 year ago
parent
commit
9c4f154f82
4 changed files with 89 additions and 8 deletions
  1. +9
    -0
      .vscode/settings.json
  2. BIN
      game.love
  3. +79
    -7
      life.lua
  4. +1
    -1
      main.lua

+ 9
- 0
.vscode/settings.json View File

@@ -0,0 +1,9 @@
{
"Lua.runtime.version": "LuaJIT",
"Lua.runtime.special": {
"love.filesystem.load": "loadfile"
},
"Lua.workspace.library": [
"${3rd}/love2d/library"
]
}

BIN
game.love View File


+ 79
- 7
life.lua View File

@@ -18,6 +18,7 @@ function life:init( y , x , fat, undead)
self.hasRandomTarget = false
self.sx = x
self.sy = y
self.hasPrey = nil
self.lifeSpan = math.random(50, 100)
if undead then
self.lifeSpan = undead
@@ -31,7 +32,7 @@ function life:update(dt)
if self.fat <= 0 or self.lifeSpan <= 0 then
self.dead = true
end
self.fat = self.fat - self.width*0.001*dt
self.fat = self.fat - self.width*0.004*dt
if not self:checkIfEating() then
self:lookForFood(dt)
end
@@ -40,26 +41,87 @@ function life:update(dt)
self.width = self.width/2
self.fat = self.fat/2-5
self.childrenCount = self.childrenCount + 1
if self.childrenCount == 1 then
self.RED = 0.2
end
if self.childrenCount == 2 then
self.RED = 0.4
self.GREEN = 0.1
end
if self.childrenCount == 3 then
self.RED = 0.6
self.GREEN = 0.2
end
if self.childrenCount == 4 then
self.RED = 0.8
self.GREEN = 0.3
self.BLUE = 0.1
end
if self.childrenCount == 5 then
self.RED = 1
self.GREEN = 0.7
self.BLUE = 0.5
end
end


end
print(self.fat)
end
function life:starving()
return self.fat < self.width * 0.5
end

function life:findPrey()
if self.hasPrey == nil then
closestPrey = 999999
for i, prey in ipairs(bugs) do
dist = math.sqrt((self.x - prey.x)^2 + (self.y-prey.y)^2)
if prey.width < self.width and prey ~= self and dist < closestPrey then
self.sx, self.sy = prey.x, prey.y
closestPrey = dist
print("going to " .. i)
self.hasPrey = prey
end
end
else
if self.hasPrey.dead then
self.hasPrey = nil
return
end
self.sx, self.sy = self.hasPrey.x, self.hasPrey.y
print("going to " .. self.sx .. self.sy)
if math.abs(self.x-self.hasPrey.x) < self.width/2 and math.abs(self.y-self.hasPrey.y) < self.width/2 then
if (math.random(0, 30) + self.width > math.random(0, 30) + self.hasPrey.width) then
self.fat = self.hasPrey.fat + self.fat - 5
self.hasPrey.fat = 5
self.hasPrey.dead = true
self.hasPrey = nil
else
self.dead = true
end
end
end
end

function life:lookForFood(dt)
self.closest = 9000000
print("looking for food...")
for i, food in ipairs(edibles) do
dist = math.sqrt((self.x - food.x)^2 + (self.y-food.y)^2)
if dist < self.closest and dist < self.fat*20 then
if dist < self.closest and dist < self.fat*20 and self.hasPrey == nil then
self.closest = math.sqrt((self.x - food.x)^2 + (self.y-food.y)^2)
self.sx, self.sy = food.x, food.y
print("going to " .. i)
self.hasRandomTarget = true
elseif not self.hasRandomTarget then
self.sx, self.sy = math.floor(math.random(0, VIRTUAL_WIDTH)), math.floor(math.random(0, VIRTUAL_HEIGHT))
print(self.sx)
self.hasRandomTarget = true
if (self:starving()) then
self:findPrey()
else
self.sx, self.sy = math.floor(math.random(0, VIRTUAL_WIDTH)), math.floor(math.random(0, VIRTUAL_HEIGHT))
print(self.sx)
self.hasRandomTarget = true
end
end
end
if self.hasRandomTarget and math.abs(self.x - self.sx) < 5 and math.abs(self.y-self.sy) < 5 then
@@ -70,7 +132,11 @@ function life:lookForFood(dt)
self:goTo(self.sx, self.sy, dt)
end
function life:goTo(x, y, dt)
self.speed = (10 + self.fat * 0.5 )
local factor = 1
if self.hasPrey ~= nil then
factor = 3
end
self.speed = (10 + self.fat * 0.5 ) * factor
if (x-self.x) ~= 0 then
self.m = (y-self.y)/(x-self.x)
if y-self.y > 0 then
@@ -127,9 +193,15 @@ function life:checkIfEating()
end

function life:render()
love.graphics.setColor(self.RED, self.GREEN, self.BLUE, self.fat*20/200)
love.graphics.setColor(self.RED, self.GREEN, self.BLUE, self.fat*25/255)
if self.hasPrey ~= nil then
love.graphics.setColor(255, 0, 0, self.fat*3/255)
end
love.graphics.circle('fill', self.x, self.y, self.width)
love.graphics.setColor(255,255,255,255)
love.graphics.printf(math.floor(self.lifeSpan), smallfont, self.x+self.width, self.y, self.width*10, 'left')
--draw line from self to self.sx, self.sy
love.graphics.setColor(255, 0, 0, 255)
love.graphics.line(self.x, self.y, self.sx, self.sy)
love.graphics.setColor(255, 255, 255, 255)
end

+ 1
- 1
main.lua View File

@@ -38,7 +38,7 @@ function regulator()
end
if #edibles < #bugs/3 and rules["abundantFood"] then
table.insert(edibles, edible(math.random(0, VIRTUAL_HEIGHT), math.random(0, VIRTUAL_WIDTH), 10))
elseif #edibles <= 10 then
elseif #edibles <= 5 then
table.insert(edibles, edible(math.random(0, VIRTUAL_HEIGHT), math.random(0, VIRTUAL_WIDTH), 10))
end
end


Loading…
Cancel
Save