added some error checking to tilemaps and alpha value to scenes

This commit is contained in:
Joe Weaver 2020-08-20 14:00:38 -04:00
parent bd961d04d3
commit 66ced08987
3 changed files with 25 additions and 6 deletions

View File

@ -9,6 +9,7 @@ import Tilemap from "../Nodes/Tilemap";
import TilemapFactory from "./Factories/TilemapFactory";
import PhysicsManager from "../Physics/PhysicsManager";
import PhysicsNodeFactory from "./Factories/PhysicsNodeFactory";
import MathUtils from "../Utils/MathUtils";
export default class Scene {
private gameState: GameState;
@ -19,6 +20,7 @@ export default class Scene {
private tilemaps: Array<Tilemap>;
private paused: boolean;
private hidden: boolean;
private alpha: number;
// Factories
public canvasNode: CanvasNodeFactory;
@ -45,6 +47,10 @@ export default class Scene {
this.paused = pauseValue;
}
setAlpha(alpha: number): void {
this.alpha = MathUtils.clamp(alpha, 0, 1);
}
isPaused(): boolean {
return this.paused;
}
@ -97,6 +103,9 @@ export default class Scene {
render(ctx: CanvasRenderingContext2D): void {
if(!this.hidden){
let previousAlpha = ctx.globalAlpha;
ctx.globalAlpha = this.alpha;
let visibleSet = this.sceneGraph.getVisibleSet();
let viewportOrigin = this.viewport.getPosition();
let origin = new Vec2(viewportOrigin.x*this.parallax.x, viewportOrigin.y*this.parallax.y);
@ -111,6 +120,8 @@ export default class Scene {
// Render visible set
visibleSet.forEach(node => node.render(ctx, origin));
ctx.globalAlpha = previousAlpha;
}
}
}

View File

@ -12,9 +12,11 @@ export default class OrthogonalTilemap extends Tilemap {
this.data = layer.data;
this.visible = layer.visible;
this.collidable = false;
for(let item of layer.properties){
if(item.name === "Collidable"){
this.collidable = item.value;
if(layer.properties){
for(let item of layer.properties){
if(item.name === "Collidable"){
this.collidable = item.value;
}
}
}
tilemapData.tilesets.forEach(tilesetData => this.tilesets.push(new Tileset(tilesetData)));

View File

@ -13,7 +13,8 @@ function main(){
let gameState = game.getGameState();
let backgroundScene = gameState.createScene();
backgroundScene.setParallax(0.5, 0.5);
backgroundScene.setParallax(0.5, 0.8);
backgroundScene.setAlpha(0.5);
let mainScene = gameState.createScene();
let uiLayer = gameState.createScene();
uiLayer.setParallax(0, 0);
@ -73,8 +74,13 @@ function main(){
pauseMenu.disable();
}
mainScene.tilemap.add(OrthogonalTilemap, "assets/tilemaps/Platformer.json");
let player = mainScene.physics.add(Player, "platformer");
// backgroundScene.tilemap.add(OrthogonalTilemap, "assets/tilemaps/Background.json");
// mainScene.tilemap.add(OrthogonalTilemap, "assets/tilemaps/Platformer.json");
// let player = mainScene.physics.add(Player, "platformer");
mainScene.tilemap.add(OrthogonalTilemap, "assets/tilemaps/TopDown.json");
let player = mainScene.physics.add(Player, "topdown");
mainScene.getViewport().follow(player);
pauseMenu.disable();