Semesterarbeit-1/main/Player.java

136 lines
3.3 KiB
Java

package minigame;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.TreeMap;
public class Player {
public static TreeMap<Integer,Horse> player_horses = new TreeMap<>();
private static int maxMapId = 0;
public int credits;
public String username;
public int level;
public int lvlpg;
public Player(String uname) {
this.username = uname;
this.credits = 50;
this.level = 1;
this.lvlpg = 0;
}
public String toString() {
return this.username + ", Credits: " + this.credits + ", Lvl: " + this.level + ", Lvl Progress: " + this.lvlpg + "%.";
}
public int getCred() {
return this.credits;
}
public void showBalance() {
System.out.println("Credits: " + this.credits);
}
public void levelUp() {
this.lvlpg-=100;
this.level+=1;
}
public void visLevelProgress() {
System.out.println("\nSYSTEM | Level Progress:");
String header = "0%[";
String ending = "]100%";
String con10 = "::::";
String pas10 = " ";
StringBuilder out1 = new StringBuilder();
int con10am = lvlpg/10;
out1.append(header);
for (int i = 0; i<con10am;i++) {
out1.append(con10);
}
for (int y = 0; y<10-con10am;y++) {
out1.append(pas10);
}
out1.append(ending);
System.out.print(out1.toString());
}
public String changeName(BufferedReader ter) {
while (true) {
Tools.printSlow30("\nSYSTEM | Choose a new Username or cancel this action with \"/p c\" : ");
try {
String uname = ter.readLine();
if (uname.equalsIgnoreCase("/p c")) {
return null;
} else {
if (uname.toCharArray().length > 15) {
System.out.println("\nSYSTEM | Please Choose a Shorter Username");
} else {
return uname;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void buyHorse(int id) {
int price = Shop.shop_horses.get(id).price;
if ((credits-price)>=0) {
credits-=price;
System.out.println();
Horse h = Shop.shop_horses.get(id);
h.tag = "Player";
maxMapId++;
player_horses.put(maxMapId, h);
Shop.shop_horses.remove(id);
Tools.printSlow30("SYSTEM | Bought " + player_horses.get(maxMapId).name+". ");
System.out.println(player_horses.get(1));
} else {
Tools.printSlow30("\nSYSTEM | You dont have enough credits for that right now!\n");
}
}
public void sellHorse(int id) {
if (player_horses.size()==1) {
Tools.printSlow30("SYSTEM | You cant sell your only horse.");
} else {
try {
int price = calcSellPrice(player_horses.get(id));
String name = player_horses.get(id).name;
player_horses.remove(id);
credits+=price;
Tools.printSlow30("SYSTEM | You sold " + name + " for " + price + " credits.");
} catch (Exception e) {
Tools.printSlow30("SYSTEM | ID: "+id+" didnt seem to work. Please try again.");
}
}
}
private int calcSellPrice(Horse horse) {
return (int)(horse.price * 0.8);
}
public void showHorses() {
for (Integer key : player_horses.keySet()) {
System.out.print("PLAYER | ID: " + key + ", " + player_horses.get(key)+"\n");
}
}
public void showOneHorse(int id) {
try {
Tools.printSlow30("\n"+player_horses.get(id));
} catch (Exception e) {
Tools.printSlow30("SYSTEM | Couldnt find: "+id+".\n");
}
}
}