All files / src/missions gear-dial.ts

88.59% Statements 303/342
57.14% Branches 24/42
79.16% Functions 19/24
88.59% Lines 303/342

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 4431x 1x   1x             1x                       1x 8x 8x 8x 8x   8x 8x 8x 8x 8x 8x 8x 8x 8x     8x 8x 8x 8x 8x   8x 8x   8x 8x 8x   8x 8x 8x   8x 8x 8x 8x 8x   8x 8x     8x 8x 8x 8x 8x 8x       8x 8x 8x 8x 8x 8x   8x     8x 8x 8x 8x 8x 8x 8x 8x   8x 8x   8x 8x 8x   8x   8x 8x     8x 8x 8x 8x 8x 8x     8x     8x 8x 8x 8x 8x     8x 8x 8x 8x 8x 8x 8x   8x 8x 8x 8x 8x 8x 8x     8x 8x 8x 8x 8x 8x 32x 32x 32x 32x 32x 32x 32x 32x 32x     8x 8x 8x   8x     8x     8x 8x 8x     8x 8x 8x 8x 8x 8x     8x 8x 8x 8x 8x 8x     8x 8x 8x 8x 8x 8x   8x 8x 8x 8x 8x 8x 8x   8x 8x   8x 40x     40x 40x 40x           40x 40x 40x     40x 40x   34x 34x 34x     34x     34x 40x   8x 14x         14x 14x 14x 14x 14x 14x     14x 14x 14x 14x     14x 14x     14x   14x 14x 14x 14x   8x 72x 72x   72x 72x 72x 72x   8x 6x 6x 6x 6x 6x 6x   6x 6x   6x 6x 6x 6x 6x   6x 6x 6x 6x   8x 4x 4x 2x 2x 4x 4x 4x   8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x     1x 1x 1x 1x 1x 1x         1x 1x 1x   8x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x   8x 14x 8x 8x 8x 14x 14x 14x 14x 14x 14x   8x       8x       8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   8x 3x 3x   8x 3x 3x   8x 17x 17x 17x 17x 17x   8x 1x 1x 1x 1x 1x   8x       8x                     8x           8x 1x 1x 1x 1x 1x 8x  
import * as THREE from 'three';
import RAPIER from '@dimforge/rapier3d-compat';
import { MissionElement } from './types';
import {
  LEGO_COLORS,
  getLegoMaterial,
  createTechnicBeamGroup,
  createLegoPlateGroup,
  createTechnicGearGroup,
} from '../view/lego-visuals';
import { DualLockMarker } from './dual-lock-marker';
 
/**
 * Rotary Gear Turnstile Mission Mechanism
 *
 * Implements an authentic FIRST LEGO League gear turnstile built from
 * official LEGO Technic 24T and 12T gears, 9L liftarms, axle shafts, and a dial face.
 *
 * Driving the robot into any of the 4 cross paddle blades rotates the vertical shaft.
 * The 24T gear drives a 12T gear, rotating a dial indicator needle.
 * An authentic LEGO spring-loaded friction detent latches the dial into the 90-degree scored position.
 */
export class GearDialMission implements MissionElement {
  public readonly id = 'gear-dial';
  public readonly name = 'Mission 2: Rotary Gear Turnstile';
  public readonly description = 'Drive into the paddle arms to rotate the turnstile 90 degrees and activate the green indicator.';
  public readonly rootGroup: THREE.Group;
 
  private world!: RAPIER.World;
  private basePos: { x: number; y: number; z: number } = { x: 0, y: 0, z: 0 };
  private yawDegrees: number = 0;
  public isPlacedOnField: boolean = true;
  public isDualLocked: boolean = true;
  private dualLockMarker: DualLockMarker | null = null;
  private dualLockLocalOffset: { x: number; z: number } | null = null;
  private pedestalBody!: RAPIER.RigidBody;
  private rotorBody!: RAPIER.RigidBody;
 
  // Visual Groups
  private baseplateGroup!: THREE.Group;
  private rotorMesh!: THREE.Group;
  private gearTrainGroup!: THREE.Group;
  private dialNeedleMesh!: THREE.Mesh;
  private flagMesh!: THREE.Mesh;
 
  private currentAngle = 0;
  private initialAngle = 0;
 
  constructor() {
    this.rootGroup = new THREE.Group();
  }
 
