Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Linq;
- using System.Collections;
- using System.Collections.Generic;
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- #if UNITY_EDITOR
- [CustomEditor(typeof(PlayerInit))]
- public class PlayerInitEditor : Editor
- {
- public override void OnInspectorGUI()
- {
- PlayerInit instance = (PlayerInit)target;
- if (GUILayout.Button("Load Resources"))
- instance.LoadPrefabResources();
- base.OnInspectorGUI();
- }
- }
- #endif
- [System.Serializable]
- public class TieValue
- {
- public int id = 0;
- public int worth = 3;
- public int tieCount = 0;
- }
- public class PlayerInit : Singleton<PlayerInit>
- {
- [Range(2, 4)]
- public int playerCount = 4;
- [Range(1, 15)]
- public int ropeSegments = 7;
- public GameObject[] playerPrefabs;
- public GameObject ropePrefab;
- public GameObject segmentPrefab;
- private List<TieValue> untiedPlayers = new List<TieValue>();
- private List<GameObject> activePlayers = new List<GameObject>();
- private GameObject spawnPosPrefab;
- private float ropeLength
- {
- get { return (6 / (playerCount - 1)); }
- }
- public void PlayerInitialize()
- {
- if (playerPrefabs.Length == 0)
- LoadPrefabResources();
- SpawnPlayerSpawn();
- SpawnPlayers();
- TiePlayers();
- RepositionPlayers();
- }
- /// <summary>
- /// Loads the prefabs to spawn
- /// </summary>
- public void LoadPrefabResources()
- {
- playerPrefabs = Resources.LoadAll(("Players"), typeof(GameObject)).Cast<GameObject>().ToArray();
- ropePrefab = Resources.Load("Rope") as GameObject;
- segmentPrefab = Resources.Load("Segment") as GameObject;
- spawnPosPrefab = Resources.Load("SpawnPosition") as GameObject;
- }
- /// <summary>
- /// Spawns the player spawn
- /// </summary>
- private void SpawnPlayerSpawn()
- {
- GameObject playerSpawn = GameObject.FindGameObjectWithTag("Spawn");
- if (playerSpawn == null)
- {
- GameObject spawner = (GameObject)Resources.Load("Tiles/SpawnPosition");
- GameObject m_Spawner = (GameObject)Instantiate(spawner);
- m_Spawner.transform.position = new Vector3(0, 0, 0);
- m_Spawner.transform.name = "SpawnPosition";
- }
- else
- spawnPosPrefab = playerSpawn;
- }
- /// <summary>
- /// Spawns the players and adds them to list
- /// </summary>
- private void SpawnPlayers()
- {
- // Spawn container object
- if (GameObject.Find("Players") == null)
- {
- GameObject players = new GameObject();
- players.name = "Players";
- }
- for (int i = 0; i < playerCount; i++)
- {
- GameObject playerInstance = (GameObject)Instantiate(playerPrefabs[i], spawnPosPrefab.transform.position, Quaternion.identity);
- playerInstance.name = "Player" + (i + 1);
- playerInstance.transform.parent = GameObject.Find("Players").transform;
- activePlayers.Add(playerInstance);
- untiedPlayers.Add(new TieValue());
- untiedPlayers[i].id = i;
- }
- }
- private void TiePlayers()
- {
- int i = 0;
- while ((untiedPlayers.Count - 1) > i)
- {
- GameObject a = TieTogether(true);
- GameObject b = TieTogether(false);
- //Debug.Log("A: " + a + ", B: " + b);
- SpawnRopes(a, b); i++;
- }
- }
- private GameObject TieTogether(bool origin)
- {
- int value = 1;
- if (origin)
- value = 2;
- GameObject instance;
- List<int> available = new List<int>();
- int bottom = 0;
- foreach (var item in untiedPlayers)
- {
- if (item.worth >= value)
- {
- if (item.worth > bottom)
- {
- bottom = item.worth;
- available.Clear();
- available.Add(item.id);
- }
- else if (item.worth == bottom)
- {
- available.Add(item.id);
- }
- }
- }
- int target = available[0];
- if (available.Count > 1)
- target = available[Random.Range(0, available.Count)];
- untiedPlayers[target].worth -= value;
- untiedPlayers[target].tieCount++;
- instance = activePlayers[target];
- return instance;
- }
- private void SpawnRopes(GameObject origin, GameObject target)
- {
- GameObject ropeInstance = (GameObject)Instantiate(ropePrefab, spawnPosPrefab.transform.position, Quaternion.identity);
- ropeInstance.name = "Rope";
- ropeInstance.transform.parent = GameObject.Find("Players").transform;
- ropeInstance.GetComponent<LineRenderer>().SetVertexCount(ropeSegments);
- SpawnSegments(ropeInstance, origin, target);
- }
- private void SpawnSegments(GameObject ropeInstance, GameObject origin, GameObject target)
- {
- for (int i = 0; i < ropeSegments; i++)
- {
- ropeInstance.GetComponent<LineRenderer>().SetPosition(i, new Vector3(0, 0, 0));
- GameObject segmentInstance = (GameObject)Instantiate(segmentPrefab, spawnPosPrefab.transform.position + new Vector3(i *.1f,0,0), Quaternion.identity);
- segmentInstance.name = "Segment" + (i + 1);
- segmentInstance.transform.SetParent(ropeInstance.transform);
- // Setup HingeJoint
- segmentInstance.AddComponent<HingeJoint2D>();
- segmentInstance.GetComponent<HingeJoint2D>().anchor = new Vector3(0.0f, 0, 0);
- segmentInstance.GetComponent<HingeJoint2D>().autoConfigureConnectedAnchor = false;
- segmentInstance.GetComponent<HingeJoint2D>().useLimits = true;
- // if it is a start connect it to the Player
- if (i == 0)
- segmentInstance.GetComponent<HingeJoint2D>().connectedBody = target.transform.GetComponent<Rigidbody2D>();
- else if (i != 0)
- segmentInstance.GetComponent<HingeJoint2D>().connectedBody = ropeInstance.transform.GetChild(i - 1).transform.GetComponent<Rigidbody2D>();
- // connect the end of the rope to the Player
- if (i == (ropeSegments - 1))
- {
- origin.AddComponent<HingeJoint2D>();
- origin.GetComponent<HingeJoint2D>().connectedBody = ropeInstance.transform.GetChild(i).transform.GetComponent<Rigidbody2D>();
- origin.AddComponent<DistanceJoint2D>();
- origin.GetComponent<DistanceJoint2D>().connectedBody = target.transform.GetComponent<Rigidbody2D>();
- origin.GetComponent<DistanceJoint2D>().distance = ropeLength;
- origin.GetComponent<DistanceJoint2D>().autoConfigureConnectedAnchor = false;
- origin.GetComponent<DistanceJoint2D>().maxDistanceOnly = true;
- }
- }
- }
- public void RepositionAllPlayers()
- {
- for (int i = 0; i < activePlayers.Count; i++)
- {
- Vector3 pos = new Vector3(spawnPosPrefab.transform.position.x + ((i - 1) * 1.5f), spawnPosPrefab.transform.position.y, spawnPosPrefab.transform.position.z);
- activePlayers[i].transform.position = pos;
- }
- }
- public void RepositionPlayers()
- {
- if (spawnPosPrefab == null)
- {
- return;
- }
- int index = 0;
- foreach (var item in untiedPlayers)
- {
- if (item.tieCount == 1)
- {
- float offset = -3f;
- if (index == 1)
- offset = 3;
- Vector3 pos = new Vector3(spawnPosPrefab.transform.position.x + offset, spawnPosPrefab.transform.position.y, spawnPosPrefab.transform.position.z);
- activePlayers[item.id].transform.position = pos;
- index++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement