/Scripts/Follow.js
http://acid-and-base.googlecode.com/ · JavaScript · 245 lines · 199 code · 22 blank · 24 comment · 76 complexity · ed4a38441519911bd0fe571c462da849 MD5 · raw file
- var userNum = -1; // 0 = red, 1 = acid, 2 = base
- var following = false; // is it following someone?
- var whoFollow = -1; // well, who is it following?
- //private var fNum = 0; // number of char folloing this char
- var fChar = -1; // what character is it following?
- var touchChar = false; // is it touching another character?
- var slot1 = -1; // slot 1 -1 = nobody
- var slot2 = -1; // slot 2 0~2 = red, acid, or base
- var slot = -1; // tells you which slot you are in...
- var rot = new Array(); // collection of rotations
- var pos = new Array();// collection of positions
- var arrMax = 60;
- var arrPt1 = arrMax - 25;
- var arrPt2 = arrMax - 50;
- var distGap = 2.00;
- var dist = 0;
- var updatePos = true;
- function Update ()
- {
- // should only start following you if the character is close to you.
- // F key should be used for following
- // E button should release them. This will make any interaction release follow script
-
- // If anything is following this character, record changes
- //if (slot1 != -1 && updatePos)
- if (updatePos)
- {
- //print(pos.length);
- rot.Add(transform.rotation);
- pos.Add(transform.position);
-
- // assuming the length are always in sync.
- if (rot.length > arrMax)
- {
- // keeping the length "arrMax"
- rot.Shift();
- pos.Shift();
- }
- }
- /* else
- {
- if (rot.length != 0)
- {
- rot.Clear();
- }
- if (pos.length != 0)
- {
- pos.Clear();
- }
- } // */
-
- // either E or Q will release the follow
- if (Input.GetKeyDown(KeyCode.E) || Input.GetKeyDown(KeyCode.Q))
- {
- following = false;
- whoFollow = -1;
- fChar = -1;
- slot1 = -1;
- slot2 = -1;
- slot = -1;
- rot.Clear();
- pos.Clear();
- updatePos = true;
- }
-
- // activation script - follow mode will be activated
- if (Input.GetKeyDown(KeyCode.F) && touchChar && !following)
- {
- var inControl = gameObject.Find("Master Controller Light").GetComponent(MasterController_edit).activePlayer;
- // only follow if the character is not in control
- if (inControl != userNum)
- {
- following = true;
- switch(inControl)
- {
- case 0:
- if (gameObject.Find("Big Red").GetComponent(Follow).slot1 == -1)
- {
- gameObject.Find("Big Red").GetComponent(Follow).slot1 = userNum;
- gameObject.Find("Big Red").GetComponent(Follow).updatePos = true;
- slot = 1;
- }
- else
- {
- gameObject.Find("Big Red").GetComponent(Follow).slot2 = userNum;
- slot = 2;
- }
- break;
- case 1:
- if (gameObject.Find("Acid").GetComponent(Follow).slot1 == -1)
- {
- gameObject.Find("Acid").GetComponent(Follow).slot1 = userNum;
- gameObject.Find("Acid").GetComponent(Follow).updatePos = true;
- slot = 1;
- }
- else
- {
- gameObject.Find("Acid").GetComponent(Follow).slot2 = userNum;
- slot = 2;
- }
- break;
- case 2:
- if (gameObject.Find("Base").GetComponent(Follow).slot1 == -1)
- {
- gameObject.Find("Base").GetComponent(Follow).slot1 = userNum;
- gameObject.Find("Base").GetComponent(Follow).updatePos = true;
- slot = 1;
- }
- else
- {
- gameObject.Find("Base").GetComponent(Follow).slot2 = userNum;
- slot = 2;
- }
- break;
- }
-
- whoFollow = inControl;
- }
- }
-
- if (following)
- {
- switch(whoFollow)
- {
- case 0:
- if (slot == 1 && gameObject.Find("Big Red").GetComponent(Follow).rot.length > arrPt1)
- {
- dist = Vector3.Distance(transform.position, gameObject.Find("Big Red").GetComponent(Transform).position);
- if (dist < distGap)
- gameObject.Find("Big Red").GetComponent(Follow).updatePos = false;
- else
- gameObject.Find("Big Red").GetComponent(Follow).updatePos = true;
- transform.rotation = gameObject.Find("Big Red").GetComponent(Follow).rot[arrPt1];
- transform.position = gameObject.Find("Big Red").GetComponent(Follow).pos[arrPt1];
-
- if (userNum == 1)
- transform.position.y += 0.2;
- else if (userNum == 2)
- transform.position.y += 0.5;
- }
- else if (gameObject.Find("Big Red").GetComponent(Follow).rot.length > arrPt2)
- {
- transform.rotation = gameObject.Find("Big Red").GetComponent(Follow).rot[arrPt2];
- transform.position = gameObject.Find("Big Red").GetComponent(Follow).pos[arrPt2];
-
- if (userNum == 1)
- transform.position.y += 0.2;
- else if (userNum == 2)
- transform.position.y += 0.5;
- }
- break;
- case 1:
- if (slot == 1 && gameObject.Find("Acid").GetComponent(Follow).rot.length > arrPt1)
- {
- dist = Vector3.Distance(transform.position, gameObject.Find("Acid").GetComponent(Transform).position);
- if (dist < distGap)
- gameObject.Find("Acid").GetComponent(Follow).updatePos = false;
- else
- gameObject.Find("Acid").GetComponent(Follow).updatePos = true;
- transform.rotation = gameObject.Find("Acid").GetComponent(Follow).rot[arrPt1];
- transform.position = gameObject.Find("Acid").GetComponent(Follow).pos[arrPt1];
-
- if (userNum == 0)
- transform.position.y -= 0.2;
- else if (userNum == 2)
- transform.position.y += 0.3;
- }
- else if (gameObject.Find("Acid").GetComponent(Follow).rot.length > arrPt2)
- {
- transform.rotation = gameObject.Find("Acid").GetComponent(Follow).rot[arrPt2];
- transform.position = gameObject.Find("Acid").GetComponent(Follow).pos[arrPt2];
- if (userNum == 0)
- transform.position.y -= 0.2;
- else if (userNum == 2)
- transform.position.y += 0.3;
- }
- break;
- case 2:
- if (slot == 1 && gameObject.Find("Base").GetComponent(Follow).rot.length > arrPt1)
- {
- dist = Vector3.Distance(transform.position, gameObject.Find("Base").GetComponent(Transform).position);
- if (dist < distGap)
- gameObject.Find("Base").GetComponent(Follow).updatePos = false;
- else
- gameObject.Find("Base").GetComponent(Follow).updatePos = true;
- transform.rotation = gameObject.Find("Base").GetComponent(Follow).rot[arrPt1];
- transform.position = gameObject.Find("Base").GetComponent(Follow).pos[arrPt1];
-
- if (userNum == 0)
- transform.position.y -= 0.5;
- else if (userNum == 1)
- transform.position.y -= 0.2;
- }
- else if (gameObject.Find("Base").GetComponent(Follow).rot.length > arrPt2)
- {
- transform.rotation = gameObject.Find("Base").GetComponent(Follow).rot[arrPt2];
- transform.position = gameObject.Find("Base").GetComponent(Follow).pos[arrPt2];
-
- if (userNum == 0)
- transform.position.y -= 0.5;
- else if (userNum == 1)
- transform.position.y -= 0.2;
- }
- break;
- }
- }
- }
- // when someone collides with this character...
- function OnTriggerEnter(col : Collider)
- {
- var cmtr: CharacterMotor = col.GetComponent(CharacterMotor);
- var properties : Properties = col.GetComponent(Properties);
- var follow : Follow = col.GetComponent(Follow);
- if(cmtr == null)
- return;
- if(properties != null)
- return;
- if(follow == null)
- return;
- touchChar = true;
- }
- function OnTriggerExit(col : Collider)
- {
- var cmtr: CharacterMotor = col.GetComponent(CharacterMotor);
- var properties : Properties = col.GetComponent(Properties);
- var follow : Follow = col.GetComponent(Follow);
- if(cmtr == null)
- return;
- if(properties != null)
- return;
- if(follow == null)
- return;
- touchChar = false;
- }