/************************************************************************************************************************* File: rail.js Purpose: This file contains the class definition for a rail 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 rail to the view by calling a single line in sketch.js. 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 Rail { // Constructor constructor(rail_obj) { /*************************************************************************************** Function: constructor Purpose: Takes a rail object from the server & converts it into this Rail object so that its Draw() function can be used. Input: rail_obj - An object containing the information for the rail that is sent as a JSON string from the server. It is marshalled into an object from the string before being passed to this constructor. ****************************************************************************************/ // Copy over the information from the rail object this.id = rail_obj.rail_id; this.x = rail_obj.x; this.y = rail_obj.y; this.orientation = rail_obj.orientation; } Draw() { /*************************************************************************************** Function: Draw Purpose: Draws the rail to the canvas according to its properties. The rail is represented by a gray line in this case. Each rail is bounded by a 'box' that is 50 px wide, and the x and y coordinate of the 'rail' represent where the top left corner of where that box is ****************************************************************************************/ // Position the rail (coordinate is in top left of 'box' the rail is on) let x = 30 + 50 * this.x; let y = 30 + 50 * this.y; // Draw the rail stroke(170, 170, 170); strokeWeight(1); switch (this.orientation % 4) { case 0: // Right & left line(x, y, x + 50, y); break; case 1: // Up Right & down left line(x, y + 50, x + 50, y); break; case 2: // Up & down line(x, y, x, y + 50); break; case 3: // Up Left & down right line(x, y, x + 50, y + 50); break; } if (this.orientation == 0) // Right { strokeCap(SQUARE); line(x, y, x + 50, y); } } } // end class