Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Linq;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. #if UNITY_EDITOR
  6. using UnityEditor;
  7. #endif
  8.  
  9. #if UNITY_EDITOR
  10. [CustomEditor(typeof(PlayerInit))]
  11. public class PlayerInitEditor : Editor
  12. {
  13. public override void OnInspectorGUI()
  14. {
  15. PlayerInit instance = (PlayerInit)target;
  16. if (GUILayout.Button("Load Resources"))
  17. instance.LoadPrefabResources();
  18.  
  19. base.OnInspectorGUI();
  20. }
  21. }
  22. #endif
  23. [System.Serializable]
  24. public class TieValue
  25. {
  26. public int id = 0;
  27. public int worth = 3;
  28. public int tieCount = 0;
  29. }
  30.  
  31. public class PlayerInit : Singleton<PlayerInit>
  32. {
  33.  
  34. [Range(2, 4)]
  35. public int playerCount = 4;
  36. [Range(1, 15)]
  37. public int ropeSegments = 7;
  38.  
  39. public GameObject[] playerPrefabs;
  40. public GameObject ropePrefab;
  41. public GameObject segmentPrefab;
  42.  
  43. private List<TieValue> untiedPlayers = new List<TieValue>();
  44. private List<GameObject> activePlayers = new List<GameObject>();
  45. private GameObject spawnPosPrefab;
  46.  
  47. private float ropeLength
  48. {
  49. get { return (6 / (playerCount - 1)); }
  50. }
  51.  
  52. public void PlayerInitialize()
  53. {
  54. if (playerPrefabs.Length == 0)
  55. LoadPrefabResources();
  56.  
  57. SpawnPlayerSpawn();
  58. SpawnPlayers();
  59. TiePlayers();
  60. RepositionPlayers();
  61.  
  62. }
  63.  
  64. /// <summary>
  65. /// Loads the prefabs to spawn
  66. /// </summary>
  67. public void LoadPrefabResources()
  68. {
  69. playerPrefabs = Resources.LoadAll(("Players"), typeof(GameObject)).Cast<GameObject>().ToArray();
  70. ropePrefab = Resources.Load("Rope") as GameObject;
  71. segmentPrefab = Resources.Load("Segment") as GameObject;
  72. spawnPosPrefab = Resources.Load("SpawnPosition") as GameObject;
  73.  
  74. }
  75.  
  76. /// <summary>
  77. /// Spawns the player spawn
  78. /// </summary>
  79. private void SpawnPlayerSpawn()
  80. {
  81. GameObject playerSpawn = GameObject.FindGameObjectWithTag("Spawn");
  82. if (playerSpawn == null)
  83. {
  84. GameObject spawner = (GameObject)Resources.Load("Tiles/SpawnPosition");
  85. GameObject m_Spawner = (GameObject)Instantiate(spawner);
  86.  
  87. m_Spawner.transform.position = new Vector3(0, 0, 0);
  88. m_Spawner.transform.name = "SpawnPosition";
  89. }
  90. else
  91. spawnPosPrefab = playerSpawn;
  92. }
  93.  
  94. /// <summary>
  95. /// Spawns the players and adds them to list
  96. /// </summary>
  97. private void SpawnPlayers()
  98. {
  99.  
  100. // Spawn container object
  101. if (GameObject.Find("Players") == null)
  102. {
  103. GameObject players = new GameObject();
  104. players.name = "Players";
  105. }
  106.  
  107. for (int i = 0; i < playerCount; i++)
  108. {
  109. GameObject playerInstance = (GameObject)Instantiate(playerPrefabs[i], spawnPosPrefab.transform.position, Quaternion.identity);
  110. playerInstance.name = "Player" + (i + 1);
  111. playerInstance.transform.parent = GameObject.Find("Players").transform;
  112.  
  113. activePlayers.Add(playerInstance);
  114.  
  115. untiedPlayers.Add(new TieValue());
  116. untiedPlayers[i].id = i;
  117. }
  118.  
  119. }
  120.  
  121.  
  122. private void TiePlayers()
  123. {
  124. int i = 0;
  125. while ((untiedPlayers.Count - 1) > i)
  126. {
  127. GameObject a = TieTogether(true);
  128. GameObject b = TieTogether(false);
  129.  
  130. //Debug.Log("A: " + a + ", B: " + b);
  131. SpawnRopes(a, b); i++;
  132. }
  133.  
  134. }
  135.  
  136. private GameObject TieTogether(bool origin)
  137. {
  138. int value = 1;
  139. if (origin)
  140. value = 2;
  141.  
  142. GameObject instance;
  143. List<int> available = new List<int>();
  144. int bottom = 0;
  145.  
  146. foreach (var item in untiedPlayers)
  147. {
  148. if (item.worth >= value)
  149. {
  150. if (item.worth > bottom)
  151. {
  152. bottom = item.worth;
  153. available.Clear();
  154. available.Add(item.id);
  155. }
  156. else if (item.worth == bottom)
  157. {
  158. available.Add(item.id);
  159. }
  160. }
  161. }
  162.  
  163. int target = available[0];
  164. if (available.Count > 1)
  165. target = available[Random.Range(0, available.Count)];
  166.  
  167. untiedPlayers[target].worth -= value;
  168. untiedPlayers[target].tieCount++;
  169.  
  170. instance = activePlayers[target];
  171. return instance;
  172. }
  173.  
  174.  
  175. private void SpawnRopes(GameObject origin, GameObject target)
  176. {
  177.  
  178. GameObject ropeInstance = (GameObject)Instantiate(ropePrefab, spawnPosPrefab.transform.position, Quaternion.identity);
  179. ropeInstance.name = "Rope";
  180. ropeInstance.transform.parent = GameObject.Find("Players").transform;
  181. ropeInstance.GetComponent<LineRenderer>().SetVertexCount(ropeSegments);
  182.  
  183. SpawnSegments(ropeInstance, origin, target);
  184.  
  185. }
  186.  
  187. private void SpawnSegments(GameObject ropeInstance, GameObject origin, GameObject target)
  188. {
  189. for (int i = 0; i < ropeSegments; i++)
  190. {
  191. ropeInstance.GetComponent<LineRenderer>().SetPosition(i, new Vector3(0, 0, 0));
  192.  
  193. GameObject segmentInstance = (GameObject)Instantiate(segmentPrefab, spawnPosPrefab.transform.position + new Vector3(i *.1f,0,0), Quaternion.identity);
  194.  
  195. segmentInstance.name = "Segment" + (i + 1);
  196. segmentInstance.transform.SetParent(ropeInstance.transform);
  197.  
  198. // Setup HingeJoint
  199. segmentInstance.AddComponent<HingeJoint2D>();
  200. segmentInstance.GetComponent<HingeJoint2D>().anchor = new Vector3(0.0f, 0, 0);
  201. segmentInstance.GetComponent<HingeJoint2D>().autoConfigureConnectedAnchor = false;
  202. segmentInstance.GetComponent<HingeJoint2D>().useLimits = true;
  203.  
  204. // if it is a start connect it to the Player
  205. if (i == 0)
  206. segmentInstance.GetComponent<HingeJoint2D>().connectedBody = target.transform.GetComponent<Rigidbody2D>();
  207.  
  208. else if (i != 0)
  209. segmentInstance.GetComponent<HingeJoint2D>().connectedBody = ropeInstance.transform.GetChild(i - 1).transform.GetComponent<Rigidbody2D>();
  210.  
  211. // connect the end of the rope to the Player
  212. if (i == (ropeSegments - 1))
  213. {
  214. origin.AddComponent<HingeJoint2D>();
  215. origin.GetComponent<HingeJoint2D>().connectedBody = ropeInstance.transform.GetChild(i).transform.GetComponent<Rigidbody2D>();
  216.  
  217. origin.AddComponent<DistanceJoint2D>();
  218. origin.GetComponent<DistanceJoint2D>().connectedBody = target.transform.GetComponent<Rigidbody2D>();
  219. origin.GetComponent<DistanceJoint2D>().distance = ropeLength;
  220. origin.GetComponent<DistanceJoint2D>().autoConfigureConnectedAnchor = false;
  221. origin.GetComponent<DistanceJoint2D>().maxDistanceOnly = true;
  222. }
  223. }
  224. }
  225.  
  226. public void RepositionAllPlayers()
  227. {
  228. for (int i = 0; i < activePlayers.Count; i++)
  229. {
  230. Vector3 pos = new Vector3(spawnPosPrefab.transform.position.x + ((i - 1) * 1.5f), spawnPosPrefab.transform.position.y, spawnPosPrefab.transform.position.z);
  231. activePlayers[i].transform.position = pos;
  232. }
  233. }
  234.  
  235. public void RepositionPlayers()
  236. {
  237. if (spawnPosPrefab == null)
  238. {
  239. return;
  240. }
  241.  
  242. int index = 0;
  243. foreach (var item in untiedPlayers)
  244. {
  245.  
  246. if (item.tieCount == 1)
  247. {
  248. float offset = -3f;
  249. if (index == 1)
  250. offset = 3;
  251.  
  252. Vector3 pos = new Vector3(spawnPosPrefab.transform.position.x + offset, spawnPosPrefab.transform.position.y, spawnPosPrefab.transform.position.z);
  253. activePlayers[item.id].transform.position = pos;
  254. index++;
  255. }
  256.  
  257. }
  258.  
  259.  
  260. }
  261.  
  262.  
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement