008 - Restructure the Assembly Line

Date: June 4th 2020

The last experiment was a big success but I ended up coding myself into a corner. In the previous experiment I had a state for each type of particle and then rendered each according to their type. This was fine, except that when one particle could turn into multiple types then it meant that i ran into issues with the callback functions. Now I need to to set up my state so that each particle has a specific type and this type can be rendered differently depending on when it completes.

I think I need to set up a particle state and then using a function, look up that type and what it can turn into and then execute this based on a certain probability.

Have had to restructure the state machine several times in order to get this to work but now it seems much more flexible for adding and removing paths while still being able to manipulate how items flow through the system. Currently there are two states, one for the materials and one for the systems. The material state can be updated through nextMaterial() which removes the material that just arrived and then looks up the next possible path and makes that material to be rendered next. The function to select the next path is particularly interesting:

 const pickPath = (path: RouteType): PathType | WastePathType => {
    //Is there the required system?
    if (systems[path.require]) {
      //Chance it still goes to waste
      if (path.wasteRoute.chance > Math.random()) {
        return path.wasteRoute;
      } else {
          //Pick random path
        const picked = Math.floor(Math.random() * path.possibleRoutes.length);
        return path.possibleRoutes[picked];
      }
      //else, goes to waste
    } else return path.wasteRoute;
  };

Here I can specify in the routes JSON where each path can lead as well as what the default waste state is incase the system isn't inplace. I can also adjust the change of an item being sent to waste anyway as well as the number of materials created for each route. Additionally I could have multiple routes take the same path but with varying amounts of materials.

Figuring out the types for this has been a bit of a challenge as these need to be passed in and returned to makes sure there are no bugs, but Typescript has been a huge help in flagging anything that was wrong or missig something.

interface MaterialType {
  name: string;
  delay: number;
  id: string;
  type: string;
  path: string;
  highlight: boolean;
}

interface RouteType {
  parent: string;
  require: string;
  possibleRoutes: Array<PathType>;
  wasteRoute: WastePathType;
}

interface PathType {
  name: string;
  type: string;
  amount: number;
  chance?: number;
  path: string;
}

interface WastePathType {
  name: string;
  type: string;
  amount: number;
  chance: number;
  path: string;
}

I'm sure i could still tidy up the WastePathType by extending the PathType, but i'm unsure how to do that just now. For now it covers all the features that I wanted to have so that I can move onto the next step of implimenting more design elements as well as figure out how to narrate the story.