File size: 1,233 Bytes
8194362
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Simulation from './Simulation'

export default class Teleporter {

  constructor (player) {
    this.player = player
    this.targets = []
  }

  addTarget ({ position, lookAt, galaxy }) {
    this.targets.push({
      position,
      lookAt,
      galaxy
    })
  }

  getClosestTeleportIndex () {
    let minDistance = Infinity
    let result = null

    for (let i = 0; i < this.targets.length; i++) {
      let distance

      if (this.targets[i].galaxy !== this.player.galaxy) {
        distance =
          this.player.object.position.distanceTo(Simulation.config.wormhole.position) +
          this.targets[i].position.distanceTo(Simulation.config.wormhole.position)
      }
      else {
        distance = this.player.object.position.distanceTo(this.targets[i].position)
      }

      if (distance < minDistance) {
        minDistance = distance
        result = i
      }
    }
    return result
  }

  teleportNext () {
    const nextIndex = (this.getClosestTeleportIndex() + 1) % this.targets.length

    this.teleportTo(this.targets[nextIndex])
  }

  teleportTo (target) {
    this.player.object.position.copy(target.position)
    this.player.galaxy = target.galaxy

    this.player.lookAt(target.lookAt)
  }

}