12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- class Drawer
- {
-
- constructor(_padding, width, height)
- {
- this.padding = _padding;
-
- this.width = width;
- this.height = height;
-
- createCanvas(width, height);
- }
-
- setTubesParams(_tubesNumbers, _tubeLevels, _tubeSize)
- {
- this.tubesNumbers = _tubesNumbers;
- this.tubeLevels = _tubeLevels;
- this.tubeSize = _tubeSize;
- }
-
- draw(game)
- {
- let tubes = game.tubes;
- let selectedTube = game.selectedTube;
-
- background('white');
-
- this.drawTubes(this.padding, this.padding+this.tubeSize, tubes, selectedTube);
-
- }
-
- drawTubes(x, y, tubes, selectedTube)
- {
- let tubeX = x;
- let tubeY = y;
- let selectionOffset = 0;
-
- tubes.forEach(tube => {
- selectionOffset = (tube == selectedTube ? this.tubeSize : 0);
-
- this.drawTube(tubeX, tubeY - selectionOffset, tube);
- tubeX += this.padding + this.tubeSize;
- });
- }
-
- drawTube(x, y, tube)
- {
-
- y += (this.tubeLevels-1) * this.tubeSize;
-
- for(let i = 0; i < this.tubeLevels; i++)
- {
- color = tube.getColorAtLevel(i);
- color == null ? noFill() : fill(color);
-
- if (i >= 0)
- {
- rect(x, y - i * this.tubeSize, this.tubeSize);
- }
- }
- }
-
- getTubeIdAt(x, y)
- {
- if (!this.isInboundsCanvas(x,y)) return -1;
-
- let idWithPadding = (x - this.padding) / (this.tubeSize + this.padding);
- return idWithPadding % 1 > 1 - (this.padding / (this.tubeSize + this.padding)) ? -1 : int(idWithPadding);
- }
-
- isInboundsCanvas(x, y)
- {
- return x >= this.padding
- && y >= this.padding
- && x < this.width - this.padding
- && y < this.height - this.padding;
- }
-
- }
|