  public init(world: RAPIER.World, basePosition: { x: number; y: number; z: number }): void {
    this.world = world;
    this.basePos = { ...basePosition };
 
    this.createPhysicsBodies();
    this.createLegoVisuals();
    this.syncVisuals();
    this.updateDualLockVisualMesh();
  }
 
  private createPhysicsBodies(): void {
    const { x, y, z } = this.basePos;
 
    // 1. Fixed Low-Profile Baseplate Anchor Body (Firmly anchored, low clearance)
    const baseDesc = RAPIER.RigidBodyDesc.fixed().setTranslation(x, y + 0.008, z);
    this.pedestalBody = this.world.createRigidBody(baseDesc);
    this.world.createCollider(
      RAPIER.ColliderDesc.cuboid(0.065, 0.008, 0.065).setFriction(0.8),
      this.pedestalBody
    );
 
    // 2. Dynamic 4-Spoke Turnstile Rotor (Revolves strictly around Y axis, zero pitch/roll wobble)
    // Centered at robot bumper height: Y = 0.035m (Spans Y = 0.021m to 0.049m)
    const rotorDesc = RAPIER.RigidBodyDesc.dynamic()
      .setTranslation(x, y + 0.035, z)
      .setAngularDamping(2.8)
      .setLinearDamping(10.0) // Resist translation; shaft holds it in place
      .lockTranslations(); // Rotate only around Y
    this.rotorBody = this.world.createRigidBody(rotorDesc);
    // Authentically rigid Technic pin fit: strictly lock X (pitch) and Z (roll) rotations
    this.rotorBody.setEnabledRotations(false, true, false, true);
 
    // 4 Cross-Paddle Colliders (Arm 1 along X, Arm 2 along Z, 160mm total span)
    const arm1 = RAPIER.ColliderDesc.cuboid(0.080, 0.014, 0.008)
      .setDensity(1.8)
      .setFriction(0.6)
      .setRestitution(0.0);
    const arm2 = RAPIER.ColliderDesc.cuboid(0.008, 0.014, 0.080)
      .setDensity(1.8)
      .setFriction(0.6)
      .setRestitution(0.0);
 
    this.world.createCollider(arm1, this.rotorBody);
    this.world.createCollider(arm2, this.rotorBody);
 
    this.initialAngle = this.getRotationAngle();
    this.currentAngle = this.initialAngle;
  }
 
