Beispiel #1
0
        void Move(Direction LTRBDir)
        {
            if (actor.Dead)
            {
                return;
            }

            this.keyLTRB = LTRBDir;

            if (!moving)
            {
                SetDir(LTRBDir);

                TilePos           targetTilePos = curTilePos + LTRBDir;
                SquareTileMapNode node          = TileMapEngine.Instance.GetTileNode(targetTilePos);
                if (node)
                {
                    if (!node.Invalid)
                    {
                        this.targetTilePos      = targetTilePos;
                        this.targetTileWorldPos = node.WorldPosition;

                        StartCoroutine(MoveProcess());
                    }
                }
            }
        }
Beispiel #2
0
        IEnumerator AutoMovePathNodesProcess()
        {
            autoMovingPathChanged = false;
            SquareTileMapNode curNode = autoMovingPathNodes[0];

            while (curNode != null)
            {
                if (autoMovingPathChanged)
                {
                    if (autoMovingPathNodes.Count == 0)
                    {
                        yield break;
                    }

                    curNode = autoMovingPathNodes[0];
                    autoMovingPathChanged = false;
                }

                if (!autoMoving)
                {
                    yield break;
                }

                if (!moving)
                {
                    if (curTilePos.x != curNode.TilePosX ||
                        curTilePos.y != curNode.TilePosY)
                    {
                        Direction LTRBDir = Direction.None;

                        if (curTilePos.x != curNode.TilePosX)
                        {
                            LTRBDir = (curNode.TilePosX > curTilePos.x) ? Direction.Right : Direction.Left;
                        }
                        else if (curTilePos.y != curNode.TilePosY)
                        {
                            LTRBDir = (curNode.TilePosY > curTilePos.y) ? Direction.Top : Direction.Bottom;
                        }

                        Move(LTRBDir);
                    }

                    autoMovingPathNodes.RemoveAt(0);
                    if (autoMovingPathNodes.Count > 0)
                    {
                        curNode = autoMovingPathNodes[0];
                    }
                    else
                    {
                        curNode = null;
                    }
                }

                yield return(null);
            }

            autoMoving = false;

            yield return(null);
        }
Beispiel #3
0
        // Update is called once per frame
        void Update()
        {
            if (Input.GetMouseButtonUp(0))
            {
                Ray          ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit2D hit;
                int          layerMask = 1 << LayerMask.NameToLayer("MapTile");

                hit = Physics2D.Raycast(ray.origin, ray.direction, 100.0f, layerMask);
                if (hit.collider)
                {
                    SquareTileMapNode mapTile  = hit.collider.gameObject.GetComponent <SquareTileMapNode>();
                    SquareTileMapNode tileNode = null;
                    if (player.AutoMoving)
                    {
                        tileNode = TileMapEngine.Instance.GetTileNode(player.TargetTilePos);
                    }
                    else
                    {
                        tileNode = TileMapEngine.Instance.GetTileNode(player.CurTilePos);
                    }

                    if (mapTile && tileNode)
                    {
                        List <SquareTileMapNode> pathNodes = TileMapEngine.Instance.Calculate(tileNode, mapTile, checkObjWhenPathFind, goalCheckObjWhenPathFind);

                        if (pathNodes != null && pathNodes.Count > 0)
                        {
                            player.AutoMove(ref pathNodes);
                        }
                    }
                }
            }
        }
Beispiel #4
0
        public void SetPos(TilePos tilePos)
        {
            this.curTilePos = tilePos;

            SquareTileMapNode node = TileMapEngine.Instance.GetTileNode(tilePos);

            if (node)
            {
                curTileWorldPos   = node.WorldPosition;
                curTileWorldPos.z = 0.0f;
            }

            transform.position = curTileWorldPos;
        }
Beispiel #5
0
        IEnumerator MoveProcess()
        {
            //Debug.Log("start MoveProcess");
            moving = true;
            float beginTime = Time.time;
            float speed     = 2.0f;

            while (true)
            {
                while (transform.position != targetTileWorldPos)
                {
                    if (!moving)
                    {
                        yield break;
                    }

                    transform.position = Vector3.Lerp(curTileWorldPos, targetTileWorldPos, (Time.time - beginTime) * speed);
                    //Debug.Log("MoveProcess setpos");

                    yield return(null);
                }

                if (!moving)
                {
                    yield break;
                }

                if (keyLTRB != Direction.None)
                {
                    this.curTilePos      = this.targetTilePos;
                    this.curTileWorldPos = this.transform.position;

                    // next move exist.
                    SetDir(keyLTRB);

                    TilePos           targetTilePos = curTilePos + LTRBDir;
                    SquareTileMapNode node          = TileMapEngine.Instance.GetTileNode(targetTilePos);
                    if (node)
                    {
                        if (!node.Invalid)
                        {
                            this.targetTilePos      = targetTilePos;
                            this.targetTileWorldPos = node.WorldPosition;
                        }
                    }

                    beginTime = Time.time;
                }
                else
                {
                    break;
                }

                yield return(null);
            }

            moving               = false;
            this.curTilePos      = this.targetTilePos;
            this.curTileWorldPos = this.transform.position;

            yield return(null);
        }