From 66ced089876744fb9aaf42a540a9d432eb416e3d Mon Sep 17 00:00:00 2001 From: Joe Weaver Date: Thu, 20 Aug 2020 14:00:38 -0400 Subject: [PATCH] added some error checking to tilemaps and alpha value to scenes --- src/GameState/Scene.ts | 11 +++++++++++ src/Nodes/Tilemaps/OrthogonalTilemap.ts | 8 +++++--- src/main.ts | 12 +++++++++--- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/GameState/Scene.ts b/src/GameState/Scene.ts index 44fcaed..0b6a031 100644 --- a/src/GameState/Scene.ts +++ b/src/GameState/Scene.ts @@ -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; 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; } } } \ No newline at end of file diff --git a/src/Nodes/Tilemaps/OrthogonalTilemap.ts b/src/Nodes/Tilemaps/OrthogonalTilemap.ts index aab4268..c3d4512 100644 --- a/src/Nodes/Tilemaps/OrthogonalTilemap.ts +++ b/src/Nodes/Tilemaps/OrthogonalTilemap.ts @@ -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))); diff --git a/src/main.ts b/src/main.ts index 56b284c..d015000 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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();