-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathWorld.js
207 lines (168 loc) · 4.76 KB
/
World.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
function World(view) {
var Thing = require("ui/common/Thing");
var self = {};
Init();
function Init() {
if(view.height == undefined || view.width == undefined) {
Ti.API.error("Error: View height or width is undefined. These must be defined as numerical values");
return;
}
if(view.gravity == undefined) {
Ti.API.warn("Warning: No gravity defined in the view");
self.gravity = 0;
}
if(view.frameRate == undefined) {
Ti.API.error("Error: No frameRate defined in the view");
self.frameRate = 0;
return;
}
self.things = [];
self.gravity = view.gravity;
self.frameRate = view.frameRate;
self.map = matrix(view.width, view.height, "0");
self.isAlive = false;
}
/**
* Create a 2D array given the following arguments.
* @param {Object} rows The number of rows to create.
* @param {Object} cols The number of columns to create.
* @param {Object} defaultValue The default value to apply to each cell.
*/
function matrix(rows, cols, defaultValue) {
var arr = new Array(rows);
for (var x = 0; x < rows; x++) {
arr[x] = new Array(cols);
for(var y = 0; y < cols; y++)
arr[x][y] = defaultValue;
}
return arr;
};
function Loop() {
if(self.isAlive) {
Update();
setTimeout(Loop, self.frameRate);
}
}
function Update() {
//Update each of the objects in the world
for(var i = 0; i < self.things.length; i++) {
self.things[i].update(self.map);
}
}
self.getThingByName = function(name) {
for(var i = 0; i < self.things.length; i++) {
if(self.things[i].name == name) {
return self.things[i];
}
}
return -1;
};
self.getThingBy = function(attribute, value) {
for(var i = 0; i < self.things.length; i++) {
if(self.things[i][attribute] == value)
return self.things[i];
}
return -1;
};
self.getThingsBy = function(attribute, value) {
var rtn = [];
for(var i = 0; i < self.things.length; i++) {
if(self.things[i][attribute] == value)
rtn.push(self.things[i]);
}
return rtn;
};
self.addThing = function(obj) {
//An object needs a name
if(obj.name == undefined) {
Ti.API.error("Error: Thing is missing a 'name' attribute. Skipping");
return;
}
//Dimensions can not be fractional, can't be negative and can't be outside the bounds of the world view
var dims = ['left', 'top', 'width', 'height'];
for(var i = 0; i < dims.length; i++) {
if(obj[dims[i]] % 1 != 0) {
Ti.API.error("Error: " + obj.name + "." + dims[i] + " can not be fractional. Skipping");
return;
}
if(obj[dims[i]] < 0) {
Ti.API.error("Error: " + obj.name + "." + dims[i] + " can not be negative. To support offscreen Things, create a world view with dimensions greater than the device screen. Skipping");
return;
}
}
if(obj.left + obj.width > view.width) {
Ti.API.error("Error: " + obj.name + " total width can not exceed the bounds of the world view. Skipping");
return;
}
if(obj.top + obj.height > view.height) {
Ti.API.error("Error: " + obj.name + " total height can not exceed the bounds of the world view. Skipping");
return;
}
//If the object has no velocity, set it to defaults here.
if(obj.velocity == undefined) {
obj.velocity = {
left: 0,
top: 0,
maxLeft: 0,
maxTop: 0
};
}
//Velocity also can not be fractional
var dims = ['left', 'top', 'maxLeft', 'maxTop'];
for(var i = 0; i < dims.length; i++) {
if(obj.velocity[dims[i]] % 1 != 0) {
Ti.API.error("Error: " + obj.name + ".velocity." + dims[i] + " can not be fractional. Skipping");
return;
}
}
//Things seem okay. Now, add the object.
obj.gravity = self.gravity;
obj.index = self.things.length;
self.things.push(new Thing(obj, self));
view.add(self.things[self.things.length-1].view);
//Set the object's position in the map
if(obj.type == "non-animate") {
for(var x = obj.left; x < obj.left + obj.width; x++) {
for(var y = obj.top; y < obj.top + obj.height; y++) {
self.map[x][y] = obj.name;
}
}
}
};
self.start = function() {
self.isAlive = true;
Loop();
};
self.stop = function() {
self.isAlive = false;
};
self.traceMap = function() {
var output = "";
for(var y = 0; y < view.height; y++) {
output += y + ": ";
for(var x = 0; x < view.width; x++) {
if(self.map[x][y] != "0")
output += "X";
else
output += "O";
}
output += "\r\n";
}
Ti.API.trace(output);
};
self.getMapHeight = function() {
return view.height;
};
self.getMapWidth = function() {
return view.width;
};
self.getMap = function() {
return self.map;
};
self.removeThingFromWorld = function(index) {
view.remove(self.things[index].view);
self.things.splice(index, 1);
};
return self;
}
module.exports = World;