-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid.js
617 lines (526 loc) · 15.9 KB
/
Grid.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
class Grid {
/**
constructor()
@description constructs the Inventory
@param manager the inventory manager
@param rows the rows of this inventory
@param columns the columns of this inventory
*/
constructor(position, player, manager, rows, columns) {
this._position = position;
this._slots = [];
this._rows = rows;
this._columns = columns;
this._selectedSlot = null;
this._inventoryManager = manager;
this._player = player;
// determines if you can move objects
// from one slot to another in the inventory
this._active = true;
this._onRightClickEnabled = true;
this._createSlots();
}
/**
* findEmitters()
* @description finds all the positions of all the emitters in the grid
* @returns the positions of all the emitters in the grid
*/
findEmitters() {
let emitters = [];
for (var x = 0; x < this._columns; x++) {
for (var y = 0; y < this._rows; y++) {
var slot = this._slots[x][y]
if(slot && slot.item instanceof Emitter) {
emitters.push({
x, y
})
}
}
}
return emitters;
}
/**
* getNextSlot()
* @param {Point} pointer the pointer to the current position of the laser
* @param {Direction} direction the direction the laser is going
*/
getNextSlot(pointer, direction) {
// choose the direction the emitter goes
// and update the pointer accordingly.
if(direction === "right") {
return {x: pointer.x + 1 , y: pointer.y}
} else if(direction === "left") {
return {x: pointer.x - 1, y: pointer.y}
} else if(direction === "down") {
return {x: pointer.x, y: pointer.y + 1}
} else if(direction === "up") {
return {x: pointer.x, y: pointer.y - 1}
}
}
/**
* @description checks if a given point is in the grid
*/
pointInGrid(point) {
return (
point.x >= 0 && point.x < this._columns &&
point.y >= 0 && point.y < this._rows
)
}
/**
* @description emmits a beam from a point
*/
emitBeam(emitter, direction, color) {
context.lineWidth = 1;
context.beginPath();
let currentSlot = this._slots[emitter.x][emitter.y]; // get the current slot the emitter is in
context.moveTo(currentSlot.center.x, currentSlot.center.y); // move the laser start position to the beam emitter
let currentDirection = direction; // the emitters current direction
let pointer = {...emitter}; // the current position of the end of the beam
let laserSize = 1;
while(this.pointInGrid(this.getNextSlot(pointer, currentDirection))) {
// update the direction of the laser based on the current block
currentDirection = (currentSlot.item) ? currentSlot.item.updateDirection(currentDirection) : currentDirection
pointer = this.getNextSlot(pointer, currentDirection);
currentSlot = this._slots[pointer.x][pointer.y]; // set current slot
context.lineTo(currentSlot.center.x, currentSlot.center.y); // draw a line to the current slot
if(currentSlot.item instanceof Target) {
if(currentSlot.item.requiredLaserSize === laserSize) {
currentSlot.item.activate();
}
break;
} else if(currentSlot.item instanceof Lens) {
context.strokeStyle = color;
context.stroke()
context.beginPath();
context.moveTo(currentSlot.center.x, currentSlot.center.y);
laserSize++;
context.lineWidth = laserSize;
} else if(currentSlot.item instanceof Block) {
break;
} else if(currentSlot.item instanceof BeamSplitter) {
context.strokeStyle = color;
context.stroke()
for (const direction of currentSlot.item.getDirections(currentDirection)) {
this.emitBeam(pointer, direction, "red");
}
context.beginPath();
break;
}
}
context.strokeStyle = color;
context.stroke()
context.lineWidth = 1;
}
/**
* @description emits a laser from a emitter
* @param {CanvasRenderingContext2D} context the context to render the laser to
*/
projectLaser(context) {
let emitters = this.findEmitters(); // finds all the emitters in the grid
for (const emitter of emitters) {
var direction = this._slots[emitter.x][emitter.y].item.direction;
this.emitBeam(emitter, direction, "red");
}
}
/**
* @description clears the grid of all items
* @returns
*/
clear() {
for (var x = 0; x < this._columns; x++) {
for (var y = 0; y < this._rows; y++) {
this._slots[x][y].removeItem();
}
}
}
/**
* findSlotContainingPoint()
* @description find the slot that contains a given point
* @param {Point} point the point to check for
*/
findSlotContainingPoint(point) {
for (var x = 0; x < this._columns; x++) {
for (var y = 0; y < this._rows; y++) {
var slot = this._slots[x][y]
if(slot.contains(point)) {
return slot;
}
}
}
return null;
}
/**
* get allowPickup()
* @description gets the allow pickup property
*/
get allowPickup() {
return this._allowPickup;
}
/**
_createSlots()
@description create all the _slots for the storage
*/
_createSlots() {
for (var x = 0; x < this._columns; x++) {
let newRow = []
for (var y = 0; y < this._rows; y++) {
let newSlot = new Slot(
this._player,
this,
{
x: this._position.x + Slot.Size * x,
y: this._position.y + Slot.Size * y
},
{ x, y }
)
newRow.push(newSlot)
}
this._slots.push(newRow);
}
}
/********************************************************
JSON Function
*********************************************************/
/**
toJSON()
@description converts this storage to its json representation
*/
toJSON() {
let storageAsJSON = {
columns: this._columns,
rows: this._rows,
slots: []
}
for (var x = 0; x < this._columns; x++) {
var newRow = []
for (var y = 0; y < this._rows; y++) {
var newSlot = this._slots[x][y].toJSON()
newRow.push(newSlot);
}
storageAsJSON.slots.push(newRow);
}
return storageAsJSON;
}
/**
fromJSON()
@description convert a json object to a storage object
*/
static fromJSON(player, inventoryManager, json) {
let inventory = new Grid(player, inventoryManager, json.rows, json.columns)
for (var x = 0; x < inventory._columns; x++) {
for (var y = 0; y < inventory._rows; y++) {
inventory._slots[x][y].destroyItem()
var item = ItemRegistry.itemFromJSON(json.slots[x][y].item);
if(item !== null) {
inventory._slots[x][y].addItem(
item, inventory._svg.layers
)
}
}
}
return inventory
}
/********************************************************
Getters and Setters
*********************************************************/
/**
* get layers
* @description gets the graphics layers of the inventory
*/
get layers() {
return this._svg.layers;
}
/**
* get onRightClickEnabled
* @description get onRightClickEnabled
*/
get onRightClickEnabled() {
return this._onRightClickEnabled;
}
/**
* set onRightClickEnabled
* @description set onRightClickEnabled
* @param value the value to set the onRightClickEnabled field to
*/
set onRightClickEnabled(value) {
this._onRightClickEnabled = value;
}
/**
get width
@description gets the width of the inventory
*/
get width() {
return Slot.size * this._columns
}
/**
get height
@description gets the height of the inventory
*/
get height() {
return Slot.size * this._rows
}
/**
get currentlySelected
@description get the currently selected slot
*/
get currentlySelected() {
return this._selectedSlot;
}
/**
get itemsMoveable
@description gets whether the items are movable
*/
get itemsMovable() {
return this._itemsMovable;
}
/**
set itemsMoveable
@description gets whether the items are movable
*/
set itemsMovable(value) {
this._itemsMovable = value;
}
/**
get active
@description gets whether the items are active
*/
get active() {
return this._active;
}
/**
activate
@description activate the inventory
*/
activate() {
this._active = true;
}
/**
deactivate
@description deactivate the inventory
*/
deactivate() {
this._active = false;
}
/**
moveTo()
@description moves the storage to a new position
@param position position to move to
*/
moveTo(position) {
for (var x = 0; x < this._slots.length; x++) {
for (var y = 0; y < this._slots[x].length; y++) {
this._slots[x][y].position = {
x: position.x + Slot.size * x,
y: position.y + Slot.size * y
}
}
}
}
/**
add()
@description adds an item to the first available slot in the
storage container.
@param item the item to add
*/
add(item) {
// find the first available slot and place the item there
for (var x = 0; x < this._slots.length; x++) {
for (var y = 0; y < this._slots[x].length; y++) {
if(this._slots[x][y].isEmpty()) {
this._slots[x][y].addItem(item);
return true;
}
}
}
return false;
}
/**
* getAllItemsByName()
* @description gets all the items by a certain name and put them into one stack
* @param {string} name the name of the item
*/
getAllItemsByName(name) {
let item = ItemRegistry.lookup(name).clone()
let self = this;
item._svg.clickArea.on("click", (event) => self.itemOnLeftClick(event, item))
item.quantity = 0;
for (var x = 0; x < this._slots.length; x++) {
for (var y = 0; y < this._slots[x].length; y++) {
if(this._slots[x][y].item && this._slots[x][y].item.name === name) {
item.quantity += this._slots[x][y].item.quantity
this._slots[x][y].destroyItem()
this.removeItemFromSlot(this._slots[x][y])
}
}
}
if(item.quantity === 0)
return null;
else
return item
}
/**
onClick()
@description the function called when this block is clicked
*/
itemOnLeftClick(event, item) {
if(this._itemsMovable) {
let pos = d3.pointer(event);
if(this._player.hand) { // there is something in the hand
// place the item in the players hand into the designated slot
if(
this._inventoryManager.addToContainingSlot({
x: pos[0], y: pos[1]
}, this._player.hand)
) {
//this._player.hand.destroy();
this._player.removeItemFromHand();
console.log("Slot clicked");
}
} else {
console.log("Hand empty");
this._player.addItemToHand(item)
}
}
}
/**
* splitStack()
* @description splits the items in a given stack
* @param slot the slot that the item stack is in
*/
splitStack(coordinate) {
// find the closest empty slot
var distance = Math.sqrt(
Math.pow(window.innerHeight, 2) + Math.pow(window.innerHeight, 2)
); // size of the window across
var closestSlot = null;
var item = this._slots[coordinate.x][coordinate.y].item;
if(item !== null) {
for (var _x = 0; _x < this._columns; _x++) {
for (var _y = 0; _y < this._rows; _y++) {
var tempDistance = this._slots[_x][_y].distanceTo(item)
if((_x !== coordinate.x && _y !== coordinate.y) && tempDistance <= distance) {
distance = tempDistance;
closestSlot = this._slots[_x][_y];
}
}
}
}
var tempItem = item.clone()
tempItem.quantity = Math.floor(item.quantity/2)
item.quantity -= tempItem.quantity
this.addItemToSlot(closestSlot, tempItem)
}
/**
getClosestSlot()
@description get the closest slot to the given unit
@param unit the unit to find the closest slot to
*/
getClosestSlot(item) {
var distance = this._slots[0][0].distanceTo(item);
var closestSlot = this._slots[0][0];
for (var x = 0; x < this._columns; x++) {
for (var y = 0; y < this._rows; y++) {
var tempDistance = this._slots[x][y].distanceTo(item)
if(tempDistance <= distance) {
distance = tempDistance;
closestSlot = this._slots[x][y];
}
}
}
return closestSlot;
}
/**
snapToClosestSlot()
@description snap a given item to the closest slot
@param item the item to snap to the closest slot
*/
snapToClosestSlot(item) {
var closestSlot = this.getClosestSlot(item)
this.addItemToSlot(closestSlot, item)
}
/**
addItemToSlot()
@description adds a item to a specified slot
@param slot the slot to add the item to
@param item the item to add
*/
addItemToSlot(slot, item) {
slot.addItem(item, this._svg.layers);
}
/**
* removeItemFromSlot()
* @description removes an item from a given slot
* @param slot the slot to remove the item from
*/
removeItemFromSlot(slot) {
slot.removeItem()
}
/********************************************************
Graphics
*********************************************************/
/**
* render()
* @param {Object} props the properties to render
*/
render(context) {
for (var x = 0; x < this._columns; x++) {
for (var y = 0; y < this._rows; y++) {
this._slots[x][y].render(context)
}
}
}
/**
delete()
@description delete the svg for this storage
*/
delete() {
this._svg.group.remove()
}
/**
deselectAll()
@description deselect all the items in the storage
*/
deselectAll() {
for (var x = 0; x < this._slots.length; x++) {
for (var y = 0; y < this._slots[x].length; y++) {
this._slots[x][y].deselect();
}
}
}
/**
select()
@description selects a given slot of this storage
@param slot slot to be selected
*/
select(slot) {
this._selectedSlot = slot;
this._selectedSlot.attach(this._svg.layers.slots);
if(this._selectedSlot.item) {
this._selectedSlot.item.attach(this._svg.layers.items);
}
this._selectedSlot.select();
}
/**
* selectSlotByPosition()
* @description selects a slot by its position
*/
selectSlotByPosition(x, y) {
this.deselectAll();
this._selectedSlot = this._slots[x][y];
this._selectedSlot.attach(this._svg.layers.slots);
if(this._selectedSlot.item) {
this._selectedSlot.item.attach(this._svg.layers.items);
}
this._selectedSlot.select();
}
/**
useSelectedItem()
@description use the selected item
*/
useSelectedSlot() {
this._selectedSlot.useSlot();
}
getSlotAt(x, y) {
return this._slots[x][y];
}
}