private void SetPointerLocomotionTarget(Vector2 target, ObjectDirection moveDirection) { //Logger.Warning($"SetPointerLocomotionTarget to target {target.x}, {target.y} in the direction {moveDirection}"); GridLocation targetGridLocation = GridLocation.FindClosestGridTile(target); if (!ValidateTarget(new TargetLocation(targetGridLocation, moveDirection))) { return; } Vector2 gridVectorTarget = GridLocation.GridToVector(targetGridLocation); if (CurrentGridLocation.X == targetGridLocation.X && CurrentGridLocation.Y == targetGridLocation.Y) { return; } if (!_animationHandler.InLocomotion) { _animationHandler.SetLocomotion(true); } IsCalculatingPath = true; PathToTarget = _pathfinding.FindNodePath(CurrentGridLocation, targetGridLocation); PathToTarget.RemoveAt(0); IsCalculatingPath = false; SetHasCalculatedTarget(true); }
// set character to current spawnpoint and reset pathfinder public void ResetCharacterPosition() { SetHasCalculatedTarget(false); _animationHandler.SetLocomotion(false); //Logger.Warning("Starting position for {0} is {1},{2}", gameObject.name, gameObject.GetComponent<PlayerCharacter>().StartingPosition.X, gameObject.GetComponent<PlayerCharacter>().StartingPosition.Y); GameManager.Instance.CharacterManager.PutCharacterOnGrid(gameObject, GridLocation.GridToVector(StartingPosition)); SetCurrentGridLocation(StartingPosition); }
public void MoveCharacter() { PathNode nextNode = PathToTarget[0]; GridLocation nextGridLocation = nextNode.Tile.GridLocation; GridLocation currentGridLocation = CurrentGridLocation; Vector3 moveDir; ObjectDirection direction = ObjectDirection.Right; if (nextGridLocation.X > currentGridLocation.X) { direction = ObjectDirection.Right; _animationHandler.SetDirection(direction); } else if (nextGridLocation.X < currentGridLocation.X) { direction = ObjectDirection.Left; _animationHandler.SetDirection(direction); } else if (nextGridLocation.Y > currentGridLocation.Y) { direction = ObjectDirection.Up; _animationHandler.SetDirection(direction); } else if (nextGridLocation.Y < currentGridLocation.Y) { direction = ObjectDirection.Down; _animationHandler.SetDirection(direction); } Vector2 targetVector2Pos = GridLocation.GridToVector(nextGridLocation); moveDir = (new Vector3(targetVector2Pos.x + GridLocation.OffsetToTileMiddle, targetVector2Pos.y + GridLocation.OffsetToTileMiddle, transform.position.z) - transform.position).normalized; float speed = 2.5f; transform.position = transform.position + moveDir * speed * Time.deltaTime; // Character reaches a tile grid location (its middle) if ((direction == ObjectDirection.Right && transform.position.x > targetVector2Pos.x + GridLocation.OffsetToTileMiddle) || (direction == ObjectDirection.Left && transform.position.x < targetVector2Pos.x + GridLocation.OffsetToTileMiddle) || (direction == ObjectDirection.Down && transform.position.y < targetVector2Pos.y + GridLocation.OffsetToTileMiddle) || direction == ObjectDirection.Up && transform.position.y > targetVector2Pos.y + GridLocation.OffsetToTileMiddle) { SetCurrentGridLocation(nextGridLocation); PathToTarget.RemoveAt(0); //Logger.Log($"New Grid location{currentGridLocation.X}, {currentGridLocation.Y}. Remaining path length is {PathToTarget.Count}"); if (PathToTarget.Count == 0) { OnTargetReached(); } return; } }
private void CheckPointerInput() { if (HasCalculatedTarget) { return; } Vector2 tempFingerPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); GridLocation closestGridLocation = GridLocation.FindClosestGridTile(tempFingerPosition); if (closestGridLocation.X == CurrentGridLocation.X && closestGridLocation.Y == CurrentGridLocation.Y) { return; } GridLocation newLocomotionTarget = CurrentGridLocation; Vector2 direction = tempFingerPosition - (Vector2)transform.position; float angle = Vector2.SignedAngle(Vector2.up, direction) * -1; new GridLocation(CurrentGridLocation.X, CurrentGridLocation.Y); ObjectDirection moveDirection = ObjectDirection.Right; if (angle <= -135) // go down { newLocomotionTarget = new GridLocation(CurrentGridLocation.X, CurrentGridLocation.Y - 1); moveDirection = ObjectDirection.Down; } else if (angle <= -45) // go left { newLocomotionTarget = new GridLocation(CurrentGridLocation.X - 1, CurrentGridLocation.Y); moveDirection = ObjectDirection.Left; } else if (angle <= 45) // go up { newLocomotionTarget = new GridLocation(CurrentGridLocation.X, CurrentGridLocation.Y + 1); moveDirection = ObjectDirection.Up; } else if (angle <= 135) // go right { newLocomotionTarget = new GridLocation(CurrentGridLocation.X + 1, CurrentGridLocation.Y); moveDirection = ObjectDirection.Right; } else // go down { newLocomotionTarget = new GridLocation(CurrentGridLocation.X, CurrentGridLocation.Y - 1); moveDirection = ObjectDirection.Down; } SetPointerLocomotionTarget(GridLocation.GridToVector(newLocomotionTarget), moveDirection); }
public void BuildTiles(OverworldData overworldData) { Dictionary <InGameOverworldTile, List <SerialisableGridLocation> > TileTransformationGridLocationByTile = new Dictionary <InGameOverworldTile, List <SerialisableGridLocation> >(); for (int i = 0; i < overworldData.Tiles.Count; i++) { SerialisableTile serialisableTile = overworldData.Tiles[i]; GameObject tileGO = GameObject.Instantiate(OverworldGameplayManager.Instance.InGameTilePrefab, _overworldContainer.transform); InGameOverworldTile tile = tileGO.GetComponent <InGameOverworldTile>(); tile.SetGridLocation(serialisableTile.GridLocation.X, serialisableTile.GridLocation.Y); tile.SetId(serialisableTile.Id); tileGO.name = "Tile" + tile.GridLocation.X + ", " + tile.GridLocation.Y; tileGO.transform.position = GridLocation.GridToVector(tile.GridLocation); Tiles.Add(tile); AddBackgroundSprites(serialisableTile, tile); AddTileAttributes(serialisableTile, tile); AddCornerFillers(serialisableTile, tile); TilesByLocation.Add(tile.GridLocation, tile); GridLocation furthestBounds = LevelBounds; if (tile.GridLocation.X > furthestBounds.X) { _levelBounds.X = tile.GridLocation.X; } if (tile.GridLocation.Y > furthestBounds.Y) { _levelBounds.Y = tile.GridLocation.Y; } TileTransformationGridLocationByTile.Add(tile, serialisableTile.TilesToTransform); } for (int k = 0; k < Tiles.Count; k++) { InGameOverworldTile tile = Tiles[k] as InGameOverworldTile; tile.AddNeighbours(this); } }
public void BuildTiles(OverworldData overworldData) { for (int i = 0; i < overworldData.Tiles.Count; i++) { SerialisableTile serialisableTile = overworldData.Tiles[i]; GameObject tileGO = GameObject.Instantiate(OverworldGameplayManager.Instance.EditorTilePrefab, _overworldContainer.transform); EditorOverworldTile tile = tileGO.GetComponent <EditorOverworldTile>(); tile.SetGridLocation(serialisableTile.GridLocation.X, serialisableTile.GridLocation.Y); tile.SetId(serialisableTile.Id); tileGO.name = "Tile" + tile.GridLocation.X + ", " + tile.GridLocation.Y; tileGO.transform.position = GridLocation.GridToVector(tile.GridLocation); Tiles.Add(tile); AddTileAttributes(serialisableTile, tile); AddBackgroundSprites(serialisableTile, tile); AddCornerFillers(serialisableTile, tile); ITileMainMaterial mainMaterial = AddMainMaterial(serialisableTile); tile.SetMainMaterial(mainMaterial); TilesByLocation.Add(tile.GridLocation, tile); GridLocation furthestBounds = LevelBounds; if (tile.GridLocation.X > furthestBounds.X) { _levelBounds.X = tile.GridLocation.X; } if (tile.GridLocation.Y > furthestBounds.Y) { _levelBounds.Y = tile.GridLocation.Y; } } for (int k = 0; k < Tiles.Count; k++) { EditorOverworldTile tile = Tiles[k] as EditorOverworldTile; tile.AddNeighbours(this); } }
public override void BuildTiles(MazeLevelData mazeLevelData) { Dictionary <InGameMazeTile, List <SerialisableGridLocation> > TileTransformationGridLocationByTile = new Dictionary <InGameMazeTile, List <SerialisableGridLocation> >(); for (int i = 0; i < mazeLevelData.Tiles.Count; i++) { SerialisableTile serialisableTile = mazeLevelData.Tiles[i]; GameObject tileGO = GameObject.Instantiate(MazeLevelGameplayManager.Instance.InGameTilePrefab, _mazeContainer.transform); InGameMazeTile tile = tileGO.GetComponent <InGameMazeTile>(); tile.SetGridLocation(serialisableTile.GridLocation.X, serialisableTile.GridLocation.Y); tile.SetId(serialisableTile.Id); tileGO.name = "Tile" + tile.GridLocation.X + ", " + tile.GridLocation.Y; tileGO.transform.position = GridLocation.GridToVector(tile.GridLocation); Tiles.Add(tile); AddBackgroundSprites(serialisableTile, tile); AddTileAttributes(serialisableTile, tile); AddCornerFillers(serialisableTile, tile); AddTileAreas(serialisableTile, tile); TilesByLocation.Add(tile.GridLocation, tile); GridLocation furthestBounds = LevelBounds; if (tile.GridLocation.X > furthestBounds.X) { _levelBounds.X = tile.GridLocation.X; } if (tile.GridLocation.Y > furthestBounds.Y) { _levelBounds.Y = tile.GridLocation.Y; } TileTransformationGridLocationByTile.Add(tile, serialisableTile.TilesToTransform); } foreach (KeyValuePair <InGameMazeTile, List <SerialisableGridLocation> > item in TileTransformationGridLocationByTile) { List <InGameMazeTile> tilesToTransform = new List <InGameMazeTile>(); for (int i = 0; i < item.Value.Count; i++) { for (int j = 0; j < Tiles.Count; j++) { InGameMazeTile tile = Tiles[j] as InGameMazeTile; if (item.Value[i].X == tile.GridLocation.X && item.Value[i].Y == tile.GridLocation.Y) { tilesToTransform.Add(tile); break; } } } item.Key.AddTilesToTransform(tilesToTransform); } for (int k = 0; k < Tiles.Count; k++) { InGameMazeTile tile = Tiles[k] as InGameMazeTile; tile.AddNeighbours(this); } ConnectBridgeEdgesToTheirBridgePieces(); }
public override void BuildTiles(MazeLevelData mazeLevelData) { Dictionary <SerialisableGridLocation, List <EditorMazeTile> > TileTransformationTriggererByGridLocation = new Dictionary <SerialisableGridLocation, List <EditorMazeTile> >(); for (int i = 0; i < mazeLevelData.Tiles.Count; i++) { SerialisableTile serialisableTile = mazeLevelData.Tiles[i]; GameObject tileGO = GameObject.Instantiate(MazeLevelGameplayManager.Instance.EditorTilePrefab, _mazeContainer.transform); EditorMazeTile tile = tileGO.GetComponent <EditorMazeTile>(); tile.SetGridLocation(serialisableTile.GridLocation.X, serialisableTile.GridLocation.Y); tile.SetId(serialisableTile.Id); tileGO.name = "Tile" + tile.GridLocation.X + ", " + tile.GridLocation.Y; tileGO.transform.position = GridLocation.GridToVector(tile.GridLocation); Tiles.Add(tile); AddTileAttributes(serialisableTile, tile); AddBackgroundSprites(serialisableTile, tile); AddCornerFillers(serialisableTile, tile); AddTileAreas(serialisableTile, tile); TilesByLocation.Add(tile.GridLocation, tile); ITileMainMaterial mainMaterial = AddMainMaterial(serialisableTile); tile.SetMainMaterial(mainMaterial); GridLocation furthestBounds = LevelBounds; if (tile.GridLocation.X > furthestBounds.X) { _levelBounds.X = tile.GridLocation.X; } if (tile.GridLocation.Y > furthestBounds.Y) { _levelBounds.Y = tile.GridLocation.Y; } if (serialisableTile.TilesToTransform != null) { for (int j = 0; j < serialisableTile.TilesToTransform.Count; j++) { if (TileTransformationTriggererByGridLocation.ContainsKey(serialisableTile.TilesToTransform[j])) { List <EditorMazeTile> transformationTriggerers = TileTransformationTriggererByGridLocation[serialisableTile.TilesToTransform[j]]; transformationTriggerers.Add(tile); } else { List <EditorMazeTile> transformationTriggerers = new List <EditorMazeTile>(); transformationTriggerers.Add(tile); TileTransformationTriggererByGridLocation.Add(serialisableTile.TilesToTransform[j], transformationTriggerers); } } } } foreach (KeyValuePair <SerialisableGridLocation, List <EditorMazeTile> > item in TileTransformationTriggererByGridLocation) { for (int i = 0; i < Tiles.Count; i++) { EditorMazeTile tile = Tiles[i] as EditorMazeTile; if (item.Key.X == tile.GridLocation.X && item.Key.Y == tile.GridLocation.Y) { tile.BeautificationTriggerers = item.Value; } } } for (int k = 0; k < Tiles.Count; k++) { EditorMazeTile tile = Tiles[k] as EditorMazeTile; tile.AddNeighbours(this); } ConnectBridgeEdgesToTheirBridgePieces(); }