Advertisement
Guest User

Untitled

a guest
Aug 20th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.68 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Text.RegularExpressions;
  6.  
  7.  
  8. #region Floor definition
  9. public class Floor {
  10.  
  11.     private GameObject Object;
  12.     public ElevatorDoor Door;
  13.    
  14.     public Vector3 Position {
  15.         get {
  16.             return Object.transform.position;
  17.         }
  18.     }
  19.    
  20.     public uint Number {
  21.         get {
  22.             return uint.Parse(Regex.Match(Object.name, @"\d+", RegexOptions.RightToLeft).Value);
  23.         }
  24.     }
  25.        
  26.     public Floor(GameObject floorobject, GameObject doorobject) {
  27.         this.Object = floorobject;
  28.         this.Door = new ElevatorDoor(doorobject);
  29.     }
  30.    
  31.    
  32.     public class ElevatorDoor {
  33.         public enum DoorState { Open, Closed, Opening, Closing };
  34.         public DoorState State { get; private set; }
  35.        
  36.         private float OpenedTime = 0; // when the door was last opened
  37.         private GameObject Object;
  38.         private Vector3 ScaleTransform = Vector3.left * Time.deltaTime * Elevator.DoorAnimationSpeed;
  39.        
  40.         public ElevatorDoor(GameObject gameobject) {
  41.             this.Object = gameobject;
  42.             State = DoorState.Closed;
  43.         }
  44.        
  45.         public static ElevatorDoor GetForFloor(uint floor) {
  46.             GameObject DoorObject = GameObject.Find("ElevatorDoor"+floor); 
  47.             return new ElevatorDoor(DoorObject);   
  48.         }
  49.        
  50.         public void Close() {
  51.             State = DoorState.Closing;     
  52.         }
  53.        
  54.         public void Open() {
  55.             State = DoorState.Opening;
  56.             Debug.Log("Elevator: Opening door: "+Object.name);
  57.         }
  58.        
  59.         public void Update() {
  60.             switch (State) {
  61.                 case DoorState.Opening:
  62.                     if (Object.transform.localScale.x > 0.0f) {
  63.                         Object.transform.localScale += ScaleTransform;
  64.                     } else if (Object.transform.localScale.x <= 0.0f) {
  65.                         Object.transform.localScale = new Vector3(0.0f, 1.0f, 1.0f);
  66.                         State = DoorState.Open;
  67.                         OpenedTime = Time.time;
  68.                     }
  69.                 break;
  70.                
  71.                 case DoorState.Closing:
  72.                     if (Object.transform.localScale.x < 1.0f) {
  73.                         Object.transform.localScale -= ScaleTransform;
  74.                     } else if (Object.transform.localScale.x >= 1.0f) {
  75.                         Object.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
  76.                         State = DoorState.Closed;
  77.                     }
  78.                 break;
  79.                
  80.                 case DoorState.Open:
  81.                     if ( (Time.time - OpenedTime) >= Elevator.DoorStayOpenSeconds ) {
  82.                         State = DoorState.Closing;
  83.                         if (Elevator.PrintDebugMessages) {
  84.                             Debug.Log("Elevator: Closing " + Object.name);
  85.                         }
  86.                     }
  87.                 break;
  88.             }
  89.         }
  90.    
  91.     }
  92. }
  93. #endregion
  94.  
  95.  
  96.  
  97. #region ElevatorCar definition
  98. public class ElevatorCar {
  99.  
  100.     public enum CarState { Moving, Still };
  101.     public CarState State { get; private set; }
  102.  
  103.    
  104.     private Floor TargetFloor;
  105.     private float Speed = 0.05f;
  106.     private GameObject Object;
  107.    
  108.     public ElevatorCar(GameObject carobject, float speed) {
  109.         Object = carobject;
  110.         Speed = speed;
  111.         State = CarState.Still;
  112.     }
  113.    
  114.     public void SetTargetFloor(Floor floor) {
  115.         TargetFloor = floor;
  116.         State = CarState.Moving;
  117.         if (Elevator.PrintDebugMessages) {
  118.             Debug.Log("Elevator target floor set to "+floor.Number);
  119.         }
  120.     }
  121.    
  122.     // returns the Floor object on arrival, otherwise null
  123.     public Floor Update() {
  124.         if (State != CarState.Moving) {
  125.             return null;
  126.         }
  127.        
  128.         float epsilon = 0.05f;
  129.         Vector3 TargetPosition = TargetFloor.Position;
  130.         Vector3 CarPosition = Object.transform.position;
  131.         Vector3 TransformVector = Vector3.up * Speed * Time.deltaTime;
  132.        
  133.         if (Mathf.Abs (CarPosition.y - TargetPosition.y) > epsilon) {
  134.             if (CarPosition.y < TargetPosition.y) {
  135.                 Object.transform.position += TransformVector;
  136.             }
  137.             if (CarPosition.y > TargetPosition.y) {
  138.                 Object.transform.position -= TransformVector;
  139.             }
  140.             State = CarState.Moving;
  141.         // close enough to target floor
  142.         } else {
  143.             CarPosition = TargetPosition;
  144.             State = CarState.Still;
  145.             return TargetFloor;
  146.         }
  147.         return null;
  148.     }
  149.    
  150.     public bool IsMoving() {
  151.         return State == CarState.Moving;
  152.     }
  153. }
  154. #endregion
  155.  
  156.  
  157.  
  158. #region Elevator definition
  159. public class Elevator : MonoBehaviour {
  160.    
  161.     public static float DoorAnimationSpeed = 0.7f;
  162.     public static float DoorStayOpenSeconds = 4.0f;
  163.     public static bool PrintDebugMessages = true;
  164.    
  165.     public uint CurrentFloor = 0;
  166.     public GameObject[] TargetFloors;
  167.     public float ElevatorSpeed = 0.01f;
  168.  
  169.     private Floor[] Floors;
  170.     private ElevatorCar Car;
  171.    
  172.    
  173.     // Use this for initialization
  174.     void Start () {
  175.         GameObject CarObject = GameObject.Find("ElevatorCar");
  176.         if (CarObject == null) {
  177.             Debug.LogError("Missing 'CarObject'");
  178.         }
  179.         Car = new ElevatorCar(CarObject, ElevatorSpeed);
  180.         int FloorCount = TargetFloors.Length;
  181.        
  182.         // initialize an array of Floors the elevator can go to. Each floor has it's own elevator door.
  183.         Floors = new Floor[FloorCount];
  184.         for (int i = 0; i < TargetFloors.Length; i++) {
  185.             Floors[i] = new Floor(TargetFloors[i], GameObject.Find("ElevatorDoor"+i));
  186.         }
  187.         Car.SetTargetFloor(Floors[CurrentFloor]);
  188.     }  
  189.    
  190.     // Update is called once per frame
  191.     void Update () {
  192.         Floor floor = Car.Update();
  193.         // car has just arrived on target floor
  194.         if (floor != null) {
  195.             if (Elevator.PrintDebugMessages) {
  196.                 Debug.Log("Elevator has arrived safely on floor " + floor.Number);
  197.             }
  198.             floor.Door.Open();
  199.         }
  200.         // todo: floorcollection class?
  201.         foreach (Floor f in Floors) {
  202.             f.Door.Update();
  203.         }
  204.         SelectFloor();
  205.     }
  206.    
  207.     // For easier testing
  208.     void SelectFloor() {
  209.         // this probably should not be called when debug messages are supressed
  210.         if (!Elevator.PrintDebugMessages) {
  211.             Debug.Log("Elevator: F-key floor selection enabled");
  212.         }
  213.         if (Input.GetKeyDown(KeyCode.F1)) {
  214.             Car.SetTargetFloor(Floors[0]);
  215.         } else if (Input.GetKeyDown(KeyCode.F2)) {
  216.             Car.SetTargetFloor(Floors[1]);
  217.         } else if (Input.GetKeyDown(KeyCode.F3)) {
  218.             Car.SetTargetFloor(Floors[2]);
  219.         }
  220.     }
  221.    
  222. }
  223. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement