| 1 | 'use strict'; |
| 2 | |
| 3 | const { describe, it } = require('node:test'); |
| 4 | const assert = require('node:assert/strict'); |
| 5 | const { makeGame, GameCore } = require('./helpers.js'); |
| 6 | |
| 7 | describe('Cell-based grab picking (t1109)', () => |
| 8 | { |
| 9 | it('a press anywhere inside the cell, including each corner, grabs that block', () => |
| 10 | { |
| 11 | const g = makeGame({ noSpawn: true }); |
| 12 | const block = g.place(2, 3, 1); |
| 13 | g.place(2, 2, 0); |
| 14 | g.place(2, 4, 2); |
| 15 | const corners = [ |
| 16 | [2.01, 3.01], |
| 17 | [2.01, 3.99], |
| 18 | [2.99, 3.01], |
| 19 | [2.99, 3.99], |
| 20 | [2.5, 3.5], |
| 21 | [2.15, 3.15], |
| 22 | [2.85, 3.85] |
| 23 | ]; |
| 24 | for (const [col, y] of corners) |
| 25 | { |
| 26 | assert.ok(g.beginDrag(col, y), 'should grab at (' + col + ', ' + y + ')'); |
| 27 | assert.equal(g.drag.blockId, block.id, 'wrong block at (' + col + ', ' + y + ')'); |
| 28 | g.endDrag(); |
| 29 | } |
| 30 | }); |
| 31 | |
| 32 | it('a press in the padding / gap of a cell still grabs the occupant (not the sprite box)', () => |
| 33 | { |
| 34 | const g = makeGame({ noSpawn: true }); |
| 35 | const block = g.place(4, 1, 3); |
| 36 | g.place(4, 0, 0); |
| 37 | // Near the edges — "gaps between sprites" if sprites are inset. |
| 38 | assert.ok(g.beginDrag(4.02, 1.02)); |
| 39 | assert.equal(g.drag.blockId, block.id); |
| 40 | g.endDrag(); |
| 41 | assert.ok(g.beginDrag(4.98, 1.98)); |
| 42 | assert.equal(g.drag.blockId, block.id); |
| 43 | g.endDrag(); |
| 44 | }); |
| 45 | |
| 46 | it('when a block is mid-animation (non-integer y), the nearest in the column within half a tile is grabbed', () => |
| 47 | { |
| 48 | const g = makeGame({ noSpawn: true }); |
| 49 | const block = g.place(1, 2, 2); |
| 50 | g.place(1, 0, 0); |
| 51 | // Simulate a bounce / settle offset: the block sits at 2.35. |
| 52 | block.y = 2.35; |
| 53 | // Press in cell 2 (overlaps the footprint [2.35, 3.35)). |
| 54 | assert.ok(g.beginDrag(1.4, 2.5)); |
| 55 | assert.equal(g.drag.blockId, block.id); |
| 56 | g.endDrag(); |
| 57 | // Press in cell 3, just above the footprint — fallback within 0.5. |
| 58 | // Footprint ends at 3.35; press at 3.55 is 0.20 away. |
| 59 | assert.ok(g.beginDrag(1.4, 3.55)); |
| 60 | assert.equal(g.drag.blockId, block.id); |
| 61 | g.endDrag(); |
| 62 | // Further than SNAP_Y (0.5) above 3.35: at 3.35+0.5+0.15=4.0, no grab. |
| 63 | assert.equal(g.beginDrag(1.4, 3.35 + GameCore.SNAP_Y + 0.15), false); |
| 64 | }); |
| 65 | |
| 66 | it('floor of the pointer chooses the cell; the cell above is a different block', () => |
| 67 | { |
| 68 | const g = makeGame({ noSpawn: true }); |
| 69 | const lower = g.place(0, 5, 1); |
| 70 | const upper = g.place(0, 6, 2); |
| 71 | g.place(0, 4, 0); |
| 72 | assert.ok(g.beginDrag(0.99, 5.99)); |
| 73 | assert.equal(g.drag.blockId, lower.id); |
| 74 | g.endDrag(); |
| 75 | assert.ok(g.beginDrag(0.01, 6.01)); |
| 76 | assert.equal(g.drag.blockId, upper.id); |
| 77 | g.endDrag(); |
| 78 | }); |
| 79 | }); |
projects-timfalken-com meteors