  private createLegoVisuals(): void {
    // 1. LEGO Dark Bluish Gray Baseplate (14 x 14 studs with authentic studs on top)
    this.baseplateGroup = createLegoPlateGroup(14, 14, LEGO_COLORS.DARK_GRAY, 1);
    this.rootGroup.add(this.baseplateGroup);
 
    // 2. Center Technic Turntable Base & Shaft Tower
    const towerGeom = new THREE.CylinderGeometry(0.018, 0.022, 0.028, 24);
    const towerMat = getLegoMaterial(LEGO_COLORS.DARK_GRAY, 0.4);
    const towerMesh = new THREE.Mesh(towerGeom, towerMat);
    towerMesh.position.set(0, 0.014, 0);
    towerMesh.castShadow = true;
    this.rootGroup.add(towerMesh);
 
    // 3. Rotating 4-Spoke Turnstile Assembly
    this.rotorMesh = new THREE.Group();
 
    // Central LEGO Technic Hub Bushing
    const hubGeom = new THREE.CylinderGeometry(0.014, 0.014, 0.032, 24);
    const hubMat = getLegoMaterial(LEGO_COLORS.BLACK, 0.4);
    const hubMesh = new THREE.Mesh(hubGeom, hubMat);
    hubMesh.castShadow = true;
    this.rotorMesh.add(hubMesh);
 
    // 4 Cross-Paddle Arms built from Medium Azure 9L Technic Liftarms with holes
    const blade1 = createTechnicBeamGroup(9, LEGO_COLORS.AZURE, {
      width: 0.008,
      thickness: 0.022,
      withPinsAt: [0, 4, 8],
    });
    blade1.position.set(0, 0, 0);
    this.rotorMesh.add(blade1);
 
    const blade2 = createTechnicBeamGroup(9, LEGO_COLORS.AZURE, {
      width: 0.008,
      thickness: 0.022,
      withPinsAt: [0, 4, 8],
    });
    blade2.rotation.y = Math.PI / 2;
    this.rotorMesh.add(blade2);
 
    // Contact Target Strips on each blade tip (Bright Yellow LEGO accents)
    for (const [bx, bz, rotY] of [
      [0.034, 0, 0],
      [-0.034, 0, 0],
      [0, 0.034, Math.PI / 2],
      [0, -0.034, Math.PI / 2],
    ] as const) {
      const tipMesh = new THREE.Mesh(
        new THREE.BoxGeometry(0.012, 0.020, 0.009),
        getLegoMaterial(LEGO_COLORS.YELLOW, 0.3)
      );
      tipMesh.position.set(bx, 0, bz);
      tipMesh.rotation.y = rotY;
      tipMesh.castShadow = true;
      this.rotorMesh.add(tipMesh);
    }
 
    // 4. Authentic LEGO Technic 24-Tooth Spur Gear mounted on the central shaft
    const gear24 = createTechnicGearGroup(24, 0.024, LEGO_COLORS.LIGHT_GRAY, 0.007);
    gear24.position.set(0, 0.018, 0);
    this.rotorMesh.add(gear24);
 
    this.rootGroup.add(this.rotorMesh);
 
    // 5. Gear Train & Dial Indicator Assembly on Base
    this.gearTrainGroup = new THREE.Group();
 
    // Meshing 12-Tooth Bevel Gear (2:1 gear ratio driving the dial)
    const gear12 = createTechnicGearGroup(12, 0.012, LEGO_COLORS.DARK_GRAY, 0.005);
    gear12.position.set(0.032, 0.018, 0);
    this.gearTrainGroup.add(gear12);
 
    // Circular Printed Dial Face Tile
    const dialFaceGeom = new THREE.CylinderGeometry(0.018, 0.018, 0.003, 32);
    const dialFaceMat = getLegoMaterial(LEGO_COLORS.WHITE, 0.3);
    const dialFace = new THREE.Mesh(dialFaceGeom, dialFaceMat);
    dialFace.position.set(0.032, 0.023, 0);
    dialFace.castShadow = true;
    this.gearTrainGroup.add(dialFace);
 
    // Red Needle Pointer on Dial Face
    const needleGeom = new THREE.BoxGeometry(0.015, 0.0015, 0.003);
    needleGeom.translate(0.007, 0, 0); // Pivot at center
    const needleMat = getLegoMaterial(LEGO_COLORS.RED, 0.2);
    this.dialNeedleMesh = new THREE.Mesh(needleGeom, needleMat);
    this.dialNeedleMesh.position.set(0.032, 0.025, 0);
    this.gearTrainGroup.add(this.dialNeedleMesh);
 
    // Green Indicator Flag that pops up upon completing 90 degrees
    const flagPole = new THREE.Mesh(
      new THREE.CylinderGeometry(0.002, 0.002, 0.030, 12),
      getLegoMaterial(LEGO_COLORS.LIGHT_GRAY)
    );
    flagPole.position.set(0, 0.032, 0);
    this.rotorMesh.add(flagPole);
 
    this.flagMesh = new THREE.Mesh(
      new THREE.BoxGeometry(0.022, 0.014, 0.002),
      getLegoMaterial(LEGO_COLORS.GREEN, 0.2)
    );
    this.flagMesh.position.set(0.011, 0.040, 0);
    this.flagMesh.castShadow = true;
    this.rotorMesh.add(this.flagMesh);
 
    this.rootGroup.add(this.gearTrainGroup);
  }
 
  public update(_dt: number): void {
    if (!this.rotorBody) return;
 
    // 1. Keep rotor body firmly centered at base
    const p = this.rotorBody.translation();
    const targetY = this.basePos.y + 0.035;
    if (Math.abs(p.x - this.basePos.x) > 0.001 || Math.abs(p.z - this.basePos.z) > 0.001) {
      this.rotorBody.setTranslation({ x: this.basePos.x, y: targetY, z: this.basePos.z }, true);
      this.rotorBody.setLinvel({ x: 0, y: 0, z: 0 }, true);
    }
 
    // 2. Sample Angle
    this.currentAngle = this.getRotationAngle();
    let angleDelta = Math.abs(this.currentAngle - this.initialAngle);
    if (angleDelta > Math.PI) angleDelta = 2 * Math.PI - angleDelta;
 
    // 3. LEGO Spring-Loaded Ratchet Detent Torque (Snap to 90-degree quadrant)
    const angVel = this.rotorBody.angvel();
    if (Math.abs(angVel.y) < 1.5) {
      // Gentle spring detent torque at every 90 degrees
      const targetQuadrant = Math.round(this.currentAngle / (Math.PI / 2)) * (Math.PI / 2);
      const err = targetQuadrant - this.currentAngle;
      if (Math.abs(err) < 0.25) {
        this.rotorBody.applyTorqueImpulse({ x: 0, y: err * 0.0006 - angVel.y * 0.0012, z: 0 }, true);
      }
      if (Math.abs(err) < 0.02 && Math.abs(angVel.y) < 0.05) {
        this.rotorBody.setAngvel({ x: 0, y: 0, z: 0 }, true);
      }
    }
  }
 
