Train Design Overview
While in the LocalMap scene the primary objective of the Players will be to move the Train from the entry point of the map along the rails to the exit point.
The Train consists of four Train Car objects which are linked together. Each Train Car object takes up 9x3 spaces on the grid of the LocalMap and can be entered into by Characters, who can then move freely through the Train Cars to interact with different *Station objects within the Train Cars.
The four Train Car objects would be:
Engine Car
This would only have a 4x1 interior space and a single interactable *Station which would cause the Train to move forwards along the tracks when activated.
A major component of the Engine Car would be an interactable *Station which takes in Coal resources. If the Coal is not above zero the interactable module will not move the Train forward when activated.
Support Car
This Train Car would have the full 9x3 interior space
Half of it would have some storage *Stations objects for keeping RawFood and CookedFood as well as a *Station for converting RawFood into CookedFood
The other half would have storage for MedicalSupply & Metal and a *Station for converting Metal into MedicalSupply. It would also have a 'self-serve' healing *Station
Industrial Car
This Train Car would have 6x3 interior space
Most of it would be dedicated to storage for Metal, a *Station for converting Metal into any kind of Tool and *Stations for converting Metal into Condition on a Tool placed into the *Station
Supply Car
This Train Car would only have 5x1 interior space
It would contain storage for all different resources
Train Movement Mechanics
Within each LocalMap a string of grid spaces from one side of the playable area to the other will be designated as 'RailNodes'. The visual sprite for these RailNodes will be larger than the single grid space and, where the rail line turns or does not run straight, would not follow the rules for the grid-arrangement.
The important part of the RailNode would be the single grid space it occupies. Each Train Car of the train would have a pair of RailAttachPoints assigned to them - one at the front of the Train Car and one at the back, both in the centreline of the Train Car. Whenever the Train moves as a result of a Character interacting with the *Station in the EngineCar an event gets fired to move the train.
All of the RailNodes are stored within a list on each RailAttachPoint, along with a variable for the RailNode the RailAttachPoint is currently sitting on.
When the event is fired from the EngineCar each pair of RailAttachPoints then interpolates from the RailNode its currently on to the next RailNode. The Train Car itself would be anchored to a game object taking up a single grid space which always keeps itself at a position exactly between the two RailAttachPoints and faces towards the front RailAttachPoint.
This would keep each TrainCar pointing in the direction the rail takes it and keep them moving and turning independant of each other along the rail.
In the image below the RailAttachPoints are indicated by red dots, while the Train Car sprite's centrepoint is indicated in green at the middlepoint between the two RailAttachPoints.
Boarding and Exiting the Train Cars
While this implementation would allow the train to follow a railway path which isn't dead straight along the grid, the interior grid layout of the train cars would end up misaligned with the LocalMap's grid. To counter this each Train Car would have a pair of interaction points (TrainDoor objects) on the access points on the side of the Train Car. When the Character is outside the train and interacts with the TrainDoor they are made a child of the Train Car in the heirarchy and they are teleported to the valid grid space inside the car. When inside the Train Car and interacting with the TrainDoor the opposite happens and they are moved to the nearest available grid space outside the car.
In order to move within the Train Car the CharacterMovement script would then most likely require a seperate state which changes how inputs move the Character, with the directional keys now moving them up/down/left/right in relation to the orientation of the grid within the Train Car.