Ejemplo n.º 1
0
 public static void create_boxcar(Train train, GameObject boxcar)
 {
     // save the train on the board, but boxcar will overwrite it
     if (train.in_city) // if train is in city then add boxcars
     {
         Tilemap tilemap          = train.station_track.tilemap;
         Boxcar  boxcar_component = boxcar.GetComponent <Boxcar>();
         boxcar_counter += 1;
         boxcar_component.attach_to_train(train);
         MovingObject last_vehicle = train.get_last_vehicle_added().GetComponent <MovingObject>();
         // initalize boxcar position
         PositionPair pos_pair = TrainRouteManager.get_initial_destination(last_vehicle, tilemap);
         initialize_position(boxcar_component, pos_pair);
         boxcar_component.set_initial_rotation(boxcar_component.orientation);
         //set_initial_angle(boxcar, boxcar_component);
         boxcar_component.city          = train.city;
         boxcar_component.station_track = train.station_track;
         boxcar_component.arrive_at_city();
         boxcar_component.initialize_boxcar(boxcar_counter);
         boxcar_component.is_wait_for_turntable = true;
         train.boxcar_squad.Add(boxcar);
         // save gameobject tile with adjustments. when a user clicks on a tile, it will be in the tile opposite the vehicle's orientation. Therefore, flip orientation
         //Vector2Int boxcar_board_position = RouteManager.get_straight_next_tile_pos(TrackManager.flip_straight_orientation(boxcar_component.orientation), (Vector2Int)boxcar_component.tile_position);
         boxcar_component.city.city_board[pos_pair.tile_dest_pos.x + 1, pos_pair.tile_dest_pos.y + 1] = boxcar; // offset boxcar to be consistent
         //print("new boxcar created at tile position " + boxcar_component.tile_position);
     }
 }
Ejemplo n.º 2
0
 public void exit_city(string exit_track_tile_type)
 {
     station_track.train    = null;
     exit_track_orientation = TrainRouteManager.get_destination_track_orientation(exit_track_tile_type);
     city.turn_table.GetComponent <Turntable>().add_train_to_queue(gameObject);
     city.total_people -= 1;
     set_boxcar_exit_track_orientation(exit_track_orientation);
 }
Ejemplo n.º 3
0
    public void spawn_moving_object(MovingObject moving_object)
    {
        // position vehicle in center of tile and set first destination
        Vector3Int tile_position          = moving_object.tile_position;
        Vector3    moving_object_position = TrainRouteManager.get_spawn_location(tile_position, moving_object.orientation);

        moving_object.transform.position = moving_object_position;
        if (moving_object.tag == "boxcar")
        {
            Boxcar boxcar = (Boxcar)moving_object;
        }
        StartCoroutine(moving_object.prepare_for_departure());
    }
Ejemplo n.º 4
0
    public IEnumerator prepare_for_departure()
    {
        Vector3 vehicle_departure_point = TrainRouteManager.get_city_boundary_location(tile_position, orientation); // tile pos is 3,6 not 10,6

        if (in_city)
        {
            next_tilemap_position = BoardManager.pos_to_tile(vehicle_departure_point);
        }
        is_halt = true; // NO MORE MOVEMNET UPDATES UJUST FINISH THE CURRENT ONE SO WE CAN PREPARE FOR DEPART
        while (in_tile) // wati for prev movment to complete
        {
            yield return(new WaitForEndOfFrame());
        }
        in_tile = true;  // allow vehicle to move to the border of tile before resuming its route
        is_halt = false; // indicates a vehicle is about to leave
        StartCoroutine(straight_move(transform.position, vehicle_departure_point));
    }
Ejemplo n.º 5
0
    public void update_vehicle_board(GameObject[,] vehicle_board, GameObject game_object, Vector3Int unadjusted_position, Vector3Int unadjusted_prev_position)
    {
        // note the OFFSET by 1 in xy direction to include negative tile positions in the board. This board is updated outside city and inside city, up to the boarding position (not after dont ask me why)
        Vector3Int position      = new Vector3Int(unadjusted_position.x + 1, unadjusted_position.y + 1, unadjusted_position.z);
        Vector3Int prev_position = new Vector3Int(unadjusted_prev_position.x + 1, unadjusted_prev_position.y + 1, unadjusted_prev_position.z);

        try
        {
            bool initial_vector = prev_position.Equals(new Vector3Int(-1, -1, -1));
            if (!initial_vector)
            {
                if (vehicle_board[prev_position.x, prev_position.y] == null)
                {
                    //print("WARNING. Gameobject " + game_object.name + " not found in previous position " + prev_position);
                }
                else
                {
                    if (vehicle_board[prev_position.x, prev_position.y] == game_object) // only remove gameobject references to itself
                    {
                        vehicle_board[prev_position.x, prev_position.y] = null;
                    }
                }
            }
            bool       in_city          = game_object.GetComponent <MovingObject>().in_city;
            GameObject city_object      = CityManager.instance.get_city(new Vector2Int(position.x, position.y));
            string     destination_type = TrainRouteManager.get_destination_type(unadjusted_position, in_city);
            if (destination_type == "city") // if vehicle arriving at city is a boxcar, don't update tile
            {
                if (game_object.tag == "train")
                {
                    vehicle_board[position.x, position.y] = game_object;                           // only trains should be in cities, it stores a list of attached boxcars
                }
            }
            else
            {
                vehicle_board[position.x, position.y] = game_object;
                ////print("Update Vehicle Board with object " + game_object.name + " to position " + position);
            }
        }
        catch (IndexOutOfRangeException e)
        {
            //print(e.Message + " Position: " + position);
        }
    }
Ejemplo n.º 6
0
    public IEnumerator straight_move(Vector2 start_position, Vector2 destination, bool turntable_dest = false, bool exit_dest = false)
    {
        float og_distance = Vector2.Distance(start_position, destination); // distance to destination
        float distance    = og_distance;

        in_tile       = true;
        next_position = start_position;
        float original_speed = speed;

        if (is_fill_void)
        {
            speed = normal_speed;
        }
        while (distance > GameManager.tolerance)
        {
            if (is_pause)
            {
                yield return(new WaitForEndOfFrame()); //delay updating the position if vehicle is idling is_inventory

                continue;                              // don't execute the code below
            }
            if (gameObject.tag == "boxcar")
            {
                Boxcar boxcar = gameObject.GetComponent <Boxcar>();
                if (boxcar.abort_move)
                {
                    boxcar.abort_move = false;
                    //print("abort straight move");
                    break;
                }
                if (!boxcar.train.is_train_departed_for_turntable) // only stop boxcar if train is stationary.
                {
                    if (!is_boxcar_stopped)
                    {
                        stop_car_if_wait_tile();
                    }
                }
            }
            float step = speed * Time.deltaTime; // calculate distance to move
            next_position      = Vector2.MoveTowards(next_position, destination, step);
            transform.position = next_position;
            distance           = Vector2.Distance(next_position, destination);
            yield return(new WaitForEndOfFrame());
        }
        if (turntable_dest) // train reaches other end of turntable
        {
            // wait for user input to choose depart track
            // city knows which train is on the turntable

            while (exit_track_orientation == RouteManager.Orientation.None)
            {
                yield return(new WaitForEndOfFrame()); //delay updating the position if vehicle is idling
            }
            if (gameObject.tag == "train")
            {
                Train train = gameObject.GetComponent <Train>();
                train.halt_train(true, true); // prevent orientation from being updated with last tile position (eg se_diag) while rotating
                while (!train.is_all_car_reach_turntable())
                {
                    yield return(new WaitForEndOfFrame());
                }
                yield return(new WaitForSeconds(1));

                city.turn_turntable(gameObject, exit_track_orientation); // turntable turns to destination track
            }
            else // reset boxcar speed multiplier after it has been created
            {
                is_halt          = true; // otherwise the boxcar wont spin with the turntable
                speed_multiplier = 1.0f;
            }
        }
        in_tile = false;
        if (exit_dest)
        {
            in_city           = false;
            orientation       = exit_track_orientation;
            final_orientation = orientation;
            if (gameObject.tag == "train")
            {
                city.delete_train(gameObject);
                prev_city = city;
                Vector3Int city_location = city.get_location();
                VehicleManager.instance.depart(gameObject, city_location);
                city.turn_table.GetComponent <Turntable>().remove_train_from_queue(gameObject);
                game_manager.enable_vehicle_for_screen(gameObject);
                gameObject.GetComponent <Train>().set_boxcar_to_depart(); // set depart = true so boxcars leave city
            }
            else
            {
                gameObject.GetComponent <Boxcar>().receive_train_order = false; // reset
            }
            prev_city               = city;
            next_tilemap_position   = TrainRouteManager.get_depart_tile_position(orientation, city.get_location()); // otherwise get stuck on track
            exit_track_orientation  = RouteManager.Orientation.None;
            steep_angle_orientation = RouteManager.Orientation.None;
            reset_departure_flag();
            // disable until given the goahead
        }
        if (arriving_in_city)
        {
            is_halt = true; // NO MREO UPDATES UNTNIL THE TRAIN IS IN NEW CITY LOCATION OTHERWISE IT WILL DERP AND TRY TO GO TO INVALID SPACE
            arrive_at_city();
            arriving_in_city = false;
        }
        if (is_fill_void)
        {
            speed        = original_speed;
            is_fill_void = false;
        }
    }