Skip to content

Commit

Permalink
[FIX] Switch texture command: problem with empty textures
Browse files Browse the repository at this point in the history
  • Loading branch information
Wano-k committed Oct 4, 2023
1 parent 7d9fc91 commit 397e31a
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 31 deletions.
8 changes: 4 additions & 4 deletions src/Core/Autotile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ class Autotile extends Land {
* @returns {StructMapElementCollision}
*/
updateGeometryAutotile(geometry: CustomGeometry, texture:
TextureBundle, position: Position, width: number, height: number, count:
number): StructMapElementCollision
TextureBundle, position: Position, width: number, height: number,
pictureID: number, count: number): StructMapElementCollision
{
let autotile = Datas.SpecialElements.getAutotile(this.autotileID);
let picture = autotile ? Datas.Pictures.get(PictureKind.Autotiles,
autotile.pictureID) : null;
pictureID) : null;
return super.updateGeometryLand(geometry, picture ? picture
.getCollisionAtIndex(Land.prototype.getIndex.call(this, picture
.width)) : null, position, width, height, ((this.tileID % 64) *
Datas.Systems.SQUARE_SIZE) / width, ((Math.floor(this.tileID / 64) +
(10 * texture.getOffset(autotile.pictureID, this.texture))) * Datas
(10 * texture.getOffset(pictureID, this.texture))) * Datas
.Systems.SQUARE_SIZE) / height, Datas.Systems.SQUARE_SIZE / width,
Datas.Systems.SQUARE_SIZE / height, count);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Autotiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ class Autotiles {
* @param {Autotile} autotile - The autotile to add to geometry
* @returns {StructMapElementCollision}
*/
updateGeometry(position: Position, autotile: Autotile):
updateGeometry(position: Position, autotile: Autotile, pictureID: number):
StructMapElementCollision
{
return this.width === null || this.height === 0 ? null : autotile
.updateGeometryAutotile(this.geometry, this.bundle, position, this
.width, this.height, this.index++);
.width, this.height, pictureID, this.index++);
}

