Skip to content

Commit 4251e70

Browse files
committed
りふぁくたりんぐとか
1 parent da40968 commit 4251e70

File tree

7 files changed

+70
-74
lines changed

7 files changed

+70
-74
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Script/
1+
Script/
2+
node_modules/

Data/Stage/format.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export type Board = {//ここにパズルの初期情報を入れてください。
2+
"size":{
3+
"Height" : number,
4+
"Width" : number
5+
},
6+
"target":{
7+
"hand":number,
8+
"score":number,
9+
"obj" : {
10+
[type:number] : number//個数
11+
},
12+
"field" : {
13+
[type:number] : number // 個数
14+
}
15+
},
16+
"board":{
17+
"obj": [number,number][][], // 左が属性、右が耐久値
18+
"field":[number,number][][]
19+
}
20+
};

Script_ts/dataSaver.js

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,21 @@
11
"use strict";
22
export default class dataSaver {
33
//class dataSaver {
4-
ref;data;
4+
#ref;data;
55
/**
66
* dataSaverクラスのインスタンスを生成。保存先がnullなら空のJSONを保存する。
77
* @constructor
88
* @this {dataSaver}
99
* @param {string} ref 保存先
1010
*/
1111
constructor(ref) {
12-
this.ref = ref;
12+
this.#ref = ref;
1313
//init
1414
if (localStorage.getItem(ref) == null) localStorage.setItem(ref, "{}");
1515
this.data = JSON.parse(localStorage.getItem(ref));
1616
}
1717
/**
18-
* データを読み取る
19-
* @param {(string | number)[]} path
20-
* @returns {any} 読み取った値
18+
* データを保存する
2119
*/
22-
getData(path) {
23-
return path.reduce((prev, curr) => prev?.[curr], this.data);
24-
}
25-
/**
26-
* データを保存します。パスがぞんざいしなかったらエラー出す
27-
* @param {(string | number)[]} path
28-
* @param {any} data 保存したいデータ
29-
* @returns {bool} 書き込みに成功したか
30-
*/
31-
setData(path, data) {
32-
if (path.length == 0) throw new Error("Can't save at root");
33-
const UNTIL_OBJ = this.getData(path.slice(0, -1));
34-
console.info(UNTIL_OBJ);
35-
const PATH_LAST = path.at(-1);
36-
UNTIL_OBJ[PATH_LAST] = data;
37-
localStorage.setItem(this.ref, JSON.stringify(this.data));
38-
return true;
39-
}
40-
/**
41-
* データが存在しない場合のみそれを保存します。
42-
* @param {(string | number)[]} path
43-
* @param {any} data 保存したいデータ
44-
* @returns {bool} 書き込みに成功したか
45-
*/
46-
initData(path, data) {
47-
if (path.length == 0) throw new Error("Can't save at root");
48-
const UNTIL_OBJ = this.getData(path.slice(0, -1));
49-
console.info(UNTIL_OBJ);
50-
const PATH_LAST = path.at(-1);
51-
if (UNTIL_OBJ[PATH_LAST] !== void 0) return false;
52-
UNTIL_OBJ[PATH_LAST] = data;
53-
localStorage.setItem(this.ref, JSON.stringify(this.data));
54-
return true;
55-
}
20+
save() {localStorage.setItem(this.#ref, JSON.stringify(this.data));}
5621
}

Script_ts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
},
88
"keywords": [],
99
"author": "",
10-
"license": "ISC",
10+
"license": "MIT",
1111
"description": ""
1212
}

Script_ts/puzzle.ts

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,29 @@ interface point {
44
y: number;
55
}
66
/**@todo Dateオブジェクトなど、一部のオブジェクトがコピーできないのでその対策。 */
7-
const object_copy = (obj: any) => JSON.parse(JSON.stringify(obj));
7+
const object_copy = (obj: unknown) => JSON.parse(JSON.stringify(obj));
88

