-
Notifications
You must be signed in to change notification settings - Fork 2
/
things.js
329 lines (229 loc) · 9.48 KB
/
things.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Constructor for a thing, takes a name and a set of additional functions from the parser.
GIDGET.Thing = function(world, name, row, col, color, tags, actions) {
this.name = name; // The name by which the thing can be referred
this.row = row; // The row of the thing
this.column = col; // The column of the thing
this.energy = 100; // The current energy of the thing; things with energy < 0 are "dead"
this.level = 1; // The current level of the object above the grid, for creating boundaries
this.labeled = true; // Determines whether the label of the thing is shown
this.speed = 1;
this.color = color;
this.tags = {};
this.code = "";
this.setEnergy = function(energy) { this.energy = energy; };
this.setLevel = function(level) { this.level = level; };
this.setCode = function(code) { this.code = code; }
this.setSpeed = function(speed) { this.speed = speed; }
this.setLabeled = function(labeled) { this.labeled = labeled; }
// Translate the list of tags into a set (well, an object literal hash table of tag keys with true values).
var i;
for(i = 0; i < tags.length; i++)
this.tags[tags[i].toLowerCase()] = true;
this.actions = actions;
this.draw = function(ctx, size) {
ctx.save();
var padding = world.grndBorder;
// Draw shadow
/*
if(this.level > 1) {
var offset = (this.level - 1) * 10;
ctx.fillStyle = "rgba(0,0,0,.2)";
ctx.fillRect(this.column * size + 1 + offset + padding, this.row * size + 1 + offset + padding, size - padding * 2, size - padding * 2);
}
*/
var animateRowOffset = 0, animateColumnOffset = 0;
var image= GIDGET.ui.getImage(this.name, this.runtime.state);
if(!isDef(image)) image = GIDGET.ui.getImage('unknown', 'default');
if(GIDGET.ui.percentRemaining > 0) {
if(this.rowDelta !== 0) animateRowOffset = -(GIDGET.ui.percentRemaining / 100.0) * size * this.rowDelta;
if(this.columnDelta !== 0) animateColumnOffset = -(GIDGET.ui.percentRemaining / 100.0) * size * this.columnDelta;
}
// Compute a level offset, so that things that are a certain height go above their cell.
var levelOffset = this.level > 1 ? (this.level - 1) * size : 0;
if(isDef(image) && image.width > 0 && image.height > 0 && size > 0) {
ctx.drawImage(image, this.column * size + padding + animateColumnOffset, this.row * size + padding + animateRowOffset - levelOffset, size - padding * 2, size - padding * 2 + levelOffset);
}
else {
ctx.fillStyle = this.color;
ctx.fillRect(this.column * size + padding + animateColumnOffset, this.row * size + padding + animateRowOffset - levelOffset, size - padding * 2, size - padding * 2 + levelOffset);
}
ctx.restore();
}
world.addThing(this);
// Give the thing a runtime.
this.runtime = new GIDGET.Runtime(this, world);
}
// *******************************************************
// *** A C T I O N - C O N S T R U C T O R ***************
// *******************************************************
GIDGET.Action = function(arguments, script) {
return {
arguments: arguments,
script: script
};
};
// *******************************************************
// *** W O R L D - C O N S T R U C T O R *****************
// *******************************************************
GIDGET.World = function(gridSize, gidgetPos, groundAtt, code) {
// Grid (World) size & Gidget's initial positioning + starting energy
var rowCount = isDef(gridSize[0]) ? gridSize[0] : 10;
var colCount = isDef(gridSize[1]) ? gridSize[1] : rowCount;
var gidgetRow = gidgetPos[0];
var gidgetCol = gidgetPos[1];
var gidgetEnergy = isDef(gidgetPos[2]) ? gidgetPos[2] : 100;
// Ground Attributed: image/name, color, and border width
this.grnd = isDef(groundAtt[0]) ? groundAtt[0] : "dirt";
this.grndColor = isDef(groundAtt[1]) ? groundAtt[1] : "rgb(124,57,10)";
this.grndBorder = isDef(groundAtt[2]) ? groundAtt[2] : 2;
// Set level name
this.levelTitle = "";
this.levelNumber;
this.numberOfLevels;
// Remember the initial code
this.code = code;
// DEPRECATED - Remember the mission so it can be placed in the UI.
//this.mission = isDef(mission) ? mission : "I don't know what I'm supposed to do here. No one gave me a mission :(";
this.missionText = [];
// Remember the goal
this.goals = [];
// All of the things in the world, starting with a Gidget
this.things = [];
// An array of arrays of arrays, representing a 2D grid where each cell contains a list of references to things.
// This creates an empty grid.
this.grid = new Array(rowCount);
var row, col;
for(row = 0; row < rowCount; row++) {
this.grid[row] = new Array(colCount);
for(col = 0; col < colCount; col++) {
this.grid[row][col] = [];
}
}
this.addTitle = function (title) {
this.levelTitle = isDef(title) ? title : "";
this.addLevelNumber(); // Also updates level number
}
this.addLevelNumber = function() {
// Get level number
var lvlNumber,index = 1;
for(var level in GIDGET.levels) {
if(GIDGET.levels.hasOwnProperty(level) && localStorage.currentLevel === level) {
this.levelNumber = index;
}
index++;
}
this.numberOfLevels = index-1;
}
this.hiddenCommands = [];
this.addHiddenCommand = function(name) {
this.hiddenCommands[name] = true;
};
// Add a paragraph of text and an associated emotional state for it.
this.addMissionText = function(state, text) {
this.missionText.push({ text: text, state: state });
};
this.addGoal = function(goal) {
this.goals.push(goal);
};
this.addThing = function(thing) {
this.things.push(thing);
this.place(thing, thing.row, thing.column);
};
this.removeThing = function(thing) {
var index = $.inArray(thing, this.things);
if(index >= 0) {
// Remove from thing list
this.things.splice(index, 1);
// Remove from grid
index = $.inArray(thing, this.grid[thing.row][thing.column]);
if(index >= 0) {
this.grid[thing.row][thing.column].splice(index, 1);
}
else {
console.error("We shouldn't be able to remove thing from thing list but not grid");
}
// Remove from scanned
index = $.inArray(thing, this.gidget.runtime.scanned);
if(index >= 0) this.gidget.runtime.scanned.splice(index, 1);
// Remove from analyzed
index = $.inArray(thing, this.gidget.runtime.analyzed);
if(index >= 0) this.gidget.runtime.analyzed.splice(index, 1);
// Remove from grabbed
index = $.inArray(thing, this.gidget.runtime.grabbed);
if(index >= 0) this.gidget.runtime.grabbed.splice(index, 1);
}
else {
console.error("It shouldn't be possible to remove something that isn't in this world.");
}
};
this.resetThingDeltas = function() {
for(var i = 0; i < this.things.length; i++) {
this.things[i].rowDelta = 0;
this.things[i].columnDelta = 0;
}
};
// First, define how to place a thing.
this.place = function(thing, row, col) {
if(!this.isLegalColumn(row, col)) {
console.error("" + row + " " + col + " isn't legal");
return;
}
if(this.isLegalColumn(thing.row, thing.column)) {
var index = this.grid[thing.row][thing.column].indexOf(thing);
if(index >= 0)
this.grid[thing.row][thing.column].splice(index, 1);
}
// Remember the most recent change.
thing.rowDelta = row - thing.row;
thing.columnDelta = col - thing.column;
thing.row = row;
thing.column = col;
this.grid[thing.row][thing.column].push(thing);
};
// Define how to check legal positions.
this.isLegalRow = function(row) { return row !== undefined && row >= 0 && row < this.grid.length; };
this.isLegalColumn = function(row, col) { return this.isLegalRow(row) && col !== undefined && col >= 0 && col < this.grid[row].length; };
// Add a bunch of ground things.
for(row = 0; row < rowCount; row++) {
for(col = 0; col < colCount; col++) {
var ground = new GIDGET.Thing(this, this.grnd, row, col, this.grndColor, {}, {}); // Brown: rgb(164,77,30)
ground.setLevel(0);
ground.setLabeled(false);
}
}
// Add gidget. Every world has a gidget in it.
this.gidget = new GIDGET.Thing(this, "gidget", gidgetRow, gidgetCol, "rgb(50,50,50)", {}, {});
this.gidget.setEnergy(gidgetEnergy);
// This is the function that causes each thing in the world to step one step, in order.
// It takes the code passed to the world and assigns it to Gidget.
this.start = function(gidgetScript) {
// Prepare all of the other Things in the world for execution.
var i;
for(i = 0; i < this.things.length; i++) {
if(this.things[i] !== this.gidget)
this.things[i].runtime.start(this.things[i].code, false, {});
}
// This prepares gidget for execution.
this.gidget.runtime.start(gidgetScript, false, {});
};
// The world is executing while Gidget has steps that he's executing. The runtime is
// executing while a thing is executing the goal or is not executing a goal and still has energy.
this.isExecuting = function() {
return isDef(this.gidget) && isDef(this.gidget.runtime) && isDef(this.gidget.runtime.steps) && this.gidget.runtime.isExecuting();
};
// Steps all objects in the world one step, gathering all of their decisions into a list of lists.
// that the caller can use to execute each decision one at a time.
this.step = function() {
var decisions = [];
// Step each thing in the world once, executing the resulting decisions (unless gidget).
var i, thing;
for(i = 0; i < this.things.length; i++) {
thing = this.things[i];
var thingDecisions = thing.runtime.step();
// Put the decisions in the table for the caller.
if(isDef(thingDecisions))
decisions.push(thingDecisions);
}
return decisions;
};
};