summaryrefslogtreecommitdiff
path: root/webcam/webcam/effects/motion/SwarmFly.as
blob: 84d751cb1a20ae91736980c7cc7c857531bda0ef (plain)
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
/**
 * Neave Webcam // Swarm Fly
 * 
 * Copyright (C) 2008 Paul Neave
 * http://www.neave.com/
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation at http://www.gnu.org/licenses/gpl.html
 */

package com.neave.webcam.effects.motion
{
	import flash.display.*;
	import flash.filters.*;
	
	final internal class SwarmFly extends Shape
	{
		private const MAX_BOREDOM:int = 10;
		private const BOREDOM_TIME:int = 20;
		private const MAX_DIST:int = 8000;
		private const BORED_DIST:int = 30;
		
		private var x0:Number;
		private var y0:Number;
		private var size:Number;
		private var speed:Number;
		private var boredom:int;
		private var wobbleAngle:Number;
		private var wobbleDist:Number;
		private var wobbleStep:Number;
		
		/**
		 * Creates a fly graphic to be used with the swarm effect
		 * 
		 * @param	x	The starting position of the fly on the x-axis
		 * @param	y	The starting position of the fly on the y-axis
		 */
		public function SwarmFly(x:Number = 0, y:Number = 0, size:Number = 1)
		{
			this.x = x0 = x;
			this.y = y0 = y;
			this.size = size;
			
			createFly();
		}
		
		/**
		 * Sets up the fly and its initial properties
		 */
		private function createFly():void
		{
			// This fly's movement speed and initial boredom threshold
			speed = Math.random() * 20 + 10;
			boredom = Math.floor(Math.random() * (MAX_BOREDOM + BOREDOM_TIME));
			
			// This fly's wobbling amount as it flies
			wobbleAngle = Math.random() * Math.PI;
			wobbleDist = Math.random() * 2.5 + 0.5;
			wobbleStep = evenRandom() * 2;
			
			// Draw the fly shape, a black circle with a black glow
			graphics.beginFill(0x000000);
			graphics.drawCircle(0, 0, size);
			filters = [ new GlowFilter(0x000000, 1, 6, 6, 2, 2, false, false) ];
			cacheAsBitmap = true;
		}
		
		/**
		 * Generates a random number between -0.5 and 0.5
		 */
		private function evenRandom():Number
		{
			return Math.random() - 0.5;
		}
		
		/**
		 * Moves the fly towards the specified position
		 * 
		 * @param	x	The position to move to on the x-axis
		 * @param	y	The position to move to on the y-axis
		 */
		internal function flyTo(x:int, y:int):void
		{
			var dx:int = this.x - x;
			var dy:int = this.y - y;
			if (boredom > MAX_BOREDOM || dx * dx + dy * dy > MAX_DIST)
			{
				// Fly around if the fly is too bored or too far away
				this.x += (x0 - this.x) / speed;
				this.y += (y0 - this.y) / speed;
				
				// Flying around reduces boredom
				if (boredom > 0) boredom--;
				
				// Not bored any more, reset boredom threshold
				if (boredom == MAX_BOREDOM) boredom = 0;
			}
			else if (x >=0 || y >= 0)
			{
				// Fly isn't bored yet so moves towards the motion
				this.x += (x - this.x) / speed / 2;
				this.y += (y - this.y) / speed / 2;
				
				// Boredom increases until the fly is totally bored and flies off
				if (boredom < MAX_BOREDOM) boredom++;
				else
				{
					// Bored now, so fly away somewhere for a time
					x0 = x + evenRandom() * BORED_DIST;
					y0 = y + evenRandom() * BORED_DIST;
					boredom = MAX_BOREDOM + Math.ceil(evenRandom() * BOREDOM_TIME);
				}
			}
			
			// Wobble the fly's movement
			wobbleAngle += wobbleStep;
			this.x += Math.sin(wobbleAngle) * wobbleDist;
			this.y += Math.cos(wobbleAngle / 3) * wobbleDist;
		}
	}
}