-
Notifications
You must be signed in to change notification settings - Fork 0
/
triangle.lua
142 lines (111 loc) · 4.63 KB
/
triangle.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
--Triangle CLASS
module(..., package.seeall)
local Triangle = {}
function Triangle.new(xLoc, yLoc)
-- Draw a triangle
local triangle = display.newLine(100,100, 100,300);
local pauser = 0;
triangle.hasTouched = false;
local randRot = math.random(-30,30);
triangle:append(0,200, 100,100);
triangle.width = 8;
-- triangle:setColor(100,255,100,255);
triangle:setColor(mono[0],mono[1],mono[2],255);
-- Add a custom physics body shape based on the triangle shape
triShape = {0,0,0,200,-100,100}
physics.addBody( triangle, { density=3.0, friction=0.8, bounce=0.3, shape = triShape } )
triangle.id = "triangle"
triangle.x = xLoc;
triangle.y = yLoc;
triangle.isSleepingAllowed = false
triangle.isBullet = true;
triangle.linearDamping = 1
triangle.angularDamping = 1;
local sinCounter = 0;
local function eachFrame()
-- sinCounter = sinCounter + .01;
--Generate a random number to determine the chance of a 'pulse'
-- print("triangle enterframe");
-- triangle:applyTorque(math.sin(sinCounter)*10);
triangle:applyTorque(randRot);
triangle:attract();
triangle:drawTrail();
end
local function checkBoundary()
if (triangle ~= nil) then
if (triangle.y < 0 or triangle.y > 768) then
print (triangle.y .. " is the triangle.y")
triangle:fadeOut();
bigObjectsOnScreen = bigObjectsOnScreen - 1;
end
end
end
local function addBoundaryListener( event )
Runtime:addEventListener( "enterFrame", checkBoundary )
end
timer.performWithDelay(5000, addBoundaryListener )
local function onTriangleCollision(self, event)
if ( event.phase == "began" ) then
print("triangle collided");
end
end
triangle.collision = onTriangleCollision
triangle:addEventListener( "collision", triangle )
Runtime:addEventListener( "enterFrame", eachFrame )
function triangle:attract()
if (attractorPresent == true) then
--print ("triangle wants to go to the attractor");
local dirX, dirY = getDirection(triangle.x,triangle.y,attractor.x,attractor.y);
triangle:applyForce(dirX*10,dirY*10,triangle.x,triangle.y);
-- print ("dir X is " .. dirX);
-- print ("dir Y is " .. dirY);
end
end
function triangle:drawTrail()
local trail;
pauser = pauser + 1;
local function trailErase()
trail:removeSelf();
trail = nil;
end
-- print("is this shit running?");
if (pauser > 3) then
trail = display.newLine(100,100, 100,300);
trail:append(0,200, 100,100);
trail.width = 8;
trail:setColor(mono[0],mono[1],mono[2],255);
trail.x = (triangle.x)
trail.y = triangle.y
-- trail = display.newRect(sqX, sqY, 100, 100);
-- trail:setStrokeColor(0,0,0,255);
trail.strokeWidth = 8;
-- trail:setFillColor(0,0,255,0);
trail.rotation = triangle.rotation;
transition.to(trail, {alpha = 0.0, time=500, onComplete = trailErase});
pauser = 0;
end
end
function triangle:destroy()
print("triangle gone");
if (triangle ~= nil) then
triangle:removeSelf()
triangle = nil
end
end
function triangle:fadeOut()
print("fading out")
local function goAway()
print("...and going away");
Runtime:removeEventListener("enterFrame", eachFrame);
Runtime:removeEventListener("enterFrame", checkBoundary);
triangle:removeSelf()
triangle = nil
end
if (triangle.hasTouched == false) then
transition.to( triangle, {delay=1, time=1000, alpha=0.0, onComplete=goAway} )
triangle.hasTouched = true;
end
end
return triangle
end
return Triangle