/**
Expand Down
36 changes: 26 additions & 10 deletions src/Core/MapPortion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,17 @@ class MapPortion {
jsonAutotile = json[i];
position = Position.createFromArray(jsonAutotile.k);
autotile = new Autotile(jsonAutotile.v);
let pictureID = Game.current.textures.autotiles[autotile.autotileID];
if (pictureID === undefined) {
pictureID = Datas.SpecialElements.getAutotile(autotile.autotileID).pictureID;
}
indexPos = position.toIndex();
texture = null;
texturesAutotile = Scene.Map.current.texturesAutotiles[autotile.autotileID];
if (texturesAutotile) {
for (j = 0, m = texturesAutotile.length; j < m; j++) {
textureAutotile = texturesAutotile[j];
if (textureAutotile.isInTexture(Datas.SpecialElements
.getAutotile(autotile.autotileID).pictureID, autotile.texture))
if (textureAutotile.isInTexture(pictureID, autotile.texture))
{
texture = textureAutotile;
autotiles = this.staticAutotilesList[autotile.autotileID][j];
Expand All @@ -243,7 +246,7 @@ class MapPortion {
}
}
if (texture !== null && texture.material !== null) {
objCollision = autotiles.updateGeometry(position, autotile);
objCollision = autotiles.updateGeometry(position, autotile, pictureID);
if (objCollision !== null) {
this.boundingBoxesLands[indexPos].push(objCollision);
}
Expand Down Expand Up @@ -362,6 +365,10 @@ class MapPortion {
s = json[i];
position = Position.createFromArray(s.k);
sprite = new SpriteWall(s.v);
let pictureID = Game.current.textures.walls[sprite.id];
if (pictureID === undefined) {
pictureID = Datas.SpecialElements.getWall(sprite.id).pictureID;
}

// Constructing the geometry
obj = hash[sprite.id];
Expand All @@ -387,7 +394,7 @@ class MapPortion {
let texture = Manager.GL.getMaterialTexture(material);
if (texture) {
result = sprite.updateGeometry(geometry, position, texture
.image.width, texture.image.height, count);
.image.width, texture.image.height, pictureID, count);
obj.c = result[0];
this.updateCollisionSprite(result[1], position);
}
Expand Down Expand Up @@ -445,18 +452,22 @@ class MapPortion {
position = Position.createFromArray(jsonMountain.k);
mountain = new Mountain;
mountain.read(jsonMountain.v);
let pictureID = Game.current.textures.mountains[mountain.mountainID];
if (pictureID === undefined) {
pictureID = Datas.SpecialElements.getMountain(mountain.mountainID)
.pictureID;
}
indexPos = position.toIndex();
for (index = 0; index < mountainsLength; index++) {
textureMountain = Scene.Map.current.texturesMountains[index];
if (textureMountain.isInTexture(Datas.SpecialElements
.getMountain(mountain.mountainID).pictureID)) {
if (textureMountain.isInTexture(pictureID)) {
texture = textureMountain;
mountains = this.staticMountainsList[index];
break;
}
}
if (texture !== null && texture.material !== null) {
objCollision = mountains.updateGeometry(position, mountain);
objCollision = mountains.updateGeometry(position, mountain, pictureID);
this.updateCollision(this.boundingBoxesMountains, objCollision,
position, true);
}
Expand Down Expand Up @@ -510,6 +521,11 @@ class MapPortion {
position = Position.createFromArray(o.k);
v = o.v;
datas = Datas.SpecialElements.getObject3D(v.did);
let pictureID = Game.current.textures.objects3D[datas.id];
if (pictureID === undefined) {
pictureID = Datas.SpecialElements.getObject3D(datas.id)
.pictureID;
}
if (datas) {
switch (datas.shapeKind) {
case ShapeKind.Box:
Expand All @@ -529,10 +545,10 @@ class MapPortion {
}

// Constructing the geometry
obj = hash[obj3D.datas.pictureID];
obj = hash[datas.id];
if (!Utils.isUndefined(obj)) {
if (obj === null) {
material = Scene.Map.current.texturesObjects3D[obj3D.datas.pictureID];
material = Scene.Map.current.texturesObjects3D[datas.id];
if (material) {
geometry = new CustomGeometry();
count = 0;
Expand All @@ -541,7 +557,7 @@ class MapPortion {
material: material,
c: count
};
hash[obj3D.datas.pictureID] = obj;
hash[datas.id] = obj;
}
} else {
geometry = obj.geometry;
Expand Down
7 changes: 4 additions & 3 deletions src/Core/Mountain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,15 +411,16 @@ class Mountain extends MapElement {
* @param {Core.CustomGeometry} geometry - The geometry of mountains
* @param {TextureBundle} texture - The texture mountain
* @param {Position} position - The position
* @param {number} pictureID - The current mountain picture ID
* @param {number} count - The faces count
* @return {any[]}
*/
updateGeometry(geometry: CustomGeometry, texture: TextureBundle,
position: Position, count: number) : any[]
position: Position, pictureID: number, count: number) : any[]
{
// General configurations
let yOffset = texture.getOffset(Datas.SpecialElements.getMountain(this
.mountainID).pictureID, null) * 4 * Datas.Systems.SQUARE_SIZE;
let yOffset = texture.getOffset(pictureID, null) * 4 * Datas.Systems
.SQUARE_SIZE;
let wp = this.getWidthTotalPixels();
let hp = this.getHeightTotalPixels();
let width = 4 * Datas.Systems.SQUARE_SIZE;
Expand Down
5 changes: 3 additions & 2 deletions src/Core/Mountains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ class Mountains {
* Update the geometry of the mountains according to a mountain.
* @param {Position} position - The position
* @param {Mountain} mountain - The moutain to update
* @param {number} pictureID - The current mountain picture ID
*/
updateGeometry(position: Position, mountain: Mountain):
updateGeometry(position: Position, mountain: Mountain, pictureID: number):
StructMapElementCollision[]
{
let res = mountain.updateGeometry(this.geometry, this.bundle, position,
this.count);
pictureID, this.count);
this.count = res[0];
return res[1];
}
Expand Down
4 changes: 2 additions & 2 deletions src/Core/SpriteWall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class SpriteWall extends MapElement {
* @return {any[]}
*/
updateGeometry(geometry: CustomGeometry, position: Position, width:
number, height: number, count: number): [number,
number, height: number, pictureID: number, count: number): [number,
StructMapElementCollision[]]
{
let vecA = new Vector3(-0.5, 1.0, 0.0);
Expand Down Expand Up @@ -108,7 +108,7 @@ class SpriteWall extends MapElement {
let collisions: number[][] = [];
let wall = Datas.SpecialElements.getWall(this.id);
if (wall) {
let picture = Datas.Pictures.get(PictureKind.Walls, wall.pictureID);
let picture = Datas.Pictures.get(PictureKind.Walls, pictureID);
if (picture) {
collisions = picture.getSquaresForWall(textureRect);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Datas/SpecialElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class SpecialElements {
static async loadAutotileTexture(id: number): Promise<TextureBundle[]> {
let autotile = this.getAutotile(id);
let pictureID = Game.current.textures.autotiles[id];
if (Utils.isUndefined(pictureID)) {
if (pictureID === undefined) {
pictureID = autotile.pictureID;
}
let texturesAutotile = this.texturesAutotiles[pictureID];
Expand Down Expand Up @@ -291,7 +291,7 @@ class SpecialElements {
static async loadWallTexture(id: number): Promise<THREE.MeshPhongMaterial> {
let wall = this.getWall(id);
let pictureID = Game.current.textures.walls[id];
if (Utils.isUndefined(pictureID)) {
if (pictureID === undefined) {
pictureID = wall.pictureID;
}
let textureWall = this.texturesWalls[pictureID];
Expand Down Expand Up @@ -377,7 +377,7 @@ class SpecialElements {
static async loadMountainTexture(id: number): Promise<TextureBundle> {
let mountain = this.getMountain(id);
let pictureID = Game.current.textures.mountains[id];
if (Utils.isUndefined(pictureID)) {
if (pictureID === undefined) {
pictureID = mountain.pictureID;
}
let textureMountain = this.texturesMountains[pictureID];
Expand Down Expand Up @@ -537,7 +537,7 @@ class SpecialElements {
static async loadObject3DTexture(id: number): Promise<THREE.MeshPhongMaterial> {
let object3D = this.getObject3D(id);
let pictureID = Game.current.textures.objects3D[id];
if (Utils.isUndefined(pictureID)) {
if (pictureID === undefined) {
pictureID = object3D.pictureID;
}
let textureObject3D = this.texturesObjects3D[pictureID];
Expand Down
23 changes: 21 additions & 2 deletions src/Scene/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,9 +774,28 @@ class Map extends Base {
{
let special: System.SpecialElement, picture: System.Picture;
for (let i = 0, l = list.length; i < l; i++) {
special = specials[list[i]];
const id = list[i];
special = specials[id];
if (special) {
picture = Datas.Pictures.get(kind, special.pictureID);
let pictureID = undefined;
switch (kind) {
case Enum.PictureKind.Autotiles:
pictureID = Game.current.textures.autotiles[id];
break;
case Enum.PictureKind.Mountains:
pictureID = Game.current.textures.mountains[id];
break;
case Enum.PictureKind.Walls:
pictureID = Game.current.textures.walls[id];
break;
case Enum.PictureKind.Objects3D:
pictureID = Game.current.textures.objects3D[id];
break;
}
if (pictureID === undefined) {
pictureID = special.pictureID;
}
picture = Datas.Pictures.get(kind, pictureID);
if (picture) {
picture.readCollisions();
}
Expand Down
3 changes: 1 addition & 2 deletions src/System/Tileset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ class Tileset extends Base {
async getTexturesObjects3D(): Promise<THREE.MeshPhongMaterial[]> {
let list: THREE.MeshPhongMaterial[] = [];
for (let id of this.objects) {
list[Datas.SpecialElements.getObject3D(id).pictureID] = await Datas
.SpecialElements.loadObject3DTexture(id);
list[id] = await Datas.SpecialElements.loadObject3DTexture(id);
}
return list;
}
Expand Down

0 comments on commit 397e31a

Please sign in to comment.