-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOctoOrb.lua
285 lines (225 loc) · 11.2 KB
/
OctoOrb.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
--OctoOrb class
module(..., package.seeall)
local OctoOrb = {}
function OctoOrb.new(xLoc, yLoc)
local octoOrb = display.newCircle(550,yLoc, 50,50);
local alreadyFadedOut = false;
local behaviorType = math.random(2);
local randRot = math.random(-30,30);
local pauser= 0;
octoOrb:setStrokeColor(mono[0],mono[1],mono[2],255)
octoOrb.strokeWidth = 5;
octoOrb:setFillColor(mono[0],mono[1],mono[2],180)
-- Add a custom physics body shape based on the octoOrb shape
physics.addBody( octoOrb, { density=3.0, friction=0.8, bounce=0.3, radius = 50 } )
octoOrb.isSleepingAllowed = false
octoOrb.isBullet = true;
octoOrb.linearDamping = 1
octoOrb.angularDamping = 1;
octoOrb.id = "octoOrb";
local leftOrb = display.newCircle(550,yLoc - 75, 25,25);
physics.addBody( leftOrb, { density=3.0, friction=0.8, bounce=0.3, radius = 25 } )
leftOrb.isSleepingAllowed = false
leftOrb.isBullet = true;
leftOrb.linearDamping = 1
leftOrb.angularDamping = 1;
octoOrb.leftOrb = leftOrb;
local leftSmOrb = display.newCircle(550,yLoc-110, 10,10);
physics.addBody( leftSmOrb, { density=3.0, friction=0.8, bounce=0.3, radius = 10 } )
leftSmOrb.isSleepingAllowed = false
leftSmOrb.isBullet = true;
leftSmOrb.linearDamping = 1
leftSmOrb.angularDamping = 1;
octoOrb.leftSmOrb = leftSmOrb;
local rightOrb = display.newCircle(550,yLoc+75, 25,25);
physics.addBody( rightOrb, { density=3.0, friction=0.8, bounce=0.3, radius = 25 } )
rightOrb.isSleepingAllowed = false
rightOrb.isBullet = true;
rightOrb.linearDamping = 1
rightOrb.angularDamping = 1;
octoOrb.rightOrb = rightOrb;
local rightSmOrb = display.newCircle(550,yLoc+110, 10,10);
physics.addBody( rightSmOrb, { density=3.0, friction=0.8, bounce=0.3, radius = 10 } )
rightSmOrb.isSleepingAllowed = false
rightSmOrb.isBullet = true;
rightSmOrb.linearDamping = 1
rightSmOrb.angularDamping = 1;
octoOrb.rightSmOrb = rightSmOrb;
-- Create joints between the orbs
local myJointR = physics.newJoint( "pivot", octoOrb, rightOrb,550, yLoc + 50 )
myJointR.isLimitEnabled = true -- (boolean)
myJointR:setRotationLimits( -60, 60 )
local myJointRsm = physics.newJoint( "pivot", rightOrb, rightSmOrb, 550,yLoc+100 )
myJointRsm.isLimitEnabled = true -- (boolean)
myJointRsm:setRotationLimits( -60, 60 )
local myJointL = physics.newJoint( "pivot", octoOrb, leftOrb, 550,yLoc-50 )
myJointL.isLimitEnabled = true -- (boolean)
myJointL:setRotationLimits( -60, 60 )
local myJointLsm = physics.newJoint( "pivot", leftOrb, leftSmOrb, 550,yLoc-100 )
myJointLsm.isLimitEnabled = true -- (boolean)
myJointLsm:setRotationLimits( -60, 60 )
rightOrb:setStrokeColor(mono[0],mono[1],mono[2],255)
rightOrb.strokeWidth = 5;
rightOrb:setFillColor(mono[0],mono[1],mono[2],180)
rightSmOrb:setStrokeColor(mono[0],mono[1],mono[2],255)
rightSmOrb.strokeWidth = 5;
rightSmOrb:setFillColor(mono[0],mono[1],mono[2],180)
leftOrb:setStrokeColor(mono[0],mono[1],mono[2],255)
leftOrb.strokeWidth = 5;
leftOrb:setFillColor(mono[0],mono[1],mono[2],180)
leftSmOrb:setStrokeColor(mono[0],mono[1],mono[2],255)
leftSmOrb.strokeWidth = 5;
leftSmOrb:setFillColor(mono[0],mono[1],mono[2],180)
local function eachFrame()
-- If behaviorType is 1, this is a 'pulser'
if (behaviorType == 1) then
--Generate a random number to determine the chance of a 'pulse'
local randChance = math.random(1,100);
if (randChance < 2) then
--choose a random direction
local randX = math.random(-1000,1000);
local randY = math.random(-1000,1000);
print("randX was "..randX);
print("randY was "..randY);
octoOrb:applyForce(randX,randY,octoOrb.x,octoOrb.y);
end
if (attractorPresent) then
--print ("square wants to go to the attractor");
local dirX, dirY = getDirection(octoOrb.x,octoOrb.y,attractor.x,attractor.y);
octoOrb:applyForce(dirX*10,dirY*10,octoOrb.x,octoOrb.y);
end
end
-- If behaviorType is 2, this is a spinner
if (behaviorType == 2) then
octoOrb:applyTorque(randRot);
end
-- octoOrb:attract();
octoOrb:drawTrail();
end
local function checkBoundary()
if (octoOrb.y < 0 or octoOrb.y > 768) then
print (octoOrb.y)
if (alreadyFadedOut == false) then
octoOrb:fadeOut();
bigObjectsOnScreen = bigObjectsOnScreen - 1;
alreadyFadedOut = true;
end
end
end
local function addBoundaryListener( event )
Runtime:addEventListener( "enterFrame", checkBoundary )
end
timer.performWithDelay(500, addBoundaryListener )
Runtime:addEventListener( "enterFrame", eachFrame )
-- function octoOrb:attract()
-- if (attractorPresent == true) then
-- print ("dir X is " .. dirX);
-- print ("dir Y is " .. dirY);
-- end
-- end
function octoOrb:drawTrail()
local trail;
pauser = pauser + 1;
local function trailErase()
if (trail ~= nil) then
trail:removeSelf();
trail = nil;
end
if (trailRight ~= nil) then
trailRight:removeSelf();
trailRight = nil;
end
if (trailLeft ~= nil) then
trailLeft:removeSelf();
trailLeft = nil;
end
if (trailRightSm ~= nil) then
trailRightSm:removeSelf();
trailRightSm = nil;
end
if (trailLeftSm ~= nil) then
trailLeftSm:removeSelf();
trailLeftSm = nil;
end
end
--draw trail here
if (pauser > 3) then
trail = display.newCircle(octoOrb.x,octoOrb.y, 50,50);
trailRight = display.newCircle(rightOrb.x,rightOrb.y, 25, 25);
trailLeft = display.newCircle(leftOrb.x,leftOrb.y, 25, 25);
trailRightSm = display.newCircle(rightSmOrb.x,rightSmOrb.y, 10, 10);
trailLeftSm = display.newCircle(leftSmOrb.x, leftSmOrb.y, 10, 10);
trail:setStrokeColor(0,0,0,255);
trail.strokeWidth = 5;
trail:setReferencePoint(display.CenterReferencePoint);
trail:setFillColor(0,0,255,0);
trail.rotation = octoOrb.rotation;
trailRight:setStrokeColor(0,0,0,255);
trailRight.strokeWidth = 5;
trailRight:setReferencePoint(display.CenterReferencePoint);
trailRight:setFillColor(0,0,255,0);
trailRight.rotation = octoOrb.rotation;
trailLeft:setStrokeColor(0,0,0,255);
trailLeft.strokeWidth = 5;
trailLeft:setReferencePoint(display.CenterReferencePoint);
trailLeft:setFillColor(0,0,255,0);
trailLeft.rotation = octoOrb.rotation;
trailRightSm:setStrokeColor(0,0,0,255);
trailRightSm.strokeWidth = 5;
trailRightSm:setReferencePoint(display.CenterReferencePoint);
trailRightSm:setFillColor(0,0,255,0);
trailRightSm.rotation = octoOrb.rotation;
trailLeftSm:setStrokeColor(0,0,0,255);
trailLeftSm.strokeWidth = 5;
trailLeftSm:setReferencePoint(display.CenterReferencePoint);
trailLeftSm:setFillColor(0,0,255,0);
trailLeftSm.rotation = octoOrb.rotation;
transition.to(trail, {alpha = 0.0, time=500, onComplete = trailErase});
transition.to(trailRight, {alpha = 0.0, time=500});
transition.to(trailLeft, {alpha = 0.0, time=500});
transition.to(trailRightSm, {alpha = 0.0, time=500});
transition.to(trailLeftSm, {alpha = 0.0, time=501, onComplete = trailErase});
pauser = 0;
end
end
function octoOrb:destroy()
print("octoOrb gone");
octoOrb:removeSelf()
octoOrb = nil
end
function octoOrb:fadeOut()
print("fading out")
local function goAway()
print("...and going away");
Runtime:removeEventListener("enterFrame", checkBoundary);
Runtime:removeEventListener("enterFrame", eachFrame);
if (leftOrb ~= nil) then
leftOrb:removeSelf();
leftSmOrb:removeSelf();
rightOrb:removeSelf();
rightSmOrb:removeSelf();
--myJointR:removeSelf();
myJointRsm:removeSelf();
myJointL:removeSelf();
myJointLsm:removeSelf();
leftOrb = nil;
leftSmOrb = nil;
rightOrb = nil;
rightSmOrb = nil;
-- myJointR = nil;
myJointRsm = nil ;
myJointL= nil;
myJointLsm= nil;
octoOrb:removeSelf()
octoOrb = nil
end
end
transition.to( octoOrb.leftOrb, {time=1000, alpha=0.0} )
transition.to( octoOrb.leftSmOrb, {time=1000, alpha=0.0} )
transition.to( octoOrb.rightOrb, {time=1000, alpha=0.0} )
transition.to( octoOrb.rightSmOrb, {time=1000, alpha=0.0} )
transition.to( octoOrb, {delay=1, time=1002, alpha=0.0, onComplete=goAway} )
end
return octoOrb
end
return OctoOrb