Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/nomadrealms/context/game/GameState.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public GameState(String name, Queue<InputEvent> uiEventChannel, MapGenerationStr
world = new World(this, mapGenerationStrategy);
}

public World world() {
return world;
}

public void render(RenderingEnvironment re) {
re.camera.update();
world.renderMap(re);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/nomadrealms/context/game/actor/HasTooltip.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package nomadrealms.context.game.actor;

import nomadrealms.context.game.event.Target;
import nomadrealms.render.ui.content.UIContent;
import nomadrealms.render.ui.custom.tooltip.TooltipDeterminer;

public interface HasTooltip extends Target {
public interface HasTooltip {

/**
* Double dispatch method for creating the tooltip.
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/nomadrealms/context/game/card/GameCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static nomadrealms.context.game.card.target.TargetType.NONE;

import nomadrealms.context.game.actor.structure.factory.StructureType;
import nomadrealms.context.game.card.condition.EmptyCondition;
import nomadrealms.context.game.card.expression.AndExpression;
import nomadrealms.context.game.card.expression.BuryAnySeedExpression;
import nomadrealms.context.game.card.expression.CardExpression;
Expand All @@ -21,16 +22,19 @@
import nomadrealms.context.game.card.expression.TeleportNoTargetExpression;
import nomadrealms.context.game.card.query.actor.ActorsOnTilesQuery;
import nomadrealms.context.game.card.query.actor.SelfQuery;
import nomadrealms.context.game.card.query.actor.TargetQuery;
import nomadrealms.context.game.card.query.actor.TargetTypeCast;
import nomadrealms.context.game.card.query.card.LastResolvedCardQuery;
import nomadrealms.context.game.card.query.tile.PreviousTileQuery;
import nomadrealms.context.game.card.query.tile.TilesInRadiusQuery;
import nomadrealms.context.game.card.target.TargetingInfo;
import nomadrealms.context.game.world.map.area.Tile;
import nomadrealms.context.game.world.map.tile.factory.TileType;
import nomadrealms.render.particle.spawner.ParticleSpawner;

/**
* An enum of all the cards that can be played in the game. Each card has a
* title, description, expression, and targeting info.
* An enum of all the cards that can be played in the game. Each card has a title, description, expression, and
* targeting info.
*
* @author Lunkle
*/
Expand All @@ -41,7 +45,8 @@ public enum GameCard implements Card {
"move",
"Move to target hexagon",
new MoveExpression(10),
new TargetingInfo(HEXAGON, 1)),
new TargetingInfo(HEXAGON, 1,
new EmptyCondition(new ActorsOnTilesQuery(new TargetTypeCast<Tile>(new TargetQuery()))))),
ATTACK(
"Attack",
"big_punch",
Expand All @@ -53,7 +58,8 @@ public enum GameCard implements Card {
"move",
"Move to target hexagon.",
new MoveExpression(10),
new TargetingInfo(HEXAGON, 2)),
new TargetingInfo(HEXAGON, 2,
new EmptyCondition(new ActorsOnTilesQuery(new TargetTypeCast<Tile>(new TargetQuery()))))),
UNSTABLE_TELEPORT(
"Unstable Teleport",
"teleport",
Expand Down Expand Up @@ -130,7 +136,7 @@ public enum GameCard implements Card {
private final TargetingInfo targetingInfo;

private GameCard(String name, String artwork, String description, CardExpression expression,
TargetingInfo targetingInfo) {
TargetingInfo targetingInfo) {
this.title = name;
this.artwork = artwork;
this.description = description;
Expand All @@ -139,7 +145,7 @@ private GameCard(String name, String artwork, String description, CardExpression
}

private GameCard(String name, String artwork, String description, CardExpression expression, ParticleSpawner onResolve,
TargetingInfo targetingInfo) {
TargetingInfo targetingInfo) {
this.title = name;
this.artwork = artwork;
this.description = description;
Expand Down
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
Expand Up @@ -23,7 +23,7 @@ public DamageActorsExpression(Query<Actor> actors, int amount) {

@Override
public List<Effect> effects(World world, Target target, CardPlayer source) {
List<Actor> result = actors.find(world, source);
List<Actor> result = actors.find(world, source, target);
return result.stream()
.map(actor -> new DamageEffect(actor, source, amount))
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public SurfaceCardExpression(Query<WorldCard> query, int delay) {

@Override
public List<Effect> effects(World world, Target target, CardPlayer source) {
List<WorldCard> cards = query.find(world, source);
List<WorldCard> cards = query.find(world, source, target);
return singletonList(new SurfaceCardEffect(cards, source));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public TeleportNoTargetExpression(Query<Tile> tile, int delay) {

@Override
public List<Effect> effects(World world, Target target, CardPlayer source) {
List<Tile> tiles = tile.find(world, source);
List<Tile> tiles = tile.find(world, source, target);
if (tiles.isEmpty()) {
return emptyList();
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/nomadrealms/context/game/card/query/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import nomadrealms.context.game.actor.cardplayer.CardPlayer;
import nomadrealms.context.game.event.Target;
import nomadrealms.context.game.world.World;

/**
Expand All @@ -13,6 +14,6 @@
*/
public interface Query<T> {

public List<T> find(World world, CardPlayer source);
public List<T> find(World world, CardPlayer source, Target target);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import static java.util.stream.Collectors.toList;

import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

import nomadrealms.context.game.actor.Actor;
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;
import nomadrealms.context.game.world.map.area.Tile;

Expand All @@ -26,13 +28,11 @@ public ActorsOnTilesQuery(Query<Tile> tileQuery) {
}

@Override
public List<Actor> find(World world, CardPlayer source) {
List<Tile> tiles = tileQuery.find(world, source);
// TODO: Tiles need to store the actors on them for efficiency. Once we have that, we can optimize this.
Stream<CardPlayer> stream = world.actors.stream()
.filter(CardPlayer.class::isInstance)
.map(CardPlayer.class::cast)
.filter(actor -> tiles.contains(actor.tile()));
public List<Actor> find(World world, CardPlayer source, Target target) {
List<Tile> tiles = tileQuery.find(world, source, target);
Stream<Actor> stream = tiles.stream()
.map(Tile::actor)
.filter(Objects::nonNull);
if (excludeSource) {
stream = stream.filter(actor -> actor != source);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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;

/**
Expand All @@ -17,7 +18,7 @@
public class SelfQuery implements Query<CardPlayer> {

@Override
public List<CardPlayer> find(World world, CardPlayer source) {
public List<CardPlayer> find(World world, CardPlayer source, Target target) {
return singletonList(source);
}

Expand Down
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
Expand Up @@ -8,6 +8,7 @@
import nomadrealms.context.game.actor.cardplayer.CardPlayer;
import nomadrealms.context.game.card.WorldCard;
import nomadrealms.context.game.card.query.Query;
import nomadrealms.context.game.event.Target;
import nomadrealms.context.game.world.World;

/**
Expand All @@ -25,8 +26,8 @@ public LastResolvedCardQuery(Query<CardPlayer> player) {
}

@Override
public List<WorldCard> find(World world, CardPlayer source) {
return player.find(world, source).stream()
public List<WorldCard> find(World world, CardPlayer source, Target target) {
return player.find(world, source, target).stream()
.map(CardPlayer::lastResolvedCard)
.filter(Objects::nonNull)
.collect(toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

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;
import nomadrealms.context.game.world.map.area.Tile;

/**
* A query expression that can be used by card expressions and intents to find {@link Tile Tiles} in the game
* world.
* A query expression that can be used by card expressions and intents to find {@link Tile Tiles} in the game world.
*
* @author Lunkle
*/
Expand All @@ -25,8 +25,8 @@ public PreviousTileQuery(Query<CardPlayer> player) {
}

@Override
public List<Tile> find(World world, CardPlayer source) {
return player.find(world, source).stream()
public List<Tile> find(World world, CardPlayer source, Target target) {
return player.find(world, source, target).stream()
.map(CardPlayer::previousTile)
.filter(Objects::nonNull)
.collect(toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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;
import nomadrealms.context.game.world.map.area.Tile;

Expand All @@ -23,7 +24,7 @@ public TilesInRadiusQuery(int radius) {
}

@Override
public List<Tile> find(World world, CardPlayer source) {
public List<Tile> find(World world, CardPlayer source, Target target) {
Tile startTile = source.tile();
if (startTile == null) {
return emptyList();
Expand Down
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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Using Arrays.asList() creates a list that is a view of the original array. If the caller modifies the conditions array after calling this constructor, it will also modify the list within this TargetingInfo instance. This can lead to unexpected behavior and subtle bugs. To ensure the TargetingInfo object is immutable with respect to its conditions, you should create a defensive copy.

Suggested change
this.conditions = Arrays.asList(conditions);
this.conditions = new java.util.ArrayList<>(Arrays.asList(conditions));

}

public TargetType targetType() {
Expand All @@ -18,4 +25,8 @@ public int range() {
return range;
}

public List<Condition> conditions() {
return conditions;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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 ArrayList as suggested in another comment.

Suggested change
return conditions;
return java.util.Collections.unmodifiableList(conditions);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public StructureGenerationStep(Zone zone, MapGenerationStrategy strategy) {

@Override
public void generate(Zone[][] surrounding, MapGenerationStrategy strategy) {
System.out.println("Generating structures...");
for (ChunkCoordinate[] chunkRow : zone.coord().chunkCoordinates()) {
for (ChunkCoordinate chunk : chunkRow) {
for (TileCoordinate[] tileRow : chunk.tileCoordinates()) {
Expand Down
Loading