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

import coffee.liz.ecs.events.ComponentAdded;
import coffee.liz.ecs.events.ComponentRemoved;
import coffee.liz.ecs.events.EntityEvent;
import coffee.liz.ecs.events.EventBus;

import lombok.AllArgsConstructor;
import lombok.Builder;
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
public class Entity extends EventBus<EntityEvent> {
    private final int id;

    @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) {
        final Class<? extends Component> componentType = component.getKey();
        if (componentMap.put(componentType, component) == null) {
            emit(new ComponentAdded(this, componentType));
        }
        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) {
        if (componentMap.remove(componentType) != null) {
            emit(new ComponentRemoved(this, componentType));
        }
        return this;
    }

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

    @Override
    public boolean equals(final Object other) {
        if (other instanceof Entity) {
            return ((Entity) other).getId() == getId();
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Integer.hashCode(getId());
    }
}