示例#1
0
    void Awake()
    {
        _navmesh = Plane.GetComponent <DotsNavNavmesh>();

        foreach (var obstacle in FindObjectsOfType <DotsNavObstacle>())
        {
            var l = new List <Vector2>();
            for (int i = 0; i < obstacle.Vertices.Length; i++)
            {
                l.Add(obstacle.GetVertexWorldSpace(i).xz);
            }
            _toDump.Add(l);
        }

        _cameraController = FindObjectOfType <CameraController>();
        _cameraController.Initialize(Plane.Size);
        _lineDrawer = GetComponent <LineDrawer>();
        _camera     = Camera.main;
        Help.gameObject.SetActive(!Application.isEditor);
        _agent = FindObjectOfType <DotsNavPathFindingAgent>();

        var tr = _agent.transform;

        _start       = tr.parent;
        _goal        = _start.Find("Goal");
        _goal.parent = null;

        if (Reverse)
        {
            var tempPos = _start.position;
            _start.position = _goal.position;
            _goal.position  = tempPos;
        }

        var size = _start.localScale.x;
        var s    = new Vector3(size, size, size);

        _goal.localScale = s;
        _agent.GetComponent <DotsNavAgent>().Radius = size / 2;
    }
示例#2
0
        void Start()
        {
            // Ensure gameobject conversion when loading a scene
            World.All[0].GetOrCreateSystem <InitializationSystemGroup>().Update();

            _navmesh          = Plane.GetComponent <DotsNavNavmesh>();
            _cameraController = FindObjectOfType <CameraController>();
            _cameraController.Initialize(Plane.Size, .5f);
            _lineDrawer = GetComponent <LineDrawer>();
            _camera     = Camera.main;
            Help.gameObject.SetActive(!Application.isEditor);
            _agent = FindObjectOfType <DotsNavPathFindingAgent>();
            var tr = _agent.transform;

            _start       = tr.parent;
            _goal        = _start.Find("Goal");
            _goal.parent = null;
            var size = _start.localScale.x;
            var s    = new Vector3(size, size, size);

            _goal.localScale = s;
            _agent.GetComponent <DotsNavAgent>().Radius = size / 2;
        }
示例#3
0
    void ProcessInputAndUpdateUi()
    {
        if (Input.GetKeyDown(KeyCode.M))
        {
            SceneManager.LoadScene("menu");
            return;
        }

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            _points.Clear();
            _points.Add(new List <Vector2>());
            _placingPrefab = -1;
        }

        var prev = _placingPrefab;

        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            _placingPrefab = 0;
        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            _placingPrefab = 1;
        }
        if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            _placingPrefab = 2;
        }
        if (Input.GetKeyDown(KeyCode.Alpha4))
        {
            _placingPrefab = 3;
        }
        if (Input.GetKeyDown(KeyCode.Alpha5))
        {
            _placingPrefab = 4;
        }

        var mousePosRaw = Input.mousePosition;

        mousePosRaw.z = _camera.nearClipPlane;
        var mousePos    = (Vector2)_camera.ScreenToWorldPoint(mousePosRaw).xz();
        var scrollDelta = Input.mouseScrollDelta.y;
        var mouseDelta  = mousePos - _previousMouse;

        _previousMouse = mousePos;

        if (_placingPrefab != -1)
        {
            if (prev != _placingPrefab)
            {
                _prefabSize = _placingPrefab < 4
                    ? _cameraController.Zoom
                    : Mathf.Min(1, _cameraController.Zoom * 2);
            }

            if (scrollDelta != 0 && Input.GetKey(KeyCode.LeftShift))
            {
                _prefabRotation += scrollDelta * PrefabRotationSpeed;
            }

            if (scrollDelta != 0 && Input.GetKey(KeyCode.LeftControl))
            {
                _prefabSize = math.clamp(_prefabSize + scrollDelta * PrefabSizeSpeed * _prefabSize, PrefabMinSize, PrefabMaxSize);
            }


            _points.Clear();

            var start = _placingPrefab;
            var end   = _placingPrefab == 4 ? 14 : _placingPrefab + 1;

            for (int i = start; i < end; i++)
            {
                var obstacle = Prefabs[i];
                var verts    = obstacle.Vertices;
                var points   = new List <Vector2>();
                foreach (var vert in verts)
                {
                    points.Add(DemoMath.Rotate(vert * _prefabSize, _prefabRotation) + mousePos);
                }
                _points.Add(points);
            }

            if (Input.GetMouseButtonDown(0))
            {
                Insert();
            }
        }
        else
        {
            if (Input.GetMouseButtonDown(1) && _points[0].Count > 0)
            {
                if (mousePos != _points[0].Last())
                {
                    _points[0].Add(mousePos);
                }
                if (_points[0].Count > 1)
                {
                    if ((Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && _points.Count > 2)
                    {
                        _points[0].Add(_points[0][0]);
                    }
                    Insert();
                }
            }

            if (Input.GetMouseButtonDown(0))
            {
                var ray = _camera.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray.origin, ray.direction * 10, out var hit))
                {
                    _target = hit.collider.gameObject;
                }
                if (_target == null || _points[0].Count > 0)
                {
                    _points[0].Add(mousePos);
                }
            }

            if (Input.GetMouseButtonUp(0))
            {
                _target = null;
            }

            if (scrollDelta != 0 && Input.GetKey(KeyCode.LeftControl))
            {
                var size = _start.localScale.x;
                size = math.clamp(size + scrollDelta * AgentSizeZoomSpeed * size, MinAgentSize, MaxAgentSize);
                var s = new Vector3(size, size, size);
                _start.localScale = s;
                _goal.localScale  = s;
                _agent.GetComponent <DotsNavAgent>().Radius = size / 2;
            }

            if (_target != null && Input.GetMouseButton(0) && !Input.GetMouseButtonDown(0) && mouseDelta != Vector2.zero)
            {
                _target.transform.position += mouseDelta.ToXxY();
            }
        }


        if (Input.GetKeyDown(KeyCode.E))
        {
            _navmesh.DrawMode = _navmesh.DrawMode == DrawMode.Constrained ? DrawMode.Both : DrawMode.Constrained;
        }
        if (Input.GetKeyDown(KeyCode.H))
        {
            Help.gameObject.SetActive(!Help.gameObject.activeSelf);
        }

        if ((Input.GetKey(KeyCode.T) || Input.GetKeyDown(KeyCode.R)) && _obstacles.Count > 0)
        {
            Plane.RemoveObstacle(_obstacles.Last());
            _obstacles.RemoveAt(_obstacles.Count - 1);
        }

        foreach (var points in _points)
        {
            for (int i = 1; i < points.Count; i++)
            {
                _lineDrawer.DrawLine(points[i - 1], points[i], Color.cyan);
            }
        }
        if (_placingPrefab == -1 && _points[0].Count > 0)
        {
            _lineDrawer.DrawLine(_points[0][_points[0].Count - 1], mousePos, Color.cyan);
        }
    }