{"id":17219476,"url":"https://github.com/kelindar/ecs","last_synced_at":"2025-04-13T10:56:36.549Z","repository":{"id":80099500,"uuid":"235717827","full_name":"kelindar/ecs","owner":"kelindar","description":"Example of Entity Component System in Go","archived":false,"fork":false,"pushed_at":"2025-02-13T20:27:41.000Z","size":98,"stargazers_count":70,"open_issues_count":2,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-27T02:11:22.314Z","etag":null,"topics":["data-oriented","ecs","entity-component-system","game-development"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kelindar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-01-23T03:45:10.000Z","updated_at":"2025-02-25T08:56:37.000Z","dependencies_parsed_at":"2025-03-27T02:11:25.760Z","dependency_job_id":null,"html_url":"https://github.com/kelindar/ecs","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelindar%2Fecs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelindar%2Fecs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelindar%2Fecs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelindar%2Fecs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kelindar","download_url":"https://codeload.github.com/kelindar/ecs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248703194,"owners_count":21148116,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-oriented","ecs","entity-component-system","game-development"],"created_at":"2024-10-15T03:49:57.333Z","updated_at":"2025-04-13T10:56:36.527Z","avatar_url":"https://github.com/kelindar.png","language":"Go","readme":"# Example of Entity Component System in Go\n\n## Introduction\n\nThis is my attempt to build a high-performance ECS (Entity, Component, System) in pure Go. Having investigated other ECS systems which have been written to date, I realised that none of them is really a pure ECS as the data layout is often using pointers or interfaces, which would discard most of the benefits of such a system.\n\nInstead of being an ECS framework, this repository contains an example of using [kelindar/column](https://github.com/kelindar/column) as the underlying storage of components and [kelindar/tile](https://github.com/kelindar/tile) as a 2D grid engine for spatial queries.\n\n## Components\n\nThe components are created using a columnar storage [kelindar/column](https://github.com/kelindar/column). The library organizes data in dense arrays with bitmaps and indexing for querying, allowing us to build systems on top which simply perform queries to and update different columns when necessary.\n\nIn order to simplify the logic a bit, `entity.Collection[T]` structure wraps around `column.Collection` to provide strong typing for working with our **entities**.\n\n```go\n// Collection represents a collection of mobile objects\ntype Collection = entity.Collection[Mobile]\n\n// NewCollection creates a new mobile object collection\nfunc NewCollection() *Collection {\n    db := entity.NewCollection(\"mobiles.bin\", At)\n    db.CreateColumn(\"img\", column.ForUint32())  // Image index\n    db.CreateColumn(\"at\", column.ForUint32())   // Location as packed tile.Point\n    db.CreateColumn(\"move\", column.ForUint16()) // Movement vector\n    return db\n}\n```\n\n## Entities\n\nThe `entities` directory contains various **entities** of the game, things such as players, items, monsters etc. An entity represents something that has a set of **components** (i.e. columns) and is expressed as a **view** over a row that is lazily evaluated by various **systems**.\n\nFor example, a mobile entity takes a pointer over a specific row and allows us to read/write columns by simply accessing the properties instead of dealing with the internals of the data storage layer. In the example below, we have `Location() tile.Point` and `SetLocation(tile.Point)` methods on our mobile entity, which contain the necessary code to read/write but the entity itself does not have any state besides a `Cursor`. This allows us to lazily evaluate and only read the necessary data when accessing it.\n\n```go\n// Mobile represents a view on a current mobile row\ntype Mobile struct {\n    row *column.Cursor\n}\n\n// Location reads the current location\nfunc (m *Mobile) Location() tile.Point {\n    // read the \"location\" column\n}\n\n// SetLocation writes the current location\nfunc (m *Mobile) SetLocation(v tile.Point) {\n    // write the \"location\" column\n}\n```\n\n## Systems\n\nThis `system` directory various game **systems** that are executed periodically and process a set of **components** (i.e. columns) for a set of **entities** (i.e. players, items, monsters). Systems access data using columnar **queries** which allow us to filter only the rows that the system can process.\n\nFor example, consider a _movement system_ that adjust both `location` and `movement action` components of a `mobile object` entity. Such system needs to first filter out entities that haven't moved, which is done using a bitmap index that checks whether the `movement action` is not empty, then the system processes all of the matching entities by updating the movement accordingly. If a lot of entities move, this is very efficient since cache misses are reduced and the movement logic is neatly contained within the system that performs the given behavior.\n\n```go\n// Interval specifies how often the system should run\nfunc (s *System) Interval() time.Duration {\n\treturn 100 * time.Millisecond\n}\n\n// Attach attaches the system to the world context\nfunc (s *System) Attach(w *world.World) error {\n    s.grid = w.Grid\n\n    // Create an index \"moving\" which will filter only entities\n    // that have \"move\" field with a distance greater than zero.\n    s.mobiles = w.Mobiles\n    s.mobiles.CreateIndex(\"moving\", \"move\", func(r column.Reader) bool {\n        return state.Movement(r.Uint()).Distance() \u003e 0\n    })\n    return nil\n}\n\n// Update is called periodically on the movement system\nfunc (s *System) Update(dt time.Duration) error {\n\treturn s.mobiles.Range(func(m mobile.Mobile) {\n            movement := m.Movement()\n            location := m.Location()\n\n            // Update the movement vector and store it\n            movement, moved = movement.Update(dt)\n            m.SetMovement(movement)\n            if !moved {\n                return false // not moved\n            }\n\n            // Try to move and check whether the location is within map bounds\n            location = location.Move(movement.Direction())\n            if !location.WithinSize(s.grid.Size){\n                return false // out of map bounds\n            }\n\n            // Update the current location\n            m.SetLocation(location)\n        }, \"moving\") // use moving index\n    })\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkelindar%2Fecs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkelindar%2Fecs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkelindar%2Fecs/lists"}