-
Notifications
You must be signed in to change notification settings - Fork 21
/
events.js
executable file
·232 lines (214 loc) · 6.4 KB
/
events.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
var Sburb = (function(Sburb){
function parseParams(info){
var params = info.split(",");
params.map(function(param) { return param.trim(); });
return params;
}
var events = {};
// Please list all 9f y9ur triggers here so I can handle them c9rrectly.
//...I can't tell if you're trying to use some troll thing or broke your "o" key
// Trigger functions are called with "new"; functions should set
// this.reset (for when the trigger is initialized/reset) and
// this.checkCompletion (which should return true or false based
// on whether the trigger has been completed).
//Check if the given sprite's property satisfies some condition
//syntax: spriteName, query (e.g. x=3)
events.spriteProperty = function(info) {
var params = parseParams(info);
var token;
var query = params[2];
if((query.indexOf(">")>-1 && (token=">")) ||
(query.indexOf("GREATER") > -1 && (token="GREATER"))){
this.trigger = function(entity,property,target){
return entity[property]>target;
};
}else if((query.indexOf("<")>-1 && (token="<")) ||
(query.indexOf("LESS") > -1 && (token="LESS"))){
this.trigger = function(entity,property,target){
return entity[property]<target;
};
}else if(query.indexOf("!=")>-1){
token = "!=";
this.trigger = function(entity,property,target){
return entity[property]!=target;
};
}else if(query.indexOf("=")>-1){
token = "=";
this.trigger = function(entity,property,target){
return entity[property]==target;
};
}
var queryParts = query.split(token);
var property = queryParts[0].trim();
var target = queryParts[1].trim();
this.reset = function() {
if(params[1]=="char"){
this.entity = params[1];
} else{
this.entity = Sburb.sprites[params[1]];
}
}
this.checkCompletion = function(){
var entity = this.entity;
if(this.entity=="char"){
entity = Sburb.char;
}
return this.trigger(entity,property,target);
}
};
//Check if the given sprite is inside a box
//syntax: spriteName, x, y, width, height
events.inBox = function(info) {
var params = parseParams(info);
var x = parseInt(params[2]);
var y = parseInt(params[3]);
var width = parseInt(params[4]);
var height = parseInt(params[5]);
this.reset = function() {
if(params[1]=="char"){
this.entity = params[1];
}else{
this.entity = Sburb.sprites[params[1]];
}
}
this.checkCompletion = function(){
var entity = this.entity;
if(this.entity=="char"){
entity = Sburb.char;
}
return entity.x >= x && entity.y >= y && entity.x <= x+width && entity.y <= y+height;
}
};
events.inBox2 = function(info){
var params = parseParams(info);
var x1 = parseInt(params[2]);
var y1 = parseInt(params[3]);
var x2 = parseInt(params[4]);
var y2 = parseInt(params[5]);
var x = Math.min(x1,x2);
var y = Math.min(y1,y2);
var width = Math.abs(x1-x2);
var height = Math.abs(y1-y2);
return new events.inBox("inBox,"+params[1]+","+x+","+y+","+width+","+height);
}
//Check if a certain interval of time has elapsed
//syntax: time
events.time = function(info) {
var params = parseParams(info);
this.reset = function() {
this.time = parseInt(params[1]);
}
this.checkCompletion = function(){
this.time--;
return this.time<=0;
};
this.serialize = function(){
return "time,"+this.time;
}
};
//Check if the sprite's animation has played
//sytax: spriteName
events.played = function(info) {
var params = parseParams(info);
this.reset = function() {
this.entity = Sburb.sprites[params[1]];
}
this.checkCompletion = function(){
var entity = this.entity;
if(this.entity=="char"){
entity = Sburb.char;
}
return entity.animation.hasPlayed();
};
};
//check if the movie has finished playing (iternal utility event)
//syntax: movieName
events.movie = function(info) {
var params = parseParams(info);
var threshold = parseInt(params[2]);
this.reset = function() {
this.movie = window.document.getElementById("movie"+params[1]);
}
this.checkCompletion = function(){
if(this.movie && (!this.movie.TotalFrames ||
(this.movie.TotalFrames()>0 && this.movie.TotalFrames()-1-this.movie.CurrentFrame()<=threshold))){
Sburb.commands.removeMovie(params[1]);
return true;
}
return false;
}
};
//check if the game state meets a certain condition
//syntax: condition (e.g. doorOpened=true)
events.gameState = function(info) {
var params = parseParams(info);
var token;
var query = params[1];
if((query.indexOf(">")>-1 && (token=">")) ||
(query.indexOf("GREATER") > -1 && (token="GREATER"))){
this.trigger = function(property,target){
return Sburb.gameState[property]>target;
};
}else if((query.indexOf("<")>-1 && (token="<")) ||
(query.indexOf("LESS") > -1 && (token="LESS"))){
this.trigger = function(property,target){
return Sburb.gameState[property]<target;
};
}else if(query.indexOf("!=")>-1){
token = "!=";
this.trigger = function(property,target){
return Sburb.gameState[property]!=target;
};
}else if(query.indexOf("=")>-1){
token = "=";
this.trigger = function(property,target){
return Sburb.gameState[property]==target;
};
}
var queryParts = query.split(token);
var property = queryParts[0].trim();
var target = queryParts[1].trim();
this.reset = function() {
// pass
}
this.checkCompletion = function(){
return this.trigger(property,target);
}
};
//check if the player is nudging the game forward (space or mouse)
//syntax: none
events.nudge = function(info){
this.reset = function(){ } //do nothing
this.checkCompletion = function(){
return Sburb.Keys.space || Sburb.Mouse.down;
}
}
//check that there are no pending or active actions on the queue
//syntax: none
events.noActions = function(info){
var params = parseParams(info);
this.reset = function(){ } //do nothing
this.checkCompletion = function(){
var queue = params.length>0 ? Sburb.getActionQueueById(params[1]) : Sburb;
return !queue || !queue.curAction;
}
}
//check if two sprites are near each other
//syntax spriteName1, spriteName2, distance (px)
events.withinRange = function(info){
var params = parseParams(info);
var spriteName1 = params[1];
var spriteName2 = params[2];
var dist = parseFloat(params[3]);
this.reset = function(){ } //do nothing
this.checkCompletion = function(){
var sprite1 = Sburb.parseCharacterString(spriteName1);
var sprite2 = Sburb.parseCharacterString(spriteName2);
var xDist = sprite1.x-sprite2.x;
var yDist = sprite1.y-sprite2.y;
return Math.sqrt(xDist*xDist + yDist*yDist) <= dist;
}
}
Sburb.events = events;
return Sburb;
})(Sburb || {});