Skip to content

Commit

Permalink
Set parallax when Map.onTransfer called
Browse files Browse the repository at this point in the history
  • Loading branch information
AntumDeluge committed Jun 1, 2024
1 parent 9b8f0fe commit 7b0a0b4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/js/stendhal/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ export class Client {
!Number.isNaN(musicVolume) ? musicVolume : 1.0);

// parallax background
stendhal.data.map.parallax.setImage(zoneinfo["parallax"]);
stendhal.data.map.setParallax(zoneinfo["parallax"]);

// coloring information
if (zoneinfo["color"] && stendhal.config.getBoolean("effect.lighting")) {
Expand Down
19 changes: 19 additions & 0 deletions src/js/stendhal/data/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class Map {
];

private parallax: ParallaxBackground;
private parallaxImage?: string;

/** Singleton instance. */
private static instance: Map;
Expand Down Expand Up @@ -126,6 +127,11 @@ export class Map {
this.layerGroupIndexes = this.mapLayerGroup();

this.strategy.onMapLoaded(this);

if (this.parallaxImage) {
this.parallax.setImage(this.parallaxImage, this.zoneSizeX * this.tileWidth,
this.zoneSizeY * this.tileHeight);
}
}

decodeTileset(content: any, name: string) {
Expand Down Expand Up @@ -226,4 +232,17 @@ export class Map {
hasSafeTileset(filename: string) {
return this.knownSafeTilesets.indexOf(filename) > -1;
}

/**
* Sets or unsets parallax image.
*
* @param {string=} name
* Background image filename.
*/
setParallax(name?: string) {
this.parallaxImage = name;
if (typeof(name) === "undefined") {
this.parallax.reset();
}
}
}
35 changes: 29 additions & 6 deletions src/js/stendhal/landscape/ParallaxBackground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,34 @@ export class ParallaxBackground {
/**
* Sets image to be drawn.
*
* @param name {string}
* @param {string} name
* Relative path (exluding .png filename suffix) to image inside "data/maps/parallax" directory
* or `undefined` to unset.
* @param {number} width
* Map pixel width.
* @param {number} height
* Map pixel height.
*/
public setImage(name?: string) {
if (!name) {
this.image = undefined;
return;
}
setImage(name: string, width: number, height: number) {
const fullPath = singletons.getPaths().parallax + "/" + name + ".png";
this.image = singletons.getSpriteStore().get(fullPath);

/* FIXME:
//this.image = singletons.getTileStore().getParallax(name, ParallaxBackground.SCROLL, width, height);
singletons.getTileStore().getParallaxPromise(name, ParallaxBackground.SCROLL, width, height)
.then(image => {
this.image = image;
}).catch(error => {
console.error("Error setting parallax background \"" + name + "\"\n", error);
});
*/
}

/**
* Unsets parallax background image.
*/
reset() {
this.image = undefined;
}

draw(ctx: CanvasRenderingContext2D, offsetX: number, offsetY: number) {
Expand All @@ -71,5 +88,11 @@ export class ParallaxBackground {
ctx.drawImage(this.image, dx, dy);
}
}

/* TODO: use the following when `setImage` fixed
const tileLeft = offsetX - (offsetX * ParallaxBackground.SCROLL);
const tileTop = offsetY - (offsetY * ParallaxBackground.SCROLL);
ctx.drawImage(this.image, tileLeft, tileTop);
*/
}
}

0 comments on commit 7b0a0b4

Please sign in to comment.