// TODO: Remove unnecessary bits from schema

AFRAME.registerComponent('socket-controls', {
  schema: {
    previousPos: {default: new THREE.Vector3()},
    currentPos: {default: new THREE.Vector3()},
    nextPos: {default: new THREE.Vector3()},
    updateRate: {default: 100}, // Dynamic updateRate in Hz
    playerId: {default: ''},
    socket: {default: null},
    lastUpdateTime: {default: 0},
    updateWaiting: {default: false},
  },

  init: function() {
    const data = this.data;
    const socket = window.socket;
    socket.on('simulationUpdate', players => {
      data.updateWaiting = true;
      data.previousPos = data.nextPos;
      data.nextPos = players[data.playerId].position;
    });
  },

  tick: function(t, dt) {
    const data = this.data;

    if (data.updateWaiting) {
      data.updateRate = (t - data.lastUpdateTime);
      data.lastUpdateTime = t;
      data.updateWaiting = false;
    }

    // linear interp from data.lastPos to data.nextPos
    data.alpha = (t - data.lastUpdateTime) / data.updateRate;
    data.currentPos.lerpVectors(data.previousPos, data.nextPos, data.alpha);
    this.el.setAttribute('position', this.data.currentPos);
  },

  update: function (previousData) {
  },

});

// TODO: Remove unnecessary bits from schema

AFRAME.registerComponent('socket-controls', {
  schema: {
    updateRate: {default: 100}, // Dynamic updateRate in Hz
    playerId: {default: ''},
    socket: {default: null},
    enabled: {default: true}
  },

  init: function() {
    const data = this.data;
    const socket = window.socket;
    this.previousPos = new THREE.Vector3();
    this.currentPos = new THREE.Vector3();
    this.nextPos = new THREE.Vector3();
    this.lastUpdateTime = 0;
    this.updateWaiting = false;
    this.updateRate = data.updateRate;

    if(data.enabled) {
      socket.on('simulationUpdate', players => {
        this.updateWaiting = true;
        this.previousPos = this.nextPos;
        this.nextPos = players[data.playerId].position;
      });
    }
  },

  tick: function(t, dt) {
    if(this.data.enabled && this.updateWaiting) {
      this.updateRate = (t - this.lastUpdateTime);
      this.lastUpdateTime = t;
      this.updateWaiting = false;
      // linear interp from data.lastPos to data.nextPos
      const alpha = (t - this.lastUpdateTime) / this.updateRate;
      this.currentPos.lerpVectors(this.previousPos, this.nextPos, alpha);
      this.el.setAttribute('position', this.currentPos);
    }
  }

});

Add enabled param and move some params from schema to init for socket-control
Add enabled param and move some params from schema to init for socket-control
