-
Notifications
You must be signed in to change notification settings - Fork 5
/
gem_class.lua
88 lines (77 loc) · 2.66 KB
/
gem_class.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
-- Copyright 2013 Arman Darini
local utils = require("utils")
GemsClass = {
gridX = nil, gridY = nil,
gemType = nil,
image = nil,
parentDisplayGroup = nil,
parentGrid = nil,
----------------------------------------------------------
new = function(self, o)
print("GemsClass:new")
-- utils.printTable(o)
o = o or {} -- create object if user does not provide one
setmetatable(o, self)
self.__index = self
return o
end,
----------------------------------------------------------
init = function(self)
print("GemsClass:init")
local originCoordinates = self.parentGrid:getXYFromGrid(self.gridX, -5)
local destinationCoordinates = self.parentGrid:getXYFromGrid(self.gridX, self.gridY)
-- self:useCircle()
self:useImage()
self.image:translate(originCoordinates.x, originCoordinates.y)
self.parentDisplayGroup:insert(self.image)
transition.to(self.image, { time = 500, delay = (game.gridHeight - self.gridY) * 100, transition = easing.inQuad, y = destinationCoordinates.y })
end,
----------------------------------------------------------
useImage = function(self)
local roll = math.random(1, game.gemTypes)
self.image = display.newImageRect("images/egg_"..roll..".png", game.cellSize, game.cellSize)
self.image:setReferencePoint(display.CenterReferencePoint)
self.gemType = "egg"..roll
end,
----------------------------------------------------------
useCircle = function(self)
self.image = display.newCircle(0, 0, game.gemRadius)
local roll = math.random(1, game.gemTypes)
if (1 == roll) then
self.image:setFillColor(200, 0, 0)
self.gemType = "red"
elseif (2 == roll) then
self.image:setFillColor(0, 200, 0)
self.gemType = "green"
elseif (3 == roll) then
self.image:setFillColor(0, 0, 200)
self.gemType = "blue"
elseif (4 == roll) then
self.image:setFillColor(200, 200, 0)
self.gemType = "yellow"
elseif (5 == roll) then
self.image:setFillColor(250, 250, 250)
self.gemType = "white"
elseif (6 == roll) then
self.image:setFillColor(100, 100, 100)
self.gemType = "gray"
else
self.image:setFillColor(roll*10, roll*15, roll*20)
self.gemType = "type"..roll
end
end,
----------------------------------------------------------
touch = function(self, event)
-- onGemTouch(self, event)
end,
----------------------------------------------------------
toStr = function(self)
return "["..self.gridX..","..self.gridY..","..self.gemType..","..self.image.x..","..self.image.y.."]"
end,
----------------------------------------------------------
removeSelf = function(self)
if self.image ~= nil then self.image:removeSelf() end
-- return self.image and self.image:removeSelf()
end,
}
return GemsClass