-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Begin support for parallax scrolling backgrounds in web client
- Loading branch information
1 parent
31a900e
commit cfe7596
Showing
6 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/*************************************************************************** | ||
* Copyright © 2024 - Faiumoni e. V. * | ||
*************************************************************************** | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU Affero General Public License as * | ||
* published by the Free Software Foundation; either version 3 of the * | ||
* License, or (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
import { singletons } from "../SingletonRepo"; | ||
|
||
|
||
export class ParallaxBackground { | ||
|
||
/** Tiled image to be drawn. */ | ||
private image?: HTMLImageElement; | ||
|
||
/** Singleton instance. */ | ||
private static instance: ParallaxBackground; | ||
|
||
|
||
/** | ||
* Retrieves singleton instance. | ||
*/ | ||
static get(): ParallaxBackground { | ||
if (!ParallaxBackground.instance) { | ||
ParallaxBackground.instance = new ParallaxBackground(); | ||
} | ||
return ParallaxBackground.instance; | ||
} | ||
|
||
/** | ||
* Hidden singleton constructor. | ||
*/ | ||
private constructor() { | ||
// do nothing | ||
} | ||
|
||
/** | ||
* Sets image to be drawn. | ||
* | ||
* @param name {string} | ||
* Relative path (exluding .png filename suffix) to image inside "data/maps/parallax" directory | ||
* or `undefined` to unset. | ||
*/ | ||
public setImage(name?: string) { | ||
if (!name) { | ||
this.image = undefined; | ||
return; | ||
} | ||
const fullPath = singletons.getPaths().parallax + "/" + name + ".png"; | ||
this.image = singletons.getSpriteStore().get(fullPath); | ||
} | ||
|
||
draw(ctx: CanvasRenderingContext2D, offsetX: number, offsetY: number) { | ||
if (!this.image || !this.image.height) { | ||
return; | ||
} | ||
// FIXME: jumps when shifting tiles | ||
const tilesX = Math.max(Math.ceil(ctx.canvas.width / this.image.width) + 1, 1); | ||
const tilesY = Math.max(Math.ceil(ctx.canvas.height / this.image.height) + 1, 1); | ||
const clipLeft = (offsetX / 2) % ctx.canvas.width; | ||
const clipTop = (offsetY / 2) % ctx.canvas.height; | ||
for (let ix = 0; ix < tilesX; ix++) { | ||
for (let iy = 0; iy < tilesY; iy++) { | ||
ctx.drawImage(this.image, | ||
0, 0, this.image.width, this.image.height, | ||
(ix*this.image.width)+offsetX-clipLeft, | ||
(iy*this.image.height)+offsetY-clipTop, | ||
this.image.width, this.image.height); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters