Graveyard terrain and background tiles implemented

This commit is contained in:
Mal
2025-05-16 23:08:35 +02:00
parent e97c04ed19
commit fc990c12aa
25 changed files with 504 additions and 50 deletions

View File

@@ -5,24 +5,69 @@ import GeometryPoint from "../geometry/GeometryPoint.js";
import GeometryRect from "../geometry/GeometryRect.js";
import GraphicSet from "../GraphicSet.js";
import Setting from "../Setting.js";
import Camera from "../Camera.js";
import Level from "../Level.js";
export default class RetroArchitecture
{
constructor(tilesetSprite, tiles, columns, rows)
/**
* @param {{sprite: RetroSprite, tiles: number}} retroTileset
* @param {number} columns
* @param {number} rows
* @param {{sprite: RetroSprite, tiles: number}} retroTilesetBackground
*/
constructor(retroTileset, columns, rows, retroTilesetBackground = null)
{
this.tilesetSprite = tilesetSprite;
this.tiles = tiles;
/** @type {RetroSprite} */
this.tilesetSprite = retroTileset.sprite;
/** @type {RetroSprite|null} */
this.tilesetSpriteBackground = retroTilesetBackground !== null
? retroTilesetBackground.sprite
: null;
/** @type {number} */
this.tiles = retroTileset.tiles;
/** @type {number} */
this.tilesBackground = retroTilesetBackground !== null
? retroTilesetBackground.tiles
: 0;
/** @type {string|null} */
this.backgroundColor = null;
/** @type {string|null} */
this.backgroundImage = null;
/** @type {number} */
this.rows = rows;
/** @type {number} */
this.columns = columns;
/** @type {Array<Array<RetroArchitectureTile>>} */
this.matrix = [];
/** @type {number} */
this.tileWidth = this.tilesetSprite.getWidth() / this.tiles;
/** @type {number} */
this.tileHeight = this.tilesetSprite.getHeight();
/** @type {number} */
this.startX = 0;
/** @type {number} */
this.startY = 0;
/** @type {number} */
this.targetX = 0;
/** @type {number} */
this.targetY = 0;
/** @type {GeometryPoint} */
this.targetPosition = new GeometryPoint(this.targetX, this.targetY);
this.init();
@@ -41,16 +86,26 @@ export default class RetroArchitecture
}
}
/**
* @param {string|null} color
*/
setBackgroundColor(color)
{
this.backgroundColor = color;
}
/**
* @param {string|null} image
*/
setBackgroundImage(image)
{
this.backgroundImage = image;
}
/**
* @param {GeometryRect} rect
* @returns {GeometryRectCollection}
*/
getCollisionRects(rect)
{
let posX = Math.floor(rect.position.x / this.tileWidth) - 2;
@@ -62,11 +117,16 @@ export default class RetroArchitecture
for (let y = Math.max(0, posY); y < rangeY; y++) {
for (let x = Math.max(0, posX); x < rangeX; x++) {
if (y < this.matrix.length && x < this.matrix[y].length && this.matrix[y][x] !== null) {
if (
y < this.matrix.length &&
x < this.matrix[y].length &&
this.matrix[y][x] !== null && this.matrix[y][x].tile.index > -1
) {
let intersection = this.matrix[y][x].rect.getRectIntersection(rect);
if (intersection !== null) {
collection.addRect(intersection);
}
}
}
}
}
@@ -83,7 +143,7 @@ export default class RetroArchitecture
for (let y = Math.max(0, posY); y < rangeY; y++) {
for (let x = Math.max(0, posX); x < rangeX; x++) {
if (this.matrix[y][x] !== null) {
if (this.matrix[y][x] !== null && this.matrix[y][x].tile.index > -1) {
if (this.matrix[y][x].rect.getRectIntersection(rect) !== null) {
return true;
}
@@ -111,7 +171,10 @@ export default class RetroArchitecture
let tilePosition = this.getTileForPosition(position, 0);
while (tilePosition !== null && tilePosition.y > 0) {
if (this.matrix[tilePosition.y][tilePosition.x] !== null) {
if (
this.matrix[tilePosition.y][tilePosition.x] !== null &&
this.matrix[tilePosition.y][tilePosition.x].tile.index > -1
) {
return tilePosition.y * this.tileHeight + this.tileHeight;
}
@@ -126,7 +189,10 @@ export default class RetroArchitecture
let tilePosition = this.getTileForPosition(position);
while (tilePosition !== null && tilePosition.y < this.rows) {
if (this.matrix[tilePosition.y][tilePosition.x] !== null) {
if (
this.matrix[tilePosition.y][tilePosition.x] !== null &&
this.matrix[tilePosition.y][tilePosition.x].tile.index > -1
) {
return tilePosition.y * this.tileHeight;
}
@@ -141,7 +207,10 @@ export default class RetroArchitecture
let tilePosition = this.getTileForPosition(new GeometryPoint(position.x, position.y), 1, 0);
while (tilePosition !== null && tilePosition.x < this.columns) {
if (this.matrix[tilePosition.y][tilePosition.x] !== null) {
if (
this.matrix[tilePosition.y][tilePosition.x] !== null &&
this.matrix[tilePosition.y][tilePosition.x].tile.index > -1
) {
return tilePosition.x * this.tileWidth;
}
@@ -156,7 +225,10 @@ export default class RetroArchitecture
let tilePosition = this.getTileForPosition(new GeometryPoint(position.x, position.y), -1,0);
while (tilePosition !== null && tilePosition.x > 0) {
if (this.matrix[tilePosition.y][tilePosition.x] !== null) {
if (
this.matrix[tilePosition.y][tilePosition.x] !== null &&
this.matrix[tilePosition.y][tilePosition.x].tile.index > -1
) {
return tilePosition.x * this.tileWidth + this.tileWidth;
}
@@ -183,8 +255,8 @@ export default class RetroArchitecture
setMovableToTargetPosition(movable)
{
movable.position.x = this.tileWidth * this.targetX + this.tileWidth * 0.5;
movable.position.y = this.tileHeight * this.targetY + this.tileHeight;
movable.position.x = this.tileWidth * this.targetX + this.tileWidth * 0.5;
movable.position.y = this.tileHeight * this.targetY + this.tileHeight;
}
isMovableInsideTargetPosition(movable)
@@ -213,8 +285,14 @@ export default class RetroArchitecture
for (let x = viewX; x < viewWidth; x++) {
let field = this.matrix[y][x];
if (field !== null) {
if (field === null || field.tile.index === -1) {
continue;
}
if (field.tile.index > -1) {
this.drawField(context, x, y, camera, field);
} else {
this.drawBackgroundField(context, x, y, camera, field);
}
}
}
@@ -235,25 +313,47 @@ export default class RetroArchitecture
);
}
drawBackgroundField(context, x, y, camera, field)
{
context.drawImage(
this.tilesetSpriteBackground.canvas,
-(field.tile.index % 2) * this.tileWidth,
0,
this.tileWidth,
this.tileHeight,
Math.round(x * this.tileWidth - camera.position.x),
Math.round(y * this.tileHeight - camera.position.y),
this.tileWidth,
this.tileHeight,
);
}
getStartPosition()
{
return new GeometryPoint(this.startX * this.tileWidth, this.startY * this.tileHeight);
}
/**
* @param {Level} level
*/
static createFromData(level)
{
let graphicSet = GraphicSet[level.getTilesetId()];
let tileset = new RetroSprite(
const tilesetSprite = new RetroSprite(
Setting.TILESET_LOCATION + graphicSet.tileset,
graphicSet.scale
);
let architecture = new RetroArchitecture(
tileset,
graphicSet.tiles,
const tilesetBackground = level.terrain.tileset.background !== null
? new RetroSprite(level.terrain.tileset.background.image.src)
: null;
const architecture = new RetroArchitecture(
{sprite: tilesetSprite, tiles: graphicSet.tiles},
level.getColumns(),
level.getRows()
level.getRows(),
tilesetBackground === null ? null : {sprite: tilesetBackground, tiles: level.terrain.tileset.background.tiles}
);
architecture.setBackgroundColor(graphicSet.backgroundColor);
@@ -267,7 +367,7 @@ export default class RetroArchitecture
for (let y = 0; y < level.getRows(); y++) {
for (let x = 0; x < level.getColumns(); x++) {
if (level.getTilesetMatrix()[y][x].index > -1) {
if (level.getTilesetMatrix()[y][x].index !== -1) {
architecture.matrix[y][x] = new RetroArchitectureTile(
level.getTilesetMatrix()[y][x],
x * architecture.tileWidth,