1
0

PushBox.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using Mirror;
  6. public class PushBox : NetworkBehaviour
  7. {
  8. public int playersRequired;
  9. [SyncVar(hook =nameof(OnTouchersChanged))]
  10. public int playersTouching;
  11. public Vector3 detectorSize;
  12. public Text numberTxt;
  13. public List<NetPlayer> DTP;
  14. public List<NetPlayer> Neighbours;
  15. public List<NetPlayer> targets;
  16. public List<NetPlayer> scannedList;
  17. void Start()
  18. {
  19. UpdateText();
  20. }
  21. void Update(){
  22. }
  23. private void OnDrawGizmos() {
  24. Gizmos.DrawWireCube(transform.position, detectorSize);
  25. }
  26. [Server]
  27. public void UpdateNeighbourCount(){
  28. }
  29. [Server]
  30. public void UpdateNeighbourCount2()
  31. {
  32. targets = new List<NetPlayer>();
  33. Neighbours = new List<NetPlayer>();
  34. scannedList= new List<NetPlayer>();
  35. targets.AddRange(DTP);
  36. int failCount = 0;
  37. while(targets.Count > 0 && failCount < 50){
  38. failCount++;
  39. if(!Neighbours.Contains(targets[0])){Neighbours.Add(targets[0]);}
  40. scannedList.Add(targets[0]);
  41. foreach(NetPlayer neighbour in targets[0].touchingNeighbours){
  42. if(!scannedList.Contains(neighbour)){
  43. targets.Add(neighbour);
  44. }
  45. }
  46. scannedList.Add(targets[0]);
  47. targets.RemoveAt(0);
  48. }
  49. if(failCount >= 50){
  50. Debug.LogError("Fail switch triggered");
  51. }
  52. playersTouching = Neighbours.Count;
  53. GetComponent<Rigidbody2D>().constraints=((playersRequired - playersTouching) > 0) ? RigidbodyConstraints2D.FreezeAll : RigidbodyConstraints2D.FreezeRotation;
  54. UpdateText();
  55. }
  56. void UpdateText()
  57. {
  58. numberTxt.text = (playersRequired - Neighbours.Count).ToString();
  59. }
  60. void UpdateText(int touchers)
  61. {
  62. numberTxt.text = (playersRequired - touchers).ToString();
  63. }
  64. void OnTouchersChanged(int oldValue, int newValue){
  65. UpdateText(newValue);
  66. }
  67. }