Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
tsukinoko-kun committed Jul 22, 2024
0 parents commit a51327c
Show file tree
Hide file tree
Showing 45 changed files with 2,163 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
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
49 changes: 49 additions & 0 deletions .github/workflows/demo.yaml
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
node_modules
dist/
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/prettier.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .npmignore
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
7 changes: 7 additions & 0 deletions LICENSE
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.
23 changes: 23 additions & 0 deletions README.md
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()
```
62 changes: 62 additions & 0 deletions lib/app.ts
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)
}
}
2 changes: 2 additions & 0 deletions lib/builtin/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./uiNode"
export * from "./uiText"
3 changes: 3 additions & 0 deletions lib/builtin/components/uiNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Component } from "../../component"

export class UiNode extends Component {}
10 changes: 10 additions & 0 deletions lib/builtin/components/uiText.ts
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
}
}
4 changes: 4 additions & 0 deletions lib/builtin/index.ts
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"
6 changes: 6 additions & 0 deletions lib/builtin/plugins/default.ts
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())
}
11 changes: 11 additions & 0 deletions lib/builtin/plugins/html.ts
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)
}
}
2 changes: 2 additions & 0 deletions lib/builtin/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./default"
export * from "./html"
12 changes: 12 additions & 0 deletions lib/builtin/resources/htmlRoot.ts
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
}
}
2 changes: 2 additions & 0 deletions lib/builtin/resources/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./htmlRoot"
export * from "./time"
6 changes: 6 additions & 0 deletions lib/builtin/resources/time.ts
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
}
18 changes: 18 additions & 0 deletions lib/builtin/systems/html.ts
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
}
}
1 change: 1 addition & 0 deletions lib/builtin/systems/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./html"
15 changes: 15 additions & 0 deletions lib/commands.ts
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) {},
}
5 changes: 5 additions & 0 deletions lib/component.ts
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
}
}
33 changes: 33 additions & 0 deletions lib/entity.ts
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
}
}
Loading

0 comments on commit a51327c

Please sign in to comment.