Skip to content

Commit 25e4993

Browse files
committed
Added x and y getters and new scaled viewport values
1 parent 0db1688 commit 25e4993

1 file changed

Lines changed: 120 additions & 4 deletions

File tree

src/cameras/2d/Camera.js

Lines changed: 120 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ var Camera = new Class({
122122
*
123123
* @name Phaser.Cameras.Scene2D.Camera#x
124124
* @type {number}
125+
* @private
125126
* @since 3.0.0
126127
*/
127-
this.x = x;
128+
this._x = x;
128129

129130
/**
130131
* The y position of the Camera, relative to the top-left of the game canvas.
@@ -133,9 +134,60 @@ var Camera = new Class({
133134
*
134135
* @name Phaser.Cameras.Scene2D.Camera#y
135136
* @type {number}
137+
* @private
136138
* @since 3.0.0
137139
*/
138-
this.y = y;
140+
this._y = y;
141+
142+
/**
143+
* The resolution of the Game, used in most Camera calculations.
144+
*
145+
* @name Phaser.Cameras.Scene2D.Camera#resolution
146+
* @type {number}
147+
* @readOnly
148+
* @since 3.12.0
149+
*/
150+
this.resolution = 1;
151+
152+
/**
153+
* Internal Camera X value multiplied by the resolution.
154+
*
155+
* @name Phaser.Cameras.Scene2D.Camera#_cx
156+
* @type {number}
157+
* @private
158+
* @since 3.12.0
159+
*/
160+
this._cx = 0;
161+
162+
/**
163+
* Internal Camera Y value multiplied by the resolution.
164+
*
165+
* @name Phaser.Cameras.Scene2D.Camera#_cy
166+
* @type {number}
167+
* @private
168+
* @since 3.12.0
169+
*/
170+
this._cy = 0;
171+
172+
/**
173+
* Internal Camera Width value multiplied by the resolution.
174+
*
175+
* @name Phaser.Cameras.Scene2D.Camera#_cw
176+
* @type {number}
177+
* @private
178+
* @since 3.12.0
179+
*/
180+
this._cw = 0;
181+
182+
/**
183+
* Internal Camera Height value multiplied by the resolution.
184+
*
185+
* @name Phaser.Cameras.Scene2D.Camera#_ch
186+
* @type {number}
187+
* @private
188+
* @since 3.12.0
189+
*/
190+
this._ch = 0;
139191

140192
/**
141193
* The width of the Camera viewport, in pixels.
@@ -1071,8 +1123,10 @@ var Camera = new Class({
10711123
var scrollX = this.scrollX;
10721124
var scrollY = this.scrollY;
10731125

1074-
var sx = x + ((scrollX * c - scrollY * s) * zoom);
1075-
var sy = y + ((scrollX * s + scrollY * c) * zoom);
1126+
var res = this.resolution;
1127+
1128+
var sx = x * res + ((scrollX * c - scrollY * s) * zoom);
1129+
var sy = y * res + ((scrollX * s + scrollY * c) * zoom);
10761130

10771131
/* Apply transform to point */
10781132
output.x = (sx * ima + sy * imc + ime);
@@ -1541,6 +1595,7 @@ var Camera = new Class({
15411595

15421596
/**
15431597
* Sets the Scene the Camera is bound to.
1598+
* Also populates the `resolution` property and updates the internal size values.
15441599
*
15451600
* @method Phaser.Cameras.Scene2D.Camera#setScene
15461601
* @since 3.0.0
@@ -1553,6 +1608,15 @@ var Camera = new Class({
15531608
{
15541609
this.scene = scene;
15551610

1611+
var res = scene.sys.game.config.resolution;
1612+
1613+
this.resolution = res;
1614+
1615+
this._cx = this._x * res;
1616+
this._cy = this._y * res;
1617+
this._cw = this._width * res;
1618+
this._ch = this._height * res;
1619+
15561620
return this;
15571621
},
15581622

@@ -1874,6 +1938,56 @@ var Camera = new Class({
18741938
this.deadzone = null;
18751939
},
18761940

1941+
/**
1942+
* The x position of the Camera viewport, relative to the top-left of the game canvas.
1943+
* The viewport is the area into which the camera renders.
1944+
* To adjust the position the camera is looking at in the game world, see the `scrollX` value.
1945+
*
1946+
* @name Phaser.Cameras.Scene2D.Camera#x
1947+
* @type {number}
1948+
* @since 3.0.0
1949+
*/
1950+
x: {
1951+
1952+
get: function ()
1953+
{
1954+
return this._x;
1955+
},
1956+
1957+
set: function (value)
1958+
{
1959+
this._x = value;
1960+
this._cx = value * this.resolution;
1961+
this.dirty = true;
1962+
}
1963+
1964+
},
1965+
1966+
/**
1967+
* The y position of the Camera viewport, relative to the top-left of the game canvas.
1968+
* The viewport is the area into which the camera renders.
1969+
* To adjust the position the camera is looking at in the game world, see the `scrollY` value.
1970+
*
1971+
* @name Phaser.Cameras.Scene2D.Camera#y
1972+
* @type {number}
1973+
* @since 3.0.0
1974+
*/
1975+
y: {
1976+
1977+
get: function ()
1978+
{
1979+
return this._y;
1980+
},
1981+
1982+
set: function (value)
1983+
{
1984+
this._y = value;
1985+
this._cy = value * this.resolution;
1986+
this.dirty = true;
1987+
}
1988+
1989+
},
1990+
18771991
/**
18781992
* The width of the Camera viewport, in pixels.
18791993
*
@@ -1894,6 +2008,7 @@ var Camera = new Class({
18942008
set: function (value)
18952009
{
18962010
this._width = value;
2011+
this._cw = value * this.resolution;
18972012
this.dirty = true;
18982013
}
18992014

@@ -1919,6 +2034,7 @@ var Camera = new Class({
19192034
set: function (value)
19202035
{
19212036
this._height = value;
2037+
this._ch = value * this.resolution;
19222038
this.dirty = true;
19232039
}
19242040

0 commit comments

Comments
 (0)