Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.61 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4.  
  5. public class ElevatorSystem : MonoBehaviour
  6. {
  7.     public UIController uiController;
  8.     Dictionary<int?, string> ParkingMap;
  9.     ElevatorController controller;
  10.     private const int ParkingCount = 32;
  11.     private string lastCardId;
  12.     private bool AttachAlertMessage = false;
  13.     private bool ChooseActionAlert = false;
  14.  
  15.     private void Start()
  16.     {
  17.         InitializeParking();
  18.         controller = GameObject.Find("BlackBox").GetComponent<ElevatorController>();
  19.     }
  20.  
  21.  
  22.     private void Update()
  23.     {
  24.         if (!controller.IsBicycleAttached() && !controller.isCompleted)
  25.         {
  26.             uiController.PromptStatus("Please wait...");
  27.         }
  28.         else if (controller.isCompleted)
  29.         {
  30.             uiController.PromptStatus("Attach the card");
  31.         }
  32.         if (AttachAlertMessage)
  33.         {
  34.             uiController.PromptStatus("Attach the bicycle!");
  35.         }
  36.         if (ChooseActionAlert)
  37.         {
  38.             uiController.PromptStatus("Choose action");
  39.         }
  40.     }
  41.  
  42.     public void FindNewCard (string cardId)
  43.     {
  44.         var parkingPlaces = ParkingMap.Where(a => a.Value == cardId).Select(a => a.Key);
  45.         if (controller.isCompleted)
  46.         {
  47.             if (parkingPlaces.Count() > 0)
  48.             {
  49.                 ChooseActionAlert = true;
  50.                 List<string> result = new List<string>();
  51.                 foreach(var i in parkingPlaces)
  52.                 {
  53.                     result.Add(i.Value.ToString());
  54.                 }
  55.                 uiController.PromptBicycles(result);
  56.                 lastCardId = cardId;
  57.                 AttachAlertMessage = false;
  58.             }
  59.             else
  60.             {
  61.                 if (controller.IsBicycleAttached())
  62.                 {
  63.                     uiController.PromptStatus("Parking...");
  64.                     var parkingPlace = ParkingMap.FirstOrDefault(a => a.Value == null);
  65.                     ParkingMap[parkingPlace.Key] = cardId;
  66.                     controller.BicycleParkingEvent(
  67.                         GetParkingLevel((int)parkingPlace.Key),
  68.                         GetParkingAngle((int)parkingPlace.Key),
  69.                         true);
  70.                     AttachAlertMessage = false;
  71.                     ChooseActionAlert = false;
  72.                 } else
  73.                 {
  74.                     AttachAlertMessage = true;
  75.                     ChooseActionAlert = false;
  76.                 }
  77.             }
  78.         }
  79.     }
  80.  
  81.     private int GetParkingLevel(int key)
  82.     {
  83.         if(key < 8)
  84.         {
  85.             return 1;
  86.         } else if(key < 16)
  87.         {
  88.             return 2;
  89.         } else if(key < 24)
  90.         {
  91.             return 3;
  92.         } else if(key < 32)
  93.         {
  94.             return 4;
  95.         }
  96.         return 0;
  97.     }
  98.  
  99.     private int GetParkingAngle(int key)
  100.     {
  101.         if (key < 8)
  102.         {
  103.             return key;
  104.         }
  105.         else if (key < 16)
  106.         {
  107.             return key - 8;
  108.         }
  109.         else if (key < 24)
  110.         {
  111.             return key - 16;
  112.         }
  113.         else if (key < 32)
  114.         {
  115.             return key - 24;
  116.         }
  117.         return 0;
  118.     }
  119.  
  120.     private void InitializeParking ()
  121.     {
  122.         ParkingMap = new Dictionary<int?, string> ();
  123.         for (int i = 0; i < ParkingCount; i++)
  124.         {
  125.             ParkingMap.Add (i, null);
  126.         }
  127.     }
  128.  
  129.     public void SelectedParking(int parkingNumber)
  130.     {
  131.         if (controller.IsBicycleAttached())
  132.         {
  133.             uiController.PromptStatus("Deattach the bicycle!");
  134.             AttachAlertMessage = false;
  135.             ChooseActionAlert = false;
  136.         }
  137.         else
  138.         {
  139.             controller.BicycleParkingEvent(
  140.                 GetParkingLevel(parkingNumber),
  141.                 GetParkingAngle(parkingNumber),
  142.                 false);
  143.             ParkingMap[parkingNumber] = null;
  144.             AttachAlertMessage = false;
  145.             ChooseActionAlert = false;
  146.         }
  147.     }
  148.  
  149.     public void ParkingBicycle()
  150.     {
  151.         if (!controller.IsBicycleAttached())
  152.         {
  153.             AttachAlertMessage = true;
  154.             ChooseActionAlert = false;
  155.         }
  156.         else
  157.         {
  158.             var parkingPlace = ParkingMap.FirstOrDefault(a => a.Value == null);
  159.             ParkingMap[parkingPlace.Key] = lastCardId;
  160.             controller.BicycleParkingEvent(
  161.                 GetParkingLevel((int)parkingPlace.Key),
  162.                 GetParkingAngle((int)parkingPlace.Key),
  163.                 true);
  164.             AttachAlertMessage = false;
  165.             ChooseActionAlert = false;
  166.         }
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement