2
0
forked from s444420/AL-2020
AL-2020/frontend/js/main.js

357 lines
7.9 KiB
JavaScript

//Klasy
class Agent{
constructor(positionX, positionY, turn){
this.positionX = positionX;
this.positionY = positionY;
this.turn = turn;
}
getParams(){
let params = {};
positionX = this.positionX;
positionY = this.positionY;
turn = this.turn;
params = {positionX, positionY, turn};
return params;
}
goForward(){
if(this.turn == 'Up'){
this.positionY += 1;
}
else if(this.turn == 'Down'){
this.positionY -= 1;
}
else if(this.turn == 'Left'){
this.positionX -= 1;
}
else if(this.turn == 'Right'){
this.positionX += 1;
}
}
turnLeft(){
if(this.turn == 'Up'){
this.turn = 'Left';
}
else if(this.turn == 'Down'){
this.turn = 'Right';
}
else if(this.turn == 'Left'){
this.turn = 'Down';
}
else if(this.turn == 'Right'){
this.turn = 'Up'
}
}
turnRight(){
if(this.turn == 'Up'){
this.turn = 'Right';
}
else if(this.turn == 'Down'){
this.turn = 'Left';
}
else if(this.turn == 'Left'){
this.turn = 'Up';
}
else if(this.turn == 'Right'){
this.turn = 'Down'
}
}
showAgent(){
if(this.turn === "Up"){
document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "url('img/Up.png')";
}
else if(this.turn === "Down"){
document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "url('img/Down.png')";
}
else if(this.turn === "Right"){
document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "url('img/Right.png')";
}
else if(this.turn === "Left"){
document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "url('img/Left.png')";
}
}
hideAgent(){
document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "";
}
}
class Field{
constructor(x, y, isShelf, isOccupiedByAgent, costOfTravel){
this.xField = x;
this.yField = y;
this.isShelf = isShelf;
this.isOccupiedByAgent = isOccupiedByAgent;
this.costOfTravel = costOfTravel;
}
getParams(){
let params = {};
xField = this.xField;
yField = this.yField;
isShelf = this.isShelf;
isOccupiedByAgent = this.isOccupiedByAgent;
costOfTravel = this.costOfTravel;
params = {xField, yField, isShelf, isOccupiedByAgent, costOfTravel}
return params;
}
setIsShelf(isShelf){
this.isShelf = isShelf;
}
getIsShelf(){
return this.isShelf;
}
setIsOccupiedByAgent(isOccupiedByAgent){
this.isOccupiedByAgent = isOccupiedByAgent;
}
setCostOfTravel(costOfTravel){
this.costOfTravel = costOfTravel;
}
}
class Shelf{
constructor(accessY, accessX, havePlace, box1, box2, box3){
this.accessX = accessX;
this.accessY = accessY;
this.havePlace = true;
this.box1 = box1;
this.box2 = box2;
this.box3 = box3;
}
getParams(){
let params = {};
xField = this.xField;
yField = this.yField;
havePlace = this.havePlace;
box1 = this.box1;
box2 = this.box2;
box3 = this.box3;
params = {xField, yField, havePlace, box1, box2, box3}
return params;
}
setbox1(box1){
this.box1 = box1;
}
setbox2(box1){
this.box2 = box2;
}
setbox3(box1){
this.box3 = box3;
}
setHavePlace(havePlace){
this.havePlace = havePlace;
}
}
class Candy{
constructor(type, taste, mark, id){
this.type = type;
this.taste = taste;
this.mark = mark;
this.id = id;
}
getId(){
return this.id;
}
}
//funckcje
function createBoard(rangeX, rangeY){
const board = [];
for(let y = 0; y < rangeY; y++){
const row = [];
for(let x = 0; x < rangeX; x++){
let field = new Field(x, y, false, false, 1);
row.push(field)
}
board.push(row)
}
return board
}
function createShelfOnBoard(board){
//pierwszy poziom
board[2][2].setIsShelf(true);
board[3][2].setIsShelf(true);
board[2][4].setIsShelf(true);
board[3][4].setIsShelf(true);
board[2][6].setIsShelf(true);
board[3][6].setIsShelf(true);
board[2][8].setIsShelf(true);
board[3][8].setIsShelf(true);
//drugi poziom
board[5][2].setIsShelf(true);
board[5][3].setIsShelf(true);
board[5][7].setIsShelf(true);
board[5][8].setIsShelf(true);
//trzeci poziom
board[7][2].setIsShelf(true);
board[8][2].setIsShelf(true);
board[7][4].setIsShelf(true);
board[8][4].setIsShelf(true);
board[7][6].setIsShelf(true);
board[8][6].setIsShelf(true);
board[7][8].setIsShelf(true);
board[8][8].setIsShelf(true);
return board
}
function createCostofField(board, rangeX, rangeY){
let cost;
let number;
for(let y = 0; y < rangeY; y++){
for(let x = 0; x < rangeX; x++){
number = 0;
if(y<9 && board[y+1][x].getIsShelf()){
number = number + 1;
}
if(y>0 && board[y-1][x].getIsShelf()){
number = number + 1;
}
if(x<9 && board[y][x+1].getIsShelf()){
number = number + 1;
}
if(x>0 && board[y][x-1].getIsShelf()){
number = number + 1;
}
cost = number*2;
if(cost==0){
cost = 1;
}
board[y][x].setCostOfTravel(cost);
}
}
return board;
}
function createShelf(){
const listOfShelf = [];
//pierwszy poziom
let shelf = new Shelf(2, 1, true, 211, 212, 0);
listOfShelf.push(shelf)
shelf = new Shelf(3, 1, true, 311, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(4, 4, false, 441, 442, 443);
listOfShelf.push(shelf)
shelf = new Shelf(1, 4, true, 0, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(4, 6, true, 461, 462, 0);
listOfShelf.push(shelf)
shelf = new Shelf(1, 6, true, 161, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(3, 9, true, 391, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(2, 9, true, 291, 0, 0);
listOfShelf.push(shelf)
//drugi poziom
shelf = new Shelf(5, 1, true, 511, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(5, 4, true, 541, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(5, 6, true, 561, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(5, 9, true, 591, 0, 0);
listOfShelf.push(shelf)
//trzeci poziom
shelf = new Shelf(7, 1, true, 711, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(8, 1, true, 811, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(6, 4, true, 641, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(9, 4, true, 941, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(6, 6, true, 661, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(9, 6, true, 961, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(7, 9, true, 791, 0, 0);
listOfShelf.push(shelf)
shelf = new Shelf(8, 9, true, 891, 0, 0);
listOfShelf.push(shelf)
return listOfShelf;
}
function showBoard(board){
for(let y = board.length - 1; y >= 0 ; y--){
document.getElementById("board").innerHTML += "<div class='row' id='row-" + y + "'> </div>";
for(let x = 0; x < board[y].length; x++){
if(board[y][x].getIsShelf())
document.getElementById("row-" + y).innerHTML += "<div class='regal2' id=" + x + "-" + y + "> </div>"
else
document.getElementById("row-" + y).innerHTML += "<div class='field' id=" + x + "-" + y + ">"+ board[y][x].costOfTravel + "</div>"
}
}
}
function getDistance(x1,y1,x2,y2){
var a = x1 - x2;
var b = y1 - y2;
var c = Math.sqrt(a*a + b*b);
return c
}
let board = createBoard(10,10);
board = createShelfOnBoard(board);
board = createCostofField(board, 10, 10);
let candy = new Candy('zelek', 'truskawkowy', 'Jumbo', 541);
let shelfs = createShelf();
let agent = new Agent(0,0, 'Right');
function start(){
showBoard(board);
console.log(board);
}