  public syncVisuals(): void {
    if (!this.isDualLocked && this.pedestalBody) {
      const p = this.pedestalBody.translation();
      this.basePos.x = p.x;
      this.basePos.z = p.z;
    }
    const { x, y, z } = this.basePos;
    const radYaw = (this.yawDegrees * Math.PI) / 180;
    this.baseplateGroup.position.set(x, y + 0.002, z);
    this.baseplateGroup.rotation.y = radYaw;
    this.gearTrainGroup.position.set(x, y + 0.002, z);
    this.gearTrainGroup.rotation.y = radYaw;
 
    // Rotor transform
    const rP = this.rotorBody.translation();
    const rR = this.rotorBody.rotation();
    this.rotorMesh.position.set(rP.x, rP.y, rP.z);
    this.rotorMesh.quaternion.set(rR.x, rR.y, rR.z, rR.w);
 
    // 2:1 Gear Ratio Needle Pointer Rotation
    const angleDelta = this.currentAngle - this.initialAngle;
    this.dialNeedleMesh.rotation.y = -angleDelta * 2.0;
 
    // Flag elevation glow on solve
    if (this.isSolved()) {
      this.flagMesh.position.y = 0.044;
    } else {
      this.flagMesh.position.y = 0.038;
    }
  }
 
  private getRotationAngle(): number {
    if (!this.rotorBody) return 0;
    const rot = this.rotorBody.rotation();
    // Yaw angle around Y
    const siny_cosp = 2 * (rot.w * rot.y - rot.z * rot.x);
    const cosy_cosp = 1 - 2 * (rot.y * rot.y + rot.x * rot.x);
    return Math.atan2(siny_cosp, cosy_cosp);
  }
 
  public reset(): void {
    const { x, y, z } = this.basePos;
    const zeroVel = { x: 0, y: 0, z: 0 };
    const radYaw = (this.yawDegrees * Math.PI) / 180;
    const qy = Math.sin(radYaw / 2);
    const qw = Math.cos(radYaw / 2);
    const yawQuat = { x: 0, y: qy, z: 0, w: qw };
 
    this.pedestalBody.setTranslation({ x, y: y + 0.008, z }, true);
    this.pedestalBody.setRotation(yawQuat, true);
 
    this.rotorBody.setTranslation({ x, y: y + 0.035, z }, true);
    this.rotorBody.setRotation(yawQuat, true);
    this.rotorBody.setLinvel(zeroVel, true);
    this.rotorBody.setAngvel(zeroVel, true);
    this.rotorBody.setEnabledRotations(false, true, false, true);
 
    this.initialAngle = this.getRotationAngle();
    this.currentAngle = this.initialAngle;
    this.syncVisuals();
  }
 
  public setPosition(pos: { x: number; y: number; z: number }, yawDegrees?: number): void {
    this.basePos = { ...pos };
    if (yawDegrees !== undefined) {
      this.yawDegrees = yawDegrees;
    }
    this.reset();
    this.updateDualLockVisualMesh();
  }
 
  public setDualLocked(locked: boolean, anchorPoint?: { x: number; z: number }): void {
    this.isDualLocked = locked;
    if (locked) {
      if (anchorPoint) {
        const radYaw = (this.yawDegrees * Math.PI) / 180;
        const cosY = Math.cos(radYaw);
        const sinY = Math.sin(radYaw);
        const dx = anchorPoint.x - this.basePos.x;
        const dz = anchorPoint.z - this.basePos.z;
        this.dualLockLocalOffset = {
          x: dx * cosY - dz * sinY,
          z: dx * sinY + dz * cosY,
        };
      } else if (!this.dualLockLocalOffset) {
        this.dualLockLocalOffset = { x: 0, z: 0 };
      }
    } else {
      this.dualLockLocalOffset = null;
    }
    if (this.pedestalBody) {
      this.pedestalBody.setBodyType(
        locked ? RAPIER.RigidBodyType.Fixed : RAPIER.RigidBodyType.Dynamic,
        true
      );
      if (!locked) {
        this.pedestalBody.setLinearDamping(2.5);
        this.pedestalBody.setAngularDamping(3.5);
        this.pedestalBody.wakeUp();
      }
    }
    this.updateDualLockVisualMesh();
  }
 
