You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.6 KiB
112 lines
3.6 KiB
2 years ago
|
class Game{
|
||
|
|
||
|
constructor(){
|
||
|
this.generateGrid();
|
||
|
this.updateVelocity();
|
||
|
this.updateDifficulty();
|
||
|
this.updateVisibilities();
|
||
|
}
|
||
|
|
||
|
generateGrid(){
|
||
|
this.algorithm = null;
|
||
|
|
||
|
let count = parseInt($('#cell_count').val());
|
||
|
let movement = $('#movement').val();
|
||
|
let difficulty = parseInt($('#difficulty').val());
|
||
|
let hasRandomDirection = $('#direction').is(':checked');
|
||
|
|
||
|
this.grid = new Grid(localSettings.app.grid, count, difficulty, hasRandomDirection);
|
||
|
$('#cell_count_text').text(count + 'x' + count + ' nodes');
|
||
|
|
||
|
//Two main nodes, start and target
|
||
|
if (hasRandomDirection){
|
||
|
this.startNode = random(this.grid.allowedNodes);
|
||
|
this.targetNode = random(this.grid.getAllowedNodesDistantTo(this.startNode)
|
||
|
.filter(n => {
|
||
|
if (movement === '1'){
|
||
|
return n.pos.x % 2 === this.startNode.pos.x % 2
|
||
|
&& n.pos.y % 2 === this.startNode.pos.y % 2;
|
||
|
}
|
||
|
return true;
|
||
|
})
|
||
|
);
|
||
|
} else {
|
||
|
this.startNode = this.grid.nodes[0][0];
|
||
|
this.targetNode = this.grid.nodes[count - 1][count - 1];
|
||
|
}
|
||
|
|
||
|
this.updateTester = 0;
|
||
|
}
|
||
|
|
||
|
update(){
|
||
|
if (this.algorithm){
|
||
|
this.updateTester += this.movesPerSecond / frameRate();
|
||
|
let i = 0;
|
||
|
for (; i < floor(this.updateTester); i++){
|
||
|
this.algorithm.update();
|
||
|
}
|
||
|
this.updateTester -= i;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
display(){
|
||
|
this.grid.display(this.appearance, this.linesAreVisible);
|
||
|
|
||
|
if (this.appearance === 'Circles'){
|
||
|
let r = 0.5;
|
||
|
fill(50, 50, 255);
|
||
|
ellipse(this.startNode.pos.x, this.startNode.pos.y, r * 2);
|
||
|
fill(50, 255, 50);
|
||
|
ellipse(this.targetNode.pos.x, this.targetNode.pos.y, r * 2);
|
||
|
}
|
||
|
if (this.appearance === 'Rectangles'){
|
||
|
fill(50, 50, 255);
|
||
|
rect(this.startNode.pos.x - 0.5, this.startNode.pos.y - 0.5, 1, 1);
|
||
|
fill(50, 255, 50);
|
||
|
rect(this.targetNode.pos.x - 0.5, this.targetNode.pos.y - 0.5, 1, 1);
|
||
|
}
|
||
|
|
||
|
|
||
|
if (this.algorithm)
|
||
|
this.algorithm.display(this.appearance, this.isInformationVisible, this.pathSmoothness);
|
||
|
}
|
||
|
|
||
|
start(algorithm){
|
||
|
this.algorithm = algorithm;
|
||
|
|
||
|
let movement = $('#movement').val();
|
||
|
this.grid.nodes.forEach(x => x.forEach(n => {
|
||
|
n.setMovement(movement);
|
||
|
n.connectToSuccessors(this.grid.nodes);
|
||
|
}));
|
||
|
}
|
||
|
|
||
|
updateVelocity(){
|
||
|
let value = parseInt($('#velocity').val());
|
||
|
$('#velocity_text').text(value + ' moves per second');
|
||
|
this.movesPerSecond = value;
|
||
|
}
|
||
|
|
||
|
updateDifficulty(){
|
||
|
let value = parseInt($('#difficulty').val());
|
||
|
$('#difficulty_text').text('Difficulty: ' + value);
|
||
|
this.generateGrid();
|
||
|
}
|
||
|
|
||
|
updateVisibilities(){
|
||
|
let appearance = $('input[name=appearance]:checked').val();
|
||
|
let displayGrid = $('#gridVisibility').is(':checked');
|
||
|
let displayInformation = $('#information').is(':checked');
|
||
|
let pathSmoothness = $('#pathSmoothness').is(':checked');
|
||
|
|
||
|
if (!appearance)
|
||
|
appearance = $('input[name=appearance]:eq(0)').val();
|
||
|
|
||
|
this.appearance = appearance;
|
||
|
this.linesAreVisible = displayGrid;
|
||
|
this.isInformationVisible = displayInformation;
|
||
|
this.pathSmoothness = pathSmoothness;
|
||
|
}
|
||
|
|
||
|
}
|