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.

73 lines
1.8 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. bool isOverlapping = false;
  34. foreach (RectTransform child in children)
  35. {
  36. if (child == parent)
  37. continue;
  38. if (child.rect.Overlaps(newPoster.rect))
  39. {
  40. isOverlapping = true;
  41. Debug.Log($"Overlapping with: {child.name} [{i}]");
  42. }
  43. }
  44. if (!isOverlapping)
  45. break;
  46. }
  47. newPoster.GetComponent<PlayerJoinIcon>().Initialise(data);
  48. newPoster.gameObject.SetActive(true);
  49. }
  50. }