-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Sleep.js
152 lines (133 loc) · 3.98 KB
/
Sleep.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Events = require('../events');
var Sleeping = require('../lib/core/Sleeping');
var MatterEvents = require('../lib/core/Events');
/**
* Enables a Matter-enabled Game Object to be able to go to sleep. Should be used as a mixin and not directly.
*
* @namespace Phaser.Physics.Matter.Components.Sleep
* @since 3.0.0
*/
var Sleep = {
/**
* Sets this Body to sleep.
*
* @method Phaser.Physics.Matter.Components.Sleep#setToSleep
* @since 3.22.0
*
* @return {this} This Game Object instance.
*/
setToSleep: function ()
{
Sleeping.set(this.body, true);
return this;
},
/**
* Wakes this Body if asleep.
*
* @method Phaser.Physics.Matter.Components.Sleep#setAwake
* @since 3.22.0
*
* @return {this} This Game Object instance.
*/
setAwake: function ()
{
Sleeping.set(this.body, false);
return this;
},
/**
* Sets the number of updates in which this body must have near-zero velocity before it is set as sleeping (if sleeping is enabled by the engine).
*
* @method Phaser.Physics.Matter.Components.Sleep#setSleepThreshold
* @since 3.0.0
*
* @param {number} [value=60] - A `Number` that defines the number of updates in which this body must have near-zero velocity before it is set as sleeping.
*
* @return {this} This Game Object instance.
*/
setSleepThreshold: function (value)
{
if (value === undefined) { value = 60; }
this.body.sleepThreshold = value;
return this;
},
/**
* Enable sleep and wake events for this body.
*
* By default when a body goes to sleep, or wakes up, it will not emit any events.
*
* The events are emitted by the Matter World instance and can be listened to via
* the `SLEEP_START` and `SLEEP_END` events.
*
* @method Phaser.Physics.Matter.Components.Sleep#setSleepEvents
* @since 3.0.0
*
* @param {boolean} start - `true` if you want the sleep start event to be emitted for this body.
* @param {boolean} end - `true` if you want the sleep end event to be emitted for this body.
*
* @return {this} This Game Object instance.
*/
setSleepEvents: function (start, end)
{
this.setSleepStartEvent(start);
this.setSleepEndEvent(end);
return this;
},
/**
* Enables or disables the Sleep Start event for this body.
*
* @method Phaser.Physics.Matter.Components.Sleep#setSleepStartEvent
* @since 3.0.0
*
* @param {boolean} value - `true` to enable the sleep event, or `false` to disable it.
*
* @return {this} This Game Object instance.
*/
setSleepStartEvent: function (value)
{
if (value)
{
var world = this.world;
MatterEvents.on(this.body, 'sleepStart', function (event)
{
world.emit(Events.SLEEP_START, event, this);
});
}
else
{
MatterEvents.off(this.body, 'sleepStart');
}
return this;
},
/**
* Enables or disables the Sleep End event for this body.
*
* @method Phaser.Physics.Matter.Components.Sleep#setSleepEndEvent
* @since 3.0.0
*
* @param {boolean} value - `true` to enable the sleep event, or `false` to disable it.
*
* @return {this} This Game Object instance.
*/
setSleepEndEvent: function (value)
{
if (value)
{
var world = this.world;
MatterEvents.on(this.body, 'sleepEnd', function (event)
{
world.emit(Events.SLEEP_END, event, this);
});
}
else
{
MatterEvents.off(this.body, 'sleepEnd');
}
return this;
}
};
module.exports = Sleep;