Advertisement
GameCook

Untitled

Mar 23rd, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.79 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using EnumCubicle;
  5.  
  6. public class CubicleSignal : MonoBehaviour {
  7.  
  8.     public delegate void EventHandler();
  9.     public event EventHandler SignalEventCall; //이곳과 결속된 Cubicle_(엣지, 플레인, 라인) 컴포넌트들이 해당 이벤트 호출을 받는다.
  10.     public delegate void SwitchHandler(SignalAxis dir);
  11.     public event SwitchHandler SignalSwitchOnCall;
  12.  
  13.     [Header("Reference Linked Component")]
  14.     public CubiclePlane mamaPlane;
  15.     public CubicleEdge papaEdge;
  16.     [Space]
  17.     [Header("Axis Direction")]
  18.     public SignalAxis direction;
  19.     public float StickOutValue = 0.3f;
  20.     [SerializeField]
  21.     private Material[] MaterialColor = new Material[3];
  22.     MeshRenderer _thisRenderer;
  23.  
  24.     private void Awake()
  25.     {
  26.         _thisRenderer = gameObject.GetComponent<MeshRenderer>();
  27.     }
  28.  
  29.     public void Initialization(int index)
  30.     {
  31.         switch (index) //머터리얼 칼라 인덱스
  32.         {
  33.             case 0:
  34.                 papaEdge.xSignal = this;
  35.                 direction = SignalAxis.X;
  36.                 _thisRenderer.material = MaterialColor[0];
  37.                 transform.rotation = Quaternion.AngleAxis(90, Vector3.forward);
  38.                 name = "Signal_X";
  39.                 break;
  40.             case 1:
  41.                 papaEdge.ySignal = this;
  42.                 direction = SignalAxis.Y;
  43.                 _thisRenderer.material = MaterialColor[1];
  44.                 transform.rotation = Quaternion.identity;
  45.                 name = "Signal_Y";
  46.                 break;
  47.             case 2:
  48.                 papaEdge.zSignal = this;
  49.                 direction = SignalAxis.Z;
  50.                 _thisRenderer.material = MaterialColor[2];
  51.                 transform.rotation = Quaternion.AngleAxis(90, Vector3.right);
  52.                 name = "Signal_Z";
  53.                 break;
  54.         }
  55.         IsSwitchOn = false;
  56.     }
  57.     private bool isSwitchOn;
  58.     public bool IsSwitchOn
  59.     {
  60.         get
  61.         {
  62.             return isSwitchOn;
  63.         }
  64.         set
  65.         {
  66.             isSwitchOn = value;
  67.             DisplaySignalState();
  68.            
  69.         }
  70.     }
  71.  
  72.     private void OnMouseDown()
  73.     {
  74.         ChangeSignalSwitch();
  75.     }
  76.  
  77.     public void DisplaySignalState()
  78.     {
  79.         switch (direction)
  80.         {
  81.             case SignalAxis.X:
  82.                 if(papaEdge.sectionX == EdgeSectionX.Right)
  83.                 {
  84.                     if (IsSwitchOn) transform.localPosition = new Vector3(0, 0, 0);
  85.                     else transform.localPosition = new Vector3(-StickOutValue, 0, 0);
  86.                 }
  87.                 else
  88.                 {
  89.                     if (IsSwitchOn) transform.localPosition = new Vector3(0, 0, 0);
  90.                     else transform.localPosition = new Vector3(StickOutValue, 0, 0);
  91.                 }
  92.                 break;
  93.             case SignalAxis.Y:
  94.                 if (papaEdge.sectionY == EdgeSectionY.Up)
  95.                 {
  96.                     if (IsSwitchOn) transform.localPosition = new Vector3(0, 0, 0);
  97.                     else transform.localPosition = new Vector3(0, -StickOutValue, 0);
  98.                 }
  99.                 else
  100.                 {
  101.                     if (IsSwitchOn) transform.localPosition = new Vector3(0, 0, 0);
  102.                     else transform.localPosition = new Vector3(0, StickOutValue, 0);
  103.                 }
  104.                 break;
  105.             case SignalAxis.Z:
  106.                 if (papaEdge.sectionZ == EdgeSectionZ.Forward)
  107.                 {
  108.                     if (IsSwitchOn) transform.localPosition = new Vector3(0, 0, 0);
  109.                     else transform.localPosition = new Vector3(0, 0, -StickOutValue);
  110.                 }
  111.                 else
  112.                 {
  113.                     if (IsSwitchOn) transform.localPosition = new Vector3(0, 0, 0);
  114.                     else transform.localPosition = new Vector3(0, 0, StickOutValue);
  115.                 }
  116.                 break;
  117.         }
  118.     }
  119.  
  120.     public void ChangeSignalSwitch()
  121.     {
  122.         if (IsSwitchOn)
  123.         {
  124.             IsSwitchOn = false;
  125.         }
  126.         else {
  127.             ActionToMamaPlane();
  128.             ActionToPapaEdge();
  129.             IsSwitchOn = true;
  130.             SignalSwitchOnCall(direction);
  131.         }
  132.         SignalEventCall();
  133.     }
  134.  
  135.     void ActionToMamaPlane()
  136.     {
  137.         for (int i = 0; i < 4; i++)
  138.         {
  139.             mamaPlane.axisSignal[i].IsSwitchOn = false;
  140.         }
  141.     }
  142.     void ActionToPapaEdge()
  143.     {
  144.         papaEdge.xSignal.IsSwitchOn = false;
  145.         papaEdge.ySignal.IsSwitchOn = false;
  146.         papaEdge.zSignal.IsSwitchOn = false;
  147.  
  148.         for (int i = 0; i < 4; i++)
  149.         {
  150.             mamaPlane.axisSignal[i].papaEdge.CheckSelectionNone();
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement