Esempio n. 1
0
    //Private methods
    private GameObject AddElement(XmlNode ElementDescription)
    {
        GameObject  _Element;
        Vector3     _Coordinates;
        XmlNodeList _CoordinatesDescription;

        switch (ElementDescription.Name)
        {
        case "Workbench":
            //Defenition
            ushort _type             = ushort.Parse(ElementDescription.ChildNodes[1].InnerText);
            PhantomConstruction _phc = new PhantomConstruction(Workbenches.Templates.Find(x => x.Type == _type));
            _CoordinatesDescription = ElementDescription.ChildNodes[0].ChildNodes;
            _Coordinates            = new Vector3(float.Parse(_CoordinatesDescription[0].InnerText), float.Parse(_CoordinatesDescription[1].InnerText) + 0.05f, float.Parse(_CoordinatesDescription[2].InnerText));
            byte _rotation = byte.Parse(ElementDescription.ChildNodes[2].InnerText);

            //Processing
            ChildConstructions.Add(_phc);
            _phc.MasterObject.transform.SetParent(MasterObject.transform);
            _phc.Rotate(_rotation);
            _phc.Show(_Coordinates);
            _Element = _phc.MasterObject;
            break;

        case "Brick":
            //Defenition
            _CoordinatesDescription = ElementDescription.ChildNodes[0].ChildNodes;
            _Coordinates            = new Vector3(float.Parse(_CoordinatesDescription[0].InnerText), float.Parse(_CoordinatesDescription[1].InnerText) + 0.05f, float.Parse(_CoordinatesDescription[2].InnerText));
            ushort _ResID = ushort.Parse(ElementDescription.ChildNodes.Item(1).InnerText);

            //Processing
            _Element = MonoBehaviour.Instantiate(Settings.BrickPrefab, Vector3.zero, Quaternion.identity);
            _Element.transform.SetParent(MasterObject.transform, false);
            _Element.transform.localPosition = _Coordinates;
            _Element.GetComponent <MeshRenderer>().material = Resources.GetResource(_ResID).Material;
            _Element.name  = Resources.GetResource(_ResID).Name;
            _Element.tag   = "Untagged";
            _Element.layer = 10;
            Highlighter.Phantomize(_Element);
            break;

        default:
            Log.Warning(scr, "Unknown element type in phantom constructing: ");
            Functions.ReadXMLNode(ElementDescription, scr);
            _Element = null;
            break;
        }
        return(_Element);
    }
