update RewardScene

This commit is contained in:
Anthony Calosa
2022-02-25 18:14:12 +08:00
parent 2a00684208
commit 0ec1e7a25a
2 changed files with 137 additions and 140 deletions

View File

@@ -18,6 +18,7 @@ import forge.adventure.util.Reward;
import forge.adventure.util.RewardActor; import forge.adventure.util.RewardActor;
import forge.adventure.world.WorldSave; import forge.adventure.world.WorldSave;
import forge.assets.ImageCache; import forge.assets.ImageCache;
import forge.gui.GuiBase;
/** /**
* Displays the rewards of a fight or a treasure * Displays the rewards of a fight or a treasure
@@ -26,79 +27,69 @@ public class RewardScene extends UIScene {
private TextButton doneButton; private TextButton doneButton;
public enum Type public enum Type {
{
Shop, Shop,
Loot Loot
} }
Type type; Type type;
Array<Actor> generated = new Array<>(); Array<Actor> generated = new Array<>();
static public final float CARD_WIDTH = 550f; static public final float CARD_WIDTH = 550f;
static public final float CARD_HEIGHT = 400f; static public final float CARD_HEIGHT = 400f;
static public final float CARD_WIDTH_TO_HEIGHT = CARD_WIDTH / CARD_HEIGHT; static public final float CARD_WIDTH_TO_HEIGHT = CARD_WIDTH / CARD_HEIGHT;
public RewardScene()
{ public RewardScene() {
super("ui/items.json"); super(GuiBase.isAndroid() ? "ui/items_mobile.json" : "ui/items.json");
} }
boolean doneClicked = false; boolean doneClicked = false;
float flipCountDown = 1.0f; float flipCountDown = 1.0f;
public boolean done()
{ public boolean done() {
GameHUD.getInstance().getTouchpad().setVisible(false); GameHUD.getInstance().getTouchpad().setVisible(false);
if (doneClicked) if (doneClicked)
return true; return true;
if(type==Type.Loot) if (type == Type.Loot) {
{
boolean wait = false; boolean wait = false;
for(Actor actor: new Array.ArrayIterator<>(generated)) for (Actor actor : new Array.ArrayIterator<>(generated)) {
{ if (!(actor instanceof RewardActor)) {
if(!(actor instanceof RewardActor))
{
continue; continue;
} }
RewardActor reward = (RewardActor) actor; RewardActor reward = (RewardActor) actor;
AdventurePlayer.current().addReward(reward.getReward()); AdventurePlayer.current().addReward(reward.getReward());
if(!reward.isFlipped()) if (!reward.isFlipped()) {
{
wait = true; wait = true;
reward.flip(); reward.flip();
} }
} }
if(wait) if (wait) {
{
flipCountDown = 3.0f; flipCountDown = 3.0f;
doneClicked = true; doneClicked = true;
} } else {
else
{
Forge.switchToLast(); Forge.switchToLast();
} }
} } else {
else
{
Forge.switchToLast(); Forge.switchToLast();
} }
return true; return true;
} }
@Override @Override
public void act(float delta) { public void act(float delta) {
stage.act(delta); stage.act(delta);
ImageCache.allowSingleLoad(); ImageCache.allowSingleLoad();
if(doneClicked) if (doneClicked) {
{
if (type == Type.Loot) if (type == Type.Loot)
flipCountDown -= Gdx.graphics.getDeltaTime(); flipCountDown -= Gdx.graphics.getDeltaTime();
if(flipCountDown<=0) if (flipCountDown <= 0) {
{
Forge.switchToLast(); Forge.switchToLast();
} }
} }
} }
@Override @Override
public void resLoaded() { public void resLoaded() {
super.resLoaded(); super.resLoaded();
@@ -112,29 +103,22 @@ public class RewardScene extends UIScene {
} }
@Override @Override
public boolean keyPressed(int keycode) public boolean keyPressed(int keycode) {
{ if (keycode == Input.Keys.ESCAPE) {
if (keycode == Input.Keys.ESCAPE)
{
done(); done();
} }
return true; return true;
} }
public void loadRewards(Array<Reward> newRewards, Type type, ShopActor shopActor) public void loadRewards(Array<Reward> newRewards, Type type, ShopActor shopActor) {
{
this.type = type; this.type = type;
doneClicked = false; doneClicked = false;
for (Actor actor : new Array.ArrayIterator<>(generated)) {
for(Actor actor: new Array.ArrayIterator<>(generated))
{
actor.remove(); actor.remove();
if(actor instanceof RewardActor) if (actor instanceof RewardActor) {
{
((RewardActor) actor).dispose(); ((RewardActor) actor).dispose();
} }
} }
@@ -167,11 +151,9 @@ public class RewardScene extends UIScene {
doneButton.setText("Take all"); doneButton.setText("Take all");
break; break;
} }
for(int h=1;h<targetHeight;h++) for (int h = 1; h < targetHeight; h++) {
{
cardHeight = h; cardHeight = h;
if(type==Type.Shop) if (type == Type.Shop) {
{
cardHeight += doneButton.getHeight(); cardHeight += doneButton.getHeight();
} }
//cardHeight=targetHeight/i; //cardHeight=targetHeight/i;
@@ -179,8 +161,7 @@ public class RewardScene extends UIScene {
newArea = newRewards.size * cardWidth * cardHeight; newArea = newRewards.size * cardWidth * cardHeight;
int rows = (int) (targetHeight / cardHeight); int rows = (int) (targetHeight / cardHeight);
int cols = (int) Math.ceil(newRewards.size / (double) rows); int cols = (int) Math.ceil(newRewards.size / (double) rows);
if(newArea>oldCardArea&&newArea<=targetArea&&rows*cardHeight<targetHeight&&cols*cardWidth<targetWidth) if (newArea > oldCardArea && newArea <= targetArea && rows * cardHeight < targetHeight && cols * cardWidth < targetWidth) {
{
oldCardArea = newArea; oldCardArea = newArea;
numberOfRows = rows; numberOfRows = rows;
numberOfColumns = cols; numberOfColumns = cols;
@@ -195,23 +176,18 @@ public class RewardScene extends UIScene {
xOff += (targetWidth - (cardWidth * numberOfColumns)) / 2f; xOff += (targetWidth - (cardWidth * numberOfColumns)) / 2f;
float spacing = 2; float spacing = 2;
int i = 0; int i = 0;
for(Reward reward:new Array.ArrayIterator<>(newRewards)) for (Reward reward : new Array.ArrayIterator<>(newRewards)) {
{
boolean skipCard = false; boolean skipCard = false;
if(type==Type.Shop) if (type == Type.Shop) {
{ if (shopActor.getMapStage().getChanges().wasCardBought(shopActor.getObjectID(), i)) {
if(shopActor.getMapStage().getChanges().wasCardBought(shopActor.getObjectID(),i))
{
skipCard = true; skipCard = true;
} }
} }
int currentRow = (i / numberOfColumns); int currentRow = (i / numberOfColumns);
float lastRowXAdjust = 0; float lastRowXAdjust = 0;
if(currentRow==numberOfRows-1) if (currentRow == numberOfRows - 1) {
{
int lastRowCount = newRewards.size % numberOfColumns; int lastRowCount = newRewards.size % numberOfColumns;
if (lastRowCount != 0) if (lastRowCount != 0)
lastRowXAdjust = ((numberOfColumns * cardWidth) - (lastRowCount * cardWidth)) / 2; lastRowXAdjust = ((numberOfColumns * cardWidth) - (lastRowCount * cardWidth)) / 2;
@@ -219,36 +195,29 @@ public class RewardScene extends UIScene {
RewardActor actor = new RewardActor(reward, type == Type.Loot); RewardActor actor = new RewardActor(reward, type == Type.Loot);
actor.setBounds(lastRowXAdjust + xOff + cardWidth * (i % numberOfColumns) + spacing, yOff + cardHeight * currentRow + spacing, cardWidth - spacing * 2, cardHeight - spacing * 2); actor.setBounds(lastRowXAdjust + xOff + cardWidth * (i % numberOfColumns) + spacing, yOff + cardHeight * currentRow + spacing, cardWidth - spacing * 2, cardHeight - spacing * 2);
if(type==Type.Shop) if (type == Type.Shop) {
{
if (currentRow != ((i + 1) / numberOfColumns)) if (currentRow != ((i + 1) / numberOfColumns))
yOff += doneButton.getHeight(); yOff += doneButton.getHeight();
TextButton buyCardButton = new BuyButton(shopActor.getObjectID(), i, shopActor.getMapStage().getChanges(), actor, doneButton); TextButton buyCardButton = new BuyButton(shopActor.getObjectID(), i, shopActor.getMapStage().getChanges(), actor, doneButton);
generated.add(buyCardButton); generated.add(buyCardButton);
if(!skipCard) if (!skipCard) {
{
stage.addActor(buyCardButton); stage.addActor(buyCardButton);
} }
} }
generated.add(actor); generated.add(actor);
if(!skipCard) if (!skipCard) {
{
stage.addActor(actor); stage.addActor(actor);
} }
i++; i++;
} }
updateBuyButtons(); updateBuyButtons();
} }
private void updateBuyButtons() { private void updateBuyButtons() {
for (Actor actor : new Array.ArrayIterator<>(generated)) {
for(Actor actor: new Array.ArrayIterator<>(generated)) if (actor instanceof BuyButton) {
{
if(actor instanceof BuyButton)
{
((BuyButton) actor).update(); ((BuyButton) actor).update();
} }
} }
@@ -260,9 +229,11 @@ public class RewardScene extends UIScene {
private final PointOfInterestChanges changes; private final PointOfInterestChanges changes;
RewardActor reward; RewardActor reward;
int price; int price;
void update() { void update() {
setDisabled(WorldSave.getCurrentSave().getPlayer().getGold() < price); setDisabled(WorldSave.getCurrentSave().getPlayer().getGold() < price);
} }
public BuyButton(int id, int i, PointOfInterestChanges ch, RewardActor actor, TextButton style) { public BuyButton(int id, int i, PointOfInterestChanges ch, RewardActor actor, TextButton style) {
super("", style.getStyle()); super("", style.getStyle());
this.objectID = id; this.objectID = id;
@@ -276,11 +247,9 @@ public class RewardScene extends UIScene {
price = CardUtil.getCardPrice(actor.getReward().getCard()); price = CardUtil.getCardPrice(actor.getReward().getCard());
setText("Buy for " + price); setText("Buy for " + price);
addListener(new ClickListener() { addListener(new ClickListener() {
@Override @Override
public void clicked(InputEvent event, float x, float y) { public void clicked(InputEvent event, float x, float y) {
if(Current.player().getGold()>= price) if (Current.player().getGold() >= price) {
{
changes.buyCard(objectID, index); changes.buyCard(objectID, index);
Current.player().takeGold(price); Current.player().takeGold(price);
Current.player().addReward(reward.getReward()); Current.player().addReward(reward.getReward());
@@ -289,7 +258,6 @@ public class RewardScene extends UIScene {
remove(); remove();
updateBuyButtons(); updateBuyButtons();
} }
} }
}); });
} }

View File

@@ -0,0 +1,29 @@
{
"width": 480,
"height": 270,
"yDown": true,
"elements": [
{
"type": "Image",
"name": "lastScreen",
"width": 480,
"height": 270
},
{
"name": "cards",
"x": 5,
"y": 5,
"width": 405,
"height": 265
},
{
"type": "TextButton",
"name": "done",
"text": "Take all",
"width": 48,
"height": 30,
"x": 420,
"y": 120
}
]
}