-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: layers and layer component (#121)
Co-authored-by: Daniel Báez <lajbelms@gmail.com>
- Loading branch information
1 parent
ec86a8f
commit a81d952
Showing
6 changed files
with
163 additions
and
121 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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
kaplay(); | ||
|
||
layers(["bg", "game", "ui"], "game"); | ||
|
||
// bg layer | ||
add([ | ||
rect(width(), height()), | ||
layer("bg"), | ||
color(rgb(64, 128, 255)), | ||
// opacity(0.5) | ||
]).add([text("BG")]); | ||
|
||
// game layer explicit | ||
add([ | ||
pos(width() / 5, height() / 5), | ||
rect(width() / 3, height() / 3), | ||
layer("game"), | ||
color(rgb(255, 128, 64)), | ||
]).add([text("GAME")]); | ||
|
||
// game layer implicit | ||
add([ | ||
pos(3 * width() / 5, 3 * height() / 5), | ||
rect(width() / 3, height() / 3), | ||
color(rgb(255, 128, 64)), | ||
]).add([pos(width() / 3, height() / 3), text("GAME"), anchor("botright")]); | ||
|
||
// ui layer | ||
add([ | ||
pos(center()), | ||
rect(width() / 2, height() / 2), | ||
anchor("center"), | ||
color(rgb(64, 255, 128)), | ||
]).add([text("UI"), anchor("center")]); |
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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { getInternalContext, getKaboomContext } from "@/kaboom"; | ||
import type { LayerComp } from "@/types"; | ||
|
||
export function layer(layer: string): LayerComp { | ||
const k = getKaboomContext(this); | ||
const internal = getInternalContext(k); | ||
let _layerIndex = internal.game.layers.indexOf(layer); | ||
return { | ||
id: "layer", | ||
get layerIndex() { | ||
return _layerIndex; | ||
}, | ||
get layer(): string { | ||
return k.layers[_layerIndex]; | ||
}, | ||
set layer(value: string) { | ||
_layerIndex = internal.game.layers.indexOf(value); | ||
if (_layerIndex == -1) throw Error("Invalid layer name"); | ||
}, | ||
inspect() { | ||
return `${this.layer}`; | ||
}, | ||
}; | ||
} |
Oops, something went wrong.