99
/**
1010
* Array.someのobject版です。
1111
*/
12-
const objectSome = (obj : any, func : any) => Object.keys(obj).some(x => func(obj[x]));
12+
const objectSome = <T>(obj : Record<string,T>, func : (x:T) => boolean) => Object.keys(obj).some(x => func(obj[x]));
13+
const enum ENUM_STATUS {
14+
"CHAINING",
15+
"IDLE",
16+
"ANIMATION"
17+
};
18+
import type {Board} from "../Data/Stage/format.ts";
1319
export default class scenePuzzle{
1420
// SECTOR_1:定数群
15-
scenes : any = {};
16-
constructor(scenes : any){
21+
scenes : {start?:any} = {start:undefined};
22+
constructor(scenes : {start?:any}){
1723
this.scenes = scenes;
1824
}
1925
SAVER = new dataSaver("savedata");
2026
ORB_COLORS = 5 as const;
2127
BASE_SCORE = 100 as const;
2228
SCORE_EXPONENT = 1.5 as const;
23-
ANIM_SPEED = 100 as const;
29+
ANIM_SPEED = 200 as const;
2430
SHORTEST_CHAIN = 3 as const;
2531
CONTINUE_BONUS = 5 as const;
2632
CONTINUE_COST = 100 as const;
@@ -31,14 +37,13 @@ export default class scenePuzzle{
3137
ALT_ORB = "□🔴🔵🟢🟡🟣" as const;
3238
ALT_OBJECT = "□🧱🌸" as const;
3339
ALT_FIELD = "□🥬" as const;
34-
ENUM_STATUS = Object.freeze({ CHAINING: 0, IDLE: 1, ANIMATION: 2 });
3540
// SECTOR_2:変数群
36-
game_state: number = this.ENUM_STATUS.CHAINING;
41+
game_state: number = ENUM_STATUS.CHAINING;
3742
chain_color: number = -1;
3843
chain_yx: point[] = []; //[i].(x | y)
3944
//SECTOR_2.5:準const変数群
4045
PUZ_BOARD_BONE: HTMLElement[][] = [];
41-
DATA: {[x:string] : any} = {};
46+
DATA: Board = {} as Board;
4247
STAGE_ID: number = -1;
4348
// SECTOR 3 : 関数群
4449
endscene(err: Error | null = null, wasSuccess = false){
@@ -58,12 +63,12 @@ export default class scenePuzzle{
5863
return true;
5964
};
6065
addScore(score_mult: number, base = this.BASE_SCORE){this.DATA.target.score += Math.floor(base * score_mult)};
61-
getType(obj: any[]){return obj[0]};
62-
getCellPos(cell: {target : any}){return [cell.target.parentNode.rowIndex, cell.target.cellIndex]};
63-
isnullobj(obj: any[]){return this.getType(obj) === 0;}
64-
fallable(obj: any[]){return this.getType(obj) > 0 || [-2].includes(this.getType(obj));}
65-
is_adj_break(obj: any[]){return [-2].includes(this.getType(obj));}
66-
dest_sync(field: any[]){return [1].includes(this.getType(field))};
66+
getType(obj: number[]){return obj[0]};
67+
getCellPos(cell: {target : any}):[number,number] {return [cell.target.parentNode.rowIndex, cell.target.cellIndex]};
68+
isnullobj(obj: number[]){return this.getType(obj) === 0;}
69+
fallable(obj: number[]){return this.getType(obj) > 0 || [-2].includes(this.getType(obj));}
70+
is_adj_break(obj: number[]){return [-2].includes(this.getType(obj));}
71+
dest_sync(field: number[]){return [1].includes(this.getType(field))};
6772
alt_text(type: number, isobj: boolean){return isobj ? (type < 0 ? this.ALT_OBJECT[-type] : this.ALT_ORB[type]) : this.ALT_FIELD[type];}
6873
get_img(type: number, isobj: boolean){return `<img src="Pictures/${
6974
isobj ? "Orbs" : "Fields"
@@ -97,7 +102,7 @@ export default class scenePuzzle{
97102
.map(type => this.get_img(type, false) + "x" + String(this.DATA.target.field[type]))
98103
.toString());
99104

100-
obj_erase(obj: any[]){[obj[0], obj[1]] = [0, 0]};
105+
obj_erase(obj: unknown[]){[obj[0], obj[1]] = [0, 0]};
101106
break_obj(y: number, x: number, ischain: boolean, isobj = true){
102107
const TARGET = isobj ? this.DATA.board.obj[y][x] : this.DATA.board.field[y][x];
103108
const OBJTYPE = this.getType(TARGET);
@@ -113,14 +118,14 @@ export default class scenePuzzle{
113118
isobj && this.dest_sync(this.DATA.board.field[y][x]) && this.break_obj(y, x, false, false);
114119
}
115120
};
116-
fall_obj<T>(obj_from: T[], obj_to: T[]){
121+
fall_obj(obj_from: number[], obj_to: number[]){
117122
if (!this.isnullobj(obj_to) || !this.fallable(obj_from)) return false;
118123
[obj_to[0], obj_to[1]] = obj_from;
119124
this.obj_erase(obj_from);
120125
return true;
121126
};
122127
falling_orb(){
123-
this.game_state = this.ENUM_STATUS.ANIMATION;
128+
this.game_state = ENUM_STATUS.ANIMATION;
124129
const FALL_TIMER = setInterval(() => {
125130
let refall = false;
126131
for (let i = this.DATA.size.Height - 1; i > 0; i--) /*性質上、下から探索したほうがいい*/ {
@@ -131,18 +136,18 @@ export default class scenePuzzle{
131136
for (let j = 0; j < this.DATA.size.Width - 1; j++)
132137
refall = this.fall_obj(this.DATA.board.obj[i - 1][j], this.DATA.board.obj[i][j + 1]) || refall; //R-shift
133138
}
134-
this.DATA.board.obj[0] = this.DATA.board.obj[0].map((x: any[]) =>
139+
this.DATA.board.obj[0] = this.DATA.board.obj[0].map((x: [number,number]) =>
135140
this.isnullobj(x) ? ((refall = true), [~~(Math.random() * this.ORB_COLORS) + 1, 1]) : x
136141
);
137142
if (!refall) {
138143
clearInterval(FALL_TIMER);
139-
this.game_state = this.ENUM_STATUS.IDLE;
144+
this.game_state = ENUM_STATUS.IDLE;
140145
}
141146
this.update_display();
142147
}, this.ANIM_SPEED);
143148
};
144149
chain_connect(cell : {target : any}){
145-
if (this.game_state !== this.ENUM_STATUS.CHAINING) return;
150+
if (this.game_state !== ENUM_STATUS.CHAINING) return;
146151
const [CELL_Y, CELL_X] = this.getCellPos(cell);
147152
const CELL_COLOR = this.getType(this.DATA.board.obj[CELL_Y][CELL_X]);
148153
const CHAIN_LAST = this.chain_yx.at(-1) as point;
@@ -175,9 +180,10 @@ export default class scenePuzzle{
175180
);
176181
this.update_display();
177182
this.falling_orb();
183+
}else{
184+
this.game_state = ENUM_STATUS.IDLE;
178185
}
179186
if (this.gameClear()) return;
180-
this.game_state = this.ENUM_STATUS.IDLE;
181187
this.chain_yx.forEach(pos => {
182188
const target = this.PUZ_BOARD_BONE[pos.y][pos.x].querySelector("img")!;
183189
target.classList.remove("chaining");
@@ -186,34 +192,36 @@ export default class scenePuzzle{
186192
this.chain_yx = [];
187193
if (this.DATA.target.hand <= 0) {
188194
if (
189-
this.SAVER.getData(["haveCoin"]) >= this.CONTINUE_COST &&
195+
this.SAVER.data.haveCoin >= this.CONTINUE_COST &&
190196
confirm(`コンテニューしますか?(手数+${this.CONTINUE_BONUS})`)
191197
) {
192198
this.DATA.target.hand += this.CONTINUE_BONUS;
193-
this.SAVER.setData(["haveCoin"],this.SAVER.getData(["haveCoin"])-this.CONTINUE_COST);
199+
this.SAVER.data.haveCoin -= this.CONTINUE_COST;
200+
this.SAVER.save();
194201
return;
195202
}
196203
alert(`ゲームオーバー! スコアは${this.DATA.target.score}でした!`);
197204
this.endscene(null, false);
198205
}
199206
};
200207
chain_start(cell : {target : any}){
208+
if(this.game_state !== ENUM_STATUS.IDLE)return;
201209
const [CELL_Y, CELL_X] = this.getCellPos(cell);
202210
const CELL_COLOR = this.getType(this.DATA.board.obj[CELL_Y][CELL_X]);
203211
if (CELL_COLOR <= 0) return;
204-
this.game_state = this.ENUM_STATUS.CHAINING;
212+
this.game_state = ENUM_STATUS.CHAINING;
205213
this.chain_color = CELL_COLOR;
206214
this.chain_yx.push({ x: CELL_X, y: CELL_Y });
207215
cell.target.querySelector("img").classList.add("chaining");
208216
};
209217
chain_toggler(cell: any){
210218
switch (this.game_state) {
211-
case this.ENUM_STATUS.ANIMATION:
219+
case ENUM_STATUS.ANIMATION:
212220
return;
213-
case this.ENUM_STATUS.CHAINING:
221+
case ENUM_STATUS.CHAINING:
214222
this.chain_over();
215223
break;
216-
case this.ENUM_STATUS.IDLE:
224+
case ENUM_STATUS.IDLE:
217225
this.chain_start(cell);
218226
break;
219227
default:

Script_ts/start.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ export default class startScene{
55
scenes : any;
66
constructor(scene : any){
77
this.scenes=scene;
8-
this.Saver.initData(["maxstage"],1);
9-
this.Saver.initData(["haveCoin"],0);
8+
if(this.Saver.data.maxstage == null)this.Saver.data.maxstage = 1;
9+
if(this.Saver.data.haveCoin == null)this.Saver.data.haveCoin = 0;
10+
this.Saver.save();
1011
}
1112
DIV_STAGE_SELECT = document.getElementById("stage_select")!;
1213
INPUT_STAGE_LINK = document.getElementById("StageLink")! as HTMLInputElement;
1314
START_BUTTON = document.getElementById("startbutton")!;
1415
initer(wasSuccess = false, stagePlayed = -1){
1516
if (wasSuccess) {
16-
this.Saver.setData(["haveCoin"],this.Saver.getData(["haveCoin"])+this.CLEARBONUS);
17-
this.Saver.setData(["maxstage"],Math.max(this.Saver.getData(["maxstage"]),stagePlayed+1));
17+
this.Saver.data.haveCoin+=this.CLEARBONUS;
18+
this.Saver.data.maxstage=Math.max(this.Saver.data.maxstage,stagePlayed+1);
19+
this.Saver.save();
1820
}
1921
this.DIV_STAGE_SELECT.style.display = "block";
2022
this.START_BUTTON.onclick = () => {
21-
const MAXSTAGE = this.Saver.getData(["maxstage"]);
23+
const MAXSTAGE = this.Saver.data.maxstage;
2224
const STAGE_ID = Number(this.INPUT_STAGE_LINK.value);
2325
if (isNaN(STAGE_ID) || STAGE_ID < 1) {
2426
alert("正の数字を入力しろぉ!");

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<button type="button" onclick="" id="startbutton">GAME START</button>
1414
</div>
1515
<div id="puz_display" style="display: none">
16-
<p id="puz_info">LOADING...</p>
16+
<p id="puz_info">Score : {{ score }},Hand : {{ hand }}</p>
1717
<p id="target_info"></p>
1818
<table class="board" id="puz_board"></table>
1919
</div>

0 commit comments

Comments
 (0)