File size: 7,138 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
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
import Phaser from 'phaser';

export default class ui extends Phaser.Scene {
  constructor () {
    super({
      key: 'ui',
      active: true
    });

    this.debug = false;
    this.debugOffset = -250;
    this.boundsBuffer = 300;
    
    this.viewport = {
      displayBounds: {},
      displayBoundsBuffer: {},
      worldBounds: {},
      worldBoundsBuffer: {}
    };

    this.preloadComplete = false;
    this.initialized = false;
  }

  preload () {
    this.common = this.sys.game.common;
    this.preloadComplete = true;
  }

  create () {
    if (!this.preloadComplete)
      return;

    this.common.ui = this;
    this.initialized = true;
  }


  updateBounds () {
    if (!this.common || !this.common.world || !this.common.world.worldCamera)
      return;

    let camera = this.common.world.worldCamera.camera;

    this.viewport.width = document.documentElement.clientWidth;
    this.viewport.height = document.documentElement.clientHeight;

    this.viewport.displayBounds.topLeft           = new Phaser.Geom.Point(0 + this.debugOffset,                     0 + this.debugOffset);
    this.viewport.displayBounds.bottomLeft        = new Phaser.Geom.Point(0 + this.debugOffset,                     this.viewport.height - this.debugOffset);
    this.viewport.displayBounds.topRight          = new Phaser.Geom.Point(this.viewport.width - this.debugOffset,   0 + this.debugOffset);
    this.viewport.displayBounds.bottomRight       = new Phaser.Geom.Point(this.viewport.width - this.debugOffset,   this.viewport.height - this.debugOffset);
    this.viewport.displayBounds.width             = this.viewport.displayBounds.bottomRight.x - this.viewport.displayBounds.topLeft.x;
    this.viewport.displayBounds.height            = this.viewport.displayBounds.bottomLeft.y - this.viewport.displayBounds.topLeft.y;
    this.viewport.displayBounds.rect              = new Phaser.Geom.Rectangle(this.viewport.displayBounds.topLeft.x, this.viewport.displayBounds.topLeft.y, this.viewport.displayBounds.width, this.viewport.displayBounds.height);

    this.viewport.displayBoundsBuffer.topLeft     = new Phaser.Geom.Point(this.viewport.displayBounds.topLeft.x + this.boundsBuffer,      this.viewport.displayBounds.topLeft.y + this.boundsBuffer);
    this.viewport.displayBoundsBuffer.bottomLeft  = new Phaser.Geom.Point(this.viewport.displayBounds.bottomLeft.x + this.boundsBuffer,   this.viewport.displayBounds.bottomLeft.y - this.boundsBuffer);
    this.viewport.displayBoundsBuffer.topRight    = new Phaser.Geom.Point(this.viewport.displayBounds.topRight.x - this.boundsBuffer,     this.viewport.displayBounds.topRight.y + this.boundsBuffer);
    this.viewport.displayBoundsBuffer.bottomRight = new Phaser.Geom.Point(this.viewport.displayBounds.bottomRight.x - this.boundsBuffer,  this.viewport.displayBounds.bottomRight.y - this.boundsBuffer);
    this.viewport.displayBoundsBuffer.width       = this.viewport.displayBoundsBuffer.bottomRight.x - this.viewport.displayBoundsBuffer.topLeft.x;
    this.viewport.displayBoundsBuffer.height      = this.viewport.displayBoundsBuffer.bottomLeft.y - this.viewport.displayBoundsBuffer.topLeft.y;
    this.viewport.displayBoundsBuffer.rect        = new Phaser.Geom.Rectangle(this.viewport.displayBoundsBuffer.topLeft.x, this.viewport.displayBoundsBuffer.topLeft.y, this.viewport.displayBoundsBuffer.width, this.viewport.displayBoundsBuffer.height);

    this.viewport.worldBounds.topLeft     = camera.getWorldPoint(this.viewport.displayBounds.topLeft.x,      this.viewport.displayBounds.topLeft.y);
    this.viewport.worldBounds.bottomLeft  = camera.getWorldPoint(this.viewport.displayBounds.bottomLeft.x,   this.viewport.displayBounds.bottomLeft.y);
    this.viewport.worldBounds.topRight    = camera.getWorldPoint(this.viewport.displayBounds.topRight.x,     this.viewport.displayBounds.topRight.y);
    this.viewport.worldBounds.bottomRight = camera.getWorldPoint(this.viewport.displayBounds.bottomRight.x,  this.viewport.displayBounds.bottomRight.y);
    this.viewport.worldBounds.width       = this.viewport.worldBounds.bottomRight.x - this.viewport.worldBounds.topLeft.x;
    this.viewport.worldBounds.height      = this.viewport.worldBounds.bottomLeft.y - this.viewport.worldBounds.topLeft.y;
    this.viewport.worldBounds.rect        = new Phaser.Geom.Rectangle(this.viewport.worldBounds.topLeft.x, this.viewport.worldBounds.topLeft.y, this.viewport.worldBounds.width, this.viewport.worldBounds.height);

    this.viewport.worldBoundsBuffer.topLeft     = camera.getWorldPoint(this.viewport.displayBoundsBuffer.topLeft.x,      this.viewport.displayBoundsBuffer.topLeft.y);
    this.viewport.worldBoundsBuffer.bottomLeft  = camera.getWorldPoint(this.viewport.displayBoundsBuffer.bottomLeft.x,   this.viewport.displayBoundsBuffer.bottomLeft.y);
    this.viewport.worldBoundsBuffer.topRight    = camera.getWorldPoint(this.viewport.displayBoundsBuffer.topRight.x,     this.viewport.displayBoundsBuffer.topRight.y);
    this.viewport.worldBoundsBuffer.bottomRight = camera.getWorldPoint(this.viewport.displayBoundsBuffer.bottomRight.x,  this.viewport.displayBoundsBuffer.bottomRight.y);
    this.viewport.worldBoundsBuffer.width       = this.viewport.worldBoundsBuffer.bottomRight.x - this.viewport.worldBoundsBuffer.topLeft.x;
    this.viewport.worldBoundsBuffer.height      = this.viewport.worldBoundsBuffer.bottomLeft.y - this.viewport.worldBoundsBuffer.topLeft.y;
    this.viewport.worldBoundsBuffer.rect        = new Phaser.Geom.Rectangle(this.viewport.worldBoundsBuffer.topLeft.x, this.viewport.worldBoundsBuffer.topLeft.y, this.viewport.worldBoundsBuffer.width, this.viewport.worldBoundsBuffer.height);

    if (!this.displayArea && !this.displayAreaBuffer && this.debug) {
      this.displayArea = this.add.graphics();
      this.displayArea.fillStyle(0xAA0000, .25);
      this.displayArea.lineStyle(2, 0xAA0000, 1);
      this.displayArea.fillRectShape(this.viewport.displayBounds.rect);
      this.displayArea.strokeRectShape(this.viewport.displayBounds.rect);
      this.displayArea.setDepth(999999999);

      this.displayAreaBuffer = this.add.graphics();
      this.displayAreaBuffer.fillStyle(0xAAAA00, .25);
      this.displayAreaBuffer.lineStyle(2, 0xAAAA00, 1);
      this.displayAreaBuffer.fillRectShape(this.viewport.displayBoundsBuffer.rect);
      this.displayAreaBuffer.strokeRectShape(this.viewport.displayBoundsBuffer.rect);
      this.displayAreaBuffer.setDepth(999999999);
      //this.displayAreaBuffer.setVisible(false);
      
    }

    this.common.viewport = this.viewport;
  }

  // update (time, delta) {
  //   this.updateBounds();
  // }

  resize () {
    this.cameras.main.setViewport(0, 0, document.documentElement.clientWidth, document.documentElement.clientHeight);

    if (this.displayArea && this.displayAreaBuffer && this.debug) {
      this.displayArea.clear();
      this.displayAreaBuffer.clear();

      this.displayArea = undefined;
      this.displayAreaBuffer = undefined;
    }
  }

  shutdown () {
    if (!this.initialized)
      return;

    this.initialized = false;
    this.preloadComplete = false;

    this.scene.stop();
  }

}