aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/java/coffee/liz/ecs/model/Entity.java
blob: 7dab66723ffe18cd1e8aa1cfaa3bd6a19b99f007 (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
package coffee.liz.ecs.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

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

@Getter
@Builder
@RequiredArgsConstructor
@AllArgsConstructor
@Data
public class Entity {
    /** Unique id. */
    private final int id;

    /** Instances of {@link Component}s. */
    @Builder.Default
    private Map<Class<? extends Component>, Component> componentMap = Collections.synchronizedMap(new HashMap<>());

    /**
     * Check if entity has component type.
     *
     * @param componentType
     *            the {@link Component} class
     * @return true if component exists
     */
    public boolean has(final Class<? extends Component> componentType) {
        return componentMap.containsKey(componentType);
    }

    /**
     * Check if entity has all component types.
     *
     * @param components
     *            collection of {@link Component} classes
     * @return true if all components exist
     */
    public boolean hasAll(final Collection<Class<? extends Component>> components) {
        return components.stream().allMatch(this::has);
    }

    /**
     * Get component by type.
     *
     * @param componentType
     *            the {@link Component} class
     * @param <C>
     *            component type
     * @return the component or throw {@link IllegalArgumentException}
     */
    @SuppressWarnings("unchecked")
    public <C extends Component> C get(final Class<C> componentType) {
        final C component = (C) componentMap.get(componentType);
        if (component == null) {
            throw new IllegalArgumentException(
                    "Entity with id " + getId() + " does not have required component " + componentType.getSimpleName());
        }
        return component;
    }

    /**
     * Add component to entity.
     *
     * @param component
     *            the {@link Component} to add
     * @param <C>
     *            component type
     * @return this {@link Entity} for chaining
     */
    public <C extends Component> Entity add(final C component) {
        componentMap.put(component.getKey(), component);
        return this;
    }

    /**
     * Remove component from entity.
     *
     * @param componentType
     *            the {@link Component} class to remove
     * @param <C>
     *            component type
     * @return this {@link Entity} for chaining
     */
    public <C extends Component> Entity remove(final Class<C> componentType) {
        componentMap.remove(componentType);
        return this;
    }

    /**
     * Get all component types.
     *
     * @return set of {@link Component} classes
     */
    public Set<Class<? extends Component>> componentTypes() {
        return componentMap.keySet();
    }
}