Archived
Private
Public Access
1
0

Initial commit

This commit is contained in:
2022-09-04 12:45:01 +02:00
commit f4a01d6a69
11601 changed files with 4206660 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
package de.craftix;
import java.util.HashMap;
public class CommandManager {
private static HashMap<String, Command> cmds = new HashMap<>();
public static void input(String qry) {
if (qry.contains(" ")) {
String[] argsRaw = qry.split(" ");
String cmd = argsRaw[0];
String[] args = new String[argsRaw.length - 1];
for (int i = 1; i < argsRaw.length; i++) args[i - 1] = argsRaw[i];
Command executor = cmds.get(cmd.toLowerCase());
if (executor != null) executor.onCommand(cmd, args);
}else {
Command executor = cmds.get(qry);
if (executor != null) executor.onCommand(qry, new String[0]);
}
}
public static void addCommand(String cmd, Command executor) {
cmds.put(cmd.toLowerCase(), executor);
}
public interface Command {
void onCommand(String cmd, String[] args);
}
}

View File

@@ -0,0 +1,144 @@
package de.craftix;
import de.craftix.commands.*;
import de.craftix.engine.*;
import de.craftix.gui.*;
import de.craftix.gui.factory.Information;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.time.Duration;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
public class IdleGame implements ActionListener {
public static long money;
public static Engine engine;
public static Timer timer;
public static Save data;
public static Screen screen;
public static MenuBar menuBar;
public static Shop shop;
public static Factory factory;
public static Upgrades upgrades;
public static Booster booster;
public static ArrayList<ContentBar> contentMenus = new ArrayList<>();
public static void main(String[] args) {
screen = new Screen("IdleGame");
setGenerators();
File file = new File("data.save");
if (file.exists()) {
data = Save.getData();
money = data.money;
engine.setGenerators(data.convertGenerators());
engine.calculate((int) (System.currentTimeMillis() - data.lastCalculationTime) / 1000);
}else data = new Save();
CommandManager.addCommand("money", new MoneyCmd());
CommandManager.addCommand("generator", new GeneratorCmd());
//Commands
new Thread(() -> {
while (!Thread.currentThread().isInterrupted()) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
CommandManager.input(reader.readLine());
}catch (Exception ignored) {}
}
}).start();
timer = new Timer(1000, new IdleGame());
timer.start();
Log.info("Game started successfully");
}
private static void setGenerators() {
engine = new Engine();
Generator genDefault = new Generator();
genDefault.owned = 1;
genDefault.baseCost = 10;
genDefault.baseRevenue = 5;
genDefault.baseProductionTimeInSeconds = 5;
genDefault.costFactor = 1.01f;
genDefault.name = "Default";
genDefault.updateProductionTime();
Engine.genPresets.add(genDefault);
engine.addGenerator(genDefault);
}
public static String convertMoney() {
String output = String.valueOf(money);
if (output.length() < 4) return output + " $";
if (output.length() > 3 && output.length() <= 6) {
output = money / 1000 + "K $";
return output;
}
if (output.length() > 6 && output.length() <= 9) {
output = money / 1000000 + "M $";
return output;
}
if (output.length() > 9 && output.length() <= 12) {
output = money / 1000000000 + "B $";
return output;
}
if (output.length() > 12 && output.length() <= 15) {
output = money / 1000000000000L + "T $";
return output;
}
if (output.length() > 15) {
output = money / 1000000000000000L + "Q $";
}
return output + " $";
}
public static String convertCost(double cost) {
String output = String.valueOf(cost);
if (cost < 1000) return output;
if (cost > 999 && cost <= 1000000) {
output = cost / 1000 + "K";
return output;
}
if (cost > 9999999 && cost <= 1000000000) {
output = cost / 1000000 + "M";
return output;
}
if (cost > 9999999999L && cost <= 1000000000000L) {
output = cost / 1000000000 + "B";
return output;
}
if (cost > 9999999999999L && cost <= 1000000000000000L) {
output = cost / 1000000000000L + "T";
return output;
}
if (cost > 9999999999999999L) {
output = cost / 1000000000000000L + "Q";
}
return output;
}
public static void setVisible(ContentBar label, boolean value) {
for (ContentBar all : contentMenus) {
if (label == all) continue;
all.setVisible(false);
all.isVisible = false;
if (label.button != null) all.updateButton();
}
label.setVisible(value);
}
@Override
public void actionPerformed(ActionEvent e) {
engine.calculate(1);
data.lastCalculationTime = System.currentTimeMillis();
}
}

View File

