summaryrefslogtreecommitdiff
path: root/core/src/main/java/coffee/liz/ecs/ComponentCache.java
blob: 1e6788ff3e0cd15a498d4b71870d5778d0478923 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package coffee.liz.ecs;

import coffee.liz.ecs.events.ComponentAdded;
import coffee.liz.ecs.events.ComponentRemoved;
import coffee.liz.ecs.events.EntityEvent;
import coffee.liz.ecs.model.Component;
import coffee.liz.ecs.model.Entity;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/** Incremental cache for component-to-entity lookups. */
public final class ComponentCache {
    private final Map<Class<? extends Component>, Set<Entity>> entitiesByComponent = new HashMap<>();

    /**
     * Index all current components on a newly tracked entity.
     *
     * @param entity
     *            entity to index
     */
    public void addEntity(final Entity entity) {
        entity.componentTypes().forEach(componentType -> addComponentEntry(componentType, entity));
    }

    /**
     * Remove all current components for a no-longer-tracked entity.
     *
     * @param entity
     *            entity to remove from the cache
     */
    public void removeEntity(final Entity entity) {
        entity.componentTypes().forEach(componentType -> removeComponentEntry(componentType, entity));
    }

    /**
     * Apply a mutation event to the cache.
     *
     * @param event
     *            mutation event emitted by an entity
     */
    public void onEntityEvent(final EntityEvent event) {
        switch (event) {
            case ComponentAdded a -> addComponentEntry(a.getComponentClazz(), a.getEntity());
            case ComponentRemoved r -> removeComponentEntry(r.getComponentType(), r.getEntity());
            default -> throw new IllegalStateException("Unexpected value: " + event);
        }
    }

    /**
     * Get all entities that contain a component type.
     *
     * @param componentType
     *            component class to query
     * @return entities containing that component
     */
    public Set<Entity> entitiesWith(final Class<? extends Component> componentType) {
        return entitiesByComponent.getOrDefault(componentType, Collections.emptySet());
    }

    /**
     * Resolve entities that contain every queried component type.
     *
     * @param componentTypes
     *            required component classes
     * @return entities that have all queried component types
     */
    public Set<Entity> entitiesMatchingAllOf(final Collection<Class<? extends Component>> componentTypes) {
        if (componentTypes.isEmpty()) {
            return Collections.emptySet();
        }

        Set<Entity> smallestBucket = null;
        for (final Class<? extends Component> componentType : componentTypes) {
            final Set<Entity> entities = entitiesWith(componentType);
            if (entities.isEmpty()) {
                return Collections.emptySet();
            }
            if (smallestBucket == null || entities.size() < smallestBucket.size()) {
                smallestBucket = entities;
            }
        }

        final Set<Entity> matches = new HashSet<>();
        if (smallestBucket == null) {
            return matches;
        }

        for (final Entity entity : smallestBucket) {
            if (entity.hasAll(componentTypes)) {
                matches.add(entity);
            }
        }
        return matches;
    }

    /**
     * Remove all cached component indexes.
     */
    public void clear() {
        entitiesByComponent.clear();
    }

    private void addComponentEntry(final Class<? extends Component> componentType, final Entity entity) {
        entitiesByComponent.computeIfAbsent(componentType, _unused -> new HashSet<>()).add(entity);
    }

    private void removeComponentEntry(final Class<? extends Component> componentType, final Entity entity) {
        final Set<Entity> entities = entitiesByComponent.get(componentType);
        if (entities == null) {
            return;
        }

        entities.remove(entity);
        if (entities.isEmpty()) {
            entitiesByComponent.remove(componentType);
        }
    }
}