-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit a51327c
Showing
45 changed files
with
2,163 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
indent_size = 4 | ||
indent_style = space | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
max_line_length = 120 | ||
tab_width = 4 |
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,49 @@ | ||
name: Deploy Demo | ||
|
||
on: | ||
push: | ||
branches: ["main"] | ||
workflow_dispatch: | ||
|
||
permissions: | ||
contents: read | ||
pages: write | ||
id-token: write | ||
|
||
concurrency: | ||
group: "pages" | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
deploy: | ||
environment: | ||
name: github-pages | ||
url: ${{ steps.deployment.outputs.page_url }} | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Git Checkout | ||
uses: actions/checkout@v4 | ||
- name: Setup pnpm | ||
uses: pnpm/[email protected] | ||
with: | ||
version: latest | ||
- name: Setup Node.js environment | ||
uses: actions/[email protected] | ||
with: | ||
node-version: lts/* | ||
cache: pnpm | ||
- name: Setup Pages | ||
uses: actions/configure-pages@v5 | ||
- name: Install Dependencies | ||
run: pnpm install | ||
- name: Prettier check | ||
run: pnpm exec prettier --check . | ||
- name: Build | ||
run: pnpm run test:build | ||
- name: Upload artifact | ||
uses: actions/upload-pages-artifact@v3 | ||
with: | ||
path: "./test/dist" | ||
- name: Deploy to GitHub Pages | ||
id: deployment | ||
uses: actions/deploy-pages@v4 |
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,3 @@ | ||
.DS_Store | ||
node_modules | ||
dist/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,9 @@ | ||
.github/ | ||
test/ | ||
dist/ | ||
*.js | ||
.DS_Store | ||
.editorconfig | ||
pnpm-lock.yaml | ||
pnpm-workspace.yaml | ||
prettier.config.mjs |
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,7 @@ | ||
Copyright 2024 Frank Mayer | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,23 @@ | ||
# ECS.ts | ||
|
||
Strongly inspired by Bevy | ||
|
||
```ts | ||
import { App, Commands, DefaultPlugin, HtmlPlugin, Schedule, UiNode, UiText } from "@tsukinoko-kun/ecs.ts" | ||
|
||
const app = new App() | ||
|
||
app.addPlugin(DefaultPlugin) | ||
.addPlugin(HtmlPlugin("#app")) | ||
.addPlugin((world) => { | ||
world.addSystem(Schedule.Start, () => { | ||
Commands.spawn(new UiNode()).withChildren((parent) => { | ||
parent.spawn(new UiText("meep")) | ||
}) | ||
|
||
Commands.spawn(new UiText("hello world")) | ||
}) | ||
}) | ||
|
||
app.run() | ||
``` |
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,62 @@ | ||
import type { Plugin } from "./plugin" | ||
import { setCurrentWorld, World } from "./world" | ||
import type { System } from "./system" | ||
import { Schedule } from "./schedule" | ||
import { Time } from "./builtin/resources/time" | ||
|
||
export class App { | ||
private readonly plugins = new Array<Plugin>() | ||
private readonly world = new World() | ||
|
||
public addPlugin(plugin: Plugin): this { | ||
plugin(this.world) | ||
return this | ||
} | ||
|
||
public addSystem(schedule: Schedule, system: System): this { | ||
this.world.addSystem(schedule, system) | ||
return this | ||
} | ||
|
||
public run(): void { | ||
setCurrentWorld(this.world) | ||
|
||
for (const system of this.world.getSystemsBySchedule(Schedule.PreStart)) { | ||
system() | ||
} | ||
for (const system of this.world.getSystemsBySchedule(Schedule.Start)) { | ||
system() | ||
} | ||
for (const system of this.world.getSystemsBySchedule(Schedule.PostStart)) { | ||
system() | ||
} | ||
|
||
setCurrentWorld(null) | ||
|
||
const update = (elapsed: number) => { | ||
setCurrentWorld(this.world) | ||
|
||
{ | ||
const time = this.world.getResourceSafe(Time) | ||
if (time) { | ||
time.delta = elapsed - time.elapsed | ||
time.elapsed = elapsed | ||
} | ||
} | ||
|
||
for (const system of this.world.getSystemsBySchedule(Schedule.PreUpdate)) { | ||
system() | ||
} | ||
for (const system of this.world.getSystemsBySchedule(Schedule.Update)) { | ||
system() | ||
} | ||
for (const system of this.world.getSystemsBySchedule(Schedule.PostUpdate)) { | ||
system() | ||
} | ||
setCurrentWorld(null) | ||
requestAnimationFrame(update) | ||
} | ||
|
||
window.requestAnimationFrame(update) | ||
} | ||
} |
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,2 @@ | ||
export * from "./uiNode" | ||
export * from "./uiText" |
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,3 @@ | ||
import { Component } from "../../component" | ||
|
||
export class UiNode extends Component {} |
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,10 @@ | ||
import { Component } from "../../component" | ||
|
||
export class UiText extends Component { | ||
public value: string | ||
|
||
constructor(value: string) { | ||
super() | ||
this.value = value | ||
} | ||
} |
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,4 @@ | ||
export * from "./components" | ||
export * from "./plugins" | ||
export * from "./resources" | ||
export * from "./systems" |
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,6 @@ | ||
import type { Plugin } from "../../plugin" | ||
import { Time } from "../resources/time" | ||
|
||
export const DefaultPlugin: Plugin = (world) => { | ||
world.insertResource(new Time()) | ||
} |
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,11 @@ | ||
import type { Plugin } from "../../plugin" | ||
import { HtmlRoot } from "../resources" | ||
import { Schedule } from "../../schedule" | ||
import { renderHtmlRoot } from "../systems" | ||
|
||
export function HtmlPlugin(rootSelector: string): Plugin { | ||
return (world) => { | ||
world.insertResource(new HtmlRoot(rootSelector)) | ||
world.addSystem(Schedule.Update, renderHtmlRoot) | ||
} | ||
} |
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,2 @@ | ||
export * from "./default" | ||
export * from "./html" |
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,12 @@ | ||
export class HtmlRoot { | ||
public readonly root: Element | ||
|
||
public constructor(selector: string) { | ||
const root = document.querySelector(selector) | ||
if (root == null) { | ||
throw new Error(`Root element with selector ${selector} not found in the document`) | ||
} | ||
|
||
this.root = root | ||
} | ||
} |
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,2 @@ | ||
export * from "./htmlRoot" | ||
export * from "./time" |
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,6 @@ | ||
export class Time { | ||
/** Time since start in milliseconds */ | ||
public elapsed: number = 0 | ||
/** Time since last frame in milliseconds */ | ||
public delta: number = 0 | ||
} |
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,18 @@ | ||
import { res } from "../../resource" | ||
import { HtmlRoot } from "../resources" | ||
import { query } from "../../query" | ||
import { UiText } from "../components" | ||
|
||
export function renderHtmlRoot(): void { | ||
const root = res(HtmlRoot) | ||
|
||
let htmlStr = "" | ||
|
||
for (const [text] of query(UiText)) { | ||
htmlStr += `<p>${text.value}</p>` | ||
} | ||
|
||
if (root.root.innerHTML !== htmlStr) { | ||
root.root.innerHTML = htmlStr | ||
} | ||
} |
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 @@ | ||
export * from "./html" |
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,15 @@ | ||
import type { Component } from "./component" | ||
import { useWorld } from "./world" | ||
import type { Entity } from "./entity" | ||
|
||
export const Commands = { | ||
spawn(...components: Component[]): Entity { | ||
const world = useWorld() | ||
return world.spawn(...components) | ||
}, | ||
insertResource<T extends Object>(resource: T): void { | ||
const world = useWorld() | ||
world.insertResource(resource) | ||
}, | ||
trigger(event: Event) {}, | ||
} |
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,5 @@ | ||
export abstract class Component { | ||
public componentId(): string { | ||
return this.constructor.name | ||
} | ||
} |
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,33 @@ | ||
import type { Component } from "./component" | ||
import { useWorld } from "./world" | ||
|
||
export class Entity { | ||
private static current = 0 | ||
public readonly children = new Array<Entity>() | ||
private readonly id: number | ||
|
||
public constructor() { | ||
this.id = Entity.current++ | ||
} | ||
|
||
public withChildren(children: (builder: ChildBuilder) => void): Entity { | ||
children(new ChildBuilder(this)) | ||
return this | ||
} | ||
} | ||
|
||
export class ChildBuilder { | ||
private readonly entity: Entity | ||
|
||
public constructor(entity: Entity) { | ||
this.entity = entity | ||
} | ||
|
||
public spawn(...components: Component[]): Entity { | ||
const world = useWorld() | ||
const entity = new Entity() | ||
this.entity.children.push(entity) | ||
world.addComponents(entity, ...components) | ||
return entity | ||
} | ||
} |
Oops, something went wrong.