Skip to content

Commit c8b6743

Browse files
committed
inverseSwitchingBlock実装
1 parent 976f43a commit c8b6743

File tree

4 files changed

+64
-16
lines changed

4 files changed

+64
-16
lines changed

src/constants.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ export enum Block {
1313
fallable = "fallable",
1414
switch = "switch",
1515
switchBase = "switch-base",
16-
switchingBlockOFF = "switching-block-off",
17-
switchingBlockON = "switching-block-on",
16+
switchingBlockOFF = "switching-block-off", // 初期状態で出現している
17+
switchingBlockON = "switching-block-on", // スイッチを押すと隠れる
18+
inverseSwitchingBlockOFF = "inverse-switching-block-off", // 初期状態で隠れている
19+
inverseSwitchingBlockON = "inverse-switching-block-on", // スイッチを押すと出現する
1820
switchPressed = "switch-pressed",
1921
goal = "goal",
2022
}
@@ -37,5 +39,6 @@ export const BlockDefinitionMap = new Map<string, Block | null>([
3739
["s", Block.switch],
3840
["S", Block.switchBase],
3941
["w", Block.switchingBlockOFF],
42+
["W", Block.inverseSwitchingBlockOFF],
4043
["g", Block.goal],
4144
]);

src/grid.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ export type GridCell =
6363
switchId?: string;
6464
objectId?: unknown;
6565
}
66+
| {
67+
block: Block.inverseSwitchingBlockOFF;
68+
switchId?: string;
69+
objectId?: unknown;
70+
}
71+
| {
72+
block: Block.inverseSwitchingBlockON;
73+
switchId?: string;
74+
objectId?: unknown;
75+
}
6676
| {
6777
block: Block.switchPressed;
6878
switchId?: string;
@@ -145,6 +155,7 @@ export class Grid {
145155
vspriteRow.push({ sprite, block: dblock, dy: 0, vy: 0 });
146156
break;
147157
}
158+
case Block.inverseSwitchingBlockOFF:
148159
case Block.switchingBlockOFF: {
149160
const switchId = (
150161
get(cx.state).cells[y][x] as {
@@ -173,6 +184,7 @@ export class Grid {
173184
vspriteRow.push({ sprite, block: dblock, dy: 0, vy: 0 });
174185
break;
175186
}
187+
case Block.inverseSwitchingBlockON:
176188
case Block.switchingBlockON:
177189
case Block.switchPressed:
178190
throw new Error(`createCellsFromStageDefinition: block is not supported: ${dblock}`);
@@ -437,50 +449,60 @@ export class Grid {
437449
});
438450
}
439451
// switchingBlockOFFがONに切り替わるとき
440-
else if (vprev?.block === Block.switchingBlockOFF) {
441-
if (cNewCell.block !== Block.switchingBlockON) {
452+
else if (vprev?.block === Block.switchingBlockOFF || vprev?.block === Block.inverseSwitchingBlockOFF) {
453+
if (cNewCell.block !== Block.switchingBlockON && cNewCell.block !== Block.inverseSwitchingBlockON) {
442454
console.warn("No block other than switchingBlockON cannot replace the switchingBlockOFF");
443455
return;
444456
}
445457
assert(
446-
cprev.block === Block.switchingBlockOFF || cprev.block === Block.switchingBlockON,
458+
cprev.block === Block.switchingBlockOFF ||
459+
cprev.block === Block.switchingBlockON ||
460+
cprev.block === Block.inverseSwitchingBlockOFF ||
461+
cprev.block === Block.inverseSwitchingBlockON,
447462
"block is not switchingBlock",
448463
);
464+
const inversed = vprev.block === Block.inverseSwitchingBlockOFF;
465+
const switchONBlock = inversed ? Block.inverseSwitchingBlockON : Block.switchingBlockON;
449466
const switchId = cprev.switchId;
450467
if (!switchId) throw new Error("switchId is undefined");
451-
const blockSprite = createSprite(blockSize, Block.switchingBlockON, x, y, marginY, switchColor(switchId));
468+
const blockSprite = createSprite(blockSize, switchONBlock, x, y, marginY, switchColor(switchId));
452469
stage.addChild(blockSprite);
453470
cells[y][x] = {
454-
block: Block.switchingBlockON,
471+
block: switchONBlock,
455472
switchId,
456473
objectId: undefined,
457474
};
458475
vprev.sprite = blockSprite;
459-
vprev.block = Block.switchingBlockON;
476+
vprev.block = switchONBlock;
460477
vprev.dy = 0;
461478
vprev.vy = 0;
462479
}
463480
// switchingBlockONがOFFに切り替わるとき
464-
else if (vprev?.block === Block.switchingBlockON) {
465-
if (cNewCell.block !== Block.switchingBlockOFF) {
481+
else if (vprev?.block === Block.switchingBlockON || vprev?.block === Block.inverseSwitchingBlockON) {
482+
if (cNewCell.block !== Block.switchingBlockOFF && cNewCell.block !== Block.inverseSwitchingBlockOFF) {
466483
console.warn("No block other than switchingBlockOFF cannot replace the switchingBlockON");
467484
return;
468485
}
469486
assert(
470-
cprev.block === Block.switchingBlockOFF || cprev.block === Block.switchingBlockON,
487+
cprev.block === Block.switchingBlockOFF ||
488+
cprev.block === Block.switchingBlockON ||
489+
cprev.block === Block.inverseSwitchingBlockOFF ||
490+
cprev.block === Block.inverseSwitchingBlockON,
471491
"block is not switchingBlock",
472492
);
493+
const inversed = vprev.block === Block.inverseSwitchingBlockON;
494+
const switchOFFBlock = inversed ? Block.inverseSwitchingBlockOFF : Block.switchingBlockOFF;
473495
const switchId = cprev.switchId;
474496
if (!switchId) throw new Error("switchId is undefined");
475-
const blockSprite = createSprite(blockSize, Block.switchingBlockOFF, x, y, marginY, switchColor(switchId));
497+
const blockSprite = createSprite(blockSize, switchOFFBlock, x, y, marginY, switchColor(switchId));
476498
stage.addChild(blockSprite);
477499
cells[y][x] = {
478-
block: Block.switchingBlockOFF,
500+
block: switchOFFBlock,
479501
switchId,
480502
objectId: undefined,
481503
};
482504
vprev.sprite = blockSprite;
483-
vprev.block = Block.switchingBlockOFF;
505+
vprev.block = switchOFFBlock;
484506
vprev.dy = 0;
485507
vprev.vy = 0;
486508
} else {
@@ -654,6 +676,8 @@ export function createCellsFromStageDefinition(stageDefinition: StageDefinition)
654676
}
655677
// switches
656678
case Block.switch:
679+
case Block.inverseSwitchingBlockON:
680+
case Block.inverseSwitchingBlockOFF:
657681
case Block.switchingBlockON:
658682
case Block.switchingBlockOFF: {
659683
const group = stageDefinition.switchGroups.find((b) => b.x === x && b.y === y);
@@ -726,13 +750,15 @@ function createSprite(
726750
updateSprite(switchBaseSprite, blockSize, x, y, marginY, 0);
727751
return switchBaseSprite;
728752
}
753+
case Block.inverseSwitchingBlockON:
729754
case Block.switchingBlockOFF: {
730755
const sprite = new Sprite(rockTexture);
731756
if (switchColor) sprite.tint = switchColor;
732757
else sprite.tint = 0xffa500;
733758
updateSprite(sprite, blockSize, x, y, marginY, 0);
734759
return sprite;
735760
}
761+
case Block.inverseSwitchingBlockOFF:
736762
case Block.switchingBlockON: {
737763
const sprite = new Sprite(rockTexture);
738764
if (switchColor) sprite.tint = switchColor;
@@ -804,9 +830,11 @@ export function printCells(cells: GridCell[][], context?: string) {
804830
case Block.switchBase:
805831
return "S";
806832
case Block.switchingBlockOFF:
807-
return "w";
808833
case Block.switchingBlockON:
809834
return "w";
835+
case Block.inverseSwitchingBlockOFF:
836+
case Block.inverseSwitchingBlockON:
837+
return "W";
810838
case Block.switchPressed:
811839
return "s";
812840
case Block.goal:

src/player.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ export function tick(cx: Context, ticker: Ticker) {
154154
cx.grid.getBlock(cx, Math.floor(x), Math.floor(y)) !== Block.switch &&
155155
cx.grid.getBlock(cx, Math.floor(x), Math.floor(y)) !== Block.switchPressed &&
156156
cx.grid.getBlock(cx, Math.floor(x), Math.floor(y)) !== Block.switchingBlockON &&
157+
cx.grid.getBlock(cx, Math.floor(x), Math.floor(y)) !== Block.inverseSwitchingBlockOFF &&
157158
cx.grid.getBlock(cx, Math.floor(x), Math.floor(y)) !== Block.goal &&
158159
cx.grid.getBlock(cx, Math.floor(x), Math.floor(y)) !== undefined;
159160
const isSwitchBase = (x: number, y: number) =>
@@ -262,13 +263,19 @@ export function tick(cx: Context, ticker: Ticker) {
262263
if (cx.grid.getBlock(cx, sb.x, sb.y) === Block.switchingBlockOFF) {
263264
cx.grid.setBlock(cx, sb.x, sb.y, { block: Block.switchingBlockON });
264265
}
266+
if (cx.grid.getBlock(cx, sb.x, sb.y) === Block.inverseSwitchingBlockOFF) {
267+
cx.grid.setBlock(cx, sb.x, sb.y, { block: Block.inverseSwitchingBlockON });
268+
}
265269
}
266270
} else {
267271
// スイッチが押されていないとき
268272
for (const sb of switchingBlock) {
269273
if (cx.grid.getBlock(cx, sb.x, sb.y) === Block.switchingBlockON) {
270274
cx.grid.setBlock(cx, sb.x, sb.y, { block: Block.switchingBlockOFF });
271275
}
276+
if (cx.grid.getBlock(cx, sb.x, sb.y) === Block.inverseSwitchingBlockON) {
277+
cx.grid.setBlock(cx, sb.x, sb.y, { block: Block.inverseSwitchingBlockOFF });
278+
}
272279
}
273280
}
274281
}

src/stages.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ export const stages = new Map<string, StageDefinition>([
449449
".........b........",
450450
".........b........",
451451
".........w........",
452-
".sss..f..w.....g..",
452+
".sss..fW.w..W..g..",
453453
"bSSSbbbbbbbbbbbbbb",
454454
],
455455
initialPlayerX: 3,
@@ -481,6 +481,16 @@ export const stages = new Map<string, StageDefinition>([
481481
y: 4,
482482
switchId: "1",
483483
},
484+
{
485+
x: 7,
486+
y: 4,
487+
switchId: "1",
488+
},
489+
{
490+
x: 12,
491+
y: 4,
492+
switchId: "1",
493+
},
484494
],
485495
},
486496
],

0 commit comments

Comments
 (0)