This commit is contained in:
nora 2021-01-10 12:40:47 +01:00
parent e36c7d258b
commit 5b021182c2
11 changed files with 230 additions and 22 deletions

View file

@ -9,6 +9,8 @@ public class ClickerPresenter {
private final ClickerModel clickerModel;
private final ArrayList<UpgradePanel> upgradePanels;
LargeFormatter formatter = new LargeFormatter();
private final Timer loop;
public ClickerPresenter(ClickerView clickerView) {
@ -20,14 +22,16 @@ public class ClickerPresenter {
UpgradePanel mysteriousCave = new UpgradePanel("Mysteriöse Höhle", 500, 1.1, 10, this);
UpgradePanel factory = new UpgradePanel("Massenfertigungsanstalt", 3000, 1.1, 150, this);
UpgradePanel herblingen = new UpgradePanel("Herblingen", 100000, 1.1, 6969, this);
UpgradePanel sihlcity = new UpgradePanel("Sihlcity", 1000000, 1.1, 50000, this);
UpgradePanel debugger = new UpgradePanel("Debugger", 1, 1, 100, this);
UpgradePanel debugger = new UpgradePanel("Debugger", 1, 1, 1000000000, this);
clickerView.addUpgrade(illusion);
clickerView.addUpgrade(cloneMachine);
clickerView.addUpgrade(mysteriousCave);
clickerView.addUpgrade(factory);
clickerView.addUpgrade(herblingen);
clickerView.addUpgrade(sihlcity);
clickerView.addUpgrade(debugger);
upgradePanels = new ArrayList<>();
@ -36,11 +40,12 @@ public class ClickerPresenter {
upgradePanels.add(mysteriousCave);
upgradePanels.add(factory);
upgradePanels.add(herblingen);
upgradePanels.add(sihlcity);
upgradePanels.add(debugger);
clickerModel = new ClickerModel();
loop = new Timer(1000/TARGET_FPS, e -> refresh());
loop = new Timer(1000 / TARGET_FPS, e -> refresh());
loop.start();
}
@ -50,17 +55,15 @@ public class ClickerPresenter {
public void nicolasButtonClick() {
clickerModel.setNicolas(clickerModel.getNicolas() + 1);
//refresh();
}
private void refresh() {
clickerView.setNicolasAmount(String.format("%.0f Nicolas", clickerModel.getNicolas()));
clickerView.setNicolasAmount(formatter.formatBigNumber(clickerModel.getNicolas()) + " Nicolas");
upgradePanels.forEach(UpgradePanel::refresh);
}
public void removeNicolas(double amount) {
clickerModel.setNicolas(clickerModel.getNicolas() - amount);
//refresh();
}
public double getNicolas() {

View file

@ -1,6 +1,4 @@
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ClickerView extends JFrame {
private JButton nicolasButton;

View file

@ -0,0 +1,37 @@
public class LargeFormatter {
private static final String[] suffixes = {"", "k", "M", "B", "T", "q", "Q", "s", "S", "O", "N"};
//private static final String[] suffixes = {"", "k"}; //for large number testing
//input: 10 230 000 -> 10.23M
public String formatBigNumber(double number) {
int suffixSize = 0;
boolean scientific = false;
while (number > 999) {
if(suffixSize == suffixes.length - 1){
scientific = true;
break;
}
suffixSize++;
number /= 1000;
}
if(scientific){
int exp = 3 * suffixSize;
while(number >= 10){
exp++;
number /= 10;
}
return String.format("%.2fE%d", number, exp);
} else {
if(Math.floor(number) == number){
return String.format("%.0f%s", number, suffixes[suffixSize]);
} else {
return String.format("%.2f%s", number, suffixes[suffixSize]);
}
}
}
}

View file

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="UpgradePanel">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="270" height="74"/>
<xy x="20" y="20" width="337" height="74"/>
</constraints>
<properties/>
<border type="none"/>
@ -11,7 +11,7 @@
<grid id="51553" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="0" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
@ -44,12 +44,28 @@
</grid>
<component id="29e2a" class="javax.swing.JButton" binding="upgradeButton">
<constraints>
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Upgrade: 34805 Nicolas"/>
</properties>
</component>
<component id="30c13" class="javax.swing.JButton" binding="x10Button">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="x10"/>
</properties>
</component>
<component id="c7089" class="javax.swing.JButton" binding="x100Button">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="x100"/>
</properties>
</component>
</children>
</grid>
</form>

View file

@ -8,6 +8,8 @@ public class UpgradePanel extends JPanel {
private JLabel nameLabel;
private JLabel levelLabel;
private JLabel gainLabel;
private JButton x10Button;
private JButton x100Button;
private String name;
@ -22,6 +24,7 @@ public class UpgradePanel extends JPanel {
private long lastFrameTimeStamp = System.currentTimeMillis();
private final ClickerPresenter presenter;
private final LargeFormatter lf = new LargeFormatter();
public UpgradePanel(String name, double baseCost, double costMultiplier, double baseGain, ClickerPresenter presenter) {
add(mainPanel);
@ -35,20 +38,33 @@ public class UpgradePanel extends JPanel {
this.nameLabel.setText(name);
upgradeButton.addActionListener(e -> upgrade(1));
x10Button.addActionListener(e -> upgrade(10));
x100Button.addActionListener(e -> upgrade(100));
}
public void upgrade(int amount) {
presenter.removeNicolas(calculateExp(cost, costMultiplier, amount));
gain += baseGain * amount;
level += amount;
cost *= Math.pow(costMultiplier, amount);
System.err.println(calculateExp(cost, costMultiplier, amount));
/*
for (int i = 0; i < amount; i++) {
presenter.removeNicolas(cost);
gain += baseGain;
cost = cost * costMultiplier;
level++;
}
}*/
}
public void refresh() {
upgradeButton.setEnabled(presenter.getNicolas() >= cost);
x10Button.setEnabled(presenter.getNicolas() >= calculateExp(cost, costMultiplier, 10));
x100Button.setEnabled(presenter.getNicolas() >= calculateExp(cost, costMultiplier, 100));
//should nicolas be added?
long currentTime = System.currentTimeMillis();
@ -56,6 +72,7 @@ public class UpgradePanel extends JPanel {
double timePerNicolas = 1 / gain * 1000;
if (timePerNicolas < lastAddDeltaTime) {
//add nicolas
long frameDeltaTime = currentTime - lastFrameTimeStamp;
System.out.printf("tpn=%.2f ladt=%d Δt=%d", timePerNicolas, lastAddDeltaTime, frameDeltaTime);
@ -76,12 +93,22 @@ public class UpgradePanel extends JPanel {
lastFrameTimeStamp = currentTime;
levelLabel.setText("Level: " + level);
if(gain < 10){
gainLabel.setText(String.format("%.2f Nicolas", gain));
} else {
gainLabel.setText(String.format("%.0f Nicolas", gain));
}
upgradeButton.setText(String.format("Upgrade: %.0f Nicolas", cost));
gainLabel.setText(lf.formatBigNumber(gain) + " Nicolas");
upgradeButton.setText("Upgrade: " + lf.formatBigNumber(cost) + " Nicolas");
x10Button.setText("x10: " + lf.formatBigNumber(calculateExp(cost, costMultiplier, 10)) + " Nicolas");
x100Button.setText("x100: " + lf.formatBigNumber(calculateExp(cost, costMultiplier, 100)) + " Nicolas");
}
private double calculateExp(double c, double fac, double amount) {
//x10 cost = c + c*x + c*x*x + c*x*x*x... = c * x^1 + c*x^2 + c*x^3 + c*x^4 + c*x^5... =
// c * (x^0 + x^1 + x^2 + x^3...)
//new cost = c * x^amount
double result = 0;
for (int i = 0; i < amount; i++) {
result += Math.pow(fac, i);
}
return result * c;
}
}

View file

@ -0,0 +1,49 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class LargeFormatterLargeTest {
LargeFormatter lf;
@BeforeEach
void setUp() {
lf = new LargeFormatter();
}
@Test
void normal(){
double n = 100000;
String s = "100k";
assertEquals(s, lf.formatBigNumber(n));
}
@Test
void smallestSci(){
double n = 1000000;
String s = "1.00E6";
assertEquals(s, lf.formatBigNumber(n));
}
@Test
void biggerSci(){
double n = 10000000;
String s = "1.00E7";
assertEquals(s, lf.formatBigNumber(n));
}
@Test
void decimalSci(){
double n = 16900000;
String s = "1.69E7";
assertEquals(s, lf.formatBigNumber(n));
}
@Test
void pointNine(){
double n = 9780000;
String s = "9.78E6";
assertEquals(s, lf.formatBigNumber(n));
}
}

View file

@ -0,0 +1,42 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class LargeFormatterTest {
LargeFormatter lf;
@BeforeEach
void setUp() {
lf = new LargeFormatter();
}
@Test
void tinyNumberTest(){
double n = 0.1;
String s = "0.10";
assertEquals(s, lf.formatBigNumber(n));
}
@Test
void smallNumberTest(){
double n = 10;
String s = "10";
assertEquals(s, lf.formatBigNumber(n));
}
@Test
void kTest(){
double n = 1000;
String s = "1k";
assertEquals(s, lf.formatBigNumber(n));
}
@Test
void mTest(){
double n = 10230000;
String s = "10.23M";
assertEquals(s, lf.formatBigNumber(n));
}
}