2020-01-23 23:09:03 +01:00
|
|
|
import RetroSprite from "./RetroSprite.js";
|
|
|
|
import RetroArchitectureTile from "./RetroArchitectureTile.js";
|
2020-01-25 13:11:25 +01:00
|
|
|
import GeometryRectCollection from "../geometry/GeometryRectCollection.js";
|
2020-01-25 23:30:36 +01:00
|
|
|
import GeometryPoint from "../geometry/GeometryPoint.js";
|
2020-02-10 19:53:30 +01:00
|
|
|
import GeometryRect from "../geometry/GeometryRect.js";
|
2020-02-14 00:12:24 +01:00
|
|
|
import GraphicSet from "../GraphicSet.js";
|
|
|
|
import Setting from "../Setting.js";
|
2020-01-23 23:09:03 +01:00
|
|
|
|
|
|
|
export default class RetroArchitecture
|
|
|
|
{
|
|
|
|
constructor(tilesetSprite, tiles, columns, rows)
|
|
|
|
{
|
2020-02-16 00:53:03 +01:00
|
|
|
this.tilesetSprite = tilesetSprite;
|
2020-01-23 23:09:03 +01:00
|
|
|
this.tiles = tiles;
|
2020-02-14 00:12:24 +01:00
|
|
|
this.backgroundColor = null;
|
|
|
|
this.backgroundImage = null;
|
2020-01-23 23:09:03 +01:00
|
|
|
this.rows = rows;
|
|
|
|
this.columns = columns;
|
|
|
|
this.matrix = [];
|
2020-02-16 00:53:03 +01:00
|
|
|
this.tileWidth = this.tilesetSprite.getWidth() / this.tiles;
|
|
|
|
this.tileHeight = this.tilesetSprite.getHeight();
|
2020-02-10 19:53:30 +01:00
|
|
|
this.startX = 0;
|
|
|
|
this.startY = 0;
|
2020-02-11 21:20:11 +01:00
|
|
|
this.targetX = 0;
|
|
|
|
this.targetY = 0;
|
2020-02-12 21:29:06 +01:00
|
|
|
this.targetPosition = new GeometryPoint(this.targetX, this.targetY);
|
2020-01-23 23:09:03 +01:00
|
|
|
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
init()
|
|
|
|
{
|
|
|
|
for (let y = 0; y < this.rows; y++) {
|
2020-02-10 19:36:46 +01:00
|
|
|
let row = [];
|
|
|
|
|
|
|
|
for (let x = 0; x < this.columns; x++) {
|
|
|
|
row.push(null);
|
|
|
|
}
|
2020-02-10 19:53:30 +01:00
|
|
|
|
2020-02-10 19:36:46 +01:00
|
|
|
this.matrix.push(row);
|
2020-01-23 23:09:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 00:12:24 +01:00
|
|
|
setBackgroundColor(color)
|
|
|
|
{
|
|
|
|
this.backgroundColor = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
setBackgroundImage(image)
|
|
|
|
{
|
|
|
|
this.backgroundImage = image;
|
|
|
|
}
|
|
|
|
|
2020-01-25 13:11:25 +01:00
|
|
|
getCollisionRects(rect)
|
|
|
|
{
|
|
|
|
let posX = Math.floor(rect.position.x / this.tileWidth) - 2;
|
|
|
|
let posY = Math.floor(rect.position.y / this.tileHeight) - 2;
|
|
|
|
let rangeX = parseInt( posX + rect.width / this.tileWidth) + 4;
|
|
|
|
let rangeY = parseInt(posY + rect.height / this.tileHeight) + 4;
|
|
|
|
|
|
|
|
let collection = new GeometryRectCollection();
|
|
|
|
|
|
|
|
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) {
|
|
|
|
let intersection = this.matrix[y][x].rect.getRectIntersection(rect);
|
|
|
|
if (intersection !== null) {
|
|
|
|
collection.addRect(intersection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return collection;
|
|
|
|
}
|
|
|
|
|
2020-01-23 23:09:03 +01:00
|
|
|
hasRectCollision(rect)
|
|
|
|
{
|
|
|
|
let posX = Math.floor(rect.position.x / this.tileWidth) - 2;
|
|
|
|
let posY = Math.floor(rect.position.y / this.tileHeight) - 2;
|
|
|
|
let rangeX = parseInt( posX + rect.width / this.tileWidth) + 4;
|
|
|
|
let rangeY = parseInt(posY + rect.height / this.tileHeight) + 4;
|
|
|
|
|
|
|
|
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].rect.getRectIntersection(rect) !== null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-25 23:30:36 +01:00
|
|
|
getTileForPosition(position, offsetX = 0, offsetY = 0)
|
2020-01-23 23:09:03 +01:00
|
|
|
{
|
2020-01-25 23:30:36 +01:00
|
|
|
let x = parseInt(position.x / this.tileWidth) + offsetX;
|
|
|
|
let y = parseInt(position.y / this.tileHeight) + offsetY;
|
2020-01-23 23:09:03 +01:00
|
|
|
|
|
|
|
if (x < 0 || x >= this.columns || y < 0 || y >= this.rows) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-02-12 21:29:06 +01:00
|
|
|
return new GeometryPoint(x, y);
|
2020-01-23 23:09:03 +01:00
|
|
|
}
|
|
|
|
|
2020-01-25 23:30:36 +01:00
|
|
|
getCeilingHeight(position)
|
|
|
|
{
|
|
|
|
let tilePosition = this.getTileForPosition(position, 0);
|
|
|
|
|
|
|
|
while (tilePosition !== null && tilePosition.y > 0) {
|
|
|
|
if (this.matrix[tilePosition.y][tilePosition.x] !== null) {
|
|
|
|
return tilePosition.y * this.tileHeight + this.tileHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
tilePosition.y--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-23 23:09:03 +01:00
|
|
|
getGroundHeight(position)
|
|
|
|
{
|
|
|
|
let tilePosition = this.getTileForPosition(position);
|
|
|
|
|
|
|
|
while (tilePosition !== null && tilePosition.y < this.rows) {
|
|
|
|
if (this.matrix[tilePosition.y][tilePosition.x] !== null) {
|
2020-01-25 23:30:36 +01:00
|
|
|
return tilePosition.y * this.tileHeight;
|
2020-01-23 23:09:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tilePosition.y++;
|
|
|
|
}
|
|
|
|
|
2020-02-12 21:29:06 +01:00
|
|
|
return (this.rows + 10) * this.tileHeight;
|
2020-01-23 23:09:03 +01:00
|
|
|
}
|
|
|
|
|
2020-01-25 23:30:36 +01:00
|
|
|
getWallRight(position)
|
|
|
|
{
|
2020-01-26 23:25:18 +01:00
|
|
|
let tilePosition = this.getTileForPosition(new GeometryPoint(position.x, position.y), 1, 0);
|
2020-01-25 23:30:36 +01:00
|
|
|
|
|
|
|
while (tilePosition !== null && tilePosition.x < this.columns) {
|
|
|
|
if (this.matrix[tilePosition.y][tilePosition.x] !== null) {
|
2020-01-26 11:25:20 +01:00
|
|
|
return tilePosition.x * this.tileWidth;
|
2020-01-25 23:30:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tilePosition.x++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.tileWidth * this.columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
getWallLeft(position)
|
|
|
|
{
|
2020-01-26 23:25:18 +01:00
|
|
|
let tilePosition = this.getTileForPosition(new GeometryPoint(position.x, position.y), -1,0);
|
2020-01-25 23:30:36 +01:00
|
|
|
|
|
|
|
while (tilePosition !== null && tilePosition.x > 0) {
|
|
|
|
if (this.matrix[tilePosition.y][tilePosition.x] !== null) {
|
2020-01-26 11:25:20 +01:00
|
|
|
return tilePosition.x * this.tileWidth + this.tileWidth;
|
2020-01-25 23:30:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tilePosition.x--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-10 19:53:30 +01:00
|
|
|
isInsideArchitecture(geometryPoint)
|
|
|
|
{
|
|
|
|
let architectureRect = new GeometryRect(
|
|
|
|
0, 0, this.tileWidth * this.columns, this.tileHeight * this.rows
|
|
|
|
);
|
|
|
|
|
|
|
|
return architectureRect.isContainingPoint(geometryPoint);
|
|
|
|
}
|
|
|
|
|
2020-02-11 21:20:11 +01:00
|
|
|
setMovableToStartPosition(movable)
|
2020-02-10 19:53:30 +01:00
|
|
|
{
|
|
|
|
movable.position.x = this.tileWidth * this.startX + this.tileWidth * 0.5;
|
|
|
|
movable.position.y = this.tileHeight * this.startY + this.tileHeight * 0.5;
|
|
|
|
}
|
|
|
|
|
2020-02-11 21:20:11 +01:00
|
|
|
setMovableToTargetPosition(movable)
|
|
|
|
{
|
|
|
|
movable.position.x = this.tileWidth * this.targetX + this.tileWidth * 0.5;
|
|
|
|
movable.position.y = this.tileHeight * this.targetY + this.tileHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
isMovableInsideTargetPosition(movable)
|
|
|
|
{
|
|
|
|
let tileRect = new GeometryRect(
|
|
|
|
this.targetX * this.tileWidth,
|
|
|
|
this.targetY * this.tileHeight,
|
|
|
|
this.tileWidth,
|
|
|
|
this.tileHeight
|
|
|
|
);
|
|
|
|
|
|
|
|
return tileRect.isContainingPoint(movable.getPositionHeadLeft()) ||
|
|
|
|
tileRect.isContainingPoint(movable.getPositionHeadRight()) ||
|
|
|
|
tileRect.isContainingPoint(movable.getPositionFootLeft()) ||
|
|
|
|
tileRect.isContainingPoint(movable.getPositionFootRight());
|
|
|
|
}
|
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
draw(context, camera = new Camera())
|
2020-01-23 23:09:03 +01:00
|
|
|
{
|
2020-02-09 22:06:59 +01:00
|
|
|
let viewX = parseInt(Math.floor(Math.max(0, camera.position.x) / this.tileWidth));
|
|
|
|
let viewWidth = parseInt(Math.min(Math.ceil(camera.width), this.columns));
|
|
|
|
let viewY = parseInt(Math.floor(Math.max(0, camera.position.y)) / this.tileHeight);
|
|
|
|
let viewHeight = parseInt(Math.min(Math.ceil(camera.height), this.rows));
|
|
|
|
|
|
|
|
for (let y = viewY; y < viewHeight; y++) {
|
|
|
|
for (let x = viewX; x < viewWidth; x++) {
|
2020-01-23 23:09:03 +01:00
|
|
|
let field = this.matrix[y][x];
|
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
if (field !== null) {
|
2020-02-14 00:12:24 +01:00
|
|
|
this.drawField(context, x, y, camera, field);
|
2020-01-23 23:09:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 00:12:24 +01:00
|
|
|
drawField(context, x, y, camera, field)
|
|
|
|
{
|
|
|
|
context.drawImage(
|
2020-02-16 00:53:03 +01:00
|
|
|
this.tilesetSprite.canvas,
|
|
|
|
field.tile.index * this.tileWidth,
|
2020-02-14 00:12:24 +01:00
|
|
|
0,
|
|
|
|
this.tileWidth,
|
|
|
|
this.tileHeight,
|
|
|
|
x * this.tileWidth - camera.position.x,
|
|
|
|
y * this.tileHeight - camera.position.y,
|
|
|
|
this.tileWidth + 1,
|
|
|
|
this.tileHeight + 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
static createFromData(level)
|
2020-01-23 23:09:03 +01:00
|
|
|
{
|
2020-02-16 00:53:03 +01:00
|
|
|
let graphicSet = GraphicSet[level.getTilesetId()];
|
|
|
|
|
2020-02-14 00:12:24 +01:00
|
|
|
let tileset = new RetroSprite(
|
|
|
|
Setting.TILESET_LOCATION + graphicSet.tileset,
|
|
|
|
graphicSet.scale
|
|
|
|
);
|
2020-01-23 23:09:03 +01:00
|
|
|
|
|
|
|
let architecture = new RetroArchitecture(
|
|
|
|
tileset,
|
2020-02-14 00:12:24 +01:00
|
|
|
graphicSet.tiles,
|
2020-02-16 00:53:03 +01:00
|
|
|
level.getColumns(),
|
|
|
|
level.getRows()
|
2020-01-23 23:09:03 +01:00
|
|
|
);
|
|
|
|
|
2020-02-14 00:12:24 +01:00
|
|
|
architecture.setBackgroundColor(graphicSet.backgroundColor);
|
|
|
|
architecture.setBackgroundImage(graphicSet.backgroundImage);
|
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
architecture.startX = level.getStartX();
|
|
|
|
architecture.startY = level.getStartY();
|
|
|
|
architecture.targetX = level.getTargetX();
|
|
|
|
architecture.targetY = level.getTargetY();
|
|
|
|
architecture.targetPosition = new GeometryPoint(level.getTargetX(), level.getTargetY());
|
2020-02-10 19:53:30 +01:00
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
for (let y = 0; y < level.getRows(); y++) {
|
|
|
|
for (let x = 0; x < level.getColumns(); x++) {
|
|
|
|
if (level.getTilesetMatrix()[y][x].index > -1) {
|
2020-01-23 23:09:03 +01:00
|
|
|
architecture.matrix[y][x] = new RetroArchitectureTile(
|
2020-02-16 00:53:03 +01:00
|
|
|
level.getTilesetMatrix()[y][x],
|
2020-01-23 23:09:03 +01:00
|
|
|
x * architecture.tileWidth,
|
|
|
|
y * architecture.tileHeight,
|
|
|
|
architecture.tileWidth,
|
|
|
|
architecture.tileHeight
|
2020-02-16 00:53:03 +01:00
|
|
|
);
|
2020-01-23 23:09:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return architecture;
|
|
|
|
}
|
|
|
|
}
|