/
fish_store.lua
141 lines (82 loc) · 2.62 KB
/
fish_store.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
--====================================================================--
-- Imports
--====================================================================--
local Objects = require( "dmc_objects" )
local inheritsFrom = Objects.inheritsFrom
local CoronaBase = Objects.CoronaBase
--====================================================================--
-- Fish Object
--====================================================================--
local Fish = inheritsFrom( CoronaBase )
Fish.NAME = "A Fish"
Fish.file1 = "assets/fish.small.red.png"
Fish.file2 = "assets/fish.small.blue.png"
function Fish:new()
local o = self:_bless()
o:_init()
o:_createView()
return o
end
function Fish:_init()
self:superCall( "_init" )
-- == Create Properties ==
-- assign each fish a random velocity
self.vx = math.random( 1, 5 )
self.vy = math.random( -2, 2 )
-- add some event listeners for those events we'd like to know about
self:addEventListener( "touch", self )
Runtime:addEventListener( "orientation", self )
end
function Fish:_createView()
local img
-- fish original
img = display.newImage( Fish.file1 )
self:insert( img, true )
-- fish different
img = display.newImage( Fish.file2 )
img.isVisible = false
self:insert( img, true )
end
-- for some reason, scaling doesn't work around the proper reference point
-- need to figure out why
-- for now, just flip each image individually
--
-- it's a great name for a method - Fish Scale !
function Fish:scale( x, y )
local d = self.display
d[1]:scale( x, y )
d[2]:scale( x, y )
end
function Fish:touch( event )
local group = self.display
if event.phase == "ended" then
local topObject = group[1]
if ( topObject.isVisible ) then
local bottomObject = group[2]
-- Dissolve to bottomObject (different color)
transition.dissolve( topObject, bottomObject, 500 )
-- Restore after some random delay
transition.dissolve( bottomObject, topObject, 500, math.random( 3000, 10000 ) )
end
return true
end
end
function Fish:orientation( event )
if ( event.delta ~= 0 ) then
local rotateParameters = { rotation = -event.delta, time=500, delta=true }
Runtime:removeEventListener( "enterFrame", self )
transition.to( self.display, rotateParameters )
local function resume( event )
Runtime:addEventListener( "enterFrame", self )
end
timer.performWithDelay( 500, resume )
end
end
--====================================================================--
-- Fish Store Factory
--====================================================================--
local FishStore = {}
FishStore.buyFish = function()
return Fish:new()
end
return FishStore