PageRenderTime 19ms CodeModel.GetById 1ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Scripts/Follow.js

http://acid-and-base.googlecode.com/
JavaScript | 245 lines | 199 code | 22 blank | 24 comment | 76 complexity | ed4a38441519911bd0fe571c462da849 MD5 | raw file
  1var userNum = -1;					// 0 = red, 1 = acid, 2 = base
  2var following = false;	// is it following someone?
  3var whoFollow = -1;		// well, who is it following?
  4//private var fNum = 0;				// number of char folloing this char
  5var fChar = -1;			// what character is it following?
  6var touchChar = false;				// is it touching another character?
  7
  8var slot1 = -1;			// slot 1		-1 = nobody
  9var slot2 = -1;			// slot 2		0~2 = red, acid, or base
 10var slot = -1;				// tells you which slot you are in...
 11
 12var rot = new Array();	// collection of rotations
 13var pos = new Array();// collection of positions
 14
 15var arrMax = 60;
 16var arrPt1 = arrMax - 25;
 17var arrPt2 = arrMax - 50;
 18
 19var distGap = 2.00;
 20
 21var dist = 0;
 22
 23var updatePos = true;
 24
 25function Update ()
 26{
 27	// should only start following you if the character is close to you.
 28	// F key should be used for following
 29	// E button should release them. This will make any interaction release follow script
 30	
 31	// If anything is following this character, record changes
 32	//if (slot1 != -1 && updatePos)
 33	if (updatePos)
 34	{
 35		//print(pos.length);
 36		rot.Add(transform.rotation);
 37		pos.Add(transform.position);
 38		
 39		// assuming the length are always in sync.
 40		if (rot.length > arrMax)
 41		{
 42			// keeping the length "arrMax"
 43			rot.Shift();
 44			pos.Shift();
 45		}
 46	}
 47/*	else
 48	{
 49		if (rot.length != 0)
 50		{
 51			rot.Clear();
 52		}
 53		if (pos.length != 0)
 54		{
 55			pos.Clear();
 56		}
 57	}	// */
 58	
 59	// either E or Q will release the follow
 60	if (Input.GetKeyDown(KeyCode.E) || Input.GetKeyDown(KeyCode.Q))
 61	{
 62		following = false;
 63		whoFollow = -1;
 64		fChar = -1;
 65		slot1 = -1;
 66		slot2 = -1;
 67		slot = -1;
 68		rot.Clear();
 69		pos.Clear();
 70		updatePos = true;
 71	}
 72	
 73	// activation script - follow mode will be activated
 74	if (Input.GetKeyDown(KeyCode.F) && touchChar && !following)
 75	{
 76		var inControl = gameObject.Find("Master Controller Light").GetComponent(MasterController_edit).activePlayer;
 77
 78		// only follow if the character is not in control
 79		if (inControl != userNum)
 80		{
 81			following = true;
 82
 83			switch(inControl)
 84			{
 85				case 0:
 86					if (gameObject.Find("Big Red").GetComponent(Follow).slot1 == -1)
 87					{
 88						gameObject.Find("Big Red").GetComponent(Follow).slot1 = userNum;
 89						gameObject.Find("Big Red").GetComponent(Follow).updatePos = true;
 90						slot = 1;
 91					}
 92					else
 93					{
 94						gameObject.Find("Big Red").GetComponent(Follow).slot2 = userNum;
 95						slot = 2;
 96					}
 97					break;
 98				case 1:
 99					if (gameObject.Find("Acid").GetComponent(Follow).slot1 == -1)
100					{
101						gameObject.Find("Acid").GetComponent(Follow).slot1 = userNum;
102						gameObject.Find("Acid").GetComponent(Follow).updatePos = true;
103						slot = 1;
104					}
105					else
106					{
107						gameObject.Find("Acid").GetComponent(Follow).slot2 = userNum;
108						slot = 2;
109					}
110					break;
111				case 2:
112					if (gameObject.Find("Base").GetComponent(Follow).slot1 == -1)
113					{
114						gameObject.Find("Base").GetComponent(Follow).slot1 = userNum;
115						gameObject.Find("Base").GetComponent(Follow).updatePos = true;
116						slot = 1;
117					}
118					else
119					{
120						gameObject.Find("Base").GetComponent(Follow).slot2 = userNum;
121						slot = 2;
122					}
123					break;
124			}
125			
126			whoFollow = inControl;
127		}
128	}
129	
130	if (following)
131	{
132		switch(whoFollow)
133		{
134			case 0:
135				if (slot == 1 && gameObject.Find("Big Red").GetComponent(Follow).rot.length > arrPt1)
136				{
137					dist = Vector3.Distance(transform.position, gameObject.Find("Big Red").GetComponent(Transform).position);
138					if (dist < distGap)
139						gameObject.Find("Big Red").GetComponent(Follow).updatePos = false;
140					else
141						gameObject.Find("Big Red").GetComponent(Follow).updatePos = true;
142					transform.rotation = gameObject.Find("Big Red").GetComponent(Follow).rot[arrPt1];
143					transform.position = gameObject.Find("Big Red").GetComponent(Follow).pos[arrPt1];
144					
145					if (userNum == 1)
146						transform.position.y += 0.2;
147					else if (userNum == 2)
148						transform.position.y += 0.5;
149				}
150				else if (gameObject.Find("Big Red").GetComponent(Follow).rot.length > arrPt2)
151				{
152					transform.rotation = gameObject.Find("Big Red").GetComponent(Follow).rot[arrPt2];
153					transform.position = gameObject.Find("Big Red").GetComponent(Follow).pos[arrPt2];
154					
155					if (userNum == 1)
156						transform.position.y += 0.2;
157					else if (userNum == 2)
158						transform.position.y += 0.5;
159				}
160				break;
161			case 1:
162				if (slot == 1 && gameObject.Find("Acid").GetComponent(Follow).rot.length > arrPt1)
163				{
164					dist = Vector3.Distance(transform.position, gameObject.Find("Acid").GetComponent(Transform).position);
165					if (dist < distGap)
166						gameObject.Find("Acid").GetComponent(Follow).updatePos = false;
167					else
168						gameObject.Find("Acid").GetComponent(Follow).updatePos = true;
169					transform.rotation = gameObject.Find("Acid").GetComponent(Follow).rot[arrPt1];
170					transform.position = gameObject.Find("Acid").GetComponent(Follow).pos[arrPt1];
171					
172					if (userNum == 0)
173						transform.position.y -= 0.2;
174					else if (userNum == 2)
175						transform.position.y += 0.3;
176				}
177				else if (gameObject.Find("Acid").GetComponent(Follow).rot.length > arrPt2)
178				{
179					transform.rotation = gameObject.Find("Acid").GetComponent(Follow).rot[arrPt2];
180					transform.position = gameObject.Find("Acid").GetComponent(Follow).pos[arrPt2];
181					if (userNum == 0)
182						transform.position.y -= 0.2;
183					else if (userNum == 2)
184						transform.position.y += 0.3;
185				}
186				break;
187			case 2:
188				if (slot == 1 && gameObject.Find("Base").GetComponent(Follow).rot.length > arrPt1)
189				{
190					dist = Vector3.Distance(transform.position, gameObject.Find("Base").GetComponent(Transform).position);
191					if (dist < distGap)
192						gameObject.Find("Base").GetComponent(Follow).updatePos = false;
193					else
194						gameObject.Find("Base").GetComponent(Follow).updatePos = true;
195					transform.rotation = gameObject.Find("Base").GetComponent(Follow).rot[arrPt1];
196					transform.position = gameObject.Find("Base").GetComponent(Follow).pos[arrPt1];
197					
198					if (userNum == 0)
199						transform.position.y -= 0.5;
200					else if (userNum == 1)
201						transform.position.y -= 0.2;
202				}
203				else if (gameObject.Find("Base").GetComponent(Follow).rot.length > arrPt2)
204				{
205					transform.rotation = gameObject.Find("Base").GetComponent(Follow).rot[arrPt2];
206					transform.position = gameObject.Find("Base").GetComponent(Follow).pos[arrPt2];
207					
208					if (userNum == 0)
209						transform.position.y -= 0.5;
210					else if (userNum == 1)
211						transform.position.y -= 0.2;
212				}
213				break;
214		}
215	}
216}
217
218// when someone collides with this character...
219function OnTriggerEnter(col : Collider)
220{
221	var cmtr: CharacterMotor = col.GetComponent(CharacterMotor);
222	var properties : Properties = col.GetComponent(Properties);
223	var follow : Follow = col.GetComponent(Follow);
224	if(cmtr == null)
225		return;
226	if(properties != null)
227		return;
228	if(follow == null)
229		return;
230	touchChar = true;
231}
232
233function OnTriggerExit(col : Collider)
234{
235	var cmtr: CharacterMotor = col.GetComponent(CharacterMotor);
236	var properties : Properties = col.GetComponent(Properties);
237	var follow : Follow = col.GetComponent(Follow);
238	if(cmtr == null)
239		return;
240	if(properties != null)
241		return;
242	if(follow == null)
243		return;
244	touchChar = false;
245}