質問内容
テトリスゲームの製作で回転したときのフォールキックパターンの配列のデータなのですがこの値は変更しないのでconst
にしたいのですが以下の///
コメント部内部のように参考サイト参考に実装したいのですが値が書き換えられてしまうので上手く実装出来ていません。クラスメンバ配列変数の場合どうやって実装するのでしょうか?
質問内容
///
コメント部内部のthis.fixRotatePosition
配列変数をconst
にして書き換え不能にする方法が知りたい。
試したこと
参考サイトを参考に実装したのですが値が書き換えられてしまいます。
参考サイト: https://web-engineer-wiki.com/javascript/const-object-array/
"use strict"
/*#############################################################
# プレイヤー 管理クラス
###############################################################*/
class PlayerManager
{
//回転時座標修正 I以外
fixRotatePosition =
[
[
{x: -1,y: 0},
{x: -1,y: -1},
{x: 0,y: -2},
{x: -1,y: -2}
],
[
{x: 1,y: 0},
{x: 1,y: -1},
{x: 0,y: -2},
{x: 1, y: 2}
],
[
{x: 1,y: 0},
{x: 1,y: 1},
{x: 0,y: -2},
{x: 1, y: -2}
],
[
{x: -1,y: 0},
{x: -1,y: -1},
{x: 0,y: 2},
{x: 1,y: -2}
],
];
/*#########################################
# コンストラクタ
###########################################*/
constructor()
{
this.random = 0; //乱数
this.piece = []; //ブロック
this.position = {}; //座標
this.rotateRadian = 0; //回転数
/////////////////////////////////////////////////////////////////
Object.freeze(this.fixRotatePosition);
this.fixRotatePosition[0][0].x = 3;
console.log(this.fixRotatePosition[0][0].x);
/////////////////////////////////////////////////////////////////
}
/*#########################################
# 初期化
###########################################*/
Init()
{
const START_POSITION = {x: 4,y: 1}; //初期座標
this.position = START_POSITION; //座標
this.random = 1;
this.piece = BitMap.getPiece(this.random);
this.rotateRadian = 0; //回転数
}
/*#########################################
# Left 移動
###########################################*/
LeftKey()
{
this.position.x += -1;
}
/*#########################################
# Right 移動
###########################################*/
RightKey()
{
this.position.x += 1;
}
/*#########################################
# スペース 回転
###########################################*/
SpaceKey()
{
this.rotateRadian += 1;
if(this.rotateRadian > 3)
{
this.rotateRadian = 1;
}
this.Rotate(this.rotateRadian);
}
/*#########################################
# 描画
###########################################*/
Render()
{
for(let i = 0; i < PIECE_HEIGHT; i++)
{
for(let j = 0; j < PIECE_WIDTH; j++)
{
if(this.piece[i][j] == 1)
{
fill(BitMap.getPieceColor(this.random));
rect( (this.position.x + j) * CELL, (this.position.y + i ) * CELL,CELL,CELL);
}
}
}
}
/*#########################################
# 当たり判定
###########################################*/
Collision(stage)
{
}
/*#########################################
# 回転
###########################################*/
Rotate(radian)
{
let rotate = new Array(4);
rotate[0] = new Array(4).fill(0);
rotate[1] = new Array(4).fill(0);
rotate[2] = new Array(4).fill(0);
rotate[3] = new Array(4).fill(0);
for(let y = 0; y < PIECE_HEIGHT; y++)
{
for(let x = 0; x < PIECE_WIDTH; x++)
{
if(this.piece[y][x] == 1)
{
let xx = (cos(PI / 2 * radian) * (x - 1.5)) + (-sin(PI / 2 * radian) * (y - 1.5));
let yy = (sin(PI / 2 * radian) * (x - 1.5)) + ( cos(PI / 2 * radian) * (y - 1.5));
rotate[Math.round((yy + 1.5))][Math.round((xx + 1.5))] = 1;
}
}
}
this.piece = rotate.slice(); //描画配列にコピー
RotateFix();
}
/*#########################################
# 回転 位置修正
###########################################*/
RotateFix()
{
let pos = Object.assign({},this.position);
pos.x += -1;
}
/*#########################################
# 計算
###########################################*/
Update()
{
}
};