Skip to content

Commit

Permalink
Add a selection step for wild card colors
Browse files Browse the repository at this point in the history
Fixes #17
  • Loading branch information
haykam821 committed Jul 26, 2024
1 parent 63b2888 commit 64b4228
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 43 deletions.
7 changes: 4 additions & 3 deletions src/main/java/io/github/haykam821/lastcard/card/Card.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import eu.pb4.mapcanvas.api.core.DrawableCanvas;
import eu.pb4.mapcanvas.api.utils.ViewUtils;
import io.github.haykam821.lastcard.card.color.CardColor;
import io.github.haykam821.lastcard.card.color.ColorRepresentation;
import io.github.haykam821.lastcard.card.color.ColorSelector;
import io.github.haykam821.lastcard.game.player.AbstractPlayerEntry;
import net.minecraft.enchantment.Enchantments;
Expand Down Expand Up @@ -69,9 +70,9 @@ public ColorSelector getSelector() {
return this.selector;
}

public final DrawableCanvas render() {
DrawableCanvas canvas = this.selector.getTemplate().copy();
CanvasColor textColor = this.selector.getCanvasTextColor();
public final DrawableCanvas render(ColorRepresentation overrideColor) {
DrawableCanvas canvas = overrideColor.getTemplate().copy();
CanvasColor textColor = overrideColor.getCanvasTextColor();

this.renderOverlay(canvas, textColor);
this.renderOverlay(ViewUtils.flipY(ViewUtils.flipX(canvas)), textColor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public interface ColorSelector extends ColorRepresentation {
*/
public CardColor select(double x, double y);

/**
* {@return the colors that can be selected when playing this card, or {@code null} if {@link #select default selection behavior} should be used
*/
public Iterable<CardColor> getSelectableColors();

public boolean isMatching(CardColor color);

public static ColorSelector of(CardColor color) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public CardColor select(double x, double y) {
return this.color;
}

@Override
public Iterable<CardColor> getSelectableColors() {
return null;
}

@Override
public boolean isMatching(CardColor color) {
return this.color == color;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.haykam821.lastcard.card.color;

import java.util.List;

import eu.pb4.mapcanvas.api.core.CanvasColor;
import eu.pb4.mapcanvas.api.core.DrawableCanvas;
import io.github.haykam821.lastcard.card.display.CardTemplates;
Expand All @@ -11,6 +13,7 @@

public class WildColorSelector implements ColorSelector {
protected static final ColorSelector INSTANCE = new WildColorSelector();
protected static final Iterable<CardColor> SELECTABLE_COLORS = List.of(CardColor.values());

@Override
public CardColor select(double x, double y) {
Expand All @@ -29,6 +32,11 @@ public CardColor select(double x, double y) {
}
}

@Override
public Iterable<CardColor> getSelectableColors() {
return SELECTABLE_COLORS;
}

@Override
public boolean isMatching(CardColor color) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import eu.pb4.mapcanvas.api.utils.VirtualDisplay;
import eu.pb4.mapcanvas.api.utils.VirtualDisplay.TypedInteractionCallback;
import io.github.haykam821.lastcard.card.Card;
import io.github.haykam821.lastcard.card.color.CardColor;
import io.github.haykam821.lastcard.card.display.layout.CardLayout;
import io.github.haykam821.lastcard.card.display.layout.CardSpacing;
import io.github.haykam821.lastcard.card.display.layout.LayoutEntry;
Expand All @@ -29,7 +30,7 @@
public abstract class CardDisplay implements TypedInteractionCallback {
protected final PlayerEntryGetter entryGetter;

private final Map<Card, DrawableCanvas> canvasCache = new HashMap<>();
private final Map<CardRenderData, DrawableCanvas> canvasCache = new HashMap<>();
private final VirtualDisplay display;

private final Set<CardRegion> regions = new HashSet<>();
Expand Down Expand Up @@ -69,9 +70,11 @@ public void update() {

for (LayoutEntry entry : layout.getEntries()) {
Card card = entry.card();
entry.render(canvas, this.getCardCanvas(card), this.hasOutline(card), startX, startY);
CardColor overrideColor = entry.overrideColor();

CardRegion region = this.getCardRegion(card, startX + entry.x(), startY + entry.y(), startX + entry.maxX(), startY + entry.maxY());
entry.render(canvas, this.getCardCanvas(card, overrideColor), this.hasOutline(card), startX, startY);

CardRegion region = this.getCardRegion(card, overrideColor, startX + entry.x(), startY + entry.y(), startX + entry.maxX(), startY + entry.maxY());
if (region != null) {
this.regions.add(region);
}
Expand Down Expand Up @@ -126,10 +129,15 @@ public final void destroy() {

public abstract Iterable<Card> getCards();

public abstract DrawableCanvas renderCardCanvas(Card card);
public boolean shouldFlattenSelectors() {
return false;
}

public abstract DrawableCanvas renderCardCanvas(CardRenderData data);

public final DrawableCanvas getCardCanvas(Card card) {
return this.canvasCache.computeIfAbsent(card, this::renderCardCanvas);
public final DrawableCanvas getCardCanvas(Card card, CardColor overrideColor) {
CardRenderData data = new CardRenderData(card, overrideColor);
return this.canvasCache.computeIfAbsent(data, this::renderCardCanvas);
}

/**
Expand All @@ -156,7 +164,7 @@ public boolean hasOutline(Card card) {
return false;
}

public CardRegion getCardRegion(Card card, int minX, int minY, int maxX, int maxY) {
public CardRegion getCardRegion(Card card, CardColor color, int minX, int minY, int maxX, int maxY) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.haykam821.lastcard.card.display;

import eu.pb4.mapcanvas.api.core.DrawableCanvas;
import io.github.haykam821.lastcard.card.Card;
import io.github.haykam821.lastcard.card.color.CardColor;
import io.github.haykam821.lastcard.card.color.ColorRepresentation;

public record CardRenderData(Card card, CardColor overrideColor) {
public DrawableCanvas renderCard() {
ColorRepresentation color = this.overrideColor == null ? this.card.getSelector() : this.overrideColor;
return this.card.render(color);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package io.github.haykam821.lastcard.card.display.layout;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import eu.pb4.mapcanvas.api.core.DrawableCanvas;
import io.github.haykam821.lastcard.card.Card;
import io.github.haykam821.lastcard.card.color.CardColor;
import io.github.haykam821.lastcard.card.display.CardDisplay;

public class CardLayout {
private static final Iterable<CardColor> NO_OVERRIDES_SET = Collections.singleton(null);

private final CardDisplay display;
private final List<LayoutEntry> entries = new ArrayList<>();

Expand Down Expand Up @@ -40,35 +44,49 @@ public void addCards(DrawableCanvas canvas, Iterable<Card> cards) {
int row = 0;

for (Card card : cards) {
DrawableCanvas cardCanvas = this.display.getCardCanvas(card);
for (CardColor overrideColor : this.getOverrideColors(card)) {
DrawableCanvas cardCanvas = this.display.getCardCanvas(card, overrideColor);

if (cardCanvas != null) {
int width = cardCanvas.getWidth();
int height = cardCanvas.getHeight();
if (cardCanvas != null) {
int width = cardCanvas.getWidth();
int height = cardCanvas.getHeight();

if (x + width >= maxX) {
row += 1;
x = this.display.getHorizontalMargin(row);
y += this.display.getVerticalSpacing(maxHeight);
if (x + width >= maxX) {
row += 1;
x = this.display.getHorizontalMargin(row);
y += this.display.getVerticalSpacing(maxHeight);

maxX = canvas.getWidth() - x;
maxX = canvas.getWidth() - x;

if (y + height >= canvas.getHeight()) {
break;
if (y + height >= canvas.getHeight()) {
break;
}
}
}

this.entries.add(new LayoutEntry(card, x, y, width, height));
this.entries.add(new LayoutEntry(card, overrideColor, x, y, width, height));

this.totalWidth = Math.max(x + width, this.totalWidth);
this.totalHeight = Math.max(y + height, this.totalHeight);
this.totalWidth = Math.max(x + width, this.totalWidth);
this.totalHeight = Math.max(y + height, this.totalHeight);

maxHeight = Math.max(maxHeight, height);
x += this.display.getHorizontalSpacing(width);
maxHeight = Math.max(maxHeight, height);
x += this.display.getHorizontalSpacing(width);
}
}
}

this.totalWidth += this.display.getHorizontalMargin(0);
this.totalHeight += CardSpacing.PADDING_Y;
}

private Iterable<CardColor> getOverrideColors(Card card) {
if (card != null && this.display.shouldFlattenSelectors()) {
Iterable<CardColor> selectableColors = card.getSelector().getSelectableColors();

if (selectableColors != null) {
return selectableColors;
}
}

return NO_OVERRIDES_SET;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import eu.pb4.mapcanvas.api.core.DrawableCanvas;
import eu.pb4.mapcanvas.api.utils.CanvasUtils;
import io.github.haykam821.lastcard.card.Card;
import io.github.haykam821.lastcard.card.color.CardColor;
import io.github.haykam821.lastcard.card.display.CardOutlineRenderer;

public record LayoutEntry(Card card, int x, int y, int width, int height) {
public record LayoutEntry(Card card, CardColor overrideColor, int x, int y, int width, int height) {
public void render(DrawableCanvas canvas, DrawableCanvas cardCanvas, boolean outline, int startX, int startY) {
int x = startX + this.x;
int y = startY + this.y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.github.haykam821.lastcard.card.Card;
import io.github.haykam821.lastcard.card.CardDeck;
import io.github.haykam821.lastcard.card.display.CardDisplay;
import io.github.haykam821.lastcard.card.display.CardRenderData;
import io.github.haykam821.lastcard.card.display.CardTemplates;
import io.github.haykam821.lastcard.game.phase.PlayerEntryGetter;
import xyz.nucleoid.map_templates.TemplateRegion;
Expand Down Expand Up @@ -37,11 +38,11 @@ public Iterable<Card> getCards() {
}

@Override
public DrawableCanvas renderCardCanvas(Card card) {
if (card == null) {
public DrawableCanvas renderCardCanvas(CardRenderData data) {
if (data.card() == null) {
return CardTemplates.BACK;
}

return card.render();
return data.renderCard();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.haykam821.lastcard.card.Card;
import io.github.haykam821.lastcard.card.CardDeck;
import io.github.haykam821.lastcard.card.color.CardColor;
import io.github.haykam821.lastcard.card.display.region.CardRegion;
import io.github.haykam821.lastcard.card.display.region.DrawCardRegion;
import io.github.haykam821.lastcard.game.phase.PlayerEntryGetter;
Expand All @@ -18,7 +19,7 @@ public boolean hasOutline(Card card) {
}

@Override
public CardRegion getCardRegion(Card card, int minX, int minY, int maxX, int maxY) {
public CardRegion getCardRegion(Card card, CardColor overrideColor, int minX, int minY, int maxX, int maxY) {
if (card == null) {
return new DrawCardRegion(minX, minY, maxX, maxY);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
package io.github.haykam821.lastcard.card.display.player;

import java.util.List;

import eu.pb4.mapcanvas.api.core.DrawableCanvas;
import io.github.haykam821.lastcard.card.Card;
import io.github.haykam821.lastcard.card.color.CardColor;
import io.github.haykam821.lastcard.card.display.CardRenderData;
import io.github.haykam821.lastcard.card.display.region.CardRegion;
import io.github.haykam821.lastcard.card.display.region.OpenSelectorCardRegion;
import io.github.haykam821.lastcard.card.display.region.PlayCardRegion;
import io.github.haykam821.lastcard.game.player.AbstractPlayerEntry;
import xyz.nucleoid.map_templates.TemplateRegion;

public class PrivateCardDisplay extends PlayerCardDisplay {
private Card selectingCard = null;

public PrivateCardDisplay(AbstractPlayerEntry player, TemplateRegion region) {
super(player, region);
}

public void setSelectingCard(Card card) {
this.selectingCard = card;
this.update();
}

@Override
public Iterable<Card> getCards() {
if (this.selectingCard != null) {
return List.of(this.selectingCard);
}

return super.getCards();
}

@Override
public boolean shouldFlattenSelectors() {
return this.selectingCard != null;
}

@Override
public DrawableCanvas renderCardCanvas(Card card) {
return card.render();
public DrawableCanvas renderCardCanvas(CardRenderData data) {
return data.renderCard();
}

@Override
Expand All @@ -23,7 +49,11 @@ public boolean hasOutline(Card card) {
}

@Override
public CardRegion getCardRegion(Card card, int minX, int minY, int maxX, int maxY) {
return new PlayCardRegion(card, minX, minY, maxX, maxY);
public CardRegion getCardRegion(Card card, CardColor overrideColor, int minX, int minY, int maxX, int maxY) {
if (card.getSelector().getSelectableColors() != null && this.selectingCard == null) {
return new OpenSelectorCardRegion(card, this, minX, minY, maxX, maxY);
}

return new PlayCardRegion(card, overrideColor, this, minX, minY, maxX, maxY);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.haykam821.lastcard.card.display.player;

import eu.pb4.mapcanvas.api.core.DrawableCanvas;
import io.github.haykam821.lastcard.card.Card;
import io.github.haykam821.lastcard.card.display.CardRenderData;
import io.github.haykam821.lastcard.card.display.CardTemplates;
import io.github.haykam821.lastcard.game.player.AbstractPlayerEntry;
import xyz.nucleoid.map_templates.TemplateRegion;
Expand All @@ -12,7 +12,7 @@ public PublicCardDisplay(AbstractPlayerEntry player, TemplateRegion region) {
}

@Override
public DrawableCanvas renderCardCanvas(Card card) {
public DrawableCanvas renderCardCanvas(CardRenderData data) {
return CardTemplates.BACK;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.github.haykam821.lastcard.card.display.region;

import io.github.haykam821.lastcard.card.Card;
import io.github.haykam821.lastcard.card.display.player.PrivateCardDisplay;
import io.github.haykam821.lastcard.game.player.AbstractPlayerEntry;

public class OpenSelectorCardRegion extends CardRegion {
private final Card card;
private final PrivateCardDisplay display;

public OpenSelectorCardRegion(Card card, PrivateCardDisplay display, int minX, int minY, int maxX, int maxY) {
super(minX, minY, maxX, maxY);

this.card = card;
this.display = display;
}

@Override
public void onClick(AbstractPlayerEntry player, int x, int y) {
this.display.setSelectingCard(this.card);
}

@Override
public String toString() {
return "OpenSelectorCardRegion{card=" + this.card + ", display=" + this.display + "}" + this.getBoundsString();
}
}
Loading

0 comments on commit 64b4228

Please sign in to comment.