Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mapArray = [
- [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
- [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
- [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
- [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0],
- [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
- ];
- function isPositionWall(ptX, ptY) {
- var gridX = Math.floor(ptX / 36);
- var gridY = Math.floor(ptY / 36);
- if(mapArray[gridX][gridY] == 1)
- return true;
- }
- var boatPosX = canvas.height/2 - 50;
- var boatPosY = canvas.height/2 - 50;
- function render(viewport) {
- context.save();
- context.translate(view.x, view.y);
- requestAnimationFrame(render);
- var oldPosX = boatPosX;
- var oldPosY = boatPosY;
- for (let i = 0; i < mapArray.length; i++) {
- for (let j = 0; j < mapArray[i].length; j++) {
- if (mapArray[j][i] == 0) {
- this.sprite.draw(
- background,
- 190,
- 230,
- 26,
- 26,
- i * this.sprite.width,
- j * this.sprite.height,
- this.sprite.width,
- this.sprite.height
- );
- }
- if (mapArray[j][i] == 1) {
- this.sprite.draw(
- background,
- 30,
- 30,
- 26,
- 26,
- i * this.sprite.width,
- j * this.sprite.height,
- this.sprite.width,
- this.sprite.height
- );
- }
- if (mapArray[j][i] == 2) {
- this.sprite.draw(
- background,
- 200,
- 20,
- 26,
- 26,
- i * this.sprite.width,
- j * this.sprite.height,
- this.sprite.width,
- this.sprite.height
- );
- }
- }
- }
- this.ship.drawimage(boat, boatPosX, boatPosY, 50, 50);
- if(isPositionWall(boatPosX, boatPosY)) {
- //boatPosX = oldPosY;
- console.log("collision");
- }
- console.log(mapArray[Math.floor(boatPosX / 36)][Math.floor(boatPosX / 36)]);
- context.restore();
- };
- function move(e) {
- if (e.keyCode == 39) {
- boatPosX += 5;
- view.x -= 5
- moveCount++;
- console.log(moveCount);
- console.log("right");
- }
Add Comment
Please, Sign In to add comment