Esempio n. 1
0
    public static PositionPair get_destination(Simple_Moving_Object moving_thing, Tilemap tilemap, Vector2 offset)
    {
        // modify by offset for a person boarding a train (so hes not standing on the tracks)
        Vector3Int   tile_coord       = new Vector3Int(moving_thing.tile_position[0], moving_thing.tile_position[1], 0);
        Tile         track_tile       = (Tile)tilemap.GetTile(tile_coord);
        Vector2      tile_world_coord = tilemap.GetCellCenterWorld(tile_coord);
        PositionPair pos_pair         = get_next_tile_pos(tilemap, track_tile, moving_thing, tile_coord, offset);

        if (moving_thing.gameObject.tag == "boxcar")
        {
            Boxcar bcar = (Boxcar)moving_thing;
        }
        if (!moving_thing.in_city) //not inside a city, so check if arrived at city
        {
            Tile city_tile = (Tile)city_tilemap.GetTile(tile_coord);
            if (city_tile != null)                                                                                   //check if arriving at city
            {
                City city = CityManager.instance.gameobject_board[tile_coord.x, tile_coord.y].GetComponent <City>(); // check if city arrived at is not the same city we're leaving
                pos_pair.abs_dest_pos = tile_world_coord;                                                            // destination is the center of the tile
                moving_thing.prepare_to_arrive_at_city(city);
            }
        }
        return(pos_pair);
    }
