-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbox.js
186 lines (165 loc) · 7.01 KB
/
box.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
const BOX_TYPE_RECT = 0;
const BOX_TYPE_LEFTSLOPE = 1;
const BOX_TYPE_RIGHTSLOPE = 2;
const BOX_TYPE_BOTTOM_LEFTSLOPE = 3;
const BOX_TYPE_BOTTOM_RIGHTSLOPE = 4;
const BOX_TYPE_COLLECTIBLE = 5;
const MAX_BOX_NUMBER = 256000;
var GlobalObjectIdentifier = 0;
class Box {
constructor(x,y,w,h,type) {
// Basic parameters
this.identifier = GlobalObjectIdentifier++;
this.x = x;
this.y = y;
this.width = w;
this.height = h;
if (type == undefined)
this.type = BOX_TYPE_RECT;
else
this.type = type;
// Assets
this.bg = new Rectangle(x,y,w,h);
this.line = new Segment(0,0,10,10);
this.otherline = new Segment(0,0,10,10);
this.triangle = new Triangle();
// Materials
this.materialColor = "#222";
this.color = this.materialColor;
this.selected = false;
// Convert shape to a type
this.convert = (type) => {
// todo: check if the type is allowed / exists
this.type = type;
if (this.type == BOX_TYPE_LEFTSLOPE) {
this.triangle.A.x = (this.x + this.width);
this.triangle.A.y = (this.y);
this.triangle.A.vecx = (this.x - (this.x + this.width));
this.triangle.A.vecy = ((this.y + this.height) - this.y);
this.triangle.B.x = this.triangle.A.x + this.triangle.A.vecx;
this.triangle.B.y = this.triangle.A.y + this.triangle.A.vecy;
this.triangle.B.vecx = this.width;
this.triangle.B.vecy = 0;
this.triangle.C.x = this.triangle.B.x + this.triangle.B.vecx;
this.triangle.C.y = this.triangle.B.y + this.triangle.B.vecy;
this.triangle.C.vecx = 0;
this.triangle.C.vecy = - this.height;
}
if (this.type == BOX_TYPE_RIGHTSLOPE) {
this.triangle.A.x = this.x;
this.triangle.A.y = this.y;
this.triangle.A.vecx = this.width;
this.triangle.A.vecy = this.height;
this.triangle.B.x = this.triangle.A.x + this.width;
this.triangle.B.y = this.triangle.A.y + this.height;
this.triangle.B.vecx = -this.width;
this.triangle.B.vecy = 0;
this.triangle.C.x = this.triangle.B.x + this.triangle.B.vecx;
this.triangle.C.y = this.triangle.B.y + this.triangle.B.vecy;
this.triangle.C.vecx = 0;
this.triangle.C.vecy = -this.height;
}
if (this.type == BOX_TYPE_COLLECTIBLE) {
this.color = "yellow";
}
// resave level data
BoxManager.save();
}
this.draw = function() {
if (this.type == BOX_TYPE_RECT) {
this.line.x = this.x;
this.line.y = this.y;
this.line.vecx = this.width;
this.line.vecy = this.height;
this.otherline.x = this.x + this.width;
this.otherline.y = this.y;
this.otherline.vecx = -this.width;
this.otherline.vecy = this.height;
this.bg.x = this.x;
this.bg.y = this.y;
this.bg.width = this.width;
this.bg.height = this.height;
// draw as rectangle
this.bg.drawAt(grid.x, grid.y, this.color, true, false);
this.line.drawAt(grid.x, grid.y, 1, "#333");
this.otherline.drawAt(grid.x, grid.y, 1, "#333");
}
if (this.type == BOX_TYPE_LEFTSLOPE) {
this.triangle.draw();
}
if (this.type == BOX_TYPE_RIGHTSLOPE) {
this.triangle.draw();
}
if (this.type == BOX_TYPE_COLLECTIBLE) {
this.line.x = this.x;
this.line.y = this.y;
this.line.vecx = this.width;
this.line.vecy = this.height;
this.otherline.x = this.x + this.width;
this.otherline.y = this.y;
this.otherline.vecx = -this.width;
this.otherline.vecy = this.height;
this.bg.x = this.x;
this.bg.y = this.y;
this.bg.width = this.width;
this.bg.height = this.height;
this.bg.drawAt(grid.x, grid.y, this.color, true, false);
this.line.drawAt(grid.x, grid.y, 1, "#333");
this.otherline.drawAt(grid.x, grid.y, 1, "#333");
}
};
}
}
class BoxManagerClass {
constructor() {
this.objects = new Array();
//this.lastObjectID = 0;
this.objectsLoaded = false;
//var objects = new Array(MAX_BOX_NUMBER);
//for (var i = 0; i < MAX_BOX_NUMBER; i++) objects[i] = new Box(0,0,0,0);
this.load = () => { // Load boxes
//console.log("BoxManager.load();");
$.ajax({url:"objects.txt", type:"post", success: function(msg) {
//console.log("objects.txt = " + msg);
var objs = JSON.parse(msg);
for (var i=0;i<objs.length;i++) {
BoxManager.objects[i] = new Box(objs[i].x,objs[i].y,objs[i].width,objs[i].height);
BoxManager.objects[i].convert(objs[i].type);
}
console.log(BoxManager.objects.length + " object(s) loaded.");
//console.log(this.objects);
//console.log(this.objects.length);
BoxManager.objectsLoaded = true;
// Load player location, only after all boxes have been loaded;
// Otherwise the player will fall through without collision detection
Player.load();
}});
};
this.save = () => {// Save boxes
console.log("BoxManager.save();");
//console.log("saving...");
//console.log(JSON.stringify());
//console.log(this.objects);
$.ajax({url:"saveboxes.php", type : "post", data:{"payload":JSON.stringify(this.objects)}, success: function(msg) {
//console.log("saveboxes.php = " + msg);
}});
};
// Add new box
this.add = (x,y,w,h) => {
this.objects[this.objects.length] = new Box(-grid.x+x,-grid.y+y,w,h);
this.save(); // Save all boxes
};
// Delete (remove) this box
this.remove = (id) => {
if (id > -1)
this.objects.splice(id,1);
};
// Draw the box
this.draw = () => {
//console.log(this.objects.length);
for (var i=0;i<this.objects.length;i++)
this.objects[i].draw();
};
}
}
var BoxManager = new BoxManagerClass();