From 3be3ffb89a2d80fa8aafc794d974ee90198470da Mon Sep 17 00:00:00 2001 From: Mal Date: Mon, 10 Feb 2020 19:53:30 +0100 Subject: [PATCH] Reset if Mr Croc left architecture. --- js/module.js | 7 +++++++ js/retro/RetroArchitecture.js | 25 +++++++++++++++++++++++-- tilorswift/js/Terrain.js | 2 -- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/js/module.js b/js/module.js index 81a416a..9d5683b 100644 --- a/js/module.js +++ b/js/module.js @@ -102,6 +102,10 @@ function MainLoop(timestamp) } } + if (!architecture.isInsideArchitecture(mrCroc.position)) { + architecture.resetMovableToStartPosition(mrCroc); + } + /* Drawing */ if (timestamp - lastRendered >= FRAME_DURATION) { context.clearRect(0, 0, window.innerWidth, window.innerHeight); @@ -186,8 +190,11 @@ window.addEventListener( camera.borderBottom = architecture.rows * architecture.tileHeight; mrCroc = new MrCroc(); + architecture.resetMovableToStartPosition(mrCroc); + /* mrCroc.position.x = architecture.tileWidth * LEVEL.startX + architecture.tileWidth * 0.5; mrCroc.position.y = architecture.tileHeight * LEVEL.startY; + */ window.requestAnimationFrame(MainLoop); } diff --git a/js/retro/RetroArchitecture.js b/js/retro/RetroArchitecture.js index 9842c0d..3064041 100644 --- a/js/retro/RetroArchitecture.js +++ b/js/retro/RetroArchitecture.js @@ -2,6 +2,7 @@ import RetroSprite from "./RetroSprite.js"; import RetroArchitectureTile from "./RetroArchitectureTile.js"; import GeometryRectCollection from "../geometry/GeometryRectCollection.js"; import GeometryPoint from "../geometry/GeometryPoint.js"; +import GeometryRect from "../geometry/GeometryRect.js"; export default class RetroArchitecture { @@ -14,6 +15,8 @@ export default class RetroArchitecture this.matrix = []; this.tileWidth = this.tileset.getWidth() / this.tiles; this.tileHeight = this.tileset.getHeight(); + this.startX = 0; + this.startY = 0; this.init(); } @@ -26,7 +29,7 @@ export default class RetroArchitecture for (let x = 0; x < this.columns; x++) { row.push(null); } - console.log(row); + this.matrix.push(row); } } @@ -113,7 +116,7 @@ export default class RetroArchitecture tilePosition.y++; } - return this.tileHeight * this.rows; + return 9999999999; } getWallRight(position) @@ -146,6 +149,21 @@ export default class RetroArchitecture return 0; } + isInsideArchitecture(geometryPoint) + { + let architectureRect = new GeometryRect( + 0, 0, this.tileWidth * this.columns, this.tileHeight * this.rows + ); + + return architectureRect.isContainingPoint(geometryPoint); + } + + resetMovableToStartPosition(movable) + { + movable.position.x = this.tileWidth * this.startX + this.tileWidth * 0.5; + movable.position.y = this.tileHeight * this.startY + this.tileHeight * 0.5; + } + draw(context, camera = null) { let viewX = parseInt(Math.floor(Math.max(0, camera.position.x) / this.tileWidth)); @@ -185,6 +203,9 @@ export default class RetroArchitecture data.rows ); + architecture.startX = data.startX; + architecture.startY = data.startY; + for (let y = 0; y < data.rows; y++) { for (let x = 0; x < data.columns; x++) { if (data.matrix[y][x] > -1) { diff --git a/tilorswift/js/Terrain.js b/tilorswift/js/Terrain.js index fbd0395..3c646e6 100644 --- a/tilorswift/js/Terrain.js +++ b/tilorswift/js/Terrain.js @@ -80,8 +80,6 @@ export default class Terrain tr.appendChild(td); } - console.log(row.length); - if (index === undefined || index >= this.tilesY - 1) { this.fields.push(row); this.htmlElement.appendChild(tr);