-
Notifications
You must be signed in to change notification settings - Fork 556
/
scenes.js
173 lines (164 loc) · 6.44 KB
/
scenes.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
var Crafty = require("../core/core.js");
module.exports = {
_scenes: {},
_current: null,
/**@
* #Crafty.scene
* @category Scenes, Stage
* @kind Method
*
* @trigger SceneChange - just before a new scene is initialized - { oldScene:String, newScene:String }
* @trigger SceneDestroy - just before the current scene is destroyed - { newScene:String }
*
* @sign public void Crafty.scene(String sceneName, Function init[, Function uninit])
* @param sceneName - Name of the scene to add
* @param init - Function to execute when scene is played
* @param uninit - Function to execute before next scene is played, after entities with `2D` are destroyed
*
* This is equivalent to calling `Crafty.defineScene`.
*
* @sign public void Crafty.scene(String sceneName[, Data])
* @param sceneName - Name of scene to play
* @param Data - The init function of the scene will be called with this data as its parameter. Can be of any type other than a function.
*
* This is equivalent to calling `Crafty.enterScene`.
*
* Method to create scenes on the stage. Pass an ID and function to register a scene.
*
* To play a scene, just pass the ID. When a scene is played, all
* previously-created entities with the `2D` component are destroyed. The
* viewport is also reset.
*
* You can optionally specify an arugment that will be passed to the scene's init function.
*
* If you want some entities to persist over scenes (as in, not be destroyed)
* simply add the component `Persist`.
*
* @example
* ~~~
* Crafty.defineScene("loading", function() {
* Crafty.background("#000");
* Crafty.e("2D, DOM, Text")
* .attr({ w: 100, h: 20, x: 150, y: 120 })
* .text("Loading")
* .textAlign("center")
* .textColor("#FFFFFF");
* });
*
* Crafty.defineScene("UFO_dance",
* function() {Crafty.background("#444"); Crafty.e("UFO");},
* function() {...send message to server...});
*
* // An example of an init function which accepts arguments, in this case an object.
* Crafty.defineScene("square", function(attributes) {
* Crafty.background("#000");
* Crafty.e("2D, DOM, Color")
* .attr(attributes)
* .color("red");
*
* });
*
* ~~~
* This defines (but does not play) two scenes as discussed below.
* ~~~
* Crafty.enterScene("loading");
* ~~~
* This command will clear the stage by destroying all `2D` entities (except
* those with the `Persist` component). Then it will set the background to
* black and display the text "Loading".
* ~~~
* Crafty.enterScene("UFO_dance");
* ~~~
* This command will clear the stage by destroying all `2D` entities (except
* those with the `Persist` component). Then it will set the background to
* gray and create a UFO entity. Finally, the next time the game encounters
* another command of the form `Crafty.scene(scene_name)` (if ever), then the
* game will send a message to the server.
* ~~~
* Crafty.enterScene("square", {x:10, y:10, w:20, h:20});
* ~~~
* This will clear the stage, set the background black, and create a red square with the specified position and dimensions.
* ~~~
*/
scene: function(name, intro, outro) {
// If there's one argument, or the second argument isn't a function, play the scene
if (arguments.length === 1 || typeof arguments[1] !== "function") {
Crafty.enterScene(name, arguments[1]);
return;
}
// Otherwise, this is a call to create a scene
Crafty.defineScene(name, intro, outro);
},
/*
* #Crafty.defineScene
* @category Scenes, Stage
* @kind Method
*
* @sign public void Crafty.enterScene(String name[, Data])
* @param name - Name of the scene to run.
* @param Data - The init function of the scene will be called with this data as its parameter. Can be of any type other than a function.
*
* @see Crafty.enterScene
* @see Crafty.scene
*/
defineScene: function(name, init, uninit) {
if (typeof init !== "function")
throw "Init function is the wrong type.";
this._scenes[name] = {};
this._scenes[name].initialize = init;
if (typeof uninit !== "undefined") {
this._scenes[name].uninitialize = uninit;
}
return;
},
/*
* #Crafty.enterScene
* @category Scenes, Stage
* @kind Method
*
* @trigger SceneChange - just before a new scene is initialized - { oldScene:String, newScene:String }
* @trigger SceneDestroy - just before the current scene is destroyed - { newScene:String }
*
* @sign public void Crafty.enterScene(String name[, Data])
* @param name - Name of the scene to run.
* @param Data - The init function of the scene will be called with this data as its parameter. Can be of any type other than a function.
*
* @see Crafty.defineScene
* @see Crafty.scene
*/
enterScene: function(name, data) {
if (typeof data === "function") throw "Scene data cannot be a function";
// ---FYI---
// this._current is the name (ID) of the scene in progress.
// this._scenes is an object like the following:
// {'Opening scene': {'initialize': fnA, 'uninitialize': fnB},
// 'Another scene': {'initialize': fnC, 'uninitialize': fnD}}
Crafty.trigger("SceneDestroy", {
newScene: name
});
Crafty.viewport.reset();
Crafty("2D").each(function() {
if (!this.has("Persist")) this.destroy();
});
// uninitialize previous scene
if (
this._current !== null &&
"uninitialize" in this._scenes[this._current]
) {
this._scenes[this._current].uninitialize.call(this);
}
// initialize next scene
var oldScene = this._current;
this._current = name;
Crafty.trigger("SceneChange", {
oldScene: oldScene,
newScene: name
});
if (this._scenes.hasOwnProperty(name)) {
this._scenes[name].initialize.call(this, data);
} else {
Crafty.error('The scene "' + name + '" does not exist');
}
return;
}
};