Beispiel #1
0
    void OnMouseDown()
    {
        if (Input.GetKey("left ctrl"))
        {
            return;
        }

        var cam = Camera.main;

        if (cam is null)
        {
            return;
        }

        var msepos = Input.mousePosition;

        if (cam.orthographic)
        {
            msepos.z = 0;
        }

        var ray   = cam.ScreenPointToRay(msepos);
        var plane = new Plane(gameObject.transform.up, gameObject.transform.position);

        if (plane.Raycast(ray, out var enter))
        {
            var main  = GameObject.Find("SystemInit").GetComponent <main>();
            var inv   = gameObject.transform.parent.InverseTransformPoint(ray.GetPoint(enter));
            var coord = main.TranslateWorldCoordToGrid(inv);

            var stickatar = GameObject.Find("Stickatar");
            var pathcom   = stickatar.GetComponent <FollowPath>();

            // cast a ray straight down toward the gameboard.

            //Debug.DrawRay(stickatar.transform.position, -gameObject.transform.up * 20, Color.red, 100);
            //Debug.DrawRay(stickatar.transform.position - (gameObject.transform.up*5), gameObject.transform.up * 20, Color.red, 100);

            var stickray = new Ray(stickatar.transform.position - (gameObject.transform.up * 5), gameObject.transform.up);
            if (plane.Raycast(stickray, out var stickrayintersect))
            {
                var invpos     = stickatar.transform.parent.InverseTransformPoint(stickray.GetPoint(stickrayintersect));
                var stickcoord = main.TranslateWorldCoordToGrid(invpos);
                var start      = new int2((int)(stickcoord.x), (int)(stickcoord.y));
                var target     = new int2((int)(coord.x), (int)(coord.y));

                var map = FloorZone.Current.Map;

                if (target.x < 0 || target.y < 0)
                {
                    return;
                }
                if (target.y >= map.Length)
                {
                    return;
                }
                if (target.x >= map[0].Length)
                {
                    return;
                }

                var path_iter = Yieldable.FindPath(map, GlobalPool.yieldablePathState, start, target);
                GlobalPool.yPathWaypoints.Clear();
                GlobalPool.AppendWaypoints(path_iter);

                var stickpath = stickatar.GetComponent <FollowPath>();
                stickpath.waypoints.Clear();

                var waypoints = GlobalPool.yPathWaypoints;
                var startidx  = waypoints.Count - 1;
                var walkstart = waypoints[startidx];
                stickpath.waypoints.Add(main.TranslateGridCoordToWorld(walkstart));

                for (int i = startidx - 1; i >= 0; --i)
                {
                    var next = waypoints[i];
                    stickpath.waypoints.Add(main.TranslateGridCoordToWorld(next));
                }
                stickpath.ApplyWaypointList();
            }

            //var start = pathcom.GetMapPos2D();
            //AStar.Yieldable.FindPath(main.map, GlobalPathState.state, start, target);
            //stickatar.GetComponent<FollowPath>().ApplyWaypointList();

            // Move cube GameObject to the point clicked
            // m_Cube.transform.position = hitPoint;
        }
    }