base game

This commit is contained in:
nora 2021-01-09 17:57:05 +01:00
commit b2a80d1c40
6 changed files with 326 additions and 0 deletions

15
src/ClickerModel.java Normal file
View file

@ -0,0 +1,15 @@
public class ClickerModel {
private double nicolas;
public ClickerModel() {
}
public double getNicolas() {
return nicolas;
}
public void setNicolas(double nicolas) {
this.nicolas = nicolas;
}
}

73
src/ClickerPresenter.java Normal file
View file

@ -0,0 +1,73 @@
import javax.swing.*;
import java.util.ArrayList;
public class ClickerPresenter {
public static final int TARGET_FPS = 30;
private final ClickerView clickerView;
private final ClickerModel clickerModel;
private final ArrayList<UpgradePanel> upgradePanels;
private final Timer loop;
public ClickerPresenter(ClickerView clickerView) {
this.clickerView = clickerView;
this.clickerView.setClickerPresenter(this);
UpgradePanel illusion = new UpgradePanel("Illision", 10, 1.05, 0.1, this);
UpgradePanel cloneMachine = new UpgradePanel("Klonmaschine", 100, 1.1, 1, this);
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 debugger = new UpgradePanel("Debugger", 1, 1, 100, this);
clickerView.addUpgrade(illusion);
clickerView.addUpgrade(cloneMachine);
clickerView.addUpgrade(mysteriousCave);
clickerView.addUpgrade(factory);
clickerView.addUpgrade(herblingen);
clickerView.addUpgrade(debugger);
upgradePanels = new ArrayList<>();
upgradePanels.add(illusion);
upgradePanels.add(cloneMachine);
upgradePanels.add(mysteriousCave);
upgradePanels.add(factory);
upgradePanels.add(herblingen);
upgradePanels.add(debugger);
clickerModel = new ClickerModel();
loop = new Timer(1000/TARGET_FPS, e -> refresh());
loop.start();
}
public static void main(String[] args) {
new ClickerPresenter(new ClickerView());
}
public void nicolasButtonClick() {
clickerModel.setNicolas(clickerModel.getNicolas() + 1);
//refresh();
}
private void refresh() {
clickerView.setNicolasAmount(String.format("%.0f Nicolas", clickerModel.getNicolas()));
upgradePanels.forEach(UpgradePanel::refresh);
}
public void removeNicolas(double amount) {
clickerModel.setNicolas(clickerModel.getNicolas() - amount);
//refresh();
}
public double getNicolas() {
return clickerModel.getNicolas();
}
public void addNicolas(double gain) {
clickerModel.setNicolas(clickerModel.getNicolas() + gain);
}
}

61
src/ClickerView.form Normal file
View file

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ClickerView">
<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">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="af3c1" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font size="22"/>
<text value="Timos Clicker Game"/>
</properties>
</component>
<hspacer id="b562">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="61c38" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="126e2" class="javax.swing.JButton" binding="nicolasButton" default-binding="true">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Nicolas"/>
</properties>
</component>
<component id="76493" class="javax.swing.JLabel" binding="nicolasLabels">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Label"/>
</properties>
</component>
</children>
</grid>
<grid id="d38b7" binding="thingsPanel" layout-manager="CardLayout" hgap="0" vgap="0">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
</children>
</grid>
</form>

35
src/ClickerView.java Normal file
View file

@ -0,0 +1,35 @@
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ClickerView extends JFrame {
private JButton nicolasButton;
private JPanel mainPanel;
private JPanel thingsPanel;
private JLabel nicolasLabels;
private ClickerPresenter clickerPresenter;
public ClickerView() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(700, 500);
setContentPane(mainPanel);
setLocationRelativeTo(null);
setVisible(true);
thingsPanel.setLayout(new BoxLayout(thingsPanel, BoxLayout.Y_AXIS));
nicolasButton.addActionListener(e -> clickerPresenter.nicolasButtonClick());
}
public void setClickerPresenter(ClickerPresenter clickerPresenter) {
this.clickerPresenter = clickerPresenter;
}
public void addUpgrade(UpgradePanel upgradePanel) {
thingsPanel.add(upgradePanel);
}
public void setNicolasAmount(String amount) {
nicolasLabels.setText(amount);
}
}

55
src/UpgradePanel.form Normal file
View file

@ -0,0 +1,55 @@
<?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">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="270" height="74"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<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"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="4ffd2" class="javax.swing.JLabel" binding="nameLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Klonmaschine"/>
</properties>
</component>
<component id="961fd" class="javax.swing.JLabel" binding="levelLabel">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Level 69"/>
</properties>
</component>
<component id="b99a1" class="javax.swing.JLabel" binding="gainLabel">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="324798 Nicolas"/>
</properties>
</component>
</children>
</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"/>
</constraints>
<properties>
<text value="Upgrade: 34805 Nicolas"/>
</properties>
</component>
</children>
</grid>
</form>

87
src/UpgradePanel.java Normal file
View file

@ -0,0 +1,87 @@
import javax.swing.*;
public class UpgradePanel extends JPanel {
private JPanel mainPanel;
private JButton upgradeButton;
private JLabel nameLabel;
private JLabel levelLabel;
private JLabel gainLabel;
private String name;
private final double costMultiplier;
private final double baseGain;
private double cost;
private double gain = 0;
private int level;
private long lastAddedTimeStamp = 0;
private long lastFrameTimeStamp = System.currentTimeMillis();
private final ClickerPresenter presenter;
public UpgradePanel(String name, double baseCost, double costMultiplier, double baseGain, ClickerPresenter presenter) {
add(mainPanel);
this.name = name;
this.cost = baseCost;
this.costMultiplier = costMultiplier;
this.baseGain = baseGain;
this.presenter = presenter;
this.nameLabel.setText(name);
upgradeButton.addActionListener(e -> upgrade(1));
}
public void upgrade(int 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);
//should nicolas be added?
long currentTime = System.currentTimeMillis();
long lastAddDeltaTime = currentTime - lastAddedTimeStamp;
double timePerNicolas = 1 / gain * 1000;
if (timePerNicolas < lastAddDeltaTime) {
long frameDeltaTime = currentTime - lastFrameTimeStamp;
System.out.printf("tpn=%.2f ladt=%d Δt=%d", timePerNicolas, lastAddDeltaTime, frameDeltaTime);
lastAddedTimeStamp = currentTime;
if (timePerNicolas < frameDeltaTime) {
System.out.printf(" FN");
double missedNicolas = frameDeltaTime / timePerNicolas;
System.out.printf(" mn=%.2f gain=%.2f add=%.2f", missedNicolas, gain, missedNicolas);
presenter.addNicolas(missedNicolas);
} else {
presenter.addNicolas(1);
}
System.out.print("\n");
}
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));
}
}