-
Notifications
You must be signed in to change notification settings - Fork 0
Add conditions to card targeting #112
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
Changes from all commits
f0ced5a
622d09f
3f9c183
575dfa7
c577d3a
aa26753
a622e28
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package nomadrealms.context.game.card.condition; | ||
|
|
||
| import nomadrealms.context.game.actor.cardplayer.CardPlayer; | ||
| import nomadrealms.context.game.event.Target; | ||
| import nomadrealms.context.game.world.World; | ||
|
|
||
| public interface Condition { | ||
|
|
||
| public boolean test(World world, Target target, CardPlayer source); | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package nomadrealms.context.game.card.condition; | ||
|
|
||
| import nomadrealms.context.game.actor.cardplayer.CardPlayer; | ||
| import nomadrealms.context.game.card.query.Query; | ||
| import nomadrealms.context.game.event.Target; | ||
| import nomadrealms.context.game.world.World; | ||
|
|
||
| public class EmptyCondition implements Condition { | ||
|
|
||
| private final Query<?> query; | ||
|
|
||
| public EmptyCondition(Query<?> query) { | ||
| this.query = query; | ||
| } | ||
|
|
||
| public boolean test(World world, Target target, CardPlayer source) { | ||
| return query.find(world, source, target).isEmpty(); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package nomadrealms.context.game.card.condition; | ||
|
|
||
| import nomadrealms.context.game.actor.cardplayer.CardPlayer; | ||
| import nomadrealms.context.game.event.Target; | ||
| import nomadrealms.context.game.world.World; | ||
|
|
||
| public class NotCondition implements Condition { | ||
|
|
||
| private final Condition condition; | ||
|
|
||
| public NotCondition(Condition condition) { | ||
| this.condition = condition; | ||
| } | ||
|
|
||
| public boolean test(World world, Target target, CardPlayer source) { | ||
| return !condition.test(world, target, source); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package nomadrealms.context.game.card.query.actor; | ||
|
|
||
| import static java.util.Collections.singletonList; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import nomadrealms.context.game.actor.cardplayer.CardPlayer; | ||
| import nomadrealms.context.game.card.query.Query; | ||
| import nomadrealms.context.game.event.Target; | ||
| import nomadrealms.context.game.world.World; | ||
|
|
||
| /** | ||
| * A query expression that can be used by card expressions and intents to find {@link CardPlayer CardPlayers} in the | ||
| * game world. | ||
| * | ||
| * @author Lunkle | ||
| */ | ||
| public class TargetQuery implements Query<Target> { | ||
|
|
||
| @Override | ||
| public List<Target> find(World world, CardPlayer source, Target target) { | ||
| return singletonList(target); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package nomadrealms.context.game.card.query.actor; | ||
|
|
||
| import static java.util.stream.Collectors.toList; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import nomadrealms.context.game.actor.cardplayer.CardPlayer; | ||
| import nomadrealms.context.game.card.query.Query; | ||
| import nomadrealms.context.game.event.Target; | ||
| import nomadrealms.context.game.world.World; | ||
|
|
||
| public class TargetTypeCast<T extends Target> implements Query<T> { | ||
|
|
||
| private final Query<Target> query; | ||
|
|
||
| public TargetTypeCast(Query<Target> query) { | ||
| this.query = query; | ||
| } | ||
|
|
||
| @Override | ||
| public List<T> find(World world, CardPlayer source, Target target) { | ||
| return query.find(world, source, target).stream().map(t -> (T) t).collect(toList()); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,13 +1,20 @@ | ||||||
| package nomadrealms.context.game.card.target; | ||||||
|
|
||||||
| import java.util.Arrays; | ||||||
| import java.util.List; | ||||||
|
|
||||||
| import nomadrealms.context.game.card.condition.Condition; | ||||||
|
|
||||||
| public class TargetingInfo { | ||||||
|
|
||||||
| private TargetType targetType; | ||||||
| private int range; | ||||||
| private final TargetType targetType; | ||||||
| private final int range; | ||||||
| private final List<Condition> conditions; | ||||||
|
|
||||||
| public TargetingInfo(TargetType targetType, int range) { | ||||||
| public TargetingInfo(TargetType targetType, int range, Condition... conditions) { | ||||||
| this.targetType = targetType; | ||||||
| this.range = range; | ||||||
| this.conditions = Arrays.asList(conditions); | ||||||
| } | ||||||
|
|
||||||
| public TargetType targetType() { | ||||||
|
|
@@ -18,4 +25,8 @@ public int range() { | |||||
| return range; | ||||||
| } | ||||||
|
|
||||||
| public List<Condition> conditions() { | ||||||
| return conditions; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning the internal list directly allows client code to modify it (if it's mutable), which breaks encapsulation. It's a good practice to return an unmodifiable view of the list to protect the internal state of the object. This is especially important if you change the constructor to create a mutable
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
Arrays.asList()creates a list that is a view of the original array. If the caller modifies theconditionsarray after calling this constructor, it will also modify the list within thisTargetingInfoinstance. This can lead to unexpected behavior and subtle bugs. To ensure theTargetingInfoobject is immutable with respect to its conditions, you should create a defensive copy.