Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
javascript-entity-component-system
Advanced tools
A simple JavaScript entity-component-system for games
JECS (javascript-entity-component-system) is a easy to use Entity Component System for JavaScript. JECS will help you to easier develop your games by making composable pieces of code, mixing and remixing them with entities to your liking.
npm i javascript-entity-component-system
In essence it works like this:
ECS.update()
in your gameloop.ES Modules / TypeScript
import {EntityComponentSystem} from "javascript-entity-component-system" // CLASS
import {Component, Entity, Processor} from "javascript-entity-component-system" // TYPES
const ECS = new EntityComponentSystem()
CommonJS / JavaScript
const {EntityComponentSystem} = require("javascript-entity-component-system") // CLASS
const ECS = new EntityComponentSystem()
Let's start with components. Components are basically just containers with data. In Computer Science terms we call those Data Structures
. This is the basic structure:
const PositionComponent: Component = {
name: "position",
state: {
x: 0,
y: 0
}
}
const MassComponent: Component = {
name: "mass",
state: {
mass: 1.5,
velocityX: 0,
velocityY: 0
}
}
The name property is required. It will define the name of the component and is important because processor will use it to check if an entity has the required component.
The state property is required too. However, sometimes you don't need any state. In that case you just put in an empty object:
const OnDeathFadeComponent: Component = {
name: "on_death_fade",
state: {}
}
It works the same way as with state, except your processor won't have any data to use. Sometimes that's enough for a processor to do it's job. This is uncommon though.
Now that we have components defined, we can get on to Processors. The structure of them is similar:
const GravityProcessor: Processor = {
name: "gravity_processor",
required: ["position", "mass"],
update(entity: Entity, components: Component[], processor: Processor) {
// This will be called for every entity that has a position and mass component
}
}
All properties are required. The first one is the name of the processor and the second one are the required components this processor will need. In this case it needs a position
and mass
component.
The third property is the update function, that's where all the logic will happen. This function will be called automagically for every entity that has a position
and mass
component attached. This is very nice because you can focus solely on the logic:
update(entity: Entity, components: Component[], processor: Processor) {
const [position, mass] = components // The components hold the state of the entity -> change those!
const gravity = 2 // Arbitrary gravity force
const result = mass.state.mass * gravity
mass.state.velocityY += result
position.state.y += mass.state.velocityY
}
This processor will calculate the gravity force using the entities mass and apply that to it's velocity. To update it's position, we will then add the velocity to it's y position, resulting the entity to move down a bit. This function will be called every frame (inside your gameloop) e.g. it will look like gravity is pulling down on this entity.
Before we can compose entities we need to register the components and processors in the system:
// Register Component
ECS.addComponent(PositionComponent)
ECS.addComponent(MassComponent)
// Register Processor
ECS.addProcessor(GravityProcessor)
So after we registered our components and processors, we can now compose our entity:
const Player: Entity = ECS.createEntity("Player", ["position", "mass"], ["gravity_processor"])
After that you register it in the system:
ECS.addEntity(Player)
You're soo close. Call the update function ECS.update()
inside the gameloop.
// You should do this every frame. preferably inside your gameloop.
ECS.update()
Well the only thing you have to worry about now is to make more components and processors that act upon them.
In a ECS, components and processors are your bread and butter. You write those to have effects on your entities. It's starts to become real fun when you realize that you can mix and match components and processors to your liking. This flexiblity is the real strength of an ECS. That's why it's such a often used pattern in game development. Enjoy :)
FAQs
A simple JavaScript entity-component-system for games
The npm package javascript-entity-component-system receives a total of 1 weekly downloads. As such, javascript-entity-component-system popularity was classified as not popular.
We found that javascript-entity-component-system demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.