/// <summary> /// 取消移动(手动取消不会触发移动完成事件) /// </summary> public void CancelMove() { // TODO: path.Clear(); path = null; currentNode = null; enabled = false; }
protected override void OnAwake() { touchRegion = transform.Find("TouchRegion").gameObject.GetComponentStrict <BoxCollider2D>(); GameObject spellGameObject = transform.Find("SpellButton").gameObject; spellRegion = spellGameObject.GetComponentStrict <BoxCollider2D>(); spellButton = spellGameObject.transform.Find("spellbutton").GetComponentStrict <SpriteWrapper>(); cooldownText = spellGameObject.transform.Find("cooldowntext").GetComponentStrict <Text>(); moveGrid = transform.Find("MoveGrid").gameObject.GetComponentStrict <MoveGrid>(); waypoint = Game.Resources.LoadPrefab <Waypoint>("Waypoints/Waypoint"); }
private void UpdateMove(float dt) { if (path == null) { return; } // TODO:这个时候结束会导致少移动半格 if (currentNode == null) { if (path.Count == 0) { Entity.State = EntityStateType.Idle; enabled = false; if (OnMoveCompleteEvent != null) { OnMoveCompleteEvent(this); } return; } currentNode = path.First.Value; path.RemoveFirst(); // 当前位置到目标位置的向量的单位化*移动速度 CalcNewVelocity(); // 战斗模式下拆墙(否则直接穿墙 if (GameWorld.Instance.worldType == WorldType.Battle || GameWorld.Instance.worldType == WorldType.Replay) { // 不可穿越墙的情况下:转到拆墙 if (!Entity.CanOverTheWall() && IsoMap.Instance.IsWallorLinker(currentNode.X, currentNode.Y)) { // 禁用AI和移动组件并唤醒拆墙AI组件 Entity.GetComponent <ActorComponent>().enabled = false; this.enabled = false; Entity.GetComponent <ActorDestroyWallComponent>().WakeUp(currentNode.X, currentNode.Y); return; } } // 动画方向调整 Entity.PlayAnimationFaceToTile(AnimationNames.Run, new TilePoint(currentNode.X, currentNode.Y)); } // [速度提升] 移动速度倍率 float speedRate = 1.0f; GameBufferComponent buffMgr = Entity.GetComponent <GameBufferComponent>(); if (buffMgr != null) { var buffer = buffMgr.GetBuffer(Constants.BUFF_TYPE_SPPEDUP); if (buffer != null) { speedRate *= buffer.buffDamage; } } // 移动 Entity.tileOffset += velocity * dt * speedRate; #if UNITY_EDITOR if (GameWorld.Instance.worldType == WorldType.Battle || GameWorld.Instance.worldType == WorldType.Replay) { BattleManager.logMoveList.Add(GameRecord.Frame.ToString() + ":" + Entity.tileOffset.ToString()); } #endif // UNITY_EDITOR // 坐标判断(REMARK:这里如果移动速度极快会出BUG 暂时忽略o.o) if (Mathf.Abs(Entity.tileOffset.x) > 0.6f || Mathf.Abs(Entity.tileOffset.y) > 0.6f) { var dx = 0; var dy = 0; if (Entity.tileOffset.x > 0.5f) { dx = 1; } else if (Entity.tileOffset.x < -0.5f) { dx = -1; } if (Entity.tileOffset.y > 0.5f) { dy = 1; } else if (Entity.tileOffset.y < -0.5f) { dy = -1; } var delta = new TilePoint(dx, dy); Entity.tileOffset -= delta; var newpt = Entity.GetTilePos() + delta; Entity.SetTilePosition(newpt); if (newpt.x == currentNode.X && newpt.y == currentNode.Y) { currentNode = null; } else { // 当前位置到目标位置的向量的单位化*移动速度 CalcNewVelocity(); } } }
protected override List <TileEntity> TryLockTargeters(out TilePoint?_targetPos, out LinkedList <IMoveGrid> _targetRoute) { _targetPos = null; _targetRoute = null; ///< 有目标了直接返回 if (m_tempTargeters != null) { return(m_tempTargeters); } ///< 方案1:计算全地图目标时间最近(并且有墙的路线)REMARK:有墙基本就可以理解为封闭区域 Dictionary <TilePoint, IsoGridTarget> gridTargets = new Dictionary <TilePoint, IsoGridTarget>(); var targets = IsoMap.Instance.GetEntitiesByTT(Attacker.GetTargetOwner(), EntityAiType.Other, Attacker); foreach (var tar in targets) { Vector2 p = tar.GetCurrentPositionCenter(); TilePoint grid = new TilePoint((int)p.x, (int)p.y); IsoGridTarget tarInfos = new IsoGridTarget() { Targeter = tar, Distance = 0, X = grid.x, Y = grid.y }; gridTargets.Add(grid, tarInfos); } LinkedList <IMoveGrid> route = IsoMap.Instance.SearchDijkstraNearestWall(Attacker, gridTargets); if (route != null) { Assert.Should(route.Count >= 2); // 墙的位置 IMoveGrid wallGrid = route.Last.Value; route.RemoveLast(); // 墙的前一格 IMoveGrid lastGrid = route.Last.Value; // 获取墙 TileEntity wallTargeter = IsoMap.Instance.GetWallTargeter(lastGrid.X, lastGrid.Y, wallGrid.X, wallGrid.Y); Assert.Should(wallTargeter != null); // 返回 _targetRoute = route; return(AuxConvertToList(wallTargeter)); } ///< 方案2:求最近的墙o.o TileEntity targeter = GetWallEntityNearest(Attacker); if (targeter == null) { return(null); } // 查找移动目标点(墙四周离自身最近的点) int self_x = Entity.GetTilePos().x; int self_y = Entity.GetTilePos().y; Vector2 c = targeter.GetCurrentPositionCenter(); int wall_x = (int)c.x; int wall_y = (int)c.y; int wall_w = 1; // REMARK:连接器到墙中心的距离 int mindiff = 999999; int goal_x = -1; int goal_y = -1; // 依次为 左上、右上、左下、右下 DetectNearestGrid(ref mindiff, ref goal_x, ref goal_y, self_x, self_y, wall_x, wall_y + wall_w); DetectNearestGrid(ref mindiff, ref goal_x, ref goal_y, self_x, self_y, wall_x + wall_w, wall_y); DetectNearestGrid(ref mindiff, ref goal_x, ref goal_y, self_x, self_y, wall_x - wall_w, wall_y); DetectNearestGrid(ref mindiff, ref goal_x, ref goal_y, self_x, self_y, wall_x, wall_y - wall_w); // 找到目标点 并且 目标点位置不是自身位置 if (goal_x >= 0 && goal_y >= 0 && ((goal_x != self_x || goal_y != self_y))) { _targetPos = new TilePoint(goal_x, goal_y); } return(AuxConvertToList(targeter)); }