Code Monkey home page Code Monkey logo

ts-ecs's Introduction

ts-ecs

npm author

Experimental ECS Framework for TypeScript.

Installation

npm install @jahed/ts-ecs

Usage

A thorough example might be provided in the future. For now, here's a simple one.

/*
 * Component Definitions are used to create and find components
 */

export const withPosition = defineComponent(
  'Position',
  (position: Position): Position => position
)

export const withRadius = defineComponent(
  'Radius',
  (radius: number): number => radius
)

/*
 * Systems are used to query and modify components.
 */

const createRenderSystem = (app: Application): System => {
  return createSystem({
    id: 'renderSystem',
    update: (ecs, elapsedMS) => {
      /*
       * Query entities that share common components.
       */
      for (const entity of ecs.getEntities(withPosition, withRadius)) {
        const { data: { x, y } } = entity.getComponent(withPosition)
        const { data: radius } = entity.getComponent(withRadius)
        app.drawCircle(x, y, radius)
      }
    }
  })
}

export const createTitleSystem = (onStartGame: () => void): System => {
  return createSystem({
    id: 'TitleSystem',
    update: (ecs) => {
      if (/* input logic */) {
        onStartGame()
      }
    }
  })
}

/*
 * Spaces are used to group entities, components and systems together
 */

export const createTitleSpace = (
  app: Application,
  onStartGame: () => void
): Space => {
  /*
   * Entities are a grouping of components.
   */
  const logo = createEntity('logo')
    .addComponent(createComponent(withPosition, { x: 10, y: 10 }))
    .addComponent(createComponent(withRadius, 5))

  const space = createSpace({ id: 'title' })
  space.addSystem(createRenderSystem(app))
  space.addSystem(createTitleSystem(onStartGame))
  space.addEntity(logo)
  return space
}


export const createBattleSpace = (
  app: Application,
  onBattleEnd: () => void
): Space => {
  // similar to createTitleSpace
}

/*
 * An "app" can be any rendering framework. e.g. pixi.js.
 */
const app = createApp();

/*
 * The Engine decided which systems execute and when.
 */
const engine = createEngine();

/*
 * Let's tie the spaces together so we can transition from one to the other and
 * back.
 */
createTitleSpace(app, () => {
  const battleSpace = createBattleSpace(app, () => {
    engine.removeSystem(battleSpace)
    engine.addSystem(titleSpace)
  })

  engine.removeSystem(titleSpace)
  engine.addSystem(battleSpace)
})

engine.addSystem(titleSpace)

/*
 * Now we can update everything on each tick.
 */
app.ticker.add(() => engine.update(app.ticker.elapsedMS))

License

Copyright (C) 2020 Jahed Ahmed

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.

ts-ecs's People

Contributors

jahed avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.