/// <summary> /// 玩家寻路组件对外接口 消息处理方法中使用 /// </summary> public static async ETVoid MoveTo(this GamerPathComponent self, Vector3 target) { if ((self.Target - target).magnitude < 0.1f) { return; } self.Target = target; Gamer gamer = self.GetParent <Gamer>(); //全局寻路组件 用于计算路径 PathfindingComponent pathfindingComponent = Game.Scene.GetComponent <PathfindingComponent>(); self.ABPath = ComponentFactory.Create <ETModel.ABPath, Vector3, Vector3>(gamer.Position, new Vector3(target.x, target.y, target.z)); //创建路径 寻路组件的接口 pathfindingComponent.Search(self.ABPath); //Log.Debug($"寻路查询结果: {self.ABPath.Result.ListToString()}"); self.CancellationTokenSource?.Cancel(); //取消当前寻路 self.CancellationTokenSource = new CancellationTokenSource(); await self.MoveAsync(self.ABPath.Result); //开始移动 self.CancellationTokenSource.Dispose(); self.CancellationTokenSource = null; }
public void Update() { PathfindingComponent pathFindingInterface = m_entity.PathfindingInterface; if (pathFindingInterface != null) { if (!pathFindingInterface.IsPathComplete) { Point3d targetRoomPosition = pathFindingInterface.CurrentWaypoint.StepPoint; Point2d targetPixelPosition = GameConstants.ConvertRoomPositionToPixelPosition(targetRoomPosition); Point2d currentPixelPosition = GameConstants.ConvertRoomPositionToPixelPosition(m_entity.Position); Vector2d offsetToTarget = targetPixelPosition - currentPixelPosition; // Throttle full speed at the target m_throttle.Copy(offsetToTarget); m_throttle.Normalize(); // Always face the direction we're moving m_facing.Copy(offsetToTarget); m_facing.NormalizeWithDefault(Vector2d.WORLD_DOWN); } else { // Leave the facing alone, clear the throttle m_throttle.Copy(Vector2d.ZERO_VECTOR); } } else { Reset(); } }
public static async ETVoid MoveTo(this UnitPathComponent self, Vector3 target) { if ((self.Target - target).magnitude < 0.1f) { return; } self.Target = target; Unit unit = self.GetParent <Unit>(); PathfindingComponent pathfindingComponent = Game.Scene.GetComponent <PathfindingComponent>(); self.ABPath = ComponentFactory.Create <ETModel.ABPath, Vector3, Vector3>(unit.Position, new Vector3(target.x, target.y, target.z)); pathfindingComponent.Search(self.ABPath); Log.Debug($"find result: {self.ABPath.Result.ListToString()}"); self.CancellationTokenSource?.Cancel(); self.CancellationTokenSource = new CancellationTokenSource(); await self.MoveAsync(self.ABPath.Result); self.CancellationTokenSource.Dispose(); self.CancellationTokenSource = null; }
private bool HasNewLocation(PositionComponent positionComponent, PathfindingComponent pathfindingComponent) { if (positionComponent.X != pathfindingComponent.TargetX || positionComponent.Y != pathfindingComponent.TargetY) { return(true); } return(false); }
public void AddToGameWorld(GameWorldController gameWorldController) { m_pathfindingComponent = new PathfindingComponent(this); m_steeringComponent = new SteeringComponent(this); m_characterWidget = gameWorldController.View.AddCharacterWidget(m_characterData); gameWorldController.Model.AddCharacterEntity(this); }
public void RemoveFromGameWorld(GameWorldController gameWorldController) { gameWorldController.Model.RemoveCharacterEntity(this); gameWorldController.View.RemoveCharacterWidget(m_characterWidget); m_characterWidget = null; m_pathfindingComponent = null; m_steeringComponent = null; }
public List <Node> FindPath(PositionComponent positionComponent, PathfindingComponent pathfindingComponent) { List <Node> openList = new List <Node>(); List <Node> closedList = new List <Node>(); List <Node> path = new List <Node>(); var nodes = GenerateNodeMap(); var startingNode = nodes[positionComponent.X, positionComponent.Y]; var targetNode = nodes[pathfindingComponent.TargetX, pathfindingComponent.TargetY]; startingNode.G = 0; startingNode.F = startingNode.G + Heuristic(startingNode, targetNode); openList.Add(startingNode); while (openList.Count > 0) { var currentNode = openList.OrderBy(n => n.F).FirstOrDefault(); if (currentNode.X == targetNode.X && currentNode.Y == targetNode.Y) { return(ConstructPath(currentNode)); } openList.Remove(currentNode); closedList.Add(currentNode); foreach (var neighbor in GetNeighbors(currentNode, nodes)) { if (!closedList.Contains(neighbor)) { var heuristic = Heuristic(neighbor, targetNode); neighbor.F = neighbor.G + heuristic; if (!openList.Contains(neighbor)) { openList.Add(neighbor); } else { var openNeighbor = openList.Where(x => x.Equals(neighbor)).FirstOrDefault(); if (neighbor.G < openNeighbor.G) { openNeighbor.G = neighbor.G; openNeighbor.ParentNode = neighbor.ParentNode; nodes[openNeighbor.X, openNeighbor.Y].ParentNode = neighbor.ParentNode; } } } } } return(path); }
public CharacterEntity(int characterId) { SessionData sessionData = SessionData.GetInstance(); m_characterId = characterId; m_characterData = sessionData.CurrentGameData.GetCharacterById(m_characterId); m_position = new Point3d(m_characterData.x, m_characterData.y, m_characterData.z); m_facing = MathConstants.GetUnitVectorForAngle(m_characterData.angle); m_characterWidget = null; m_pathfindingComponent = null; m_steeringComponent = null; }
private static bool WaypointWithinToleranceRule(PathfindingComponent pathfindingInterface) { float distanceToWaypointSquared = Point3d.DistanceSquared( pathfindingInterface.m_entity.Position, pathfindingInterface.CurrentWaypoint.StepPoint); float toleranceSquared = pathfindingInterface.IsLastStep ? DESTINATION_TOLERANCE * DESTINATION_TOLERANCE : WAYPOINT_TOLERANCE * WAYPOINT_TOLERANCE; bool hitWaypoint = (distanceToWaypointSquared <= toleranceSquared); return(hitWaypoint); }
public MobEntity(int characterId) { SessionData sessionData = SessionData.GetInstance(); m_mobId = characterId; m_mobData = sessionData.CurrentGameData.CurrentRoom.GetMobById(m_mobId); m_position = new Point3d(m_mobData.x, m_mobData.y, m_mobData.z); m_facing = MathConstants.GetUnitVectorForAngle(m_mobData.angle); m_dialogTimer = -1.0f; m_mobWidget = null; m_pathfindingComponent = null; m_steeringComponent = null; }
public Mech(Parameters inParameters) { guid = inParameters.guid; owner = Program.networkManager.CurrentRoom.GetPlayer(inParameters.ownerID); bodyType = new MechBodyType(inParameters.bodyTypeName); mobilityType = new MechMobilityType(inParameters.mobilityTypeName); armorType = new MechArmorType(inParameters.armorTypeName); healthComponent = new HealthComponent(this); utilityComponent = new UtilityComponent(this, inParameters.equipedWeaponNames); movementComponent = new MovementComponent(this, inParameters.spawnPosition); pathfindingComponent = new PathfindingComponent(this); viewComponent = new ViewComponent(this); inputComponent = new InputComponent(this); actionComponent = new ActionComponent(this); uiComponent = new UIComponent(this); OnComponentsCreated?.Invoke(); OnComponentsInitialized?.Invoke(); }
public static async ETVoid MoveTo(this MapPathComponent self, Vector3 target) { if ((self.Target - target).magnitude < 0.1f) { return; } self.Target = target; Unit unit = self.GetParent <Unit>(); PathfindingComponent pathfindingComponent = Game.Scene.GetComponent <PathfindingComponent>(); self.ABPath = ComponentFactory.Create <ABPathWrap, Vector3, Vector3>(unit.Position, new Vector3(target.x, target.y, target.z)); pathfindingComponent.Search(self.ABPath); self.CancellationTokenSource?.Cancel(); self.CancellationTokenSource = new CancellationTokenSource(); //ToTo 同步到服务端 本身坐标和向量坐标 await self.MoveAsync(self.ABPath.Result); self.CancellationTokenSource.Dispose(); self.CancellationTokenSource = null; }
public void RemoveFromGameWorld(GameWorldController gameWorldController) { gameWorldController.Model.RemoveMobEntity(this); gameWorldController.View.RemoveMobWidget(m_mobWidget); m_mobWidget = null; m_pathfindingComponent = null; m_steeringComponent = null; }
public void AddToGameWorld(GameWorldController gameWorldController) { m_pathfindingComponent = new PathfindingComponent(this); m_steeringComponent = new SteeringComponent(this); m_mobWidget = gameWorldController.View.AddMobWidget(m_mobData); gameWorldController.Model.AddMobEntity(this); }
private static bool WaypointWithinToleranceRule(PathfindingComponent pathfindingInterface) { float distanceToWaypointSquared = Point3d.DistanceSquared( pathfindingInterface.m_entity.Position, pathfindingInterface.CurrentWaypoint.StepPoint); float toleranceSquared = pathfindingInterface.IsLastStep ? DESTINATION_TOLERANCE * DESTINATION_TOLERANCE : WAYPOINT_TOLERANCE * WAYPOINT_TOLERANCE; bool hitWaypoint= (distanceToWaypointSquared <= toleranceSquared); return hitWaypoint; }