Experimental canvas 2D engine for tile-based sidescroller/sandbox games, created strictly for educational purposes.
entity-component-system game-engine canvas-2d
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

TypeScript 95.2%
HTML 2.5%
CSS 2.3%
15 2 0

Clone this repository

https://tangled.org/thevoid.cafe/cosmic https://tangled.org/did:plc:jzipyzvpwhyfnn3jeymuabfv/cosmic
git@tangled.org:thevoid.cafe/cosmic git@tangled.org:did:plc:jzipyzvpwhyfnn3jeymuabfv/cosmic

For self-hosted knots, clone URLs may differ based on your setup.

Download tar.gz
README.md

Cosmic#

The experimental engine for tile-based 2D games, created strictly for educational purposes.

Requirements#

To run the demo project and experiment with the engine, the following tools are required.

  • Bun v1.3+

Try it out!#

You can try out the ever evolving demo project at any time, by running the following commands.

bun install
bun dev

Architecture#

The following will explain the data-flow and general architecture of the engine, including the main class, the ECS and more!

The Cosmic class#

The Pipeline class#

Pipelines are singular, scoped and enqueue-able looping directives.

For example, there might be a "rendering" pipeline, which draws the state of various entities onto the screen, on each frame, using drawing APIs.

These are enqueued separately, rather than having one hard-coded main loop, to allow varying execution speeds, like a rendering pipeline running on every frame, and a physics pipeline running at a calculated interval of 60 FPS.

The Layer class#

The Entity Component System#

The ECS handles Entities, their Components and the Features (Systems), that work with the former.

The Entity type#

The Entity type is the most simple part of the ECS, it's a numeric value that identifies the entity within the rest of the stack.

Internally, a secondary map within the ECS

The Component class#

Components are classes that hold flat data for a specific feature/function of an entity.

An example of a component could be "Position", which holds x and y values, as shown below.

class Position {
  public constructor(
    public x = 0,
    public y = 0
  ) {}
}

The System class#

Systems declare a set of components whose data is required for an entity to be passed through them.

Once all supported entities have been collected, the System.execute() method is called by the ECS pipeline, receiving exactly one argument, an array of all entities to process.

The system then transforms the data connected to each entity, which could be something like moving a falling entity based on gravitational values of a world, by updating the aforementioned x and y properties of the "Position" component.