Esempio n. 2
0
    public static PositionPair get_next_tile_pos(Tilemap tilemap, Tile track_tile, Simple_Moving_Object moving_thing, Vector3Int tile_coord, Vector2 offset)
    {
        // get the next tile position for train movement AND placing boxcars (reverse orientation)
        Vector2Int next_tilemap_pos = new Vector2Int(tile_coord.x, tile_coord.y);
        Vector2    tile_world_coord = tilemap.GetCellCenterWorld(tile_coord);
        Vector2    final_cell_dest  = moving_thing.transform.position; // end of track default destination ( dont move )

        try
        {
            if (track_tile != null)
            {
                string tile_name = track_tile.name;
                switch (tile_name)
                {
                //tricky curve tile updates. the train has already arrived in the tile so only adjust one coordinate
                case "ES":
                    if (moving_thing.orientation == Orientation.West || moving_thing.orientation == Orientation.South)
                    {
                        moving_thing.final_orientation = Orientation.South;
                        final_cell_dest  = get_straight_final_dest(Orientation.South, tile_world_coord);
                        next_tilemap_pos = new Vector2Int(next_tilemap_pos.x, next_tilemap_pos.y - 1);
                    }
                    else if (moving_thing.orientation == Orientation.North || moving_thing.orientation == Orientation.East)     // 2nd condition is for opposite direction when placing boxcars using flipped orientation
                    {
                        moving_thing.final_orientation = Orientation.East;
                        final_cell_dest  = get_straight_final_dest(Orientation.East, tile_world_coord);
                        next_tilemap_pos = new Vector2Int(next_tilemap_pos.x + 1, next_tilemap_pos.y);
                    }
                    else
                    {
                        throw new NullReferenceException();
                    }
                    break;

                case "NE":
                    if (moving_thing.orientation == Orientation.South || moving_thing.orientation == Orientation.East)
                    {
                        moving_thing.final_orientation = Orientation.East;
                        final_cell_dest  = get_straight_final_dest(Orientation.East, tile_world_coord);
                        next_tilemap_pos = new Vector2Int(next_tilemap_pos.x + 1, next_tilemap_pos.y);
                    }
                    else if (moving_thing.orientation == Orientation.West || moving_thing.orientation == Orientation.North)
                    {
                        moving_thing.final_orientation = Orientation.North;
                        final_cell_dest  = get_straight_final_dest(Orientation.North, tile_world_coord);
                        next_tilemap_pos = new Vector2Int(next_tilemap_pos.x, next_tilemap_pos.y + 1);
                    }
                    else
                    {
                        throw new NullReferenceException();
                    }
                    break;

                case "WN":
                    if (moving_thing.orientation == Orientation.East || moving_thing.orientation == Orientation.North)
                    {
                        moving_thing.final_orientation = Orientation.North;
                        final_cell_dest  = get_straight_final_dest(Orientation.North, tile_world_coord);
                        next_tilemap_pos = new Vector2Int(next_tilemap_pos.x, next_tilemap_pos.y + 1);
                    }
                    else if (moving_thing.orientation == Orientation.South || moving_thing.orientation == Orientation.West)
                    {
                        moving_thing.final_orientation = Orientation.West;
                        final_cell_dest  = get_straight_final_dest(Orientation.West, tile_world_coord);
                        next_tilemap_pos = new Vector2Int(next_tilemap_pos.x - 1, next_tilemap_pos.y);
                    }
                    else
                    {
                        throw new NullReferenceException();
                    }
                    break;

                case "WS":
                    if (moving_thing.orientation == Orientation.East || moving_thing.orientation == Orientation.South)
                    {
                        moving_thing.final_orientation = Orientation.South;
                        final_cell_dest  = get_straight_final_dest(Orientation.South, tile_world_coord);
                        next_tilemap_pos = new Vector2Int(next_tilemap_pos.x, next_tilemap_pos.y - 1);
                    }
                    else if (moving_thing.orientation == Orientation.North || moving_thing.orientation == Orientation.West)
                    {
                        moving_thing.final_orientation = Orientation.West;
                        final_cell_dest  = get_straight_final_dest(Orientation.West, tile_world_coord);
                        next_tilemap_pos = new Vector2Int(next_tilemap_pos.x - 1, next_tilemap_pos.y);
                    }
                    else
                    {
                        throw new NullReferenceException();
                    }
                    break;

                case "vert":
                    if (moving_thing.orientation == Orientation.North || moving_thing.orientation == Orientation.South)
                    {
                        final_cell_dest  = get_straight_final_dest(moving_thing.orientation, tile_world_coord);
                        next_tilemap_pos = get_straight_next_tile_pos(moving_thing.orientation, next_tilemap_pos);
                    }
                    else
                    {
                        throw new NullReferenceException();
                    }
                    break;

                case "hor":
                    if (moving_thing.orientation == Orientation.East || moving_thing.orientation == Orientation.West)
                    {
                        final_cell_dest  = get_straight_final_dest(moving_thing.orientation, tile_world_coord);
                        next_tilemap_pos = get_straight_next_tile_pos(moving_thing.orientation, next_tilemap_pos);
                    }
                    else
                    {
                        throw new NullReferenceException();
                    }
                    break;

                case "ne_diag":
                    moving_thing.final_orientation = Orientation.ne_SteepCurve;
                    break;

                case "nw_diag":
                    moving_thing.final_orientation = Orientation.nw_SteepCurve;
                    break;

                case "se_diag":
                    moving_thing.final_orientation = Orientation.se_SteepCurve;
                    break;

                case "sw_diag":
                    moving_thing.final_orientation = Orientation.sw_SteepCurve;
                    break;

                case "less_diag_ne_turn":
                    moving_thing.final_orientation = Orientation.ne_LessSteepCurve;
                    break;

                case "less_diag_nw_turn":
                    moving_thing.final_orientation = Orientation.nw_LessSteepCurve;
                    break;

                case "less_diag_se_turn":
                    moving_thing.final_orientation = Orientation.se_LessSteepCurve;
                    break;

                case "less_diag_sw_turn":
                    moving_thing.final_orientation = Orientation.sw_LessSteepCurve;
                    break;

                default:
                    moving_thing.final_orientation = Orientation.None;
                    break;
                }
                if (tile_name == "ne_diag" || tile_name == "nw_diag" || tile_name == "se_diag" || tile_name == "sw_diag" || tile_name == "less_diag_ne_turn" ||
                    tile_name == "less_diag_nw_turn" || tile_name == "less_diag_se_turn" || tile_name == "less_diag_sw_turn")
                {
                    final_cell_dest  = get_straight_final_dest(moving_thing.orientation, tile_world_coord);
                    next_tilemap_pos = get_straight_next_tile_pos(moving_thing.orientation, next_tilemap_pos);
                }
            }
        }
        catch (NullReferenceException e)
        {
            final_cell_dest = tile_world_coord;
            //print(e.Message);
        }

        final_cell_dest += offset;
        return(new PositionPair(final_cell_dest, next_tilemap_pos, (Vector2Int)moving_thing.prev_tile_position));
    }