-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,92 @@ | ||
|
||
//% block="Split Screen" | ||
//% icon="\uf030" | ||
//% color="#bd7844" | ||
namespace splitScreen { | ||
let rowBuff: Buffer; | ||
class SplitScreenState { | ||
fakeCamera: scene.Camera; | ||
fakeScreen: ScreenImage; | ||
renderable: scene.Renderable; | ||
isRendering: boolean; | ||
realScreen: ScreenImage; | ||
realCamera: scene.Camera; | ||
|
||
constructor() { | ||
this.fakeCamera = new scene.Camera(); | ||
this.fakeScreen = image.create(screen.width, screen.height) as ScreenImage; | ||
this.isRendering = false; | ||
|
||
|
||
this.renderable = scene.createRenderable(99, () => { | ||
if (!this.fakeCamera.sprite) return; | ||
|
||
if (!this.isRendering) { | ||
this.fakeCamera.update(); | ||
this.realScreen = screen; | ||
this.realCamera = game.currentScene().camera | ||
screen = this.fakeScreen | ||
game.currentScene().camera = this.fakeCamera | ||
|
||
this.isRendering = true; | ||
// black magic | ||
game.currentScene().flags &= ~(scene.Flag.IsRendering) | ||
game.currentScene().render(); | ||
this.isRendering = false; | ||
|
||
screen = this.realScreen; | ||
game.currentScene().camera = this.realCamera; | ||
this.realScreen.fillRect(79, 0, 2, 120, 15) | ||
} | ||
else { | ||
const tm = game.currentScene().tileMap; | ||
|
||
const realOffsetX = Math.clamp(0, tm.areaWidth(), this.realCamera.sprite.x - 40); | ||
shiftScreen(this.realScreen, Math.min(realOffsetX - this.realCamera.offsetX, 80)); | ||
|
||
const fakeOffsetX = Math.clamp(0, tm.areaWidth(), this.fakeCamera.sprite.x - 40); | ||
|
||
shiftScreen(this.fakeScreen, Math.min(fakeOffsetX - this.fakeCamera.offsetX, 80)); | ||
this.realScreen.drawImage(this.fakeScreen, 80, 0); | ||
} | ||
}) | ||
} | ||
} | ||
|
||
let stateStack: SplitScreenState[]; | ||
|
||
/** | ||
* Adds a splitscreen camera to a game | ||
*/ | ||
//% blockId="splitscreencamerafollowsprite" | ||
//% block="split screen camera follow $sprite" | ||
export function splitScreenCameraFollow(sprite: Sprite) { | ||
state().fakeCamera.sprite = sprite; | ||
} | ||
|
||
function init() { | ||
if (stateStack) return; | ||
stateStack = [new SplitScreenState()]; | ||
rowBuff = control.createBuffer(screen.height); | ||
|
||
game.addScenePushHandler(() => { | ||
stateStack.push(new SplitScreenState()) | ||
}) | ||
|
||
game.addScenePopHandler(() => { | ||
stateStack.pop(); | ||
if (stateStack.length === 0) stateStack.push(new SplitScreenState()) | ||
}) | ||
} | ||
|
||
function state() { | ||
init(); | ||
return stateStack[stateStack.length - 1]; | ||
} | ||
|
||
function shiftScreen(target: Image, numPixels: number) { | ||
for (let x = 0; x < target.width - numPixels; x++) { | ||
target.getRows(x + numPixels, rowBuff) | ||
target.setRows(x, rowBuff) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "Untitled", | ||
"name": "arcade-split-screen", | ||
"description": "", | ||
"dependencies": { | ||
"device": "*" | ||
|