board.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import * as three from "three";
  2. class Board{
  3. constructor(scene, height, width, length){
  4. this.scene = scene;
  5. this.height = height;
  6. this.width = width;
  7. this.length = length;
  8. const geometry = new three.BoxGeometry(height, width, length);
  9. const edges = new three.EdgesGeometry(geometry);
  10. const lineMaterial = new three.LineBasicMaterial({color: 0xffffff});
  11. const line = new three.LineSegments(edges, lineMaterial);
  12. const material = new three.MeshBasicMaterial({color: 0x7a4300});
  13. const board = new three.Mesh(geometry, material);
  14. this.board = new three.Group();
  15. this.board.add(board);
  16. this.board.add(line);
  17. scene.add(this.board);
  18. }
  19. translate(x, y, z){
  20. this.board.position.x += x;
  21. this.board.position.y += y;
  22. this.board.position.z += z;
  23. }
  24. rotate(x, y, z){
  25. this.board.rotation.x += this.toRadians(x);
  26. this.board.rotation.y += this.toRadians(y);
  27. this.board.rotation.z += this.toRadians(z);
  28. }
  29. copyBoardData(board){
  30. board.position.x = this.board.position.x;
  31. board.position.y = this.board.position.y;
  32. board.position.z = this.board.position.z;
  33. board.rotation.x = this.board.rotation.x;
  34. board.rotation.y = this.board.rotation.y;
  35. board.rotation.z = this.board.rotation.z;
  36. }
  37. surfaceArea(){
  38. let w = this.width;
  39. let l = this.length;
  40. let h = this.height;
  41. return 2*((w*l)+(l*h)+(h*w));
  42. }
  43. toRadians(n){
  44. return n*(Math.PI/180);
  45. }
  46. }
  47. export class CheapBoard extends Board{
  48. constructor(scene, length){
  49. super(scene, 0.625, 5.5, length);
  50. }
  51. clone(){
  52. let newBoard = new CheapBoard(this.scene, this.length);
  53. this.copyBoardData(newBoard.board);
  54. return newBoard;
  55. }
  56. }
  57. export class TwoByFour extends Board{
  58. constructor(scene, length){
  59. super(scene, 2, 4, length);
  60. }
  61. clone(){
  62. let newBoard = new TwoByFour(this.scene, this.length);
  63. this.copyBoardData(newBoard.board);
  64. return newBoard;
  65. }
  66. }
  67. export class FourByFour extends Board{
  68. constructor(scene, length){
  69. super(scene, 4, 4, length);
  70. }
  71. clone(){
  72. let newBoard = new TwoByFour(this.scene, this.length);
  73. this.copyBoardData(newBoard.board);
  74. return newBoard;
  75. }
  76. }