Example #1
0
    protected override void OnRecv(Connection connection)
    {
        var entity = BattleManager.Instance.GetEntity(message.id);

        if (entity != null)
        {
            Vector3 pos      = new Vector3(message.position.x, 0, message.position.y);
            Vector3 velocity = new Vector3(message.velocity.x, 0, message.velocity.y);

            EntityAction action = entity.GetFirst(ActionType.Run);

            if (action != null)
            {
                action.AddPathPoint(pos, velocity, false);
            }
            else
            {
                action = ObjectPool.GetInstance <EntityAction>();
                action.AddPathPoint(pos, velocity, false);

                entity.PlayAction(ActionType.Run, action);
            }
        }
    }
Example #2
0
    // Update is called once per frame
    public void Update()
    {
        BattleManager.Instance.Update(Time.deltaTime);

        if (mEntity == null)
        {
            mEntity = BattleManager.Instance.GetEntity(1);
        }

        if (mEntity == null)
        {
            return;
        }
        if (Input.GetMouseButtonDown(1))
        {
            Ray   ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            float distance;
            mPlane.Raycast(ray, out distance);
            Vector3 point = ray.GetPoint(distance);
            var     tile  = TileAt(point);
            if (tile != null)
            {
                tile.isValid = !tile.isValid;
                tile.SetColor();
            }
        }

        if (Input.GetMouseButtonDown(0))
        {
            mShowPath = true;
        }

        if (Input.GetMouseButtonUp(0))
        {
            mShowPath = false;

            if (mTile != null)
            {
                if (mEntity != null)
                {
                    EntityAction jump = mEntity.GetFirst(ActionType.Jump);

                    if (Chess.Instance.jump)
                    {
                        if (jump == null)
                        {
                            jump = ObjectPool.GetInstance <EntityAction>();
                            jump.AddPathPoint(mTile.position, Vector3.zero, false, (entity, destination) =>
                            {
                                var p = TileAt(destination);
                                if (p != null)
                                {
                                    p.SetColor();
                                }
                            });
                            mEntity.PlayAction(ActionType.Jump, jump);
                        }
                        else
                        {
                            jump.AddPathPoint(mTile.position, Vector3.zero, false, (entity, destination) =>
                            {
                                var p = TileAt(destination);
                                if (p != null)
                                {
                                    p.SetColor();
                                }
                            });
                        }
                    }
                    else
                    {
                        var position = mEntity.position;
                        if (jump != null && jump.paths.Count > 0)
                        {
                            position = jump.paths.Last.Value.destination;
                        }

                        var result = FindPath(TileAt(position), mTile,
                                              (tile) => { return(tile.isValid); },
                                              (tile) => { return(Neighbours(tile)); },
                                              GetCostValue);

                        if (jump == null)
                        {
                            jump = ObjectPool.GetInstance <EntityAction>();
                            while (result.Count > 0)
                            {
                                var path = result.Pop();
                                jump.AddPathPoint(path.position, Vector3.zero, false, (entity, destination) =>
                                {
                                    var p = TileAt(destination);
                                    if (p != null)
                                    {
                                        p.SetColor();
                                    }
                                },
                                                  (entity, destination) =>
                                {
                                    var p = TileAt(destination);
                                    if (p != null)
                                    {
                                        p.SetColor();
                                    }
                                });
                            }

                            mEntity.PlayAction(ActionType.Jump, jump);
                        }
                        else
                        {
                            while (result.Count > 0)
                            {
                                var path = result.Pop();
                                jump.AddPathPoint(path.position, Vector3.zero, false, (entity, destination) =>
                                {
                                    var p = TileAt(destination);
                                    if (p != null)
                                    {
                                        p.SetColor();
                                    }
                                },
                                                  (entity, destination) =>
                                {
                                    var p = TileAt(destination);
                                    if (p != null)
                                    {
                                        p.SetColor();
                                    }
                                });
                            }
                        }
                    }
                }
            }

            mTile = null;
        }

        if (mShowPath)
        {
            if (mPathRenderer == null)
            {
                if (root)
                {
                    mPathRenderer = root.GetComponent <LineRenderer>();
                    if (mPathRenderer == null)
                    {
                        mPathRenderer = root.gameObject.AddComponent <LineRenderer>();
                    }
                    AssetLoader.LoadAsset <Material>("arrow.mat", (asset) => {
                        mPathRenderer.material = asset.assetObject;
                    });
                    mPathRenderer.startWidth        = 1f;
                    mPathRenderer.endWidth          = 1f;
                    mPathRenderer.startColor        = Color.yellow;
                    mPathRenderer.endColor          = Color.yellow;
                    mPathRenderer.receiveShadows    = false;
                    mPathRenderer.shadowCastingMode = ShadowCastingMode.Off;
                }
            }

            if (mPathRenderer != null)
            {
                Ray   ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                float distance;
                mPlane.Raycast(ray, out distance);
                Vector3 point = ray.GetPoint(distance);
                var     tile  = TileAt(point);
                if (tile == null)
                {
                    int minDistance = -1;
                    var it          = tiles.GetEnumerator();
                    while (it.MoveNext())
                    {
                        int d = Distance(point, it.Current.Value.position);
                        if (minDistance == -1 || d < minDistance)
                        {
                            tile        = it.Current.Value;
                            minDistance = d;
                        }
                    }
                }
                if (tile != null)
                {
                    if (mTile == null || (tile.index != mTile.index))
                    {
                        Vector3 position = mEntity.position;
                        var     run      = mEntity.GetLast(ActionType.Jump);
                        if (run != null)
                        {
                            if (run.paths.Count > 0)
                            {
                                position = run.paths.Last.Value.destination;
                            }
                        }
                        BattleHexTile t = TileAt(position);

                        if (t != null && t.index == tile.index)
                        {
                            ClearPath();
                        }
                        else
                        {
                            if (mTile != null)
                            {
                                mTile.Select(false);
                            }

                            mTile = tile;

                            mTile.Select(true);
                            if (Chess.Instance.jump == false)
                            {
                                var result = FindPath(t, mTile,
                                                      (o) => { return(o.isValid); },
                                                      (o) => { return(Neighbours(o)); },
                                                      GetCostValue
                                                      );

                                var it = tiles.GetEnumerator();
                                while (it.MoveNext())
                                {
                                    it.Current.Value.SetColor();
                                }

                                var jumps = mEntity.GetActions(ActionType.Jump);
                                if (jumps != null)
                                {
                                    var j = jumps.GetEnumerator();
                                    while (j.MoveNext())
                                    {
                                        var jump = j.Current as EntityAction;
                                        var d    = jump.paths.GetEnumerator();
                                        while (d.MoveNext())
                                        {
                                            var dest = TileAt(d.Current.destination);
                                            if (dest != null)
                                            {
                                                dest.SetColor(Color.green);
                                            }
                                        }
                                    }
                                }


                                var r = result.GetEnumerator();
                                while (r.MoveNext())
                                {
                                    r.Current.SetColor(Color.green);
                                }
                            }
                            BattleBezierPath.GetPath(position,
                                                     mTile.position,
                                                     0.5f,
                                                     ActionJumpPlugin.SPEED,
                                                     ActionJumpPlugin.MINHEIGHT,
                                                     ActionJumpPlugin.MAXHEIGHT,
                                                     ActionJumpPlugin.GRAVITY,
                                                     ref mPathPoints);


                            mPathRenderer.positionCount = mPathPoints.Count;
                            mPathRenderer.SetPositions(mPathPoints.ToArray());
                        }
                    }
                }
                else
                {
                    ClearPath();
                }
            }
        }
        else
        {
            ClearPath();
        }
    }