Skip to content

Commit 5075d40

Browse files
committed
Check if a new table card would obscure the hot corners of the existing cards (this has a bug)
1 parent c347d26 commit 5075d40

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

playing-cards-2-planes/Table.js

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,16 @@ export class Table extends Container {
115115
}
116116

117117
/**
118-
* Checks if a hot corner rectangle at given position/angle intersects with a card.
118+
* Checks if a hot corner rectangle at given position intersects with a card at given position/angle.
119119
* @param {number} cornerX - Hot corner center X in world coordinates
120120
* @param {number} cornerY - Hot corner center Y in world coordinates
121-
* @param {Card} card - The card to check against
121+
* @param {number} cardX - Card center X
122+
* @param {number} cardY - Card center Y
123+
* @param {number} cardAngle - Card rotation in degrees
122124
* @returns {boolean} True if the hot corner overlaps with the card
123125
* @private
124126
*/
125-
_hotCornerIntersectsCard(cornerX, cornerY, card) {
127+
_hotCornerIntersectsCard(cornerX, cornerY, cardX, cardY, cardAngle) {
126128
// Hot corner dimensions - the rank/suit indicator area at card corners
127129
const hotCornerWidth = CARD_WIDTH / 6;
128130
const hotCornerHeight = CARD_HEIGHT / 5;
@@ -134,9 +136,9 @@ export class Table extends Container {
134136
hotCornerRect.height = hotCornerHeight;
135137

136138
// Set up card's transform matrix
137-
const angleRad = (card.angle * Math.PI) / 180;
139+
const angleRad = (cardAngle * Math.PI) / 180;
138140
tempMatrix.identity();
139-
tempMatrix.translate(-card.x, -card.y);
141+
tempMatrix.translate(-cardX, -cardY);
140142
tempMatrix.rotate(-angleRad);
141143

142144
// Set up card rectangle (centered at origin after transform)
@@ -186,34 +188,29 @@ export class Table extends Container {
186188
}
187189

188190
/**
189-
* Checks if a card at the given position would have at least one visible hot corner.
190-
* A hot corner is visible if it doesn't overlap with any existing card on the table.
191-
* @param {number} x - Card center X
192-
* @param {number} y - Card center Y
193-
* @param {number} angle - Card rotation in degrees
194-
* @returns {boolean} True if at least one hot corner would be visible
191+
* Checks if placing a new card at given position would obscure all hot corners of any existing card.
192+
* @param {number} x - New card center X
193+
* @param {number} y - New card center Y
194+
* @param {number} angle - New card rotation in degrees
195+
* @returns {boolean} True if any existing card would have all its hot corners covered
195196
* @private
196197
*/
197-
_hasVisibleCorner(x, y, angle) {
198+
_wouldObscureExistingCards(x, y, angle) {
198199
const existingCards = this.children.filter((child) => child instanceof Card);
199200

200-
if (existingCards.length === 0) {
201-
return true;
202-
}
203-
204-
const corners = this._getHotCorners(x, y, angle);
205-
206-
for (const corner of corners) {
207-
let cornerVisible = true;
201+
for (const card of existingCards) {
202+
const corners = this._getHotCorners(card.x, card.y, card.angle);
203+
let allCornersCovered = true;
208204

209-
for (const card of existingCards) {
210-
if (this._hotCornerIntersectsCard(corner.x, corner.y, card)) {
211-
cornerVisible = false;
205+
for (const corner of corners) {
206+
// Check if the NEW card would cover this existing card's corner
207+
if (!this._hotCornerIntersectsCard(corner.x, corner.y, x, y, angle)) {
208+
allCornersCovered = false;
212209
break;
213210
}
214211
}
215212

216-
if (cornerVisible) {
213+
if (allCornersCovered) {
217214
return true;
218215
}
219216
}
@@ -235,21 +232,26 @@ export class Table extends Container {
235232
const { minX, maxX, minY, maxY } = this._bounds;
236233

237234
let x, y, angle;
235+
let attempt;
238236

239-
// Try to find a position where at least one hot corner is visible
240-
for (let attempt = 0; attempt < MAX_PLACEMENT_ATTEMPTS; attempt++) {
237+
// Try to find a position where the new card doesn't obscure all hot corners of any existing card
238+
for (attempt = 0; attempt < MAX_PLACEMENT_ATTEMPTS; attempt++) {
241239
// Pick random position within rectangle bounds
242240
x = minX + Math.random() * (maxX - minX);
243241
y = minY + Math.random() * (maxY - minY);
244242

245243
// Random angle between -20 and +20 degrees
246244
angle = Math.random() * 40 - 20;
247245

248-
if (this._hasVisibleCorner(x, y, angle)) {
246+
if (!this._wouldObscureExistingCards(x, y, angle)) {
249247
break;
250248
}
251249
}
252250

251+
if (attempt === MAX_PLACEMENT_ATTEMPTS) {
252+
console.log(`Table: failed to find valid position for ${textureKey} after ${MAX_PLACEMENT_ATTEMPTS} attempts`);
253+
}
254+
253255
const card = new Card(spriteSheet, textureKey, clickHandler, x, y, angle);
254256
this.addChild(card);
255257

0 commit comments

Comments
 (0)