-
Notifications
You must be signed in to change notification settings - Fork 0
/
rect.js
169 lines (138 loc) · 5.66 KB
/
rect.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
class Rect {
constructor(x, y, width, height) {
this.x = x;
this.y = y;
this.originalX = x;
this.originalY = y;
this.width = width;
this.height = height;
this.maxHeight = height;
this.maxWidth = width;
this.strategy = stratArray[Math.floor(Math.random() * (stratArray.length - 1))];
this.neighbours = [];
this.score = 0;
this.strategyNew = false;
this.color = colorDict[this.strategy];
this.colorRGB = colorDictRGB[this.strategy];
this.counter = 0;
this.proposal = parseFloat(Math.random().toFixed(2));
this.acceptance = parseFloat(Math.random().toFixed(2));
this.wasDu = false
this.wasCu = false
this.calcRGBcolor = transitionSpeed => {
if (!this.strategyNew) {
return;
}
const [r1, g1, b1] = this.colorRGB;
const [r2, g2, b2] = colorDictRGB[this.strategyNew];
this.colorRGB = [
Math.round((r2 - r1) * transitionSpeed + r1),
Math.round((g2 - g1) * transitionSpeed + g1),
Math.round((b2 - b1) * transitionSpeed + b1)
]
return `rgb(${this.colorRGB[0]}, ${ this.colorRGB[1]}, ${ this.colorRGB[2]})`;
};
this.draw = () => {
ctx.beginPath();
if (document.querySelector('#selectGameMenu').value == 3) {
const hue = Math.sqrt(this.proposal);
const [r, g, b] = HSVtoRGB(hue % 1, 1, 1);
ctx.fillStyle = `rgb(${r}, ${g}, ${b})`;
} else {
ctx.fillStyle = this.calcRGBcolor(transitionSpeed);
}
if (this.strategy === 'empty') {
if (displayStyle === 1) {
ctx.strokeRect(this.originalX, this.originalY, this.width + borderSize, this.height + borderSize);
}
else {
ctx.strokeRect(this.x, this.y, this.width + borderSize, this.height + borderSize);
}
} else {
if (displayStyle === 1) {
ctx.rect(this.originalX, this.originalY, this.width + borderSize, this.height + borderSize);
}
else {
ctx.rect(this.x, this.y, this.width + borderSize, this.height + borderSize);
}
}
ctx.fill();
};
this.update = function() {
//interactivity
if (this.height > newHeight && this.width > newWidth) {
this.height -= 0.3;
this.width -= 0.3;
}
if (drawMode === false) {
if (mouse.x - this.x < 50 && mouse.x - this.x > -50 && mouse.y - this.y < 50 && mouse.y - this.y > -50) {
if (this.height < height && this.width < width) {
this.height += 1;
this.width += 1;
}
}
else {
if (this.height > height - borderSize && this.width > width - borderSize) {
this.height -= 0.3;
this.width -= 0.3;
}
}
}
let dx = (Math.random() - 0.5) * z;
let dy = (Math.random() - 0.5) * z;
if (this.x + dx < this.originalX + wanderX && this.x + dx > this.originalX - wanderX) {
this.x += dx;
}
if (this.y + dy < this.originalY + wanderY && this.y + dy > this.originalY - wanderY) {
this.y += dy;
}
//drawing cells with mouse
this.handleClick();
//update cell
this.draw();
}
this.handleClick = function() {
if (mouse.target != canvas) {
return
}
if (mouse.buttons !== 1) return;
// Adjust the clickable area by adding or subtracting values
var expandedX = this.x - 15; // Adjust as needed
var expandedY = this.y - 15; // Adjust as needed
var expandedWidth = this.width + 50; // Adjust as needed
var expandedHeight = this.height + 50; // Adjust as needed
if (
(mouse.x >= expandedX && mouse.x <= expandedX + expandedWidth) &&
(mouse.y >= expandedY && mouse.y <= expandedY + expandedHeight)
) {
this.strategy = game.stratArray[thingToDrawCounter];
this.strategyNew = game.stratArray[thingToDrawCounter];
this.score = 0;
}
}
this.gameBoardSizeChange = function() {
const newX= newBoard[this.index].x;
const newY= newBoard[this.index].y;
}
this.findNeighbours = function(gameBoard) {
const directions = [
[width, 0],
[-width, 0],
[0, height],
[0, -height],
[width, height],
[-width, height],
[width, -height],
[-width, -height],
];
for (const [offsetX, offsetY] of directions) {
const targetX = this.x + offsetX;
const targetY = this.y + offsetY;
const neighbor = gameBoard.find(rect => rect.x === targetX && rect.y === targetY);
if (neighbor) {
this.neighbours.push(neighbor);
}
}
};
}
}