  public getDualLockPosition(): { x: number; z: number } | null {
    if (!this.isDualLocked) return null;
    const offset = this.dualLockLocalOffset || { x: 0, z: 0 };
    const radYaw = (this.yawDegrees * Math.PI) / 180;
    const cosY = Math.cos(radYaw);
    const sinY = Math.sin(radYaw);
    return {
      x: this.basePos.x + (offset.x * cosY + offset.z * sinY),
      z: this.basePos.z + (-offset.x * sinY + offset.z * cosY),
    };
  }
 
  private updateDualLockVisualMesh(): void {
    if (!this.dualLockMarker) {
      this.dualLockMarker = new DualLockMarker(this.id);
      this.rootGroup.add(this.dualLockMarker.group);
    }
    this.dualLockMarker.setVisible(this.isDualLocked);
    if (this.isDualLocked) {
      const pos = this.getDualLockPosition() || this.basePos;
      this.dualLockMarker.setPosition(pos.x, 0.002, pos.z);
    }
  }
 
  public getDualLockMarker(): DualLockMarker | null {
    return this.dualLockMarker;
  }
 
  public setDualLockHoverHighlight(isHovered: boolean, isEraseMode: boolean = false): void {
    this.dualLockMarker?.setHoverHighlight(isHovered, isEraseMode);
  }
 
  public setRotation(yawDegrees: number): void {
    if (this.isDualLocked) {
      const fixedPoint = this.getDualLockPosition();
      if (fixedPoint) {
        const offset = this.dualLockLocalOffset || { x: 0, z: 0 };
        const radNew = (yawDegrees * Math.PI) / 180;
        const cosN = Math.cos(radNew);
        const sinN = Math.sin(radNew);
        this.basePos.x = fixedPoint.x - (offset.x * cosN + offset.z * sinN);
        this.basePos.z = fixedPoint.z - (-offset.x * sinN + offset.z * cosN);
      }
    }
    this.yawDegrees = yawDegrees;
    this.reset();
    this.updateDualLockVisualMesh();
  }
 
  public getYawDegrees(): number {
    return this.yawDegrees;
  }
 
  public getPosition(): { x: number; y: number; z: number } {
    return { ...this.basePos };
  }
 
  public isSolved(): boolean {
    this.currentAngle = this.getRotationAngle();
    let angleDeltaDeg = (Math.abs(this.currentAngle - this.initialAngle) * 180) / Math.PI;
    if (angleDeltaDeg > 180) angleDeltaDeg = 360 - angleDeltaDeg;
    return angleDeltaDeg >= 70; // Rotated past 70 degrees
  }
 
  public getScore(): number {
    this.currentAngle = this.getRotationAngle();
    let angleDeltaDeg = (Math.abs(this.currentAngle - this.initialAngle) * 180) / Math.PI;
    if (angleDeltaDeg > 180) angleDeltaDeg = 360 - angleDeltaDeg;
    return Math.min(100, Math.max(0, Math.round((angleDeltaDeg / 90) * 100)));
  }
 
  public getInteractiveMeshes(): THREE.Object3D[] {
    return this.rotorMesh ? [this.rotorMesh] : [];
  }
 
  public applyUserDrag(groundTarget: THREE.Vector3): void {
    if (!this.rotorBody) return;
    const dx = groundTarget.x - this.basePos.x;
    const dz = groundTarget.z - this.basePos.z;
    const targetAngle = Math.atan2(dx, dz);
    let diff = targetAngle - this.currentAngle;
    while (diff > Math.PI) diff -= 2 * Math.PI;
    while (diff < -Math.PI) diff += 2 * Math.PI;
    this.rotorBody.setAngvel({ x: 0, y: diff * 12.0, z: 0 }, true);
  }
 
  public stopUserDrag(): void {
    if (this.rotorBody) {
      this.rotorBody.setAngvel({ x: 0, y: 0, z: 0 }, true);
    }
  }
 
  public destroy(): void {
    if (this.pedestalBody) this.world.removeRigidBody(this.pedestalBody);
    if (this.rotorBody) this.world.removeRigidBody(this.rotorBody);
    this.dualLockMarker?.destroy();
    this.rootGroup.clear();
  }
}