Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added supportedEventTypes back #42

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
4 changes: 1 addition & 3 deletions .idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/main/java/eod/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import eod.card.abstraction.action.ActionCard;
import eod.effect.Effect;
import eod.effect.EffectExecutor;
import eod.event.AttackEvent;
import eod.event.Event;
import eod.event.relay.EventManager;
import eod.event.RoundEndEvent;
Expand Down Expand Up @@ -186,7 +187,7 @@ public ArrayList<Damageable> damage(CanAttack attacker,
public ArrayList<Damageable> damage(CanAttack attacker,
Point target,
AttackParam param) throws NotSupportedException {
ArrayList<Point> targets = new ArrayList<Point>(){{
ArrayList<Point> targets = new ArrayList<Point>() {{
add(target);
}};
return damage(attacker, targets, param);
Expand Down
50 changes: 22 additions & 28 deletions src/main/java/eod/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Objects;
import java.util.stream.Collectors;

public class Player implements Snapshotted<Player.Snapshot>,
GameObject, EventReceiver, EventSender, EffectExecutor {
Expand All @@ -42,7 +44,7 @@ public class Player implements Snapshotted<Player.Snapshot>,
private String name;
private boolean isPlayerA;
private boolean isActingPlayer = false;
private ArrayList<EventReceiver> receivers;
private HashMap<Class<? extends Event>, ArrayList<EventReceiver>> receivers;

public Player(Deck deck, String name) {
this(deck, new Hand(), name);
Expand All @@ -52,7 +54,7 @@ public Player(Deck deck, Hand hand, String name) {
this.deck = deck;
this.hand = hand;
this.name = name;
receivers = new ArrayList<>();
receivers = new HashMap<>();
}

public String getName() {
Expand Down Expand Up @@ -107,16 +109,14 @@ public void drawFromDeck(int count) {
Card[] cards = deck.draw(count);
handReceive(new ArrayList<>(Arrays.asList(cards)));

output.sendReceivedCards(this, cards);
}

public void drawFromDeck(Class<? extends Card> cardType, int count) {
output.sendDrawingCards(this);

Card[] cards = deck.draw(cardType, count);
hand.receive(new ArrayList<>(Arrays.asList(cards)));
handReceive(new ArrayList<>(Arrays.asList(cards)));
aoaaceai marked this conversation as resolved.
Show resolved Hide resolved

output.sendReceivedCards(this, cards);
}

public boolean checkCardTypeInHand(Class<? extends Card> c) {
Expand Down Expand Up @@ -319,8 +319,8 @@ public void teardown() {
deck = null;
leader.teardown();
leader = null;
for(EventReceiver receiver:receivers) {
receiver.teardown();
for(ArrayList<EventReceiver> subReceiver:receivers.values()) {
subReceiver.forEach(GameObject::teardown);
}
receivers.clear();
}
Expand Down Expand Up @@ -436,44 +436,31 @@ public BoardPosition getPosition(Point p) {

@Override
public void registerReceiver(EventReceiver receiver) {
receivers.add(receiver);
for(Class<? extends Event> supportedEvent:receiver.supportedEventTypes()) {
receivers.putIfAbsent(supportedEvent, new ArrayList<>());
receivers.get(supportedEvent).add(receiver);
}
}



@Override
public void unregisterReceiver(EventReceiver receiver) {
receivers.remove(receiver);
}

@Override
public StatusHolder[] getStatusHolders() {
return receivers.stream().filter(receiver -> receiver instanceof StatusHolder).map(receiver -> (StatusHolder) receiver).toArray(StatusHolder[]::new);
for(Class<? extends Event> supportedEvent:receiver.supportedEventTypes()) {
receivers.get(supportedEvent).remove(receiver);
}
}

@Override
public void send(GameObject sender, Event event) {
receivers.stream()
.filter(receiver -> receiver.supportedEventTypes().contains(event.getClass()))
receivers.get(event.getClass())
.forEach(receiver -> receiver.onEventOccurred(sender, event));
}

public void sendUp(GameObject sender, Event event) {
game.sendEvent(sender, event);
}

@Override
public ArrayList<Class<? extends Event>> supportedEventTypes() {
//TODO: add RegionalAttackEvent support
return new ArrayList<Class<? extends Event>>(){{
add(RoundStartEvent.class);
add(RoundEndEvent.class);
add(DirectAttackEvent.class);
add(ObjectDeadEvent.class);
add(ObjectMovingEvent.class);
}};
}

@Override
public void onEventOccurred(GameObject sender, Event event) {
if(event instanceof RoundStartEvent) {
Expand All @@ -498,6 +485,13 @@ public void onEventOccurred(GameObject sender, Event event) {
send(sender, event);
}

@Override
public ArrayList<Class<? extends Event>> supportedEventTypes() {
return new ArrayList<Class<? extends Event>>(){{
addAll(Arrays.stream(Event.allEvents).collect(Collectors.toList()));
}};
}

public class Snapshot implements eod.snapshots.Snapshot {
private Deck.Snapshot deckSnapshot = deck.takeSnapshot();
private Hand.Snapshot handSnapshot = hand.takeSnapshot();
Expand Down
33 changes: 18 additions & 15 deletions src/main/java/eod/card/concrete/normal/blue/SupportAttack.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
import eod.effect.EffectExecutor;
import eod.event.Event;
import eod.event.ObjectDeadEvent;
import eod.event.RoundEndEvent;
import eod.event.TargetedEvent;
import eod.event.relay.EventReceiver;
import eod.param.DamageParam;
import eod.warObject.Damageable;
import eod.warObject.Status;
import eod.warObject.WarObject;
import eod.warObject.character.abstraction.Character;

import java.util.ArrayList;
Expand Down Expand Up @@ -54,32 +57,26 @@ public Party getParty() {

public class EnemyAttack implements EventReceiver, GameObject {
private Character holder;
private ArrayList<Class<? extends Event>> canHandle;

public EnemyAttack(Character holder) {
this.holder = holder;
canHandle = new ArrayList<>();
canHandle.add(ObjectDeadEvent.class);
canHandle.add(TargetedEvent.class);
holder.registerReceiver(this);
}

@Override
public ArrayList<Class<? extends Event>> supportedEventTypes() {
return canHandle;
}

@Override
public void onEventOccurred(GameObject sender, Event event) {
if(event instanceof ObjectDeadEvent) {
ObjectDeadEvent e = (ObjectDeadEvent) event;
if(e.getDeadObject() == holder) {
if(event instanceof RoundEndEvent) {
RoundEndEvent e = (RoundEndEvent) event;
if(e.getEndedRound().getPlayer().isPlayerA() != holder.getPlayer().isPlayerA()) {
teardown();
}
}
if(event instanceof TargetedEvent) {
if(holder.hasStatus(Status.NO_EFFECT)) {
return;
}
TargetedEvent e = (TargetedEvent) event;
if(e.getTarget() != holder) {
if(e.getTarget() != holder && ((WarObject)e.getAttacker()).getPlayer().isPlayerA() != holder.getPlayer().isPlayerA()) {
player.tryToExecute(
Damage(new DamageParam(2), Effect.HandlerType.Rival).on((Damageable) e.getAttacker())
);
Expand All @@ -88,11 +85,17 @@ public void onEventOccurred(GameObject sender, Event event) {
}
}

@Override
public ArrayList<Class<? extends Event>> supportedEventTypes() {
return new ArrayList<Class<? extends Event>>() {{
add(RoundEndEvent.class);
add(TargetedEvent.class);
}};
}

@Override
public void teardown() {
holder.unregisterReceiver(this);
canHandle.clear();
canHandle = null;
holder = null;
}
}
Expand Down
34 changes: 19 additions & 15 deletions src/main/java/eod/card/concrete/normal/transparent/FightBack.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import eod.param.DamageParam;
import eod.warObject.CanAttack;
import eod.warObject.Damageable;
import eod.warObject.Status;
import eod.warObject.WarObject;

import java.util.ArrayList;
Expand All @@ -29,11 +30,12 @@ public FightBack() {

@Override
public void applyEffect(EffectExecutor executor) {
new AttackDetect(player.selectObject(
WarObject(player.getBoard())
.which(Being(Damageable.class))
.which(OwnedBy(player)).get())
WarObject selected = player.selectObject(
WarObject(player.getBoard())
.which(Being(Damageable.class))
.which(OwnedBy(player)).get()
);
selected.registerReceiver(new AttackDetect(selected));
}

@Override
Expand All @@ -55,30 +57,25 @@ public Party getParty() {

public class AttackDetect implements EventReceiver, GameObject {
private WarObject watching;
private ArrayList<Class<? extends Event>> canHandle;

public AttackDetect(WarObject watching) {
this.watching = watching;
canHandle = new ArrayList<>();
canHandle.add(TargetedEvent.class);
canHandle.add(RoundEndEvent.class);
watching.registerReceiver(this);
}
@Override
public ArrayList<Class<? extends Event>> supportedEventTypes() {
return canHandle;
}

@Override
public void onEventOccurred(GameObject sender, Event event) {
if(event instanceof RoundEndEvent) {
if(((RoundEndEvent) event).getEndedRound().getPlayer().isPlayerA() == player.rival().isPlayerA()) {
if(((RoundEndEvent) event).getEndedRound().getPlayer().isPlayerA() != watching.getPlayer().isPlayerA()) {
teardown();
}
}
if(event instanceof TargetedEvent) {

TargetedEvent e = (TargetedEvent) event;
if(e.getTarget() == watching) {
if(watching.hasStatus(Status.NO_EFFECT)) {
return;
}
CanAttack attacker = e.getAttacker();
((WarObject) attacker).getPlayer().tryToExecute(
Damage(new DamageParam(4), Effect.HandlerType.Rival).on((Damageable) attacker)
Expand All @@ -88,9 +85,16 @@ public void onEventOccurred(GameObject sender, Event event) {
}
}

@Override
public ArrayList<Class<? extends Event>> supportedEventTypes() {
return new ArrayList<Class<? extends Event>>(){{
add(TargetedEvent.class);
add(RoundEndEvent.class);
}};
}

@Override
public void teardown() {
canHandle.clear();
watching.unregisterReceiver(this);
watching = null;
}
Expand Down
33 changes: 13 additions & 20 deletions src/main/java/eod/card/concrete/normal/transparent/Sneak.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static eod.specifier.WarObjectSpecifier.WarObject;
import static eod.specifier.condition.Conditions.Being;
import static eod.specifier.condition.Conditions.OwnedBy;
import static eod.warObject.Status.NO_EFFECT;

public class Sneak extends NormalCard {

Expand All @@ -43,7 +44,7 @@ public void applyEffect(EffectExecutor executor) {
if(afterEffect) {
Arrays.stream(assassins)
.map(object -> (Assassin) object)
.forEach(NextDamageDouble::new);
.forEach(assassin -> assassin.registerReceiver(new NextDamageDouble(assassin)));
}
}

Expand All @@ -65,45 +66,37 @@ public Party getParty() {
return Party.TRANSPARENT;
}

public class NextDamageDouble implements EventReceiver, GameObject {
private class NextDamageDouble implements EventReceiver, GameObject {
private Assassin assassin;
private ArrayList<Class<? extends Event>> canHandle;

public NextDamageDouble(Assassin assassin) {
this.assassin = assassin;
canHandle = new ArrayList<>();
canHandle.add(ObjectDeadEvent.class);
canHandle.add(AttackEvent.class);
assassin.registerReceiver(this);
}

@Override
public ArrayList<Class<? extends Event>> supportedEventTypes() {
return canHandle;
}

@Override
public void onEventOccurred(GameObject sender, Event event) {
if(event instanceof AttackEvent) {
AttackEvent e = (AttackEvent) event;
if(e.getAttacker() == assassin) {
e.param.hp *= 2;
teardown();
}
}
if(event instanceof ObjectDeadEvent) {
if(((ObjectDeadEvent) event).getDeadObject() == assassin) {
if (!assassin.hasStatus(NO_EFFECT)) {
e.param.hp *= 2;
}
teardown();
}
}
}

@Override
public ArrayList<Class<? extends Event>> supportedEventTypes() {
return new ArrayList<Class<? extends Event>>() {{
add(AttackEvent.class);
}};
}

@Override
public void teardown() {
assassin.unregisterReceiver(this);
assassin = null;
canHandle.clear();
canHandle = null;
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/eod/event/Event.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package eod.event;

public interface Event {
// TODO: remove DirectAttackEvent and RegionalAttackEvent.
Class<? extends Event>[] allEvents = new Class[] {AfterObjectDamageEvent.class, AttackEvent.class,
BeforeObjectDamageEvent.class, ObjectDeadEvent.class, ObjectEnterEvent.class,
ObjectEnterEnemyBaseEvent.class, ObjectMovingEvent.class, RoundEndEvent.class,
RoundStartEvent.class, StatusAcquiredEvent.class, TargetedEvent.class};
}
Loading