/Scripts/Follow.js

http://acid-and-base.googlecode.com/ · JavaScript · 245 lines · 199 code · 22 blank · 24 comment · 76 complexity · ed4a38441519911bd0fe571c462da849 MD5 · raw file

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