void Spawn(){ if(!Pathfinding.DataLoaded()){ Debug.Log ("Navigation data is not loaded!"); return; } int p; Pathfinding.Node[] nodes = new Pathfinding.Node[2]; nodes[0] = Pathfinding.GetRandomPathsNode(availabledPaths); p = Pathfinding.FindPathOfType(nodes[0].type); if(p<0) return; for(int i=0;i<Pathfinding.Paths[p].size;i++){ if(Pathfinding.Paths[p].connectionLength[nodes[0].number,i]<Mathf.Infinity){ nodes[1] = new Pathfinding.Node(i,nodes[0].type); break; } } if(!Pathfinding.NodeIsEmpty(nodes[0]) && !Pathfinding.NodeIsEmpty(nodes[1])){ Vector3 spawnPos = Pathfinding.GetPointInArea(nodes[0],nodes[1],Vector3.zero,-1f); Transform newNPC = Instantiate (NPC,spawnPos,Quaternion.identity) as Transform; newNPC.position = spawnPos; //Debug.Log ("pos:"+spawnPos); newNPC.parent = transform; newNPC = newNPC.GetChild (0).GetChild (0); newNPC.GetComponent<Animation>()["walk_fun"].time = Random.Range (0f,newNPC.GetComponent<Animation>()["walk_fun"].length); curNumber++; curNumberStr = curNumber.ToString(); }else return; }
public void AddInteractedNode(Pathfinding.Node a_Node) { m_InteractedNodes.Add(a_Node); a_Node.InteractionObjects.Add(m_Object); m_Pathfinding.PathfindingVersion++; }
public void AddBlockedNode(Pathfinding.Node a_Node) { m_BlockedNodes.Add(a_Node); a_Node.BlockingObjects.Add(m_Object); m_Pathfinding.PathfindingVersion++; }
public void AddInteractedNode(int a_PosX, int a_PosY, bool a_IsUnderground) { Pathfinding.Node _Node = m_Pathfinding.GetNode(a_PosX, a_PosY, a_IsUnderground); m_InteractedNodes.Add(_Node); _Node.InteractionObjects.Add(m_Object); m_Pathfinding.PathfindingVersion++; }
public void AddBlockedNode(int a_PosX, int a_PosY, bool a_IsUnderground) { Pathfinding.Node _Node = m_Pathfinding.GetNode(a_PosX, a_PosY, a_IsUnderground); m_BlockedNodes.Add(_Node); _Node.BlockingObjects.Add(m_Object); m_Pathfinding.PathfindingVersion++; }
private void Update() { elapsed += Time.deltaTime; if (elapsed > 1f && calculatePath) { var playerPos = _tilemap.WorldToCell(target.position); var myPos = _tilemap.WorldToCell(transform.position); var targetNode = new Pathfinding.Node { X = playerPos.x, Y = playerPos.y }; var startNode = new Pathfinding.Node { X = myPos.x, Y = myPos.y }; _nodes = pathfinding.Search(startNode, targetNode); elapsed = 0; } if (_nodes.Count > 0) { var position = transform.position; var currentTile = _tilemap.WorldToCell(position); var nextTile = new Vector3Int(_nodes.First().X, _nodes.First().Y, 0); var smoothedDelta = Vector3.MoveTowards(position, _tilemap.GetCellCenterWorld(nextTile), enemyStats.stats[6].GetValue() * Time.deltaTime); _rb2d.MovePosition(smoothedDelta); if (Metrics.EqualFloats(position.x, _tilemap.GetCellCenterWorld(nextTile).x, 0.01f) && Metrics.EqualFloats(position.y, _tilemap.GetCellCenterWorld(nextTile).y, 0.01f)) { _nodes.Remove(_nodes.First()); } if (Vector2.Distance(transform.position, target.position) < attackRadius) { _nodes.Clear(); pathfinding.pathLine.positionCount = 0; calculatePath = false; combat.Attack(targetStats); } } if (Vector2.Distance(transform.position, target.position) > attackRadius) { calculatePath = true; } SetAnimatorValues(); }
void Update() { // If mouse is in the actual map area if (Input.mousePosition.x >= GameScreenScaler.VIEWPORT_PADDING_LEFT && Input.mousePosition.y >= GameScreenScaler.VIEWPORT_PADDING_BOTTOM && Input.mousePosition.x <= Screen.width - GameScreenScaler.VIEWPORT_PADDING_RIGHT && Input.mousePosition.y <= Screen.height - GameScreenScaler.VIEWPORT_PADDING_TOP && !m_TownScreen.Enabled) { Vector3 _WorldMousePos = m_Camera.ScreenToWorldPoint(Input.mousePosition - new Vector3(0, 8, 0)); Vector2Int _WorldMouseCoords = new Vector2Int ( (int)(_WorldMousePos.x + 0.5f), (int)(-_WorldMousePos.y + 0.5f) ); // If mouse has moved onto a different tile in the world if (m_PreviousWorldMouseCoords != _WorldMouseCoords || m_UpdateCursor) { // If mouse coords are within the bounds of the map if (_WorldMouseCoords.x >= 0 && _WorldMouseCoords.y >= 0 && _WorldMouseCoords.x < m_GameSettings.Scenario.Size && _WorldMouseCoords.y < m_GameSettings.Scenario.Size) { m_HoveredTown = null; m_HoveredHero = null; MapHero _SelectedHero = m_LocalOwnership.SelectedHero; m_PreviousWorldMouseCoords = _WorldMouseCoords; m_UpdateCursor = false; Pathfinding.Node _Node = m_Pathfinding.GetNode(_WorldMouseCoords, false); List <MapObjectBase> _Objects = new List <MapObjectBase>(_Node.BlockingObjects.Count + _Node.InteractionObjects.Count); for (int i = 0; i < _Node.BlockingObjects.Count; i++) { _Objects.Add(_Node.BlockingObjects[i]); } for (int i = 0; i < _Node.InteractionObjects.Count; i++) { _Objects.Add(_Node.InteractionObjects[i]); } // Flag used to break out of logic if an earlier bit of logic has already determined what the cursor should be bool _CursorSelected = false; int _TurnCost = 0; if (_SelectedHero != null) { _TurnCost = _SelectedHero.GetPathingTurnCost(_WorldMouseCoords.x, _WorldMouseCoords.y); } // Check if any of the objects are heroes for (int i = 0; i < _Objects.Count; i++) { MapHero _Hero = _Objects[i] as MapHero; if (_Hero != null) { m_HoveredObject = _Hero; if (_SelectedHero != null) { if (_SelectedHero == _Hero) { CursorManager.SetCursor(m_HeroCursor, new Vector2(-12, 10)); } else if (_Hero.IsPrison) { switch (_TurnCost) { case 0: CursorManager.ResetCursor(); break; case 1: CursorManager.SetCursor(m_InteractCursor, new Vector2(-14, 15)); break; case 2: CursorManager.SetCursor(m_InteractCursor2, new Vector2(-14, 15)); break; case 3: CursorManager.SetCursor(m_InteractCursor3, new Vector2(-14, 15)); break; default: CursorManager.SetCursor(m_InteractCursor4, new Vector2(-14, 15)); break; } } else if (_Hero.PlayerIndex == m_GameSettings.LocalPlayerIndex) { switch (_TurnCost) { case 0: CursorManager.ResetCursor(); break; case 1: CursorManager.SetCursor(m_TradeCursor, new Vector2(-8, 9)); break; case 2: CursorManager.SetCursor(m_TradeCursor2, new Vector2(-8, 9)); break; case 3: CursorManager.SetCursor(m_TradeCursor3, new Vector2(-8, 9)); break; default: CursorManager.SetCursor(m_TradeCursor4, new Vector2(-8, 9)); break; } } else { switch (_TurnCost) { case 0: CursorManager.ResetCursor(); break; case 1: CursorManager.SetCursor(m_AttackCursor, new Vector2(-13, 13)); break; case 2: CursorManager.SetCursor(m_AttackCursor2, new Vector2(-13, 13)); break; case 3: CursorManager.SetCursor(m_AttackCursor3, new Vector2(-13, 13)); break; default: CursorManager.SetCursor(m_AttackCursor4, new Vector2(-13, 13)); break; } } _CursorSelected = true; } else if (!_Hero.IsPrison && _Hero.PlayerIndex == m_GameSettings.LocalPlayerIndex) { CursorManager.SetCursor(m_HeroCursor, new Vector2(-12, 10)); m_HoveredHero = _Hero; _CursorSelected = true; } break; } } // Check if any of the objects are towns if (!_CursorSelected) { for (int i = 0; i < _Objects.Count; i++) { MapTown _Town = _Objects[i] as MapTown; if (_Town != null) { m_HoveredObject = _Objects[i]; int _XIndex = 8 - Mathf.Clamp(Mathf.CeilToInt(_Objects[i].transform.position.x - _WorldMousePos.x), 0, 7); int _YIndex = 6 - Mathf.Clamp(Mathf.CeilToInt(_WorldMousePos.y - _Objects[i].transform.position.y), 0, 5); // If the cursor is specifically on the castle's entrance, horse rear cursor if (_SelectedHero != null && _XIndex == 5 && _YIndex == 5) { switch (_TurnCost) { case 0: CursorManager.ResetCursor(); break; case 1: CursorManager.SetCursor(m_InteractCursor, new Vector2(-14, 15)); break; case 2: CursorManager.SetCursor(m_InteractCursor2, new Vector2(-14, 15)); break; case 3: CursorManager.SetCursor(m_InteractCursor3, new Vector2(-14, 15)); break; default: CursorManager.SetCursor(m_InteractCursor4, new Vector2(-14, 15)); break; } } else if (_Town.PlayerIndex == m_GameSettings.LocalPlayerIndex) { CursorManager.SetCursor(m_CastleCursor, new Vector2(-12, 12)); m_HoveredTown = _Town; } else { CursorManager.ResetCursor(); } _CursorSelected = true; break; } } } // If a hero is currently selected, set movement cursor if (_SelectedHero != null) { if (!_CursorSelected) { // If the mouse is on an interaction tile, horse rear cursor if (_Objects.Count > 0) { int _XIndex = 8 - Mathf.Clamp(Mathf.CeilToInt(_Objects[0].transform.position.x - _WorldMousePos.x), 0, 7); int _YIndex = 6 - Mathf.Clamp(Mathf.CeilToInt(_WorldMousePos.y - _Objects[0].transform.position.y), 0, 5); if ((_Objects[0].InteractionCollision[_YIndex] & 1 << _XIndex) != 0) { switch (_TurnCost) { case 0: CursorManager.ResetCursor(); break; case 1: CursorManager.SetCursor(m_InteractCursor, new Vector2(-14, 15)); break; case 2: CursorManager.SetCursor(m_InteractCursor2, new Vector2(-14, 15)); break; case 3: CursorManager.SetCursor(m_InteractCursor3, new Vector2(-14, 15)); break; default: CursorManager.SetCursor(m_InteractCursor4, new Vector2(-14, 15)); break; } _CursorSelected = true; } } } if (!_CursorSelected) { // If the mouse is on the end point of the current selected destination, horse rear cursor if (_SelectedHero.GetTargetDestination() == _WorldMouseCoords) { switch (_TurnCost) { case 0: CursorManager.ResetCursor(); break; case 1: CursorManager.SetCursor(m_InteractCursor, new Vector2(-14, 15)); break; case 2: CursorManager.SetCursor(m_InteractCursor2, new Vector2(-14, 15)); break; case 3: CursorManager.SetCursor(m_InteractCursor3, new Vector2(-14, 15)); break; default: CursorManager.SetCursor(m_InteractCursor4, new Vector2(-14, 15)); break; } } else { switch (_TurnCost) { case 0: CursorManager.ResetCursor(); break; case 1: CursorManager.SetCursor(m_MoveCursor, new Vector2(-15, 13)); break; case 2: CursorManager.SetCursor(m_MoveCursor2, new Vector2(-15, 13)); break; case 3: CursorManager.SetCursor(m_MoveCursor3, new Vector2(-15, 13)); break; default: CursorManager.SetCursor(m_MoveCursor4, new Vector2(-15, 13)); break; } } _CursorSelected = true; } } if (!_CursorSelected) { CursorManager.ResetCursor(); } } else { CursorManager.ResetCursor(); } } if (Input.GetMouseButtonDown(0)) { if (m_HoveredTown != null) { if (m_HoveredTown != m_LocalOwnership.SelectedTown) { m_LocalOwnership.SelectTown(m_HoveredTown); } else { m_TownScreen.OpenTown(m_HoveredTown); } } else if (m_LocalOwnership.SelectedHero != null) { if (m_LocalOwnership.SelectedHero.IsMoving) { m_LocalOwnership.SelectedHero.CancelMovement(); } else { m_LocalOwnership.SelectedHero.OnLeftClick(_WorldMouseCoords.x, _WorldMouseCoords.y, m_HoveredObject); } } else if (m_HoveredHero != null) { m_LocalOwnership.SelectHero(m_HoveredHero); } m_UpdateCursor = true; } } else { if (Input.GetMouseButtonDown(0)) { if (m_LocalOwnership.SelectedHero != null) { if (m_LocalOwnership.SelectedHero.IsMoving) { m_LocalOwnership.SelectedHero.CancelMovement(); } } } m_UpdateCursor = true; CursorManager.ResetCursor(); } if (!m_TownScreen.Enabled) { if (Input.mousePosition.x <= 2) { if (Input.mousePosition.y <= 2) { CursorManager.SetCursor(m_BottomLeftCursor, new Vector2(0, 18)); } else if (Input.mousePosition.y >= Screen.height - 3) { CursorManager.SetCursor(m_TopLeftCursor, new Vector2(0, 1)); } else { CursorManager.SetCursor(m_LeftCursor, new Vector2(0, 5)); } } else if (Input.mousePosition.x >= Screen.width - 3) { if (Input.mousePosition.y <= 2) { CursorManager.SetCursor(m_BottomRightCursor, new Vector2(-18, 18)); } else if (Input.mousePosition.y >= Screen.height - 3) { CursorManager.SetCursor(m_TopRightCursor, new Vector2(-18, 1)); } else { CursorManager.SetCursor(m_RightCursor, new Vector2(-23, 6)); } } else { if (Input.mousePosition.y <= 2) { CursorManager.SetCursor(m_BottomCursor, new Vector2(-6, 23)); } else if (Input.mousePosition.y >= Screen.height - 3) { CursorManager.SetCursor(m_TopCursor, new Vector2(-6, 0)); } } } }
void DrawAtypicalSelectedPaths(){ if(!drawCustomPath) return; if(Time.time>drawTime){ drawTime = Time.time+0.1f; }else return; if(Pathfinding.newAtypicalPaths.nodes.Length<1) return; if(startPoint.number == endPoint.number && startPoint.type == endPoint.type) return; //Debug.Log ("Request route: startPoint number="+startPoint.number+" of type "+startPoint.type); //Debug.Log (" endPoint number="+endPoint.number+" of type "+endPoint.type); int i; Pathfinding.Node prev = new Pathfinding.Node(startPoint.number,startPoint.type); routeData.curNode = new Pathfinding.Node(startPoint.number,startPoint.type); routeData.destinationNode = new Pathfinding.Node(endPoint.number,endPoint.type); routeData.nextNode = new Pathfinding.Node(-1,""); routeData.nextNodeIndex = 0; routeData = Pathfinding.GetNextNode(routeData); for(i=0;i<34567;i++){ Debug.DrawLine(Pathfinding.GetWaypointInSpace(0.5f,prev.number,prev.type),Pathfinding.GetWaypointInSpace(0.5f,routeData.curNode.number,routeData.curNode.type),Color.blue,0.1f); if(routeData.curNode.number == routeData.destinationNode.number) if(routeData.curNode.type == routeData.destinationNode.type) break; //Debug.Log ("prev:"+prev.number.ToString ()+prev.type+"; curPoint:"+routeData.curPoint.number.ToString ()+routeData.curPoint.type); prev = new Pathfinding.Node(routeData.curNode.number,routeData.curNode.type); //Debug.Log ("Calculate new cur point(step "+i+")"); routeData = Pathfinding.GetNextNode(routeData); } }
IEnumerator Move() { if (m_Path.Count == 0) { yield break; } IsAsleep = false; m_StopMovement = false; IsMoving = true; m_FlagRenderer.enabled = false; HeroFlagVisualData _FlagData = m_PlayerColors.Flags[PlayerIndex]; m_DynamicObstacle.ClearNodes(); Pathfinding.PrecomputedNode _Node = m_Path[0]; Destroy(m_PathNodes[0]); Destroy(m_PathNodeShadows[0]); m_PathNodes.RemoveAt(0); m_PathNodeShadows.RemoveAt(0); int _AnimationIndex = 0; int _Frame = 0; m_GameReferences.InputBlocker.SetActive(true); Vector3 _MovementDirection; void SetDirection() { _MovementDirection = new Vector3 ( m_Path[0].PosX - m_PathfindingPos.x, -(m_Path[0].PosY - m_PathfindingPos.y), 0 ) / 4; if (_MovementDirection.x > 0) { if (_MovementDirection.y > 0) { m_Direction = DIRECTION_NE; } else if (_MovementDirection.y < 0) { m_Direction = DIRECTION_SE; } else { m_Direction = DIRECTION_E; } } else if (_MovementDirection.x < 0) { if (_MovementDirection.y > 0) { m_Direction = DIRECTION_NW; } else if (_MovementDirection.y < 0) { m_Direction = DIRECTION_SW; } else { m_Direction = DIRECTION_W; } } else if (_MovementDirection.y > 0) { m_Direction = DIRECTION_N; } else { m_Direction = DIRECTION_S; } } SetDirection(); OnStartMovement?.Invoke(); // We don't check IsMoving here, because when cancelled, we still want to finish walking to the next tile while (true) { if (_Frame == 4) { _Frame = 0; m_PathfindingPos = new Vector2Int(_Node.PosX, _Node.PosY); m_Path.RemoveAt(0); if (m_Path.Count == 0 || m_StopMovement) { break; } Destroy(m_PathNodes[0]); Destroy(m_PathNodeShadows[0]); m_PathNodes.RemoveAt(0); m_PathNodeShadows.RemoveAt(0); _Node = m_Path[0]; SetDirection(); } m_HeroRenderer.sprite = Hero.HeroVisualData.MovingSprites[m_Direction].Array[_AnimationIndex]; m_HeroShadowRenderer.sprite = Hero.HeroVisualData.ShadowMovingSprites[m_Direction].Array[_AnimationIndex]; m_FlagSpriteRenderer.sprite = _FlagData.MovingSprites[m_Direction].Array[_AnimationIndex]; m_HeroRenderer.flipX = HeroVisualData.SPRITES_FLIPPED[m_Direction]; m_HeroShadowRenderer.flipX = HeroVisualData.SPRITES_FLIPPED[m_Direction]; m_FlagSpriteRenderer.flipX = HeroVisualData.SPRITES_FLIPPED[m_Direction]; if (m_HeroRenderer.flipX) { m_HeroRenderer.transform.localPosition = new Vector3(-3, 0, 0); m_HeroShadowRenderer.transform.localPosition = new Vector3(-3, 0, 0); m_FlagSpriteRenderer.transform.localPosition = new Vector2(-3, 0) + Hero.HeroVisualData.MovingFlagOffsets[m_Direction]; } else { m_HeroRenderer.transform.localPosition = Vector3.zero; m_HeroShadowRenderer.transform.localPosition = Vector3.zero; m_FlagSpriteRenderer.transform.localPosition = Hero.HeroVisualData.MovingFlagOffsets[m_Direction]; } transform.position += _MovementDirection; yield return(new WaitForSeconds(0.05f)); _AnimationIndex++; _Frame++; if (_AnimationIndex == 8) { _AnimationIndex = 0; } } m_GameReferences.InputBlocker.SetActive(false); m_FlagRenderer.enabled = true; m_HeroRenderer.sprite = Hero.HeroVisualData.IdleSprites[m_Direction]; m_HeroShadowRenderer.sprite = Hero.HeroVisualData.ShadowIdleSprites[m_Direction]; m_HeroRenderer.flipX = HeroVisualData.SPRITES_FLIPPED[m_Direction]; m_HeroShadowRenderer.flipX = HeroVisualData.SPRITES_FLIPPED[m_Direction]; m_FlagSpriteRenderer.flipX = HeroVisualData.SPRITES_FLIPPED[m_Direction]; if (m_HeroRenderer.flipX) { m_HeroRenderer.transform.localPosition = new Vector3(-3, 0, 0); m_HeroShadowRenderer.transform.localPosition = new Vector3(-3, 0, 0); m_FlagSpriteRenderer.transform.localPosition = new Vector2(-3, 0) + Hero.HeroVisualData.IdleFlagOffsets[m_Direction]; } else { m_HeroRenderer.transform.localPosition = Vector3.zero; m_HeroShadowRenderer.transform.localPosition = Vector3.zero; m_FlagSpriteRenderer.transform.localPosition = Hero.HeroVisualData.IdleFlagOffsets[m_Direction]; } Pathfinding.Node _FinalNode = m_GameReferences.Pathfinding.GetNode(m_PathfindingPos, IsUnderground); m_DynamicObstacle.AddInteractedNode(_FinalNode); for (int i = 0; i < _FinalNode.InteractionObjects.Count; i++) { MapTown _Town = _FinalNode.InteractionObjects[i] as MapTown; if (_Town != null) { m_GameReferences.TownScreen.OpenTown(_Town); } } m_PathableArea = m_GameReferences.Pathfinding.GetPathableArea(m_PathfindingPos, IsUnderground); if (m_Path.Count == 0) { m_TargetNodeIndex = -1; HasPath = false; OnPathRemoved?.Invoke(); } IsMoving = false; }