Introduction
Ship Building Phase is built using two main architectures: State Machine and Pub-Sub Events. The state machine controls the flow of the ship-building phase. It allows each script to know the current state of the ship-building phase and enable/disable specific actions. Pub-Sub events are the communication method scripts use to communicate with each other. This documentation will roughly go through the states of the Ship Building Phase, provide a diagram of pub-sub events, and go into details about the important how-tos. Enjoy!
Related Scripts
- GameData
- PlayerData
- Refund Info
- Ship Builder
- Ship Building Manager
- Ship Building UI Manager
- Shop UI Manager
- Crew Member Shop Content
- Ship System Shop Content
- Highlight Tiles and Room
The State Machine
States of Ship Building Phase
Idle: Nothing happens here. We are in a state where the players can freely choose their next action: Open the shop (to In_Shop state) or Proceed to Battle.
In_Shop: Shop window is open. Players can choose to: Purchase a System (to Placing_System state), Purchase a Crew (to Placing_Crew state), or Close the Shop (to Idle state)
Placing_System: Shop window closed. Rooms are highlighted. Players can choose to: Cancel Purchase (to In_Shop state), Select an Empty Room (to In_Shop state), or Select an Occupied Room (to In_Shop state)
Placing_Crew: Sjop window closed. Tiles are highlighted. Players can choose to: Cancel Purchase (to In_Shop state), Select and Empty Tile (to In_Shop state)
Pub-Sub Events Communication
ShipBuildingEvents.cs and InputEvents.cs is the two event types you need to know. Input Events deal with player inputs. This includes right clicks, left clicks, and hovers. These inputs are detected by PlayerController.cs. PlayerController.cs will publish corresponding events, and the scripts that subscribed to it will be triggered when publishing happens. Ship Building Events deals with everything else, from UI display to signaling an item is bought. One event is especially important! UpdateShipBuildingStateEvent is THE event that updates the state machine. Almost every script in the ship-building phase (all listed in the diagram) is read and write it.
"How to use this diagram?"
Whenever you see a "Publish" or "Subscribe" and don't know which script is affected, just look up the event's name! Note that it only helps you connect from function and function. To understand what each function does, please refer to the script's documentation.
Input Events Diagram
Ship Building Events Diagram
How Tos
How to store data?
To answer this, I have to talk about SaveManager.cs
first. It stores any serializable type into your local machine with a key (the key will be written in the filename). What you can do is use save<T>(string key, T value)
and load<T>(string key)
to save and load your data. All data is saved as a key-value pair. The best part of this Save-Load system is that it can couple with C#'s getter and setter properties to make save and load automatic (Just call the variable!). There are a lot of example in PlayerData.cs.
For example:
public static int Credits
{
get { return SaveManager.Load<int>("Credits"); }
set { SaveManager.Save<int>("Credits", value); }
}
If the programmer called PlayerData.Credits
in another script, Load
in SaveManager.cs
will go to your local file system and file the file name that has "Credits" associated with it and load the data from that file. When the programmer wants to assign "Credits" with other values, save is called in the process, to the changes will be stored automatically on your local machine. For example, PlayerData.Credits = 3
is called, SaveManager.cs
will override the local save with 3. This also allows more complex operations like ... to work. Loading and saving a list also works! However, it is a bit tricky. It only returns a copy to the list, and you will need to create a List<T> temp
, modify it, and save it for the changes to be saved. (I am still working on it to make saving easier).
So how is data stored? Since we want to save and load to be compatible with the save/load systems, both crew and system info are stored in structs. Systems are stored as ShipRoom, and crews are stored as Crew. The Ship Builder will then look at the struct and spawn in the correct system/crew.
How to select rooms/tiles?
Input Events! Player inputs are detected using functions in ..., which will publish an event accordingly. When the ship-building state is at "Placing_System" and "Placing_Crew" and room/tile is pressed. CheckTileSelected in CrewMemberShopContent or CheckRoomSelected in ShipSystemShopContent will be triggered by it. They will then convert this Input Event into a ShipBuildingEvent by publishing ShipRoomSelectedForCrew and ShipRoomSelectedForRoom accordingly. Ship Building Manager will be invoked by this event and will store the data in the PlayerData.cs.