2024-01-08 02:13:20 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { EventEmitter } from 'eventemitter3';
|
|
|
|
import * as Matter from 'matter-js';
|
2024-01-09 12:15:56 +00:00
|
|
|
import seedrandom from 'seedrandom';
|
2024-01-08 02:13:20 +00:00
|
|
|
|
|
|
|
export type Mono = {
|
|
|
|
id: string;
|
|
|
|
level: number;
|
2024-01-12 05:48:44 +00:00
|
|
|
sizeX: number;
|
|
|
|
sizeY: number;
|
2024-01-08 02:13:20 +00:00
|
|
|
shape: 'circle' | 'rectangle';
|
|
|
|
score: number;
|
|
|
|
dropCandidate: boolean;
|
|
|
|
};
|
|
|
|
|
2024-01-09 12:15:56 +00:00
|
|
|
type Log = {
|
|
|
|
frame: number;
|
|
|
|
operation: 'drop';
|
|
|
|
x: number;
|
|
|
|
} | {
|
|
|
|
frame: number;
|
|
|
|
operation: 'hold';
|
|
|
|
} | {
|
|
|
|
frame: number;
|
|
|
|
operation: 'surrender';
|
|
|
|
};
|
|
|
|
|
2024-01-13 02:43:13 +00:00
|
|
|
const NORMAL_BASE_SIZE = 30;
|
|
|
|
const NORAML_MONOS: Mono[] = [{
|
|
|
|
id: '9377076d-c980-4d83-bdaf-175bc58275b7',
|
|
|
|
level: 10,
|
|
|
|
sizeX: NORMAL_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
sizeY: NORMAL_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
shape: 'circle',
|
|
|
|
score: 512,
|
|
|
|
dropCandidate: false,
|
|
|
|
}, {
|
|
|
|
id: 'be9f38d2-b267-4b1a-b420-904e22e80568',
|
|
|
|
level: 9,
|
|
|
|
sizeX: NORMAL_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
sizeY: NORMAL_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
shape: 'circle',
|
|
|
|
score: 256,
|
|
|
|
dropCandidate: false,
|
|
|
|
}, {
|
|
|
|
id: 'beb30459-b064-4888-926b-f572e4e72e0c',
|
|
|
|
level: 8,
|
|
|
|
sizeX: NORMAL_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
sizeY: NORMAL_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
shape: 'circle',
|
|
|
|
score: 128,
|
|
|
|
dropCandidate: false,
|
|
|
|
}, {
|
|
|
|
id: 'feab6426-d9d8-49ae-849c-048cdbb6cdf0',
|
|
|
|
level: 7,
|
|
|
|
sizeX: NORMAL_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
sizeY: NORMAL_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
shape: 'circle',
|
|
|
|
score: 64,
|
|
|
|
dropCandidate: false,
|
|
|
|
}, {
|
|
|
|
id: 'd6d8fed6-6d18-4726-81a1-6cf2c974df8a',
|
|
|
|
level: 6,
|
|
|
|
sizeX: NORMAL_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
sizeY: NORMAL_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
shape: 'circle',
|
|
|
|
score: 32,
|
|
|
|
dropCandidate: false,
|
|
|
|
}, {
|
|
|
|
id: '249c728e-230f-4332-bbbf-281c271c75b2',
|
|
|
|
level: 5,
|
|
|
|
sizeX: NORMAL_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
sizeY: NORMAL_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
shape: 'circle',
|
|
|
|
score: 16,
|
|
|
|
dropCandidate: true,
|
|
|
|
}, {
|
|
|
|
id: '23d67613-d484-4a93-b71e-3e81b19d6186',
|
|
|
|
level: 4,
|
|
|
|
sizeX: NORMAL_BASE_SIZE * 1.25 * 1.25 * 1.25,
|
|
|
|
sizeY: NORMAL_BASE_SIZE * 1.25 * 1.25 * 1.25,
|
|
|
|
shape: 'circle',
|
|
|
|
score: 8,
|
|
|
|
dropCandidate: true,
|
|
|
|
}, {
|
|
|
|
id: '3cbd0add-ad7d-4685-bad0-29f6dddc0b99',
|
|
|
|
level: 3,
|
|
|
|
sizeX: NORMAL_BASE_SIZE * 1.25 * 1.25,
|
|
|
|
sizeY: NORMAL_BASE_SIZE * 1.25 * 1.25,
|
|
|
|
shape: 'circle',
|
|
|
|
score: 4,
|
|
|
|
dropCandidate: true,
|
|
|
|
}, {
|
|
|
|
id: '8f86d4f4-ee02-41bf-ad38-1ce0ae457fb5',
|
|
|
|
level: 2,
|
|
|
|
sizeX: NORMAL_BASE_SIZE * 1.25,
|
|
|
|
sizeY: NORMAL_BASE_SIZE * 1.25,
|
|
|
|
shape: 'circle',
|
|
|
|
score: 2,
|
|
|
|
dropCandidate: true,
|
|
|
|
}, {
|
|
|
|
id: '64ec4add-ce39-42b4-96cb-33908f3f118d',
|
|
|
|
level: 1,
|
|
|
|
sizeX: NORMAL_BASE_SIZE,
|
|
|
|
sizeY: NORMAL_BASE_SIZE,
|
|
|
|
shape: 'circle',
|
|
|
|
score: 1,
|
|
|
|
dropCandidate: true,
|
|
|
|
}];
|
|
|
|
|
|
|
|
const YEN_BASE_SIZE = 30;
|
|
|
|
const YEN_SATSU_BASE_SIZE = 70;
|
|
|
|
const YEN_MONOS: Mono[] = [{
|
|
|
|
id: '880f9bd9-802f-4135-a7e1-fd0e0331f726',
|
|
|
|
level: 10,
|
|
|
|
sizeX: (YEN_SATSU_BASE_SIZE * 2) * 1.25 * 1.25 * 1.25,
|
|
|
|
sizeY: YEN_SATSU_BASE_SIZE * 1.25 * 1.25 * 1.25,
|
|
|
|
shape: 'rectangle',
|
|
|
|
score: 10000,
|
|
|
|
dropCandidate: false,
|
|
|
|
}, {
|
|
|
|
id: 'e807beb6-374a-4314-9cc2-aa5f17d96b6b',
|
|
|
|
level: 9,
|
|
|
|
sizeX: (YEN_SATSU_BASE_SIZE * 2) * 1.25 * 1.25,
|
|
|
|
sizeY: YEN_SATSU_BASE_SIZE * 1.25 * 1.25,
|
|
|
|
shape: 'rectangle',
|
|
|
|
score: 5000,
|
|
|
|
dropCandidate: false,
|
|
|
|
}, {
|
|
|
|
id: '033445b7-8f90-4fc9-beca-71a9e87cb530',
|
|
|
|
level: 8,
|
|
|
|
sizeX: (YEN_SATSU_BASE_SIZE * 2) * 1.25,
|
|
|
|
sizeY: YEN_SATSU_BASE_SIZE * 1.25,
|
|
|
|
shape: 'rectangle',
|
|
|
|
score: 2000,
|
|
|
|
dropCandidate: false,
|
|
|
|
}, {
|
|
|
|
id: '410a09ec-5f7f-46f6-b26f-cbca4ccbd091',
|
|
|
|
level: 7,
|
|
|
|
sizeX: YEN_SATSU_BASE_SIZE * 2,
|
|
|
|
sizeY: YEN_SATSU_BASE_SIZE,
|
|
|
|
shape: 'rectangle',
|
|
|
|
score: 1000,
|
|
|
|
dropCandidate: false,
|
|
|
|
}, {
|
|
|
|
id: '2aae82bc-3fa4-49ad-a6b5-94d888e809f5',
|
|
|
|
level: 6,
|
|
|
|
sizeX: YEN_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
sizeY: YEN_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
shape: 'circle',
|
|
|
|
score: 500,
|
|
|
|
dropCandidate: false,
|
|
|
|
}, {
|
|
|
|
id: 'a619bd67-d08f-4cc0-8c7e-c8072a4950cd',
|
|
|
|
level: 5,
|
|
|
|
sizeX: YEN_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
sizeY: YEN_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
shape: 'circle',
|
|
|
|
score: 100,
|
|
|
|
dropCandidate: true,
|
|
|
|
}, {
|
|
|
|
id: 'c1c5d8e4-17d6-4455-befd-12154d731faa',
|
|
|
|
level: 4,
|
|
|
|
sizeX: YEN_BASE_SIZE * 1.25 * 1.25 * 1.25,
|
|
|
|
sizeY: YEN_BASE_SIZE * 1.25 * 1.25 * 1.25,
|
|
|
|
shape: 'circle',
|
|
|
|
score: 50,
|
|
|
|
dropCandidate: true,
|
|
|
|
}, {
|
|
|
|
id: '7082648c-e428-44c4-887a-25c07a8ebdd5',
|
|
|
|
level: 3,
|
|
|
|
sizeX: YEN_BASE_SIZE * 1.25 * 1.25,
|
|
|
|
sizeY: YEN_BASE_SIZE * 1.25 * 1.25,
|
|
|
|
shape: 'circle',
|
|
|
|
score: 10,
|
|
|
|
dropCandidate: true,
|
|
|
|
}, {
|
|
|
|
id: '0d8d40d5-e6e0-4d26-8a95-b8d842363379',
|
|
|
|
level: 2,
|
|
|
|
sizeX: YEN_BASE_SIZE * 1.25,
|
|
|
|
sizeY: YEN_BASE_SIZE * 1.25,
|
|
|
|
shape: 'circle',
|
|
|
|
score: 5,
|
|
|
|
dropCandidate: true,
|
|
|
|
}, {
|
|
|
|
id: '9dec1b38-d99d-40de-8288-37367b983d0d',
|
|
|
|
level: 1,
|
|
|
|
sizeX: YEN_BASE_SIZE,
|
|
|
|
sizeY: YEN_BASE_SIZE,
|
|
|
|
shape: 'circle',
|
|
|
|
score: 1,
|
|
|
|
dropCandidate: true,
|
|
|
|
}];
|
|
|
|
|
|
|
|
const SQUARE_BASE_SIZE = 28;
|
|
|
|
const SQUARE_MONOS: Mono[] = [{
|
|
|
|
id: 'f75fd0ba-d3d4-40a4-9712-b470e45b0525',
|
|
|
|
level: 10,
|
|
|
|
sizeX: SQUARE_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
sizeY: SQUARE_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
shape: 'rectangle',
|
|
|
|
score: 512,
|
|
|
|
dropCandidate: false,
|
|
|
|
}, {
|
|
|
|
id: '7b70f4af-1c01-45fd-af72-61b1f01e03d1',
|
|
|
|
level: 9,
|
|
|
|
sizeX: SQUARE_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
sizeY: SQUARE_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
shape: 'rectangle',
|
|
|
|
score: 256,
|
|
|
|
dropCandidate: false,
|
|
|
|
}, {
|
|
|
|
id: '41607ef3-b6d6-4829-95b6-3737bf8bb956',
|
|
|
|
level: 8,
|
|
|
|
sizeX: SQUARE_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
sizeY: SQUARE_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
shape: 'rectangle',
|
|
|
|
score: 128,
|
|
|
|
dropCandidate: false,
|
|
|
|
}, {
|
|
|
|
id: '8a8310d2-0374-460f-bb50-ca9cd3ee3416',
|
|
|
|
level: 7,
|
|
|
|
sizeX: SQUARE_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
sizeY: SQUARE_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
shape: 'rectangle',
|
|
|
|
score: 64,
|
|
|
|
dropCandidate: false,
|
|
|
|
}, {
|
|
|
|
id: '1092e069-fe1a-450b-be97-b5d477ec398c',
|
|
|
|
level: 6,
|
|
|
|
sizeX: SQUARE_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
sizeY: SQUARE_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
shape: 'rectangle',
|
|
|
|
score: 32,
|
|
|
|
dropCandidate: false,
|
|
|
|
}, {
|
|
|
|
id: '2294734d-7bb8-4781-bb7b-ef3820abf3d0',
|
|
|
|
level: 5,
|
|
|
|
sizeX: SQUARE_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
sizeY: SQUARE_BASE_SIZE * 1.25 * 1.25 * 1.25 * 1.25,
|
|
|
|
shape: 'rectangle',
|
|
|
|
score: 16,
|
|
|
|
dropCandidate: true,
|
|
|
|
}, {
|
|
|
|
id: 'ea8a61af-e350-45f7-ba6a-366fcd65692a',
|
|
|
|
level: 4,
|
|
|
|
sizeX: SQUARE_BASE_SIZE * 1.25 * 1.25 * 1.25,
|
|
|
|
sizeY: SQUARE_BASE_SIZE * 1.25 * 1.25 * 1.25,
|
|
|
|
shape: 'rectangle',
|
|
|
|
score: 8,
|
|
|
|
dropCandidate: true,
|
|
|
|
}, {
|
|
|
|
id: 'd0c74815-fc1c-4fbe-9953-c92e4b20f919',
|
|
|
|
level: 3,
|
|
|
|
sizeX: SQUARE_BASE_SIZE * 1.25 * 1.25,
|
|
|
|
sizeY: SQUARE_BASE_SIZE * 1.25 * 1.25,
|
|
|
|
shape: 'rectangle',
|
|
|
|
score: 4,
|
|
|
|
dropCandidate: true,
|
|
|
|
}, {
|
|
|
|
id: 'd8fbd70e-611d-402d-87da-1a7fd8cd2c8d',
|
|
|
|
level: 2,
|
|
|
|
sizeX: SQUARE_BASE_SIZE * 1.25,
|
|
|
|
sizeY: SQUARE_BASE_SIZE * 1.25,
|
|
|
|
shape: 'rectangle',
|
|
|
|
score: 2,
|
|
|
|
dropCandidate: true,
|
|
|
|
}, {
|
|
|
|
id: '35e476ee-44bd-4711-ad42-87be245d3efd',
|
|
|
|
level: 1,
|
|
|
|
sizeX: SQUARE_BASE_SIZE,
|
|
|
|
sizeY: SQUARE_BASE_SIZE,
|
|
|
|
shape: 'rectangle',
|
|
|
|
score: 1,
|
|
|
|
dropCandidate: true,
|
|
|
|
}];
|
|
|
|
|
2024-01-08 02:13:20 +00:00
|
|
|
export class DropAndFusionGame extends EventEmitter<{
|
|
|
|
changeScore: (newScore: number) => void;
|
|
|
|
changeCombo: (newCombo: number) => void;
|
|
|
|
changeStock: (newStock: { id: string; mono: Mono }[]) => void;
|
2024-01-09 04:25:33 +00:00
|
|
|
changeHolding: (newHolding: { id: string; mono: Mono } | null) => void;
|
2024-01-11 03:34:03 +00:00
|
|
|
dropped: (x: number) => void;
|
2024-01-13 02:43:13 +00:00
|
|
|
fusioned: (x: number, y: number, nextMono: Mono | null, scoreDelta: number) => void;
|
|
|
|
collision: (energy: number, bodyA: Matter.Body, bodyB: Matter.Body) => void;
|
2024-01-08 02:13:20 +00:00
|
|
|
monoAdded: (mono: Mono) => void;
|
|
|
|
gameOver: () => void;
|
|
|
|
}> {
|
2024-01-09 04:25:33 +00:00
|
|
|
private PHYSICS_QUALITY_FACTOR = 16; // 低いほどパフォーマンスが高いがガタガタして安定しなくなる、逆に高すぎても何故か不安定になる
|
2024-01-10 02:38:49 +00:00
|
|
|
private COMBO_INTERVAL = 60; // frame
|
2024-01-12 06:28:41 +00:00
|
|
|
public readonly GAME_VERSION = 2;
|
2024-01-11 03:34:03 +00:00
|
|
|
public readonly GAME_WIDTH = 450;
|
|
|
|
public readonly GAME_HEIGHT = 600;
|
2024-01-08 02:13:20 +00:00
|
|
|
public readonly DROP_INTERVAL = 500;
|
|
|
|
public readonly PLAYAREA_MARGIN = 25;
|
|
|
|
private STOCK_MAX = 4;
|
2024-01-09 12:15:56 +00:00
|
|
|
private TICK_DELTA = 1000 / 60; // 60fps
|
2024-01-11 09:13:39 +00:00
|
|
|
|
2024-01-11 03:34:03 +00:00
|
|
|
public frame = 0;
|
|
|
|
public engine: Matter.Engine;
|
2024-01-09 12:15:56 +00:00
|
|
|
private tickCallbackQueue: { frame: number; callback: () => void; }[] = [];
|
2024-01-08 02:13:20 +00:00
|
|
|
private overflowCollider: Matter.Body;
|
|
|
|
private isGameOver = false;
|
2024-01-13 02:43:13 +00:00
|
|
|
private gameMode: 'normal' | 'yen' | 'square';
|
2024-01-09 12:15:56 +00:00
|
|
|
private rng: () => number;
|
|
|
|
private logs: Log[] = [];
|
|
|
|
private replaying = false;
|
2024-01-08 02:13:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* フィールドに出ていて、かつ合体の対象となるアイテム
|
|
|
|
*/
|
2024-01-12 05:48:44 +00:00
|
|
|
private fusionReadyBodyIds: Matter.Body['id'][] = [];
|
|
|
|
|
|
|
|
private gameOverReadyBodyIds: Matter.Body['id'][] = [];
|
2024-01-08 02:13:20 +00:00
|
|
|
|
2024-01-11 03:34:03 +00:00
|
|
|
/**
|
|
|
|
* fusion予約アイテムのペア
|
|
|
|
* TODO: これらのモノは光らせるなどの演出をすると視覚的に楽しそう
|
|
|
|
*/
|
|
|
|
private fusionReservedPairs: { bodyA: Matter.Body; bodyB: Matter.Body }[] = [];
|
|
|
|
|
2024-01-08 02:13:20 +00:00
|
|
|
private latestDroppedAt = 0;
|
2024-01-10 02:38:49 +00:00
|
|
|
private latestFusionedAt = 0; // frame
|
2024-01-08 02:13:20 +00:00
|
|
|
private stock: { id: string; mono: Mono }[] = [];
|
2024-01-09 04:25:33 +00:00
|
|
|
private holding: { id: string; mono: Mono } | null = null;
|
2024-01-08 02:13:20 +00:00
|
|
|
|
2024-01-13 02:43:13 +00:00
|
|
|
private get monoDefinitions() {
|
|
|
|
switch (this.gameMode) {
|
|
|
|
case 'normal':
|
|
|
|
return NORAML_MONOS;
|
|
|
|
case 'yen':
|
|
|
|
return YEN_MONOS;
|
|
|
|
case 'square':
|
|
|
|
return SQUARE_MONOS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 02:13:20 +00:00
|
|
|
private _combo = 0;
|
|
|
|
private get combo() {
|
|
|
|
return this._combo;
|
|
|
|
}
|
|
|
|
private set combo(value: number) {
|
|
|
|
this._combo = value;
|
|
|
|
this.emit('changeCombo', value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private _score = 0;
|
|
|
|
private get score() {
|
|
|
|
return this._score;
|
|
|
|
}
|
|
|
|
private set score(value: number) {
|
|
|
|
this._score = value;
|
|
|
|
this.emit('changeScore', value);
|
|
|
|
}
|
|
|
|
|
2024-01-13 02:43:13 +00:00
|
|
|
private getMonoRenderOptions: null | ((mono: Mono) => Partial<Matter.IBodyRenderOptions>) = null;
|
|
|
|
|
2024-01-10 02:38:49 +00:00
|
|
|
public replayPlaybackRate = 1;
|
|
|
|
|
2024-01-12 05:48:44 +00:00
|
|
|
constructor(env: {
|
|
|
|
seed: string;
|
2024-01-13 02:43:13 +00:00
|
|
|
gameMode: DropAndFusionGame['gameMode'];
|
2024-01-12 05:48:44 +00:00
|
|
|
replaying?: boolean;
|
2024-01-13 02:43:13 +00:00
|
|
|
getMonoRenderOptions?: (mono: Mono) => Partial<Matter.IBodyRenderOptions>;
|
2024-01-12 05:48:44 +00:00
|
|
|
}) {
|
2024-01-08 02:13:20 +00:00
|
|
|
super();
|
|
|
|
|
2024-01-13 02:43:13 +00:00
|
|
|
//#region BIND
|
|
|
|
this.tick = this.tick.bind(this);
|
|
|
|
//#endregion
|
|
|
|
|
2024-01-11 03:34:03 +00:00
|
|
|
this.replaying = !!env.replaying;
|
2024-01-13 02:43:13 +00:00
|
|
|
this.gameMode = env.gameMode;
|
|
|
|
this.getMonoRenderOptions = env.getMonoRenderOptions ?? null;
|
2024-01-11 03:34:03 +00:00
|
|
|
this.rng = seedrandom(env.seed);
|
2024-01-09 12:15:56 +00:00
|
|
|
|
2024-01-08 02:13:20 +00:00
|
|
|
this.engine = Matter.Engine.create({
|
2024-01-09 04:25:33 +00:00
|
|
|
constraintIterations: 2 * this.PHYSICS_QUALITY_FACTOR,
|
|
|
|
positionIterations: 6 * this.PHYSICS_QUALITY_FACTOR,
|
|
|
|
velocityIterations: 4 * this.PHYSICS_QUALITY_FACTOR,
|
2024-01-08 02:13:20 +00:00
|
|
|
gravity: {
|
|
|
|
x: 0,
|
|
|
|
y: 1,
|
|
|
|
},
|
|
|
|
timing: {
|
|
|
|
timeScale: 2,
|
|
|
|
},
|
|
|
|
enableSleeping: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.engine.world.bodies = [];
|
|
|
|
|
|
|
|
//#region walls
|
|
|
|
const WALL_OPTIONS: Matter.IChamferableBodyDefinition = {
|
2024-01-10 04:44:00 +00:00
|
|
|
label: '_wall_',
|
2024-01-08 02:13:20 +00:00
|
|
|
isStatic: true,
|
|
|
|
friction: 0.7,
|
|
|
|
slop: 1.0,
|
|
|
|
render: {
|
|
|
|
strokeStyle: 'transparent',
|
|
|
|
fillStyle: 'transparent',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const thickness = 100;
|
|
|
|
Matter.Composite.add(this.engine.world, [
|
2024-01-11 03:34:03 +00:00
|
|
|
Matter.Bodies.rectangle(this.GAME_WIDTH / 2, this.GAME_HEIGHT + (thickness / 2) - this.PLAYAREA_MARGIN, this.GAME_WIDTH, thickness, WALL_OPTIONS),
|
|
|
|
Matter.Bodies.rectangle(this.GAME_WIDTH + (thickness / 2) - this.PLAYAREA_MARGIN, this.GAME_HEIGHT / 2, thickness, this.GAME_HEIGHT, WALL_OPTIONS),
|
|
|
|
Matter.Bodies.rectangle(-((thickness / 2) - this.PLAYAREA_MARGIN), this.GAME_HEIGHT / 2, thickness, this.GAME_HEIGHT, WALL_OPTIONS),
|
2024-01-08 02:13:20 +00:00
|
|
|
]);
|
|
|
|
//#endregion
|
|
|
|
|
2024-01-11 03:34:03 +00:00
|
|
|
this.overflowCollider = Matter.Bodies.rectangle(this.GAME_WIDTH / 2, 0, this.GAME_WIDTH, 200, {
|
2024-01-12 05:48:44 +00:00
|
|
|
label: '_overflow_',
|
2024-01-08 02:13:20 +00:00
|
|
|
isStatic: true,
|
|
|
|
isSensor: true,
|
|
|
|
render: {
|
|
|
|
strokeStyle: 'transparent',
|
|
|
|
fillStyle: 'transparent',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
Matter.Composite.add(this.engine.world, this.overflowCollider);
|
|
|
|
}
|
|
|
|
|
2024-01-11 09:13:39 +00:00
|
|
|
private msToFrame(ms: number) {
|
|
|
|
return Math.round(ms / this.TICK_DELTA);
|
|
|
|
}
|
|
|
|
|
2024-01-08 02:13:20 +00:00
|
|
|
private createBody(mono: Mono, x: number, y: number) {
|
|
|
|
const options: Matter.IBodyDefinition = {
|
|
|
|
label: mono.id,
|
|
|
|
//density: 0.0005,
|
2024-01-12 05:48:44 +00:00
|
|
|
density: ((mono.sizeX + mono.sizeY) / 2) / 1000,
|
2024-01-08 02:13:20 +00:00
|
|
|
restitution: 0.2,
|
|
|
|
frictionAir: 0.01,
|
|
|
|
friction: 0.7,
|
|
|
|
frictionStatic: 5,
|
|
|
|
slop: 1.0,
|
|
|
|
//mass: 0,
|
2024-01-13 02:43:13 +00:00
|
|
|
render: this.getMonoRenderOptions ? this.getMonoRenderOptions(mono) : undefined,
|
2024-01-08 02:13:20 +00:00
|
|
|
};
|
|
|
|
if (mono.shape === 'circle') {
|
2024-01-12 05:48:44 +00:00
|
|
|
return Matter.Bodies.circle(x, y, mono.sizeX / 2, options);
|
2024-01-09 04:25:33 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
2024-01-08 02:13:20 +00:00
|
|
|
} else if (mono.shape === 'rectangle') {
|
2024-01-12 05:48:44 +00:00
|
|
|
return Matter.Bodies.rectangle(x, y, mono.sizeX, mono.sizeY, options);
|
2024-01-08 02:13:20 +00:00
|
|
|
} else {
|
|
|
|
throw new Error('unrecognized shape');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fusion(bodyA: Matter.Body, bodyB: Matter.Body) {
|
2024-01-10 02:38:49 +00:00
|
|
|
if (this.latestFusionedAt > this.frame - this.COMBO_INTERVAL) {
|
2024-01-08 02:13:20 +00:00
|
|
|
this.combo++;
|
|
|
|
} else {
|
|
|
|
this.combo = 1;
|
|
|
|
}
|
2024-01-10 02:38:49 +00:00
|
|
|
this.latestFusionedAt = this.frame;
|
2024-01-08 02:13:20 +00:00
|
|
|
|
2024-01-08 03:46:20 +00:00
|
|
|
// TODO: 単に位置だけでなくそれぞれの動きベクトルも融合する?
|
2024-01-08 02:13:20 +00:00
|
|
|
const newX = (bodyA.position.x + bodyB.position.x) / 2;
|
|
|
|
const newY = (bodyA.position.y + bodyB.position.y) / 2;
|
|
|
|
|
2024-01-12 05:48:44 +00:00
|
|
|
this.fusionReadyBodyIds = this.fusionReadyBodyIds.filter(x => x !== bodyA.id && x !== bodyB.id);
|
|
|
|
this.gameOverReadyBodyIds = this.gameOverReadyBodyIds.filter(x => x !== bodyA.id && x !== bodyB.id);
|
2024-01-08 02:13:20 +00:00
|
|
|
Matter.Composite.remove(this.engine.world, [bodyA, bodyB]);
|
|
|
|
|
|
|
|
const currentMono = this.monoDefinitions.find(y => y.id === bodyA.label)!;
|
2024-01-13 02:43:13 +00:00
|
|
|
const nextMono = this.monoDefinitions.find(x => x.level === currentMono.level + 1) ?? null;
|
2024-01-08 02:13:20 +00:00
|
|
|
|
|
|
|
if (nextMono) {
|
|
|
|
const body = this.createBody(nextMono, newX, newY);
|
|
|
|
Matter.Composite.add(this.engine.world, body);
|
|
|
|
|
|
|
|
// 連鎖してfusionした場合の分かりやすさのため少し間を置いてからfusion対象になるようにする
|
2024-01-09 12:15:56 +00:00
|
|
|
this.tickCallbackQueue.push({
|
2024-01-11 09:13:39 +00:00
|
|
|
frame: this.frame + this.msToFrame(100),
|
2024-01-09 12:15:56 +00:00
|
|
|
callback: () => {
|
2024-01-12 05:48:44 +00:00
|
|
|
this.fusionReadyBodyIds.push(body.id);
|
2024-01-09 12:15:56 +00:00
|
|
|
},
|
|
|
|
});
|
2024-01-08 02:13:20 +00:00
|
|
|
|
|
|
|
this.emit('monoAdded', nextMono);
|
2024-01-11 03:34:03 +00:00
|
|
|
}
|
2024-01-13 02:43:13 +00:00
|
|
|
|
|
|
|
const comboBonus = this.gameMode === 'yen' ? 1 : 1 + ((this.combo - 1) / 5);
|
|
|
|
const additionalScore = Math.round(currentMono.score * comboBonus);
|
|
|
|
this.score += additionalScore;
|
|
|
|
|
|
|
|
this.emit('fusioned', newX, newY, nextMono, additionalScore);
|
2024-01-11 03:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private onCollision(event: Matter.IEventCollision<Matter.Engine>) {
|
|
|
|
for (const pairs of event.pairs) {
|
|
|
|
const { bodyA, bodyB } = pairs;
|
|
|
|
|
|
|
|
const shouldFusion = (bodyA.label === bodyB.label) &&
|
|
|
|
!this.fusionReservedPairs.some(x =>
|
|
|
|
x.bodyA.id === bodyA.id ||
|
|
|
|
x.bodyA.id === bodyB.id ||
|
|
|
|
x.bodyB.id === bodyA.id ||
|
|
|
|
x.bodyB.id === bodyB.id);
|
|
|
|
|
|
|
|
if (shouldFusion) {
|
2024-01-12 05:48:44 +00:00
|
|
|
if (this.fusionReadyBodyIds.includes(bodyA.id) && this.fusionReadyBodyIds.includes(bodyB.id)) {
|
2024-01-11 03:34:03 +00:00
|
|
|
this.fusion(bodyA, bodyB);
|
|
|
|
} else {
|
|
|
|
this.fusionReservedPairs.push({ bodyA, bodyB });
|
|
|
|
this.tickCallbackQueue.push({
|
2024-01-11 09:13:39 +00:00
|
|
|
frame: this.frame + this.msToFrame(100),
|
2024-01-11 03:34:03 +00:00
|
|
|
callback: () => {
|
|
|
|
this.fusionReservedPairs = this.fusionReservedPairs.filter(x => x.bodyA.id !== bodyA.id && x.bodyB.id !== bodyB.id);
|
|
|
|
this.fusion(bodyA, bodyB);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2024-01-13 02:43:13 +00:00
|
|
|
const energy = pairs.collision.depth;
|
|
|
|
|
2024-01-12 05:48:44 +00:00
|
|
|
if (bodyA.label === '_overflow_' || bodyB.label === '_overflow_') continue;
|
|
|
|
|
|
|
|
if (bodyA.label !== '_wall_' && bodyB.label !== '_wall_') {
|
|
|
|
if (!this.gameOverReadyBodyIds.includes(bodyA.id)) this.gameOverReadyBodyIds.push(bodyA.id);
|
|
|
|
if (!this.gameOverReadyBodyIds.includes(bodyB.id)) this.gameOverReadyBodyIds.push(bodyB.id);
|
|
|
|
}
|
|
|
|
|
2024-01-13 02:43:13 +00:00
|
|
|
this.emit('collision', energy, bodyA, bodyB);
|
2024-01-11 03:34:03 +00:00
|
|
|
}
|
2024-01-08 02:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-12 05:48:44 +00:00
|
|
|
private onCollisionActive(event: Matter.IEventCollision<Matter.Engine>) {
|
|
|
|
for (const pairs of event.pairs) {
|
|
|
|
const { bodyA, bodyB } = pairs;
|
|
|
|
|
|
|
|
// ハコからあふれたかどうかの判定
|
|
|
|
if (bodyA.id === this.overflowCollider.id || bodyB.id === this.overflowCollider.id) {
|
|
|
|
if (this.gameOverReadyBodyIds.includes(bodyA.id) || this.gameOverReadyBodyIds.includes(bodyB.id)) {
|
|
|
|
this.gameOver();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 12:15:56 +00:00
|
|
|
public surrender() {
|
|
|
|
this.logs.push({
|
|
|
|
frame: this.frame,
|
|
|
|
operation: 'surrender',
|
|
|
|
});
|
|
|
|
|
|
|
|
this.gameOver();
|
|
|
|
}
|
|
|
|
|
2024-01-08 02:13:20 +00:00
|
|
|
private gameOver() {
|
|
|
|
this.isGameOver = true;
|
|
|
|
this.emit('gameOver');
|
|
|
|
}
|
|
|
|
|
2024-01-11 03:34:03 +00:00
|
|
|
public start() {
|
2024-01-08 02:13:20 +00:00
|
|
|
for (let i = 0; i < this.STOCK_MAX; i++) {
|
|
|
|
this.stock.push({
|
2024-01-09 12:15:56 +00:00
|
|
|
id: this.rng().toString(),
|
|
|
|
mono: this.monoDefinitions.filter(x => x.dropCandidate)[Math.floor(this.rng() * this.monoDefinitions.filter(x => x.dropCandidate).length)],
|
2024-01-08 02:13:20 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
this.emit('changeStock', this.stock);
|
|
|
|
|
2024-01-11 03:34:03 +00:00
|
|
|
Matter.Events.on(this.engine, 'collisionStart', this.onCollision.bind(this));
|
2024-01-12 05:48:44 +00:00
|
|
|
Matter.Events.on(this.engine, 'collisionActive', this.onCollisionActive.bind(this));
|
2024-01-09 12:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public getLogs() {
|
|
|
|
return this.logs;
|
|
|
|
}
|
|
|
|
|
2024-01-11 03:34:03 +00:00
|
|
|
public tick() {
|
2024-01-09 12:15:56 +00:00
|
|
|
this.frame++;
|
2024-01-11 03:34:03 +00:00
|
|
|
|
2024-01-10 02:38:49 +00:00
|
|
|
if (this.latestFusionedAt < this.frame - this.COMBO_INTERVAL) {
|
|
|
|
this.combo = 0;
|
|
|
|
}
|
2024-01-11 03:34:03 +00:00
|
|
|
|
2024-01-09 12:15:56 +00:00
|
|
|
this.tickCallbackQueue = this.tickCallbackQueue.filter(x => {
|
|
|
|
if (x.frame === this.frame) {
|
|
|
|
x.callback();
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2024-01-08 02:13:20 +00:00
|
|
|
|
2024-01-11 03:34:03 +00:00
|
|
|
Matter.Engine.update(this.engine, this.TICK_DELTA);
|
2024-01-09 04:25:33 +00:00
|
|
|
|
2024-01-11 03:34:03 +00:00
|
|
|
const hasNextTick = !this.isGameOver;
|
2024-01-08 02:13:20 +00:00
|
|
|
|
2024-01-11 03:34:03 +00:00
|
|
|
return hasNextTick;
|
2024-01-08 02:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public getActiveMonos() {
|
|
|
|
return this.engine.world.bodies.map(x => this.monoDefinitions.find((mono) => mono.id === x.label)!).filter(x => x !== undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
public drop(_x: number) {
|
|
|
|
if (this.isGameOver) return;
|
2024-01-11 03:34:03 +00:00
|
|
|
// TODO: フレームで計算するようにすればリプレイかどうかのチェックは不要になる
|
2024-01-09 12:15:56 +00:00
|
|
|
if (!this.replaying && (Date.now() - this.latestDroppedAt < this.DROP_INTERVAL)) return;
|
2024-01-09 07:06:22 +00:00
|
|
|
|
2024-01-09 04:25:33 +00:00
|
|
|
const head = this.stock.shift()!;
|
2024-01-08 02:13:20 +00:00
|
|
|
this.stock.push({
|
2024-01-09 12:15:56 +00:00
|
|
|
id: this.rng().toString(),
|
|
|
|
mono: this.monoDefinitions.filter(x => x.dropCandidate)[Math.floor(this.rng() * this.monoDefinitions.filter(x => x.dropCandidate).length)],
|
2024-01-08 02:13:20 +00:00
|
|
|
});
|
|
|
|
this.emit('changeStock', this.stock);
|
|
|
|
|
2024-01-09 22:26:16 +00:00
|
|
|
const inputX = Math.round(_x);
|
2024-01-12 05:48:44 +00:00
|
|
|
const x = Math.min(this.GAME_WIDTH - this.PLAYAREA_MARGIN - (head.mono.sizeX / 2), Math.max(this.PLAYAREA_MARGIN + (head.mono.sizeX / 2), inputX));
|
|
|
|
const body = this.createBody(head.mono, x, 50 + head.mono.sizeY / 2);
|
2024-01-09 12:15:56 +00:00
|
|
|
this.logs.push({
|
|
|
|
frame: this.frame,
|
|
|
|
operation: 'drop',
|
2024-01-09 22:26:16 +00:00
|
|
|
x: inputX,
|
2024-01-09 12:15:56 +00:00
|
|
|
});
|
2024-01-08 02:13:20 +00:00
|
|
|
Matter.Composite.add(this.engine.world, body);
|
2024-01-12 05:48:44 +00:00
|
|
|
|
|
|
|
this.fusionReadyBodyIds.push(body.id);
|
2024-01-08 02:13:20 +00:00
|
|
|
this.latestDroppedAt = Date.now();
|
2024-01-12 05:48:44 +00:00
|
|
|
|
2024-01-11 03:34:03 +00:00
|
|
|
this.emit('dropped', x);
|
2024-01-09 04:25:33 +00:00
|
|
|
this.emit('monoAdded', head.mono);
|
|
|
|
}
|
|
|
|
|
|
|
|
public hold() {
|
|
|
|
if (this.isGameOver) return;
|
|
|
|
|
2024-01-09 12:15:56 +00:00
|
|
|
this.logs.push({
|
|
|
|
frame: this.frame,
|
|
|
|
operation: 'hold',
|
|
|
|
});
|
|
|
|
|
2024-01-09 04:25:33 +00:00
|
|
|
if (this.holding) {
|
|
|
|
const head = this.stock.shift()!;
|
|
|
|
this.stock.unshift(this.holding);
|
|
|
|
this.holding = head;
|
|
|
|
this.emit('changeHolding', this.holding);
|
|
|
|
this.emit('changeStock', this.stock);
|
|
|
|
} else {
|
|
|
|
const head = this.stock.shift()!;
|
|
|
|
this.holding = head;
|
|
|
|
this.stock.push({
|
2024-01-09 12:15:56 +00:00
|
|
|
id: this.rng().toString(),
|
|
|
|
mono: this.monoDefinitions.filter(x => x.dropCandidate)[Math.floor(this.rng() * this.monoDefinitions.filter(x => x.dropCandidate).length)],
|
2024-01-09 04:25:33 +00:00
|
|
|
});
|
|
|
|
this.emit('changeHolding', this.holding);
|
|
|
|
this.emit('changeStock', this.stock);
|
|
|
|
}
|
2024-01-08 02:13:20 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 09:13:39 +00:00
|
|
|
public static serializeLogs(logs: Log[]) {
|
|
|
|
const _logs: number[][] = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < logs.length; i++) {
|
|
|
|
const log = logs[i];
|
|
|
|
const frameDelta = i === 0 ? log.frame : log.frame - logs[i - 1].frame;
|
|
|
|
|
|
|
|
switch (log.operation) {
|
|
|
|
case 'drop':
|
|
|
|
_logs.push([frameDelta, 0, log.x]);
|
|
|
|
break;
|
|
|
|
case 'hold':
|
|
|
|
_logs.push([frameDelta, 1]);
|
|
|
|
break;
|
|
|
|
case 'surrender':
|
|
|
|
_logs.push([frameDelta, 2]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _logs;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static deserializeLogs(logs: number[][]) {
|
|
|
|
const _logs: Log[] = [];
|
|
|
|
|
|
|
|
let frame = 0;
|
|
|
|
|
|
|
|
for (const log of logs) {
|
|
|
|
const frameDelta = log[0];
|
|
|
|
frame += frameDelta;
|
|
|
|
|
|
|
|
const operation = log[1];
|
|
|
|
|
|
|
|
switch (operation) {
|
|
|
|
case 0:
|
|
|
|
_logs.push({
|
|
|
|
frame,
|
|
|
|
operation: 'drop',
|
|
|
|
x: log[2],
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
_logs.push({
|
|
|
|
frame,
|
|
|
|
operation: 'hold',
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
_logs.push({
|
|
|
|
frame,
|
|
|
|
operation: 'surrender',
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _logs;
|
|
|
|
}
|
|
|
|
|
2024-01-08 02:13:20 +00:00
|
|
|
public dispose() {
|
|
|
|
Matter.World.clear(this.engine.world, false);
|
|
|
|
Matter.Engine.clear(this.engine);
|
|
|
|
}
|
|
|
|
}
|