Esempio n. 2
0
    void Update()
    {
        if (!_BuildModeOff)
        {
            //Camera moving rules
            if (Input.mouseScrollDelta.y < 0 && _BasicObject.transform.localScale.x > 0.1f)
            {
                _BasicObject.transform.localScale += Vector3.one * (_ScrollSpeed * Input.mouseScrollDelta.y * Time.deltaTime); // когда крутишь колесиком, надо чуть чуть смещать основной блок к центру. (или отдалять)
                _BasicObject.transform.position   -= Vector3.Normalize(_Center - _BasicObject.transform.position) * (_ScrollSpeed * Input.mouseScrollDelta.y * Time.deltaTime);
            }
            if (Input.mouseScrollDelta.y > 0 && _BasicObject.transform.localScale.x < 25f)
            {
                _BasicObject.transform.localScale += Vector3.one * (_ScrollSpeed * Input.mouseScrollDelta.y * Time.deltaTime);
                _BasicObject.transform.position   -= Vector3.Normalize(_Center - _BasicObject.transform.position) * (_ScrollSpeed * Input.mouseScrollDelta.y * Time.deltaTime);
            }
            if (Input.GetMouseButtonDown(1))
            {
                _PreviousMousePosition = Input.mousePosition;
            }
            if (Input.GetMouseButton(1))
            {
                _HorisontalDeltaPos = (_PreviousMousePosition.x - Input.mousePosition.x);
                _VerticalDeltaPos   = -(_PreviousMousePosition.y - Input.mousePosition.y);
                _BasicObject.transform.RotateAround(_BasicObject.transform.up, _HorisontalDeltaPos * _RotateSpeed / 32); // нужно чтоб фигура вращалась вокруг центра, а не вокруг основного блока. А для этого нужно перемещать основной блок немножко в сторону
                _BasicObject.transform.RotateAround(_RightForCameraDirection, _VerticalDeltaPos * _RotateSpeed / 32);
                _PreviousMousePosition = Input.mousePosition;
            }
            if (Input.GetMouseButtonDown(2))
            {
                _PreviousMousePosition = Input.mousePosition;
            }
            if (Input.GetMouseButton(2))
            {
                _VerticalDeltaPos = _PreviousMousePosition.y - Input.mousePosition.y;
                _BasicObject.transform.Translate(Vector3.up * _VerticalDeltaPos * -1 * _TransformSpeed);
                _HorisontalDeltaPos              = _PreviousMousePosition.x - Input.mousePosition.x;
                _BasicObject.transform.position += _RightForCameraDirection * _HorisontalDeltaPos * -1 * _TransformSpeed;
                _PreviousMousePosition           = Input.mousePosition;
            }

            //User inputs reaction
            if (!EventSystem.current.IsPointerOverGameObject())
            {
                if (_IsHit)
                {
                    Highlighter.UnHighLight(_Hit.transform.gameObject);
                }
                _IsHit = Physics.Raycast(Links.MainCamera.ScreenPointToRay(Input.mousePosition), out _Hit, 200, _mask);
                if (_IsHit)
                {
                    /*if(_Hit.transform.tag == "Brick")*/ Highlighter.HighLight(_Hit.transform.gameObject);
                    //Debug.Log("Dont touch my " + _Hit.transform.name + "!!!1!11odinodin");
                    if (Input.GetKey("r"))
                    {
                        _Hit.transform.gameObject.GetComponent <Renderer>().material = Resources.ResourceLibrary[_ResourceSelectDD.value].Material;
                    }
                    if (Input.GetMouseButtonDown(0) && _Hit.transform.tag == "Brick")
                    {
                        if (_PhantomWorkbench != null)
                        {
                            Highlighter.UnHighLight(_Hit.transform.gameObject);
                            _IsHit = false;
                            _PhantomWorkbench.UnPhantomize();
                            _BuildingWorkbenches.Add(_PhantomWorkbench);
                            Debug.Log("Create workbench, master local position is:" + _PhantomWorkbench.MasterObject.transform.localPosition);
                            ClearASpaceForWorkbench(_BuildingWorkbenches.Count - 1);
                            _PhantomWorkbench = null;
                        }
                        else
                        {
                            _Normal           = _Hit.normal;
                            _NewBrickPosition = _Hit.transform.position + _Normal * _BasicObject.transform.lossyScale.x;
                            _BuildingElements.Add(Instantiate(Settings.BrickPrefab, _NewBrickPosition, _Hit.transform.rotation));
                            _BuildingElements[_BuildingElements.Count - 1].transform.localScale = _BasicObject.transform.localScale;
                            _BuildingElements[_BuildingElements.Count - 1].layer = 9;
                            _BuildingElements[_BuildingElements.Count - 1].transform.SetParent(_BasicObject.transform, true);
                            _BuildingElements[_BuildingElements.Count - 1].GetComponent <Renderer>().material = Resources.ResourceLibrary.Find(x => x.Name == _ResourceSelectDD.options[_ResourceSelectDD.value].text).Material;
                        }
                    }
                    if (_PhantomWorkbench != null)
                    {
                        _PhantomWorkbench.Hide();
                        _PhantomWorkbench.Show(_Hit.transform.localPosition);
                        if (Input.GetKeyDown("r"))
                        {
                            _PhantomWorkbench.Rotate();
                        }
                    }
                    if (Input.GetKeyUp("d"))
                    {
                        _IsHit = false;
                        switch (_Hit.transform.tag)
                        {
                        case "Brick":
                            _BuildingElements.Remove(_Hit.transform.gameObject);
                            Destroy(_Hit.transform.gameObject);
                            break;

                        case "WorkbenchBrick":
                            _BuildingWorkbenches.RemoveAll(x => x.MasterObject == _Hit.transform.parent.gameObject);
                            Destroy(_Hit.transform.parent.gameObject);
                            break;

                        default:
                            break;
                        }
                        if (_PhantomWorkbench != null)
                        {
                            _PhantomWorkbench.Hide();
                        }
                    }
                }
                else
                if (_PhantomWorkbench != null)
                {
                    _PhantomWorkbench.Hide();
                }
                if (Input.GetMouseButtonDown(1))
                {
                    if (_PhantomWorkbench != null)
                    {
                        _PhantomWorkbench.Destroy();
                        _PhantomWorkbench = null;
                    }
                }
            }
        }
    }
 void Update()
 {
     if (!_PopupMode)
     {
         if (_CurrentObject != null)
         {
             Highlighter.UnHighLight(_CurrentObject);
             _CurrentObject = null;
         }
         if (_CurrentUnit != null)
         {
             Highlighter.UnHighLight(_CurrentUnit);
             _CurrentUnit = null;
         }
         if (Input.GetKeyDown("z"))
         {
             Settings.ZoneRod.SetActive(true);
         }
         if (Input.GetKeyUp("z") && _ZoneStartPoint == null)
         {
             Settings.ZoneRod.SetActive(false);
         }
         if (!EventSystem.current.IsPointerOverGameObject())             //Изучить. Непонятно что это, но работает
         {
             _CameraRay = _Camera.ScreenPointToRay(Input.mousePosition); //создаем луч, идущий из камеры через координаты мышки
             _IsHit     = Physics.Raycast(_CameraRay, out _Hit, 1000, mask);
             if (_IsHit)
             {
                 if (_Hit.transform.tag == "Brick")
                 {
                     if ((_BuildingMode) && (_Hit.transform.gameObject != _CurrentObject))
                     {
                         _PhantomBuilding.Hide();
                         _PhantomBuilding.Show(_Hit.transform.position);
                     }
                     _CurrentObject = _Hit.transform.gameObject;
                     Highlighter.HighLight(_CurrentObject);
                     if (Settings.ZoneRod.activeInHierarchy)
                     {
                         Settings.ZoneRod.transform.SetParent(_CurrentObject.transform, false);
                     }
                 }
                 if (_Hit.transform.tag == "Unit")
                 {
                     _CurrentUnit = _Hit.transform.gameObject;
                     Highlighter.HighLight(_CurrentUnit);
                 }
             }
             if (Input.GetMouseButtonDown(0))
             {
                 if (PickedObject != null)
                 {
                     Highlighter.UnPick(PickedObject);
                 }
                 if (_IsHit)
                 {
                     if (_Hit.transform.tag == "Brick" && _BuildingMode)
                     {
                         new ProcessManager.Building(_PhantomBuilding);
                         //Building _b = new Building(_BuildingSelectionDD.value, _PhantomBuilding.Rotation, _PhantomBuilding.MasterObject.transform.position);
                         //_PhantomBuilding.Destroy();
                         //Brick_intreraction_script.AddBuilding(_b);
                         _PhantomBuilding = null;
                         _BuildingMode    = false;
                     }
                     else
                     {
                         if (Settings.ZoneRod.activeInHierarchy) //В режиме выбора зоны
                         {
                             if (_ZoneStartPoint == null)        //Стартовая точка не задана
                             {
                                 _ZoneStartPoint = Instantiate(Settings.ZoneRod);
                                 _ZoneStartPoint.transform.SetParent(_CurrentObject.transform, false);
                             }
                             else //Стартовая точка задана
                             {
                                 //_ZoneEndPoint = Settings.ZoneRod; Пока написана херня для теста
                                 _ZoneEndPoint = Instantiate(Settings.ZoneRod);
                                 _ZoneEndPoint.transform.SetParent(_CurrentObject.transform, false);
                                 Settings.ZoneRod.SetActive(false);
                                 Links.Interface.SwitchToZoneMenu(0);
                             }
                         }
                         PickedObject = _Hit.transform.gameObject;
                         PickedObject.BroadcastMessage("Click");
                         Highlighter.Pick(PickedObject);
                         _Normal = _Hit.normal;
                         Log.Notice(scr + scrm, "Picked Object : " + PickedObject.name);
                     }
                 }
                 else
                 {
                     PickedObject = null;
                     Settings.ZoneRod.SetActive(false);
                     Destroy(_ZoneStartPoint);
                 }
             }
         }
         if (Input.GetKeyDown("a"))
         {
             if (PickedObject != null && PickedObject.tag == "Unit")
             {
                 if (_ActorUnit != null)
                 {
                     Highlighter.UnPick(_ActorUnit);
                 }
                 _ActorUnit   = PickedObject;
                 PickedObject = null;
                 Highlighter.BrightPick(_ActorUnit);
             }
             else
             {
                 if (_ActorUnit != null)
                 {
                     Highlighter.UnPick(_ActorUnit);
                 }
                 _ActorUnit = null;
             }
         }
         if (Input.GetKeyDown("r"))
         {
             if (_PhantomBuilding != null)
             {
                 _PhantomBuilding.Rotate();
             }
         }
         if (Input.GetMouseButton(1) && _ActorUnit != null)
         {
             Log.Notice(scr, "Walking");
             if ((_IsHit) && (_Hit.normal == Vector3.up))
             {
                 //_Normal.x = Mathf.RoundToInt(_Hit.point.x);
                 //_Normal.z = Mathf.RoundToInt(_Hit.point.z);
                 //_Normal.y = _Hit.point.y;
                 Log.Notice(scr, "Waybuilder call");
                 //_ActorUnit.GetComponent<RouteBuilder>().Build(_Normal);
                 _ActorUnit.GetComponent <Unit>().GoTo(_Hit.point);
             }
         }
         if (Input.GetMouseButton(1))//Massive reject
         {
             _BuildingMode = false;
             if (_PhantomBuilding != null)
             {
                 _PhantomBuilding.Destroy();
                 _PhantomBuilding = null;
             }
             CancelZoneCreation();
         }
         if (PickedObject != null && PickedObject.tag == "Unit" && Input.GetKey("l"))
         {
             PickedObject.GetComponent <RouteBuilder>().LogRoute();
         }
         if (Input.GetKey("o"))
         {
             if (!(PickedObject == null || PickedObject.tag == "Unit"))
             {
                 CreateUnit(PickedObject.transform.position);
                 Highlighter.UnPick(PickedObject);
                 PickedObject = null;
             }
             else
             {
                 Log.Notice(scr, "There is no picked cube");
             }
         }
     }
     else // Popup is open
     {
     }
 }