Reset if Mr Croc left architecture.

This commit is contained in:
Mal 2020-02-10 19:53:30 +01:00
parent b58cb552c8
commit 3be3ffb89a
3 changed files with 30 additions and 4 deletions

View File

@ -102,6 +102,10 @@ function MainLoop(timestamp)
} }
} }
if (!architecture.isInsideArchitecture(mrCroc.position)) {
architecture.resetMovableToStartPosition(mrCroc);
}
/* Drawing */ /* Drawing */
if (timestamp - lastRendered >= FRAME_DURATION) { if (timestamp - lastRendered >= FRAME_DURATION) {
context.clearRect(0, 0, window.innerWidth, window.innerHeight); context.clearRect(0, 0, window.innerWidth, window.innerHeight);
@ -186,8 +190,11 @@ window.addEventListener(
camera.borderBottom = architecture.rows * architecture.tileHeight; camera.borderBottom = architecture.rows * architecture.tileHeight;
mrCroc = new MrCroc(); mrCroc = new MrCroc();
architecture.resetMovableToStartPosition(mrCroc);
/*
mrCroc.position.x = architecture.tileWidth * LEVEL.startX + architecture.tileWidth * 0.5; mrCroc.position.x = architecture.tileWidth * LEVEL.startX + architecture.tileWidth * 0.5;
mrCroc.position.y = architecture.tileHeight * LEVEL.startY; mrCroc.position.y = architecture.tileHeight * LEVEL.startY;
*/
window.requestAnimationFrame(MainLoop); window.requestAnimationFrame(MainLoop);
} }

View File

@ -2,6 +2,7 @@ import RetroSprite from "./RetroSprite.js";
import RetroArchitectureTile from "./RetroArchitectureTile.js"; import RetroArchitectureTile from "./RetroArchitectureTile.js";
import GeometryRectCollection from "../geometry/GeometryRectCollection.js"; import GeometryRectCollection from "../geometry/GeometryRectCollection.js";
import GeometryPoint from "../geometry/GeometryPoint.js"; import GeometryPoint from "../geometry/GeometryPoint.js";
import GeometryRect from "../geometry/GeometryRect.js";
export default class RetroArchitecture export default class RetroArchitecture
{ {
@ -14,6 +15,8 @@ export default class RetroArchitecture
this.matrix = []; this.matrix = [];
this.tileWidth = this.tileset.getWidth() / this.tiles; this.tileWidth = this.tileset.getWidth() / this.tiles;
this.tileHeight = this.tileset.getHeight(); this.tileHeight = this.tileset.getHeight();
this.startX = 0;
this.startY = 0;
this.init(); this.init();
} }
@ -26,7 +29,7 @@ export default class RetroArchitecture
for (let x = 0; x < this.columns; x++) { for (let x = 0; x < this.columns; x++) {
row.push(null); row.push(null);
} }
console.log(row);
this.matrix.push(row); this.matrix.push(row);
} }
} }
@ -113,7 +116,7 @@ export default class RetroArchitecture
tilePosition.y++; tilePosition.y++;
} }
return this.tileHeight * this.rows; return 9999999999;
} }
getWallRight(position) getWallRight(position)
@ -146,6 +149,21 @@ export default class RetroArchitecture
return 0; 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) draw(context, camera = null)
{ {
let viewX = parseInt(Math.floor(Math.max(0, camera.position.x) / this.tileWidth)); let viewX = parseInt(Math.floor(Math.max(0, camera.position.x) / this.tileWidth));
@ -185,6 +203,9 @@ export default class RetroArchitecture
data.rows data.rows
); );
architecture.startX = data.startX;
architecture.startY = data.startY;
for (let y = 0; y < data.rows; y++) { for (let y = 0; y < data.rows; y++) {
for (let x = 0; x < data.columns; x++) { for (let x = 0; x < data.columns; x++) {
if (data.matrix[y][x] > -1) { if (data.matrix[y][x] > -1) {

View File

@ -80,8 +80,6 @@ export default class Terrain
tr.appendChild(td); tr.appendChild(td);
} }
console.log(row.length);
if (index === undefined || index >= this.tilesY - 1) { if (index === undefined || index >= this.tilesY - 1) {
this.fields.push(row); this.fields.push(row);
this.htmlElement.appendChild(tr); this.htmlElement.appendChild(tr);