@@ -0,0 +1,26 @@
package de.craftix;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Log {
public static void info(String message) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.out.println("[" + dtf.format(now) + "] [INFO] " + message);
}
public static void warning(String message) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.out.println("\u001B[33m" + "[" + dtf.format(now) + "] [WARNING] " + message);
}
public static void error(String message) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.err.println("[" + dtf.format(now) + "] [ERROR] " + message);
}
}

View File

@@ -0,0 +1,39 @@
package de.craftix.commands;
import de.craftix.CommandManager;
import de.craftix.IdleGame;
import de.craftix.Log;
import de.craftix.engine.Generator;
public class GeneratorCmd implements CommandManager.Command {
@Override
public void onCommand(String cmd, String[] args) {
if (args[0].equalsIgnoreCase("add")) {
Generator gen = IdleGame.engine.getGenerator(args[1]);
if (gen == null) {
Log.error("This generator does not exists");
return;
}
if (args.length == 3) gen.owned += Integer.parseInt(args[2]);
else gen.owned++;
}
if (args[0].equalsIgnoreCase("remove")) {
Generator gen = IdleGame.engine.getGenerator(args[1]);
if (gen == null) {
Log.error("This generator does not exists");
return;
}
if (args.length == 3) gen.owned -= Integer.parseInt(args[2]);
else gen.owned--;
}
if (args[0].equalsIgnoreCase("set")) {
Generator gen = IdleGame.engine.getGenerator(args[1]);
if (gen == null) {
Log.error("This generator does not exists");
return;
}
if (args.length == 3) gen.owned = Integer.parseInt(args[2]);
else gen.owned = 1;
}
}
}

View File

@@ -0,0 +1,14 @@
package de.craftix.commands;
import de.craftix.CommandManager;
import de.craftix.IdleGame;
public class MoneyCmd implements CommandManager.Command {
@Override
public void onCommand(String cmd, String[] args) {
if (args.length != 2) return;
if (args[0].equalsIgnoreCase("set")) IdleGame.money = Long.parseLong(args[1]);
if (args[0].equalsIgnoreCase("add")) IdleGame.money += Long.parseLong(args[1]);
if (args[0].equalsIgnoreCase("remove")) IdleGame.money -= Long.parseLong(args[1]);
}
}

View File

@@ -0,0 +1,56 @@
package de.craftix.engine;
import java.util.ArrayList;
public class Engine extends Thread {
private ArrayList<Generator> generatorTypes = new ArrayList<>();
public static ArrayList<Generator> genPresets = new ArrayList<>();
public void calculate(int timeInterval) {
for (Generator all : generatorTypes) {
all.produce(timeInterval);
}
}
public String[] getGeneratorNames() {
String[] array = new String[generatorTypes.size()];
for (int i = 0; i < array.length; i++) array[i] = generatorTypes.get(i).name;
return array;
}
public Generator getGeneratorByPeace(String name) {
char[] chars = name.toCharArray();
StringBuilder countString = new StringBuilder();
for (char c : chars) {
if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9') {
countString.append(c);
}
}
name = name.replace(" - " + countString.toString(), "");
for (Generator all : generatorTypes) if (all.name.equals(name)) return all;
return null;
}
public Generator[] getGenerators() { return generatorTypes.toArray(new Generator[0]); }
public void addGenerator(Generator gen) { generatorTypes.add(gen); }
public void setGenerators(ArrayList<Generator> gens) { generatorTypes = gens; }
public ArrayList<Generator> getGeneratorsAsList() { return generatorTypes; }
public Generator getGeneratorByName(String name) {
for (Generator all : generatorTypes) {
if (all.name.equals(name)) return all;
}
return null;
}
public static Generator getGenerator(String name) {
for (Generator all : genPresets) {
if (all.name.equals(name)) return all;
}
return null;
}
}

View File

@@ -0,0 +1,45 @@
package de.craftix.engine;
import de.craftix.IdleGame;
public class Generator {
public int owned;
public double baseCost;
public double baseRevenue;
public float baseProductionTimeInSeconds;
public float costFactor;
public float productionTimeInSeconds;
public double productionCycleInSeconds;
public String name;
public double calculateNextBuildingCosts(int times) {
double kOverR = Math.pow(costFactor, owned);
double kPlusNOverR = Math.pow(costFactor, owned + times);
return baseCost + ((kOverR - kPlusNOverR) / (1 - costFactor));
}
public boolean canBeBuild() { return IdleGame.money >= calculateNextBuildingCosts(1); }
public void Build() {
if (!canBeBuild()) return;
IdleGame.money -= calculateNextBuildingCosts(1);
owned++;
updateProductionTime();
}
public void produce(long deltaTimeInSeconds) {
if (owned == 0) return;
productionCycleInSeconds += deltaTimeInSeconds;
double calculatedSum = 0;
while (productionCycleInSeconds >= productionTimeInSeconds)
{
calculatedSum += baseRevenue * owned;
productionCycleInSeconds -= productionTimeInSeconds;
}
IdleGame.money += calculatedSum;
}
public void updateProductionTime() { productionTimeInSeconds = baseProductionTimeInSeconds; }
}

