As for the data of the array of fall kick patterns when rotating in the production of the Tetris game, I would like to use const
, but I would like to use //
comment section as a reference site, but it is not implemented well.How do I implement class member array variables?
//
I want to know how to make the this.fixRotatePosition
array variable inside the comment section non-rewritable by setting it to const
.
I implemented it with reference to the reference site, but the value will be rewritten.
Reference site: https://web-engineer-wiki.com/javascript/const-object-array/
"use strict"
/*#############################################################
# player management class
###############################################################*/
class PlayerManager
{
// Other than rotational coordinate correction 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
###########################################*/
constructor()
{
This.random=0;// random number
This.piece = [ ]; // Block
This.position={};// coordinates
This.rotateRadian = 0;// number of revolutions
/////////////////////////////////////////////////////////////////
Object.freeze(this.fixRotatePosition);
This.fixRotatePosition[0][0].x=3;
console.log (this.fixRotatePosition[0][0].x);
/////////////////////////////////////////////////////////////////
}
/*#########################################
# initialization
###########################################*/
Init()
{
constSTART_POSITION={x:4,y:1};//initial coordinates
this.position=START_POSITION;// coordinates
This.random=1;
this.piece = BitMap.getPiece (this.random);
This.rotateRadian = 0;// number of revolutions
}
/*#########################################
# Left Move
###########################################*/
LeftKey()
{
This.position.x+=-1;
}
/*#########################################
# Right Move
###########################################*/
RightKey()
{
This.position.x+=1;
}
/*#########################################
# space rotation
###########################################*/
SpaceKey()
{
This.rotateRadian+=1;
if(this.rotateRadian>3)
{
This.rotateRadian=1;
}
This.Rotate(this.rotateRadian);
}
/*#########################################
# drawing
###########################################*/
Render()
{
for (leti=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);
}
}
}
}
/*#########################################
# winning decision
###########################################*/
Collision (stage)
{
}
/*#########################################
# revolving
###########################################*/
Rotate (radian)
{
letrotate=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 (letty=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));
lettyy=(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(); // Copy to drawing array
RotateFix();
}
/*#########################################
# rotational position correction
###########################################*/
RotateFix()
{
let pos = Object.assign({}, this.position);
pos.x+=-1;
}
/*#########################################
# calculation
###########################################*/
Update()
{
}
};
Object.freeze()
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
What is a shallow freeze?
The result of invoking Object.freeze(object) applies only to the object's immediate properties and prohibits subsequent addition, deletion, or value reassignment operations on the object only.If the values for these properties are the objects themselves, they are not frozen and can be subject to addition, deletion, and value reassignment operations.
So even if you freeze the array from the outside, we assume that only the first-dimensional array is frozen.
For array arrays, you should freeze from the outside and then freeze all the elements in the first dimension.
© 2024 OneMinuteCode. All rights reserved.