-
Notifications
You must be signed in to change notification settings - Fork 2
/
food.lua
40 lines (34 loc) · 824 Bytes
/
food.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
require 'vector.lua'
Food = {
identity = "Food class",
radius = 30
}
function Food:new(x, y)
local instance = {}
setmetatable(instance, self)
self.__index = self
instance:spawn(x, y)
return instance
end
function Food:spawn(x, y)
if x == nil then
x = love.graphics.getWidth() * math.random()
end
if y == nil then
y = love.graphics.getHeight() * math.random()
end
self.size = Food.radius * 2
self.position = Vector:new(x, y)
self.sprite = love.graphics.newImage("images/plant1.png")
end
function Food:isEaten(boid)
if self.position:isNearby(Food.radius, boid.position) then
self.size = self.size - 1
end
if self.size <= 0 then
self:spawn()
end
end
function Food:draw()
love.graphics.draw(self.sprite, self.position.x, self.position.y)
end