Waypoint.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import * as THREE from '../threejs/build/three.module.js';
  2. import { Hive } from './Hive.js';
  3. import { LineMaterial } from '../threejs/examples/jsm/lines/LineMaterial.js';
  4. import { LineGeometry } from '../threejs/examples/jsm/lines/LineGeometry.js';
  5. import { Line2 } from '../threejs/examples/jsm/lines/Line2.js';
  6. export class Waypoint {
  7. static collection = [];
  8. constructor(scene) {
  9. this.scene = scene;
  10. this.position = new THREE.Vector3();
  11. this.links = [];
  12. this.lines = [];
  13. this.special = null;
  14. }
  15. init() {
  16. }
  17. update(delta, timer) {
  18. }
  19. addLink(link) {
  20. this.links.push(link);
  21. }
  22. removeLink(link) {
  23. this.links.splice(this.links.indexOf(link), 1);
  24. return link;
  25. }
  26. rebuildLines() {
  27. for (let i = 0; i < this.lines.length; i++) {
  28. this.scene.remove(this.lines[i]);
  29. }
  30. this.lines.length = 0;
  31. for (let s = 0; s < this.links.length; s++) {
  32. let link = this.links[s];
  33. let line = Waypoint.buildLine(this, link);
  34. this.lines.push(line);
  35. }
  36. }
  37. addLine(link) {
  38. this.lines.push(Waypoint.buildLine(this, link));
  39. }
  40. removeLine(link) {
  41. this.rebuildLines();
  42. }
  43. contains(link) {
  44. return this.links.includes(link);
  45. }
  46. visitFromBee(bee) {
  47. if (bee.carryingHoney > 0) {
  48. bee.setTarget(this.links.filter(l => l instanceof Hive)[0]);
  49. return;
  50. }
  51. bee.setTarget(this.links[Math.floor(this.links.length * Math.random())]);
  52. }
  53. static addNewHive(scene, hive) {
  54. let newWaypoint = new Waypoint(scene);
  55. newWaypoint.position.x = hive.position.x + (30 * Math.cos((Math.PI / 2) - hive.rotation.y));
  56. newWaypoint.position.y = hive.position.y;
  57. newWaypoint.position.z = hive.position.z + (30 * Math.sin((Math.PI / 2) - hive.rotation.y));
  58. newWaypoint.addLink(hive);
  59. Waypoint.collection.push(newWaypoint);
  60. return newWaypoint;
  61. }
  62. static buildLine(startObj, endObj) {
  63. let points = [];
  64. points.push(new THREE.Vector3(startObj.position.x, 1.2, startObj.position.z));
  65. points.push(new THREE.Vector3(endObj.position.x, 1.2, endObj.position.z));
  66. let color = new THREE.Color();
  67. color.setHSL(Math.floor(360 * Math.random()) / 360, 1.0, 0.7);
  68. let positions = [];
  69. let colors = [];
  70. for (let w = 0; w < points.length; w++) {
  71. positions.push(points[w].x, points[w].y, points[w].z);
  72. colors.push(color.r, color.g, color.b);
  73. }
  74. let lineGeo = new LineGeometry();
  75. lineGeo.setPositions(positions);
  76. lineGeo.setColors(colors);
  77. let lineMat = new LineMaterial({
  78. color: 0xffffff,
  79. linewidth: 5, // in pixels
  80. vertexColors: true,
  81. //resolution: // to be set by renderer, eventually
  82. dashed: false
  83. });
  84. lineMat.resolution.set(window.innerWidth, window.innerHeight);
  85. let line = new Line2(lineGeo, lineMat);
  86. line.computeLineDistances();
  87. line.scale.set(1, 1, 1);
  88. return line;
  89. }
  90. }