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

View File

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

View File

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