T
- The type of CellGame that uses this Thinker's GameStatesU
- The type of GameState that uses this ThinkerV
- The type of SubThinker that can be assigned to this Thinkerpublic abstract class Thinker<T extends CellGame,U extends GameState<T,U,V>,V extends SubThinker<T,U,V>>
extends java.lang.Object
A Thinker is a collection of methods that contributes to the mechanics of a GameState to which it is assigned. A Thinker can be a GameState itself, in which case it is considered assigned to itself. It can also be a SubThinker that can be assigned to another Thinker, in which case it is considered assigned to a GameState if it is assigned to another Thinker that is itself assigned to the GameState.
A Thinker's assigned GameState will keep track of time for the Thinker, thus allowing the Thinker to take its own time-dependent actions, while the GameState is active. A Thinker's time factor represents the average number of discrete time units the Thinker will experience each frame while assigned to an active GameState. If its time factor is negative, as it is by default, a Thinker will use a time factor of one fracunit (if it is a GameState) or share the time factor of the Thinker to which it is assigned (if it is a SubThinker). If a Thinker is assigned to an inactive GameState or none at all, time will not pass for it.
A Thinker has timers that can perform Events after a certain number of time units. Timers have integer values, with a positive value x indicating that the Event will be performed in x time units, a negative value indicating that the timer is not running, and a value of 0 indicating that either the Event was performed, or the value was deliberately set to 0, this time unit. Each time unit, a Thinker decreases its non-negative timers' values by 1 and performs the Events whose timers have reached 0. Each frame, each Thinker experiences all of its time units immediately before its assigned SubThinkers experience any of theirs.
A Thinker has frameActions() that it takes exactly once each frame, after all Thinkers have experienced all of their time units for that frame. It also has an EventGroup of frame Events that it performs once each frame immediately after it takes its frameActions(). Both taking frameActions() and performing frame Events count as time-dependent actions, and thus a Thinker will only do them when assigned to an active GameState.
SubThinkers may be assigned to one Thinker each. Because a Thinker's internal list of SubThinkers cannot be modified while it is being iterated over, the actual addition or removal of a SubThinker to or from a Thinker is delayed until any and all iterations over its SubThinkers, such as the periods during which its SubThinkers perform their timer Events, have been completed. Multiple delayed instructions may be successfully given to Thinkers regarding the same SubThinker without having to wait until all iterations have finished.
Constructor and Description |
---|
Thinker(java.lang.Class<T> gameClass,
java.lang.Class<U> stateClass,
java.lang.Class<V> subThinkerClass)
Constructs a Thinker.
|
Modifier and Type | Method and Description |
---|---|
boolean |
addSubThinker(V subThinker)
Adds the specified SubThinker to this Thinker if it is not already
assigned to a Thinker, and if doing so would not create a loop of
assignments in which SubThinkers are directly or indirectly assigned to
themselves.
|
void |
addSubThinkerActions(T game,
U state,
V subThinker)
Actions for this Thinker to take immediately after adding a SubThinker to
itself, before the added SubThinker takes its addedActions().
|
void |
clearLineages()
Removes from their super-Thinkers all of the SubThinkers that are
directly or indirectly assigned to this Thinker.
|
void |
clearSubThinkers()
Removes from this Thinker all of the SubThinkers that are currently
assigned to it.
|
void |
frameActions(T game,
U state)
Actions for this Thinker to take once each frame.
|
abstract long |
getEffectiveTimeFactor()
Returns this Thinker's effective time factor; that is, the average number
of time units it experiences every frame.
|
EventGroup<T,U> |
getFrameEvents()
Returns the EventGroup of this Thinker's frame Events.
|
abstract T |
getGame()
Returns the CellGame of the GameState to which this Thinker is assigned,
or null if it is not assigned to a GameState.
|
java.lang.Class<T> |
getGameClass()
Returns the Class object representing the type of CellGame that uses this
Thinker's GameStates.
|
abstract U |
getGameState()
Returns the GameState to which this Thinker is assigned, or null if it is
not assigned to one.
|
int |
getNumSubThinkers()
Returns the number of SubThinkers that are assigned to this Thinker.
|
java.lang.Class<U> |
getStateClass()
Returns the Class object representing the type of GameState that uses
this Thinker.
|
java.lang.Class<V> |
getSubThinkerClass()
Returns the Class object representing the type of SubThinker that can be
assigned to this Thinker.
|
long |
getTimeFactor()
Returns this Thinker's time factor.
|
int |
getTimerValue(Event<T,U> event)
Returns the current value of this Thinker's timer for the specified
Event.
|
boolean |
iteratingThroughSubThinkers()
Returns whether any Iterators over this Thinker's list of SubThinkers are
in progress.
|
boolean |
removeLineage(V subThinker)
Removes from their super-Thinkers all of the SubThinkers that are
directly or indirectly assigned to this Thinker, and are either assigned
to or assignees of the specified SubThinker.
|
boolean |
removeSubThinker(V subThinker)
Removes the specified SubThinker from this Thinker if it is currently
assigned to it.
|
void |
removeSubThinkerActions(T game,
U state,
V subThinker)
Actions for this Thinker to take immediately before removing a SubThinker
from itself, after the soon-to-be-removed SubThinker takes its
removedActions().
|
void |
setTimeFactor(long timeFactor)
Sets this Thinker's time factor to the specified value.
|
void |
setTimerValue(Event<T,U> event,
int value)
Sets the value of this Thinker's timer for the specified Event to the
specified value.
|
SafeIterator<V> |
subThinkerIterator()
Returns a new SafeIterator over this Thinker's list of SubThinkers.
|
public Thinker(java.lang.Class<T> gameClass, java.lang.Class<U> stateClass, java.lang.Class<V> subThinkerClass)
gameClass
- The Class object representing the type of CellGame that
uses this Thinker's GameStatesstateClass
- The Class object representing the type of GameState
that uses this ThinkersubThinkerClass
- The Class object representing the type of
SubThinker that can be assigned to this Thinkerpublic final java.lang.Class<T> getGameClass()
public final java.lang.Class<U> getStateClass()
public final java.lang.Class<V> getSubThinkerClass()
public abstract T getGame()
public abstract U getGameState()
public final long getTimeFactor()
public abstract long getEffectiveTimeFactor()
public final void setTimeFactor(long timeFactor)
timeFactor
- The new time factorpublic final int getTimerValue(Event<T,U> event)
event
- The Event whose timer value should be returnedpublic final void setTimerValue(Event<T,U> event, int value)
event
- The Event whose timer value should be setvalue
- The new value of the specified Event's timerpublic void frameActions(T game, U state)
game
- This Thinker's GameState's CellGamestate
- This Thinker's GameStatepublic final EventGroup<T,U> getFrameEvents()
public final int getNumSubThinkers()
public final boolean iteratingThroughSubThinkers()
public final SafeIterator<V> subThinkerIterator()
public final boolean addSubThinker(V subThinker)
subThinker
- The SubThinker to be addedpublic final boolean removeSubThinker(V subThinker)
subThinker
- The SubThinker to be removedpublic final void clearSubThinkers()
public final boolean removeLineage(V subThinker)
subThinker
- The SubThinker with which the removed SubThinkers must
share a lineage of assignmentspublic final void clearLineages()
public void addSubThinkerActions(T game, U state, V subThinker)
game
- This Thinker's GameState's CellGame, or null if it has no
GameStatestate
- This Thinker's GameState, or null if it has nonesubThinker
- The SubThinker that was addedpublic void removeSubThinkerActions(T game, U state, V subThinker)
game
- This Thinker's GameState's CellGame, or null if it has no
GameStatestate
- This Thinker's GameState, or null if it has nonesubThinker
- The SubThinker that is about to be removed