-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass discovery system through events (and thus happen every turn until
other events compete). Follow ideas from #47
- Loading branch information
1 parent
f0fd259
commit 8068884
Showing
8 changed files
with
166 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.galvarez.ttw.model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.artemis.Aspect; | ||
import com.artemis.ComponentMapper; | ||
import com.artemis.Entity; | ||
import com.artemis.EntitySystem; | ||
import com.artemis.annotations.Wire; | ||
import com.artemis.utils.ImmutableBag; | ||
import com.galvarez.ttw.model.components.EventsCount; | ||
|
||
@Wire | ||
public final class EventsSystem extends EntitySystem { | ||
|
||
public interface EventHandler { | ||
|
||
/** Get the score progress toward next event of this type. */ | ||
int getProgress(Entity e); | ||
|
||
/** Execute the event on the entity. */ | ||
void execute(Entity e); | ||
|
||
} | ||
|
||
private ComponentMapper<EventsCount> counts; | ||
|
||
private final List<EventHandler> types = new ArrayList<>(); | ||
|
||
public EventsSystem() { | ||
super(Aspect.getAspectForAll(EventsCount.class)); | ||
} | ||
|
||
public void addEventType(EventHandler type) { | ||
types.add(type); | ||
} | ||
|
||
@Override | ||
protected void inserted(Entity e) { | ||
super.inserted(e); | ||
for (EventHandler type : types) | ||
counts.get(e).scores.put(type, 0); | ||
} | ||
|
||
@Override | ||
protected void processEntities(ImmutableBag<Entity> entities) { | ||
int maxScore = Integer.MIN_VALUE; | ||
EventHandler selected = null; | ||
for (Entity e : entities) { | ||
EventsCount count = counts.get(e); | ||
for (EventHandler type : count.scores.keys()) { | ||
int newScore = type.getProgress(e) + count.scores.get(type, 0); | ||
count.scores.put(type, newScore); | ||
if (newScore > maxScore) { | ||
selected = type; | ||
maxScore = newScore; | ||
} | ||
} | ||
if (selected != null) { | ||
selected.execute(e); | ||
// also reset score | ||
count.scores.put(selected, 0); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
core/src/com/galvarez/ttw/model/components/EventsCount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.galvarez.ttw.model.components; | ||
|
||
import com.artemis.Component; | ||
import com.badlogic.gdx.utils.ObjectIntMap; | ||
import com.galvarez.ttw.model.EventsSystem.EventHandler; | ||
|
||
public class EventsCount extends Component { | ||
|
||
public final ObjectIntMap<EventHandler> scores = new ObjectIntMap<>(); | ||
|
||
public EventsCount() { | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters