-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.js
243 lines (191 loc) · 5.9 KB
/
interface.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
// SET Conference 21 Model
//// CONFIG
var SQ_SIZE = 20; // size of circles
var buildCoords = [
[ 900, 500],
[ 680, 50],
[ 520, 230],
[1000, 100],
[ 100, 120],
[ 520, 520],
[ 850, 100],
[ 810, 40],
[ 580, 200],
[ 310, 120]];
var turbineCoords = [
[200, 500]
];
// ADDING MORE THAN ONE TURBINE BREAKS IT BECAUSE YOU CAN CONNECT TO ALTERNATING PLANTS
//// CONFIG
var canvas; // canvas itself
var ctx; // context for canvas
var buildings = Array(); // buildings
var turbines = Array(); // turbines
var currSelected; // currently selected building/turbine
class Building {
constructor(px, py, num) {
this.type = "Building";
this.px = px;
this.py = py;
this.connectsTo = null;
this.connectedFrom = null;
this.num = num
}
draw() {
if (currSelected == this) ctx.strokeStyle = "green";
else ctx.strokeStyle = "black";
ctx.beginPath();
ctx.arc(this.px, this.py, SQ_SIZE, 0, 2 * Math.PI);
ctx.stroke();
ctx.fillText(this.num, this.px, this.py - 1.5 * SQ_SIZE);
if (this.connectsTo) {
ctx.beginPath();
ctx.moveTo(this.px, this.py);
ctx.lineTo(this.connectsTo.px, this.connectsTo.py);
ctx.stroke();
}
}
click() {
if (!currSelected) currSelected = this;
// if nothing selected, select this
else if (currSelected == this) currSelected = null;
// clicking again deselects
else {
if (currSelected.type == "Building")
currSelected.connectsTo = this;
// Establish the connection to the target
else if (currSelected.type == "Turbine")
currSelected.connectsTo.push(this);
if (this.connectedFrom && this.connectedFrom.type == "Building" && this.connectedFrom != currSelected) {
// PREVENT MORE THAN ONE INCOMING CONNECTION
this.connectedFrom.connectsTo = null;
// break links with existing connections
}
else if (this.connectedFrom && this.connectedFrom.type == "Turbine" && this.connectedFrom != currSelected) {
// PREVENT MORE THAN ONE INCOMING CONNECTION
this.connectedFrom.connectsTo.splice(this.connectedFrom.connectsTo.indexOf(this), 1);
// break links with existing connections
}
this.connectedFrom = currSelected; // marks connection for reference
currSelected = this;
}
}
}
class Turbine {
constructor(px, py, num) {
this.type = "Turbine";
this.px = px;
this.py = py;
this.connectsTo = [];
this.num = num
}
draw() {
if (currSelected == this) ctx.strokeStyle = "blue";
else ctx.strokeStyle = "purple";
ctx.beginPath();
ctx.arc(this.px, this.py, SQ_SIZE, 0, 2 * Math.PI);
ctx.stroke();
ctx.fillText(this.num, this.px, this.py - 1.5 * SQ_SIZE);
for (var i=0; i < this.connectsTo.length; i++) {
ctx.beginPath();
ctx.moveTo(this.px, this.py);
ctx.lineTo(this.connectsTo[i].px, this.connectsTo[i].py);
ctx.stroke();
}
}
click() {
if (!currSelected) currSelected = this;
// if nothing selected, select this
else if (currSelected == this) currSelected = null;
// clicking again deselects
else if (currSelected.type == "Building") {
currSelected.connectsTo = this;
// Establish the connection to the target
currSelected = this;
}
}
}
function initialize() {
gamediv = document.getElementById("circuit");
document.addEventListener("click", mouseClicked); // add event
setInterval(draw, 20); // draw
canvas = document.createElement("canvas")
canvas.width = gamediv.offsetWidth - 30; // minus padding
canvas.height = canvas.width * 3/5;
gamediv.appendChild(canvas);// put the canvas into our document
ctx = canvas.getContext("2d"); // sets the ctx variable
ctx.lineWidth = 3;
var b_count = 0; // total building + turbines count
for (var i=0; i<turbineCoords.length; i++) {
turbines.push(new Turbine(turbineCoords[i][0], turbineCoords[i][1], b_count++));
// add new turbines
}
for (var i=0; i<buildCoords.length; i++) {
buildings.push(new Building(buildCoords[i][0], buildCoords[i][1], b_count++));
// add new buildings
}
}
function mouseClicked(e) {
var mX = e.clientX - canvas.offsetLeft + window.scrollX;
var mY = e.clientY - canvas.offsetTop + window.scrollY;
for (var i=0; i < buildings.length; i++) {
if ((mX-buildings[i].px)**2 + (mY-buildings[i].py)**2 < SQ_SIZE**2) {
buildings[i].click();
generateADJ();
return
}
}
for (var i=0; i < turbines.length; i++) {
if ((mX-turbines[i].px)**2 + (mY-turbines[i].py)**2 < SQ_SIZE**2) {
turbines[i].click();
generateADJ();
return
}
}
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height); // clears the canvas for redraw
for (var i=0; i < buildings.length; i++) {
buildings[i].draw();
}
for (var i=0; i < turbines.length; i++) {
turbines[i].draw();
}
}
function generateADJ() {
// generates adjacency list
var connections = {};
var fullyconn = true;
// CHECK BUILDINGS
for (var i=0; i < buildings.length; i++) {
if (buildings[i].connectsTo) {
connections[buildings[i].num] = [buildings[i].connectsTo.num]
}
else {
connections[buildings[i].num] = "none";
fullyconn = false;
}
}
// CHECK TURBINES
for (var i=0; i < turbines.length; i++) {
if (turbines[i].connectsTo) {
var adjs = [];
for (var j=0; j < turbines[i].connectsTo.length; j++) {
adjs.push(turbines[i].connectsTo[j].num);
}
connections[turbines[i].num] = adjs;
}
else {
connections[turbines[i].num] = "none";
fullyconn = false;
}
}
fieldNameElement = document.getElementById("print")
fieldNameElement.textContent = JSON.stringify(connections);
if (fullyconn) {
fieldNameElement.textContent += " Ready to GO!"
}
else {
fieldNameElement.textContent += " NOT FULLY CONNECTED!"
}
}