/************************************************************************************************************************* File: turnout.js Purpose: This file contains the class definition for a turnout to be used in the view. The main reason to use this class instead of just using a raw object gleaned from a JSON string from the server is that this class implements a Draw function that is capable of displaying the turnout to the view by calling a single line in sketch.js. An alternate implementation would have been to just write a function to do this in sketch.js, but this is how I have chosen to implement the idea. References: Daniel Shiffman - The Coding Train - "Classes in JavaScript with ES6": https://www.youtube.com/watch?v=T-HGdc8L-7w P5.JS Reference - https://p5js.org/reference/ Written by John McGorey - Spring 2019 - Using Microsoft visual Studio Code 2019 For use in the St. Norbert College CSCI460 Senior Capstone Experience **************************************************************************************************************************/ class Turnout { // Constructor constructor(turnout_obj) { /*************************************************************************************** Function: constructor Purpose: Takes a turnout object from the server & converts it into this Turnout object so that its Draw() function can be used. Input: turnout_obj - An object containing the information for the turnout that is sent from the server. ****************************************************************************************/ this.id = turnout_obj.turnout_id; // The ID # of the turnout this.progress = turnout_obj.progress; // The progress towards switching from one turnout state to the other this.orientation = turnout_obj.orientation; // The orientation of the turnout, 0-7 this.x = turnout_obj.x; // The x grid-coordinate of the turnout this.y = turnout_obj.y; // The y grid-coordinate of the turnout this.status = turnout_obj.status; // The status of the turnout: STRAIGHT, CURVED, or MOVING } Draw() { /*************************************************************************************** Function: Draw Purpose: Draws the turnout to the canvas according to its properties. The current position of the rail is represented as a thicker orange line, while the possible configurations of the rail for the STRAIGHT and CURVED statuses are drawn as thin yellow lines. Note: I took a bit of a shortcut and just made turnouts 'grow' to fit their diagonals, rather than using a fixed sized line to represent the track. This was just for my ease of use and could totally be changed in future versions of the client. ****************************************************************************************/ // Get the coordinates to place the track based on its grid position let x = 30 + 50 * this.x; let y = 30 + 50 * this.y; // Draw the rail stroke(255,185,53); // Faint yellow color strokeWeight(0.4); // Draw the potential paths of the turnout for the STRAIGHT and CURVED statuses switch (this.orientation) { case 0: // Left -> Right, Curved down line(x, y, x + 50, y); line(x, y, x + 50, y + 50); break; case 1: // Left -> Right, curved up line(x, y + 50, x + 50, y + 50); line(x, y + 50, x + 50, y); break; case 2: // Down -> Up, curved right line(x, y, x, y + 50); line(x, y + 50, x + 50, y); break; case 3: // Right -> Left, curved up line(x, y + 50, x + 50, y + 50); line(x, y, x + 50, y + 50); break; case 4: // Right -> Left, curved down line(x, y, x + 50, y); line(x, y + 50, x + 50, y); break; case 5: // Up -> Down, curved left line(x + 50, y, x + 50, y + 50); line(x, y + 50, x + 50, y); break; case 6: // Down -> Up, curved left line(x + 50, y, x + 50, y + 50); line(x, y, x + 50, y + 50); break; case 7: // Up -> Down, curved right line(x, y, x, y + 50); line(x, y, x + 50, y + 50); break; } // Draw where the actual track is stroke(230, 81, 0); strokeWeight(2); let change = 50 * this.progress / 100; // Find how far along the turnout is in switching between statuses // Draw the turnout according to its orientation switch (this.orientation) { case 0: if (this.status == "STRAIGHT") line(x, y, x + 50, y); else if (this.status == "CURVED") line(x, y, x + 50, y + 50); else { // If moving if (this.progress > 0) { // If Straight -> Curved line(x, y, x + 50, y + change); } else { // If Curved -> Straight line(x, y, x + 50, y + 50 + change); } } break; case 1: if (this.status == "STRAIGHT") line(x, y + 50, x + 50, y + 50); else if (this.status == "CURVED") line(x, y + 50, x + 50, y); else { if (this.progress > 0) // If straight -> Curved line(x, y + 50, x + 50, y + 50 - change); else // If Curved -> Straight line(x, y + 50, x + 50, y - change); } break; case 2: if (this.status == "STRAIGHT") line(x, y+50, x, y); // Draw a line from the bottom left to the top left else if (this.status == "CURVED") line(x, y+50, x + 50, y); // Draw a line from the bottom left to the top right else { if (this.progress > 0) // If straight -> Curved line(x, y + 50, x + change, y); else // If curved -> straight line(x, y + 50, x + 50 + change, y); // Draw a line from the bottom left to the change } break; case 3: if (this.status == "STRAIGHT") line(x, y + 50, x + 50, y + 50); else if (this.status == "CURVED") line(x, y, x + 50, y + 50); else { if (this.progress > 0) // If straight -> Curved line(x + 50, y + 50, x, y + 50 - change); else // If curved -> Straight line(x + 50, y + 50, x, y - change); } break; case 4: if (this.status == "STRAIGHT") line(x, y, x + 50, y); else if (this.status == "CURVED") line(x, y + 50, x + 50, y); else { if (this.progress > 0) // If straight -> Curved line(x + 50, y, x, y + change); else // If curved -> Straight line(x + 50, y, x, y + 50 + change); } break; case 5: if (this.status == "STRAIGHT") line(x + 50, y, x+50, y+50); // Draw from top right to bottom right else if (this.status == "CURVED") line(x + 50, y, x, y + 50); // Draw from top right to bottom left else { if (this.progress > 0) // If straight -> Curved line(x + 50, y, x + 50 - change, y + 50); else // If curved -> Straight line(x + 50, y, x - change, y + 50); } break; case 6: if (this.status == "STRAIGHT") line(x + 50, y + 50, x + 50, y ); // Draw from bottom left to top left else if (this.status == "CURVED") line(x + 50, y + 50, x, y); // Draw from bottom left to top right else { if (this.progress > 0) // If straight -> Curved line(x + 50, y + 50, x + 50 - change, y); else // If curved -> Straight line(x + 50, y + 50, x - change, y); } break; case 7: // Pivot point top left corner if (this.status == "STRAIGHT") line(x, y, x, y + 50); else if (this.status == "CURVED") line(x, y, x + 50, y + 50); else { if (this.progress > 0) // If straight -> Curved line(x, y, x + change, y + 50); else // If curved -> Straight line(x, y, x + 50 + change, y + 50); } break; } // Draw the turnout labels so the users can identify which one is which noStroke(); fill(255, 255, 255); text('T' + this.id + '', x + 25, y + 25); } }