Global Game Jam 2021
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.1 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlayerJoinUI : MonoBehaviour
  5. {
  6. [SerializeField]
  7. private PlayerList m_ConnectedPlayers;
  8. [SerializeField]
  9. private RectTransform m_prefab;
  10. [SerializeField]
  11. private List<RectTransform> m_zones;
  12. private int count;
  13. private void OnEnable()
  14. {
  15. m_ConnectedPlayers.OnPlayerJoin += OnPlayerJoin;
  16. }
  17. private void OnDisable()
  18. {
  19. m_ConnectedPlayers.OnPlayerJoin -= OnPlayerJoin;
  20. }
  21. private void OnPlayerJoin(object sender, PlayerData data)
  22. {
  23. RectTransform parent = m_zones[count++];
  24. count = count % m_zones.Count;
  25. RectTransform newPoster = Instantiate(m_prefab);
  26. newPoster.parent = parent;
  27. RectTransform[] children = parent.GetComponentsInChildren<RectTransform>();
  28. for (int i = 0; i < 10; i++)
  29. {
  30. float randomX = Random.Range(parent.rect.xMin + newPoster.rect.width / 2, parent.rect.xMax - newPoster.rect.width / 2);
  31. float randomY = Random.Range(parent.rect.yMin + newPoster.rect.height / 2, parent.rect.yMax - newPoster.rect.height / 2);
  32. newPoster.localPosition = new Vector3(randomX, randomY, 0.0f);
  33. newPoster.Rotate(Vector3.forward, Random.Range(-20.0f, 20.0f));
  34. if (Random.Range(0, 2) > 0)
  35. newPoster.transform.localScale = new Vector3(-newPoster.transform.localScale.x, newPoster.transform.localScale.y, newPoster.transform.localScale.z);
  36. bool isOverlapping = false;
  37. foreach (RectTransform child in children)
  38. {
  39. if (child == parent)
  40. continue;
  41. if (child.rect.Overlaps(newPoster.rect))
  42. {
  43. isOverlapping = true;
  44. Debug.Log($"Overlapping with: {child.name} [{i}]");
  45. }
  46. }
  47. if (!isOverlapping)
  48. break;
  49. }
  50. newPoster.GetComponent<PlayerJoinIcon>().Initialise(data);
  51. newPoster.gameObject.SetActive(true);
  52. }
  53. }