Created
October 16, 2025 23:52
-
-
Save unrays/02102957a75cc96653ca90707ed7262f to your computer and use it in GitHub Desktop.
A Minimal ECS Registry in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Copyright (c) October 2025 Félix-Olivier Dumas. All rights reserved. | |
| // Licensed under the terms described in the LICENSE file | |
| package com.ECS.ecs; | |
| import com.ECS.components.Component; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| public class Registry { | |
| private final Map<Entity, Map<Class<? extends Component>, Component>> registry = new HashMap<>(); | |
| public <T extends Component> void addComponent(Entity e, T... components) { | |
| for (T c : components) registry.computeIfAbsent(e, key -> new HashMap<>()).put(c.getClass(), c); | |
| } | |
| public <T extends Component> void removeComponent(Entity e, Class<T> c) { | |
| for (Component cc : registry.get(e).values()) if (registry.get(e) != null) registry.get(e).remove(c); | |
| } | |
| public <T extends Component> T getComponent(Entity e, Class<T> c) { | |
| for (Component cc : registry.get(e).values()) if (c.isInstance(cc)) return c.cast(cc); | |
| return null; | |
| } | |
| public <T extends Component> boolean hasComponent(Entity e, Class<?> c) { | |
| return registry.get(e).containsKey(c); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It’s definitely not perfect but it’s the first one I’ve ever made so I’m pretty proud of it :)