Giter VIP home page Giter VIP logo

ecs's Introduction

ecs

data oriented, functional entity component system.

usage example

This is a minimal example of what you can do with ecs. It's not functional but illustrates how to declare your components, systems, and entities.

import ECS      from 'ecs'
import Keyboard from './my/game/keyboard.js'
import clamp    from 'clamp'


// generates a new entity component system
const world = ECS.createWorld() 


// define a component type named position and give it a default value
const POSITION = ECS.createComponentType(world, 'position', { x: 0, y: 0 })

// define another component type named moveable
const MOVEABLE = ECS.createComponentType(world, 'moveable', { dx: 0, dy: 0 })

// set up the player
const PLAYER = ECS.createEntity(world)
ECS.addComponentToEntity(world, PLAYER, POSITION)
ECS.addComponentToEntity(world, PLAYER, MOVEABLE)


// update entity velocity based on key pressed
function keyboardControlSystem (world) {
    // declare a filter that is used at run time to choose which entities to operate on
    // the 2nd argument is all of the required component types the entity must have to
    // be included
    const moveableFilter = ECS.createFilter(world, [ MOVEABLE ])

    // called each game loop
    const onUpdate = function () {
        // get all of the entities in the world that pass the filter
        for (const entity of ECS.getEntities(world, moveableFilter)) {
            // update the entity position according to what is pressed
            if (Keyboard.keyPressed('up'))
                entity.moveable.dy -= 1;
            if (Keyboard.keyPressed('down'))
                entity.moveable.dy += 1;
            if (Keyboard.keyPressed('left'))
                entity.moveable.dx -= 1;
            if (Keyboard.keyPressed('right'))
                entity.moveable.dx += 1;

           entity.moveable.dx = clamp(entity.moveable.dx, -10, 10);
           entity.moveable.dy = clamp(entity.moveable.dy, -10, 10);
        }
    }

    return { onUpdate }
}


function movementSystem (world) {
    const positionMoveFilter = ECS.createFilter(world, [ POSITION, MOVEABLE ])

    const onUpdate = function () {
        for (const entity of ECS.getEntities(world, positionMoveFilter)) {
            entity.position.x += entity.moveable.dx
            entity.position.y += entity.moveable.dy
        }
    }

    return { onUpdate }
}


ECS.addSystem(world, keyboardControlSystem)
ECS.addSystem(world, movementSystem)


function gameLoop() {
    // run onUpdate for all added systems
    ECS.update(world);

    requestAnimationFrame(gameLoop);
}


// finally start the game loop
gameLoop()

ecs's People

Contributors

mreinstein avatar

Watchers

Sung-ho Song avatar James Cloos 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.