Skip to content

Instantly share code, notes, and snippets.

@unrays
Created October 16, 2025 23:52
Show Gist options
  • Select an option

  • Save unrays/02102957a75cc96653ca90707ed7262f to your computer and use it in GitHub Desktop.

Select an option

Save unrays/02102957a75cc96653ca90707ed7262f to your computer and use it in GitHub Desktop.
A Minimal ECS Registry in Java
// 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);
}
}
@unrays
Copy link
Author

unrays commented Oct 16, 2025

It’s definitely not perfect but it’s the first one I’ve ever made so I’m pretty proud of it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment