File size: 2,308 Bytes
4ee4376
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Phaser from 'phaser';
import * as CONST from '../constants';

export default class viewport {
  constructor(options) {
    this.scene        = options.scene;
    this.camera       = this.scene.cameras.main;
    this.camera.name  = CONST.CAMERA_NAME;
    this.worldView    = this.camera.worldView;

    this.objectsRendered = 0;

    this.viewportPaddingMultiplier = 1.5;

    this.worldPoint = {
      x: 0,
      y: 0
    };

    let keys = this.scene.input.keyboard.addKeys({
      'up': Phaser.Input.Keyboard.KeyCodes.W,
      'down': Phaser.Input.Keyboard.KeyCodes.S,
      'left': Phaser.Input.Keyboard.KeyCodes.A,
      'right': Phaser.Input.Keyboard.KeyCodes.D,
      'zoomIn': Phaser.Input.Keyboard.KeyCodes.Q,
      'zoomOut': Phaser.Input.Keyboard.KeyCodes.E,
    });

    let controlConfig = {
      camera: this.camera,
      up: keys.up,
      down: keys.down,
      left: keys.left,
      right: keys.right,
      zoomIn: keys.zoomIn,
      zoomOut: keys.zoomOut,
      acceleration: 0.04,
      drag: 0.0005,
      maxSpeed: 1
    };

    this.controls = new Phaser.Cameras.Controls.SmoothedKeyControl(controlConfig);

    this.camera.scrollX = -1531;
    this.camera.scrollY = 140;
    this.camera.zoom = 1;
  }


  onPointerMove (pointer) {
    let { x, y } = this.camera.getWorldPoint(pointer.x, pointer.y);

    this.worldPoint.x = x;
    this.worldPoint.y = y;
  }


  update (delta) {
    this.controls.update(delta);

    if (this.controls._speedX !== 0 || this.controls._speedY !== 0 || this.controls._zoom !== 0)
      this.cullObjects();
  }


  cullObjects () {
    let cells = this.scene.city.map.cells;
    let view  = Phaser.Geom.Rectangle.Clone(this.worldView);
    let cx = view.centerX;
    let cy = view.centerY;

    Phaser.Geom.Rectangle.Scale(view, this.viewportPaddingMultiplier);
    Phaser.Geom.Rectangle.CenterOn(view, cx, cy);

    for (let x = 0; x < CONST.MAP_SIZE; x++)
      for (let y = 0; y < CONST.MAP_SIZE; y++)
        if (Phaser.Geom.Rectangle.Contains(view, cells[x][y].position.center.x, cells[x][y].position.center.y))
          cells[x][y].show();
        else
          cells[x][y].hide();


    this.objectsRendered = 0;

    this.scene.children.list.forEach((gameObject) => {
      if (gameObject.visible) this.objectsRendered++;
    });
  }
}