1
0
mirror of https://github.com/Klagarge/PokeHES.git synced 2025-03-14 06:44:33 +00:00
PokeHES/src/Text/TextEnemy.java

185 lines
5.4 KiB
Java
Raw Normal View History

package Text;
2022-06-14 01:14:54 +02:00
import Entity.Enemy;
2022-06-16 22:09:17 +02:00
import java.util.Vector;
2022-06-12 12:10:57 +02:00
import java.util.Random;
public class TextEnemy {
2022-06-17 08:48:57 +02:00
private static final int CUT = 55;
public FightData fightData;
public SpeechData speechData;
2022-06-10 19:33:24 +02:00
public Vector<Line> lines = new Vector<Line>();
2022-06-13 11:41:24 +02:00
private int[] orderAttack;
private int[] orderAnswer;
2022-06-12 12:10:57 +02:00
2022-06-13 20:53:09 +02:00
private Vector<int[]> currentData;
2022-06-14 01:14:54 +02:00
public TextEnemy(Enemy e){
//generate the vector of fight
2022-06-14 01:14:54 +02:00
fightData = new FightData(e.getBranch());
fightData.readFile();
//generate the vector of Speechs
2022-06-14 01:14:54 +02:00
speechData = new SpeechData(e.getName());
speechData.readFile();
2022-06-17 07:37:07 +02:00
//save random data (attack and answer) : attack, answer 1, answer 2 answer 3, answer 4
2022-06-13 20:53:09 +02:00
currentData = new Vector<int[]>();
}
2022-06-17 07:37:07 +02:00
public static int[] randomGenerate( int min, int max, int nbrRandom){
2022-06-13 11:41:24 +02:00
//create an array with all the number I need
int[] a = new int[max-min+1];
int k = min;
for(int i=0;k<=max;i++){
a[i] = k;
k++;
2022-06-12 14:26:02 +02:00
}
2022-06-12 12:10:57 +02:00
2022-06-13 11:41:24 +02:00
//create a new array with the numbers I want
2022-06-17 07:37:07 +02:00
int[] b = new int[nbrRandom];
2022-06-13 11:41:24 +02:00
// Creating object for Random class
Random rd = new Random();
2022-06-16 22:09:17 +02:00
2022-06-13 11:41:24 +02:00
// Starting from the last element and swapping one by one.
for (int i = a.length-1; i > 0; i--) {
2022-06-16 22:09:17 +02:00
2022-06-13 11:41:24 +02:00
// Pick a random index from 0 to i
int j = rd.nextInt(i+1);
2022-06-16 22:09:17 +02:00
2022-06-13 11:41:24 +02:00
// Swap array[i] with the element at random index
int temp = a[i];
a[i] = a[j];
a[j] = temp;
2022-06-12 12:10:57 +02:00
}
2022-06-13 11:41:24 +02:00
//add the numbers I want
2022-06-17 07:37:07 +02:00
for(int i=0;i<nbrRandom;i++){
2022-06-13 11:41:24 +02:00
b[i] = a[i];
}
return b;
2022-06-12 12:10:57 +02:00
}
2022-06-16 23:29:36 +02:00
//generate the text who is displays in battle screen
public void generateText(){
2022-06-10 19:33:24 +02:00
int i =1;
2022-06-12 12:10:57 +02:00
//introduction line
2022-06-16 12:23:33 +02:00
String introduction = formatLine(speechData.getSpeechs(0), CUT);
lines.add(new Line(introduction, false));
2022-06-17 07:37:07 +02:00
orderAttack = randomGenerate(0, fightData.nbr_line-1, 4);
2022-06-16 23:29:36 +02:00
2022-06-10 19:33:24 +02:00
for(int j=0; j<4;j++){
2022-06-13 20:53:09 +02:00
int[] currentRandom = new int[5];
currentRandom[0] = orderAttack[j];
2022-06-12 12:10:57 +02:00
2022-06-17 09:05:22 +02:00
//generate a random array to determine the order of the answer
2022-06-13 11:41:24 +02:00
orderAnswer = randomGenerate(0, 3, 4);
2022-06-13 20:53:09 +02:00
//save the order of answer and attack
for(int k=1;k<5;k++){
currentRandom[k] = orderAnswer[k-1];
}
2022-06-16 12:23:33 +02:00
//Format the line
String attack = formatLine(speechData.getSpeechs(i++) + fightData.getAttack(orderAttack[j]).attack + " ? ("+fightData.getAttack(orderAttack[j]).getXp()+ ") ", CUT);
String answer1 = formatLine("1. " + fightData.getAttack(orderAttack[j]).getAnswer(orderAnswer[0]) , CUT);
String answer2 = formatLine("2. " + fightData.getAttack(orderAttack[j]).getAnswer(orderAnswer[1]) , CUT);
String answer3 = formatLine("3. " + fightData.getAttack(orderAttack[j]).getAnswer(orderAnswer[2]) , CUT);
String answer4 = formatLine("4. " + fightData.getAttack(orderAttack[j]).getAnswer(orderAnswer[3]) , CUT);
2022-06-15 22:29:34 +02:00
2022-06-13 20:53:09 +02:00
//attack and answer (number on vector : 1-4)
2022-06-16 12:23:33 +02:00
lines.add(new Line(attack + "\n" +answer1 + "\n" + answer2 + "\n" + answer3 + "\n" + answer4, true));
2022-06-13 20:53:09 +02:00
2022-06-16 23:29:36 +02:00
//save the order of the answer
2022-06-13 20:53:09 +02:00
currentData.add(currentRandom);
}
2022-06-13 20:53:09 +02:00
2022-06-16 23:29:36 +02:00
/*
2022-06-13 20:53:09 +02:00
for(int[] a : currentData){
System.out.println(Arrays.toString(a));
}
2022-06-16 23:29:36 +02:00
*/
2022-06-13 20:53:09 +02:00
//finish (win and death)
2022-06-16 12:23:33 +02:00
String dead = formatLine(speechData.getSpeechs(5),CUT);
String alive = formatLine(speechData.getSpeechs(6), CUT);
lines.add(new Line(dead, false));
lines.add(new Line(alive, false));
}
2022-06-16 23:29:36 +02:00
//get the saved order of the attacks and answer
2022-06-13 20:53:09 +02:00
public Vector<int[]> getCurrentData() {
return currentData;
}
2022-06-16 23:29:36 +02:00
//format a String with a specific length of char
2022-06-15 22:29:34 +02:00
public String formatLine(String line, int cut){
String cutLine = "";
String newLine = "";
int startC = 0;
2022-06-17 07:37:07 +02:00
int stopC = cut;
2022-06-15 22:29:34 +02:00
2022-06-16 23:29:36 +02:00
//check if the line is shorter than the character limit
2022-06-16 12:23:33 +02:00
if(cut>line.length()-1){
newLine =line;
}
else{
2022-06-16 23:29:36 +02:00
//create a array with the line
2022-06-16 12:23:33 +02:00
char[] c = new char[line.length()];
for(int i=0; i<c.length;i++){
c[i] = line.charAt(i);
}
2022-06-15 22:29:34 +02:00
2022-06-16 12:23:33 +02:00
while(true){
2022-06-17 07:37:07 +02:00
for(int i =stopC; i>=startC; i--){
2022-06-17 08:56:31 +02:00
2022-06-16 12:23:33 +02:00
if(c[i] == ' '){
2022-06-17 07:37:07 +02:00
stopC = i;
2022-06-16 12:23:33 +02:00
break;
}
2022-06-17 07:37:07 +02:00
else if(stopC == c.length-1){
2022-06-16 12:23:33 +02:00
break;
}
2022-06-15 22:29:34 +02:00
}
2022-06-16 12:23:33 +02:00
//découper le mot
2022-06-17 07:37:07 +02:00
for(int i=startC;i<=stopC;i++){
2022-06-17 08:56:31 +02:00
2022-06-16 12:23:33 +02:00
cutLine += c[i];
}
2022-06-15 22:29:34 +02:00
2022-06-16 23:29:36 +02:00
//rebuild the line with the line breaks
2022-06-16 12:23:33 +02:00
newLine += cutLine+"\n";
cutLine = "";
2022-06-15 22:29:34 +02:00
2022-06-17 07:37:07 +02:00
startC = stopC + 1;
2022-06-16 11:57:05 +02:00
2022-06-16 12:23:33 +02:00
2022-06-17 07:37:07 +02:00
if(c.length-1-stopC <=0){
2022-06-16 11:57:05 +02:00
2022-06-16 12:23:33 +02:00
break;
}
2022-06-17 07:37:07 +02:00
else if(c.length-1-stopC <= cut){
stopC = c.length-1;
2022-06-16 12:23:33 +02:00
}
else{
2022-06-17 07:37:07 +02:00
stopC += cut;
2022-06-16 12:23:33 +02:00
}
2022-06-15 22:29:34 +02:00
}
}
return newLine;
}
}
2022-06-09 06:58:49 +02:00