CarrotzRule123 / ecs

Build your own Game-Engine based on the Entity Component System concept in Golang.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ECS - Entity Component System

Go Report Card BCH compliance

Build your own Game-Engine based on the Entity Component System concept in Golang

The architectural pattern of an ECS is mostly used in game development, to provide long-term maintainability and extendability of large, dynamic systems.

Overview

An Entity is basically a composition of different components and has an ID.
A Component contains only the state or data of one specific aspect like health, position, velocity etc.
A System handles the behaviour or logic of the components. A movement system uses the position and velocity to implement an entities movement.

Table of Contents

Goals

  • Provide an easy-to-use framework to build a game engine from scratch.
  • No dependencies to other modules or specific game libraries - Feel free to use what fits your needs.
  • Minimum overhead - use only what is really needed.
  • Plugins to offer unlimited room for improvements.
  • Interoperability between non-Go libraries and Go via Main/Do.

Installation

From Source

go get -u github.com/andygeiss/ecs

Steps to start

In the first step we have to be clear about what our game engine should do. The main task is to make sure that we have all the essential components that are necessary for the technical and logical aspects are responsible, are combined with each other.

An Entity Component System (ECS) helps us to do just that, as the logical components (data) such as entities and their components can be separated from the actual logic. One of the advantages of this is that we can implement and test the game mechanics independently of the rest. So let's start...

We decide to use 2D and define the three most important components:

  • Position
  • Size
  • Velocity

We store these as components.go (Example: here).

In the next step, the three most important systems implement

  • Collision
  • Movement
  • Rendering

We store these as systems.go (Example: here).

The collision and movement system contains the actual game mechanics:

func (m *Collision) Process(em *ecs.EntityManager) (state int) {
	for _, entity := range em.FilterByMask(MaskPosition | MaskVelocity) {
		position := entity.Get(MaskPosition).(*Position)
		velocity := entity.Get(MaskVelocity).(*Velocity)
		if position.X >= m.width || position.X <= 0 {
			velocity.X = -velocity.X
		}
		if position.Y >= m.height || position.Y <= 0 {
			velocity.Y = -velocity.Y
		}
	}
	return ecs.StateEngineContinue
}

The rendering system must be adapted to a specific game library. In our example we have used SDL. In the example of Pong we used Raylib.

Finally we create a main.go file (Example: here) and link the systems together:

func run() {
	em := ecs.NewEntityManager()
	em.Add(generateEntities(1000)...)
	sm := ecs.NewSystemManager()
	sm.Add(
		engine.NewMovement(),
		engine.NewCollision(Width, Height),
		engine.NewRendering(Width, Height, "ECS with SDL Demo"),
	)
	ecs.Run(em, sm)
}

func main() {
	ecs.Main(func() {
		run()
	})
}

stats

About

Build your own Game-Engine based on the Entity Component System concept in Golang.

License:MIT License


Languages

Language:Go 100.0%