Skip to content

Commit

Permalink
feat: Add Scene specific background color (#2861)
Browse files Browse the repository at this point in the history
This PR is the first a few adding more Scene specific APIs. In particular this adds a background color property to Scenes.
  • Loading branch information
eonarheim authored Jan 1, 2024
1 parent 1da7290 commit 661cd24
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Added

- Added Scene specific background color
- Added ability to apply draw offset to `ex.IsometricMap` and `ex.Tilemap`
- Added `visibility` and `opacity` to `ex.IsometricMap`
- Added base elevation for `ex.IsometricMap` so multiple maps can sort correctly
Expand Down
1 change: 1 addition & 0 deletions sandbox/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ player.on('pointerwheel', () => {
});

var newScene = new ex.Scene();
newScene.backgroundColor = ex.Color.Yellow;
newScene.add(new ex.Label({text: 'MAH LABEL!', x: 200, y: 100}));
newScene.on('activate', (evt?: ex.ActivateEvent) => {
console.log('activate newScene');
Expand Down
3 changes: 2 additions & 1 deletion src/engine/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,8 @@ O|===|* >________________>\n\
return;
}

this.graphicsContext.backgroundColor = this.backgroundColor;
// Use scene background color if present, fallback to engine
this.graphicsContext.backgroundColor = this.currentScene.backgroundColor ?? this.backgroundColor;

this.currentScene.draw(this.graphicsContext, delta);

Expand Down
6 changes: 6 additions & 0 deletions src/engine/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { OffscreenSystem } from './Graphics/OffscreenSystem';
import { ExcaliburGraphicsContext } from './Graphics';
import { PhysicsWorld } from './Collision/PhysicsWorld';
import { EventEmitter, EventKey, Handler, Subscription } from './EventEmitter';
import { Color } from './Color';

export type SceneEvents = {
initialize: InitializeEvent<Scene>,
Expand Down Expand Up @@ -75,6 +76,11 @@ implements CanInitialize, CanActivate<TActivationData>, CanDeactivate, CanUpdate
*/
public camera: Camera = new Camera();

/**
* Scene specific background color
*/
public backgroundColor?: Color;

/**
* The ECS world for the scene
*/
Expand Down
15 changes: 15 additions & 0 deletions src/spec/SceneSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ describe('A scene', () => {
expect(ex.Scene).toBeTruthy();
});

it('can have a background color set', () => {
engine.backgroundColor = ex.Color.Black;

const newScene = new ex.Scene();
newScene.backgroundColor = ex.Color.Yellow;

engine.addScene('background', newScene);
engine.goToScene('background');

(engine as any)._draw(100);

expect(engine.graphicsContext.backgroundColor).toEqual(ex.Color.Yellow);
expect(engine.graphicsContext.backgroundColor).toEqual(newScene.backgroundColor);
});

it('cannot have the same ScreenElement added to it more than once', () => {
engine.goToScene('root');
const screenElement = new ex.ScreenElement();
Expand Down

0 comments on commit 661cd24

Please sign in to comment.