forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInWorld.js
More file actions
139 lines (118 loc) · 4.2 KB
/
Copy pathInWorld.js
File metadata and controls
139 lines (118 loc) · 4.2 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The InWorld component checks if a Game Object is within the Game World Bounds.
* An object is considered as being "in bounds" so long as its own bounds intersects at any point with the World bounds.
* If the AutoCull component is enabled on the Game Object then it will check the Game Object against the Camera bounds as well.
*
* @class
*/
Phaser.Component.InWorld = function () {};
/**
* The InWorld component preUpdate handler.
* Called automatically by the Game Object.
*
* @method
*/
Phaser.Component.InWorld.preUpdate = function () {
// Cache the bounds if we need it
if (this.autoCull || this.checkWorldBounds)
{
this._bounds.copyFrom(this.getBounds());
this._bounds.x += this.game.camera.view.x;
this._bounds.y += this.game.camera.view.y;
if (this.autoCull)
{
// Won't get rendered but will still get its transform updated
if (this.game.world.camera.view.intersects(this._bounds))
{
this.renderable = true;
this.game.world.camera.totalInView++;
}
else
{
this.renderable = false;
if (this.outOfCameraBoundsKill)
{
this.kill();
return false;
}
}
}
if (this.checkWorldBounds)
{
// The Sprite is already out of the world bounds, so let's check to see if it has come back again
if (this._outOfBoundsFired && this.game.world.bounds.intersects(this._bounds))
{
this._outOfBoundsFired = false;
this.events.onEnterBounds$dispatch(this);
}
else if (!this._outOfBoundsFired && !this.game.world.bounds.intersects(this._bounds))
{
// The Sprite WAS in the screen, but has now left.
this._outOfBoundsFired = true;
this.events.onOutOfBounds$dispatch(this);
if (this.outOfBoundsKill)
{
this.kill();
return false;
}
}
}
}
return true;
};
Phaser.Component.InWorld.prototype = {
/**
* If this is set to `true` the Game Object checks if it is within the World bounds each frame.
*
* When it is no longer intersecting the world bounds it dispatches the `onOutOfBounds` event.
*
* If it was *previously* out of bounds but is now intersecting the world bounds again it dispatches the `onEnterBounds` event.
*
* It also optionally kills the Game Object if `outOfBoundsKill` is `true`.
*
* When `checkWorldBounds` is enabled it forces the Game Object to calculate its full bounds every frame.
*
* This is a relatively expensive operation, especially if enabled on hundreds of Game Objects. So enable it only if you know it's required,
* or you have tested performance and find it acceptable.
*
* @property {boolean} checkWorldBounds
* @default
*/
checkWorldBounds: false,
/**
* If this and the `checkWorldBounds` property are both set to `true` then the `kill` method is called as soon as `inWorld` returns false.
*
* @property {boolean} outOfBoundsKill
* @default
*/
outOfBoundsKill: false,
/**
* If this and the `autoCull` property are both set to `true`, then the `kill` method
* is called as soon as the Game Object leaves the camera bounds.
*
* @property {boolean} outOfCameraBoundsKill
* @default
*/
outOfCameraBoundsKill: false,
/**
* @property {boolean} _outOfBoundsFired - Internal state var.
* @private
*/
_outOfBoundsFired: false,
/**
* Checks if the Game Objects bounds are within, or intersect at any point with the Game World bounds.
*
* @property {boolean} inWorld
* @readonly
*/
inWorld: {
get: function () {
return this.game.world.bounds.intersects(this.getBounds());
}
}
};