-
Notifications
You must be signed in to change notification settings - Fork 0
/
Component.java
102 lines (90 loc) · 2.85 KB
/
Component.java
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
package com.csse3200.game.components;
import com.csse3200.game.entities.Entity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Core component class from which all components inherit. Contains logic for creating, updating,
* and disposing. Components can be attached to an entity to give it specific behaviour. It is
* unlikely that changes will need to be made here.
*/
public class Component {
private static final Logger logger = LoggerFactory.getLogger(Component.class);
protected Entity entity;
protected boolean enabled = true;
/**
* Called when the entity is created and registered. Initial logic such as calls to GetComponent
* should be made here, not in the constructor which is called before an entity is finished.
*/
public void create() {
// No action by default.
}
/**
* Early update called once per frame of the game, before update(). Use this only for logic that
* must run before other updates, such as physics. Not called if component is disabled.
*/
public void earlyUpdate() {
// No action by default.
}
/**
* Called once per frame of the game, and should be used for most component logic. Not called if
* component is disabled.
*/
public void update() {
// No action by default.
}
/** Called when the component is disposed. Dispose of any internal resources here. */
public void dispose() {
// No action by default.
}
/**
* Set the entity to which this component belongs. This is called by the Entity, and should not be
* set manually.
*
* @param entity The entity to which the component is attached.
*/
public void setEntity(Entity entity) {
logger.debug("Attaching {} to {}", this, entity);
this.entity = entity;
}
/**
* Get the entity to which this component belongs.
* @return entity
*/
public Entity getEntity() {
return entity;
}
/**
* Enable or disable the component. While disabled, a component does not run update() or
* earlyUpdate(). Other events inside the component may still fire. The component can still be
* disposed while disabled.
*
* @param enabled Should component be enabled
*/
public void setEnabled(boolean enabled) {
logger.debug("Setting enabled={} on {}", enabled, this);
this.enabled = enabled;
}
/** Used to trigger the component to update itself. This should not need to be called manually. */
public final void triggerUpdate() {
if (enabled) {
update();
}
}
/**
* Used to trigger the component to early-update itself. This should not need to be called
* manually.
*/
public final void triggerEarlyUpdate() {
if (enabled) {
earlyUpdate();
}
}
@Override
public String toString() {
String className = this.getClass().getSimpleName();
if (entity == null) {
return className;
}
return entity + "." + className;
}
}