In order to test, open another terminal window and then use the following
npm run test
How it works
Is the same matrix you have to build, but created in a sugar nicer way ✨✨✨
I'm using all the knowledge you gathered along the course (nothing strange or weird, I promise)
Nice explanation
First, we need to gather the .csv data in a way we can handle.
//Fetch the dataletgame: MapElem[][];//this variable will represent our matrix along the codep.setup=()=>{fetch("../data/map.csv",{//the path of our local fileheaders: {"content-type": "text/csv;charset=UTF-8"},}).then((raw: any)=>raw.text())//transform the raw data into string.then((data: string)=>{//here we have access to the data in string format//as we have access to a bunch of chars and commas, we need to transform it to objectsgame=data.split(/\r?\n/).map((chunk: string)=>chunk.split(",").map((obj: string)=>{//According to the value we set the object type//At this point, the posx and posy does not matter at all, I will set that laterswitch(obj){case"1":
returnnewWall(0,0);case"3":
returnnewPlayer(0,0);default:
returnnewGround(0,0);}}));});};
Before we continue, there is an important notion to settle down
We need to get track
constgetPlayerReference=(_game: MapElem[][])=>{const playerRow =_game.findIndex((l: MapElem[])=>l.find((j: MapElem)=>jinstanceofPlayer));constplayerIndex=_game[playerRow].findIndex((j: MapElem|Player)=>jinstanceofPlayer);constplayer=_game[playerRow][playerIndex];return{playerRow, playerIndex, player };};
Now, in the draw is where the nice stuff happens
p.draw=()=>{p.background(80);const{ player }=getPlayerReference(game);for(leti=0;i<game.length;i+=1){for(letj=0;j<game[i].length;j+=1){game[i][j].setX(j*20);game[i][j].setY(i*20);game[i][j].show(p);}}if(playerinstanceofPlayer){player.getBullets().forEach((bullet: Bullet,bulletIndex: number)=>{bullet.show(p);if(bullet.getX()>500){player.spliceBullet(bulletIndex);}constcallBack=(row:number,col: number)=>{game[row][col]=newGround(0,0);player.spliceBullet(bulletIndex);};bullet.shoot(callBack,game);});}};