This repository has been archived by the owner on Feb 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fruit.lua
67 lines (57 loc) · 1.79 KB
/
fruit.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require 'entity'
require 'presets'
Fruit = {}
Fruit.__index = Fruit
setmetatable(Fruit, {__index = Entity})
function Fruit:new(game, config, flavour)
local config = config or {}
local newFruit = Entity:new(game)
newFruit.type = "Fruit"
newFruit.x = config.x or 0
newFruit.y = config.y or 0
newFruit.size = config.size or {
x = 40,
y = 40
}
-- if flavour isnt defined, choose a random one
local flavour = flavour or Fruit:getRandomFlavour()
newFruit.flavour = flavour
newFruit.value = GAME_FRUIT_TYPES_NEW[flavour]
newFruit.graphics = config.graphics or {
source = "assets/images/" .. flavour .. ".png"
}
if game.graphics ~= nil and game.animation ~= nil then
newFruit.graphics.sprites = game.graphics.newImage(newFruit.graphics.source)
newFruit.graphics.grid = game.animation.newGrid(
newFruit.size.x, newFruit.size.y,
newFruit.graphics.sprites:getWidth(),
newFruit.graphics.sprites:getHeight()
)
newFruit.graphics.animation = game.animation.newAnimation(
newFruit.graphics.grid("1-1", 1),
0.5
)
end
return setmetatable(newFruit, self)
end
function Fruit:collide(other, fruitPlacer)
-- remove fruit when player collides with it
for i,v in pairs(entities) do
if v == self then
other:collect(self.value)
table.remove(entities, i)
fruitPlacer:place(1)
end
end
end
function Fruit:getRandomFlavour()
-- collect just names of fruits (the key collection)
local flavours = {}
for k,v in pairs(GAME_FRUIT_TYPES_NEW) do
table.insert(flavours, k)
end
-- return random flavour
return flavours[math.random(#flavours)]
end
function Fruit:update(dt)
end