View File

@@ -0,0 +1,70 @@
package de.craftix.engine;
import de.craftix.Log;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
public class Save implements Serializable {
public static void save(Save instance) { writeObjectToFile(instance, "data.save"); }
public static Save getData() { return (Save) readObjectFromFile("data.save"); }
private static void writeObjectToFile(Object serObj, String filepath) {
try {
FileOutputStream fileOut = new FileOutputStream(filepath);
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(serObj);
objectOut.close();
Log.info("The Object was successfully written to a file");
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static Object readObjectFromFile(String filepath) {
try {
FileInputStream fileIn = new FileInputStream(filepath);
ObjectInputStream objectIn = new ObjectInputStream(fileIn);
Object obj = objectIn.readObject();
Log.info("The Object has been read from the file");
objectIn.close();
return obj;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
//nonStatic
public long money;
public long lastCalculationTime;
public HashMap<String, Integer> generators = new HashMap<>();
public Save() {}
public ArrayList<Generator> convertGenerators() {
ArrayList<Generator> gens = new ArrayList<>();
for (String name : generators.keySet()) {
Generator gen = Engine.getGenerator(name);
if (gen == null) continue;
gen.owned = generators.get(name);
gens.add(gen);
}
return gens;
}
public void convertGenerators(ArrayList<Generator> gens) {
for (Generator all : gens) generators.put(all.name, all.owned);
}
}

View File

@@ -0,0 +1,18 @@
package de.craftix.gui;
import de.craftix.IdleGame;
import java.awt.*;
public class Booster extends ContentBar {
public Booster(int width, int height, int x, int y) {
super(width, height, x, y);
IdleGame.booster = this;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
repaint();
}
}

View File

@@ -0,0 +1,57 @@
package de.craftix.gui;
import de.craftix.IdleGame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ContentBar extends JLabel implements ActionListener {
public boolean isVisible;
protected int width;
protected int height;
protected int x;
protected int y;
public JButton button;
public ContentBar(int width, int height, int x, int y) {
setVisible(false);
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.isVisible = false;
setSize(width, height);
setBounds(x, y, width, height);
IdleGame.contentMenus.add(this);
IdleGame.screen.add(this);
}
@Override
public void actionPerformed(ActionEvent e) {
isVisible = !isVisible;
IdleGame.setVisible(this, isVisible);
updateButton();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
g.setColor(Color.GRAY);
g.fillRect(0, 0, width, height);
g.setColor(Color.BLACK);
}
public void updateButton() {
if (isVisible) button.setBackground(Color.GRAY);
else button.setBackground(Color.DARK_GRAY);
}
public void setButton(JButton button) {
this.button = button;
}
}

View File

@@ -0,0 +1,35 @@
package de.craftix.gui;
import de.craftix.IdleGame;
import de.craftix.engine.Generator;
import de.craftix.gui.factory.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
public class Factory extends ContentBar implements ListSelectionListener {
public static Generator selectedGen;
public Factory(int width, int height, int x, int y) {
super(width, height, x, y);
IdleGame.factory = this;
add(new GenList());
add(new Information(550, 490, 200, 10));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
repaint();
}
@Override
public void valueChanged(ListSelectionEvent e) {
DefaultListSelectionModel list = (DefaultListSelectionModel) e.getSource();
if (list.getSelectedIndices().length == 0) return;
selectedGen = IdleGame.engine.getGeneratorByName(IdleGame.engine.getGeneratorNames()[list.getSelectedIndices()[0]]);
}
}

View File

@@ -0,0 +1,71 @@
package de.craftix.gui;
import de.craftix.IdleGame;
import javax.swing.*;
import java.awt.*;
public class MenuBar extends JLabel {
private final int width;
private final int height;
private final int x;
private final int y;
private final int contentX;
private final int contentY;
private final int contentWidth;
private final int contentHeight;
public MenuBar(int width, int height, int x, int y) {
this.width = width;
this.height = height;
this.x = x;
this.y = y;
contentWidth = Screen.width;
contentHeight = Screen.height - height;
contentX = 0;
contentY = y + height;
setSize(width, height);
setBounds(x, y, width, height);
IdleGame.menuBar = this;
add(buttonSetup("Factory", 0, new Factory(contentWidth, contentHeight, contentX, contentY)));
add(buttonSetup("Shop", 1, new Shop(contentWidth, contentHeight, contentX, contentY)));
add(buttonSetup("Upgrades", 2, new Upgrades(contentWidth, contentHeight, contentX, contentY)));
add(buttonSetup("Booster", 3, new Booster(contentWidth, contentHeight, contentX, contentY)));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
g.setColor(Color.LIGHT_GRAY);
g.fillRect(x, y, width, height);
g.setColor(Color.BLACK);
g.drawString("Money: " + IdleGame.convertMoney(), 690, 25);
repaint();
}
private Rectangle getButtonPosition(int buttonID) {
Rectangle rec = new Rectangle();
rec.x = 10 + (110 * buttonID);
rec.y = 10;
rec.width = 100;
rec.height = 30;
return rec;
}
private JButton buttonSetup(String title, int buttonID, ContentBar bar) {
JButton button = new JButton(title);
bar.setButton(button);
button.setBounds(getButtonPosition(buttonID));
button.setVisible(true);
button.setBackground(Color.DARK_GRAY);
button.setForeground(Color.WHITE);
button.setFocusPainted(false);
button.addActionListener(bar);
return button;
}
}

View File

@@ -0,0 +1,68 @@
package de.craftix.gui;
import de.craftix.IdleGame;
import de.craftix.Log;
import de.craftix.engine.Save;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class Screen extends JFrame {
public static int width = 800;
public static int height = 600;
public Screen(String title) {
setSize(width, height);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setTitle(title);
setVisible(true);
setBackground(Color.GRAY);
addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
IdleGame.data.money = IdleGame.money;
IdleGame.data.convertGenerators(IdleGame.engine.getGeneratorsAsList());
Save.save(IdleGame.data);
Log.info("Game stopped successfully");
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
});
add(new MenuBar(width, 50, 0, 0));
}
}

View File

@@ -0,0 +1,19 @@
package de.craftix.gui;
import de.craftix.IdleGame;
import java.awt.*;
public class Shop extends ContentBar {
public Shop(int width, int height, int x, int y) {
super(width, height, x, y);
IdleGame.shop = this;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
repaint();
}
}

View File

@@ -0,0 +1,18 @@
package de.craftix.gui;
import de.craftix.IdleGame;
import java.awt.*;
public class Upgrades extends ContentBar{
public Upgrades(int width, int height, int x, int y) {
super(width, height, x, y);
IdleGame.upgrades = this;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
repaint();
}
}

View File

@@ -0,0 +1,30 @@
package de.craftix.gui.factory;
import de.craftix.IdleGame;
import javax.swing.*;
import java.awt.*;
public class GenList extends JList {
public GenList() {
setSize(150, 490);
setBounds(10, 10, 150, 490);
setVisible(true);
setVisible(true);
setBackground(Color.LIGHT_GRAY);
setSelectionBackground(Color.WHITE);
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
getSelectionModel().addListSelectionListener(IdleGame.factory);
setListData(IdleGame.engine.getGeneratorNames());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
repaint();
updateUI();
}
}

View File

@@ -0,0 +1,47 @@
package de.craftix.gui.factory;
import de.craftix.IdleGame;
import de.craftix.engine.Generator;
import de.craftix.gui.Factory;
import javax.swing.*;
import java.awt.*;
public class Information extends JLabel {
private final int width;
private final int height;
public Information(int width, int height, int x, int y) {
this.width = width;
this.height = height;
setSize(width, height);
setBounds(x, y, width, height);
setVisible(true);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0, 0, width, height);
g.setColor(Color.GRAY);
g.fill3DRect(150, 150, 100, 100, true);
g.fillRect(300, 150, 100, 100);
if (Factory.selectedGen != null) {
Generator gen = Factory.selectedGen;
g.setColor(Color.BLACK);
g.drawString("Generator: " + gen.name, 10, 15);
g.drawString("Owned: " + IdleGame.convertCost(gen.owned), 10, 30);
g.drawString("Base Revenue: " + IdleGame.convertCost(gen.baseRevenue), 10, 45);
g.drawString("Base Cost: " + IdleGame.convertCost(gen.baseCost), 10, 60);
g.drawString("Next Generator costs: " + IdleGame.convertCost(Math.round(gen.calculateNextBuildingCosts(1))), 10, 75);
}
repaint();
}
}

View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: de.craftix.IdleGame