Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Scene specific background color #2861

Merged
merged 2 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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