package pelletQuest.entities;
import java.util.ArrayList;
import pelletQuest.resources.*;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Vector2f;
public class Voice {
private int timer;
private String speech;
public Voice() {
timer = 0;
speech = "";
}
public void update (int delta) {
timer -= delta;
}
public void render (Graphics g, float x, float y) {
if (timer > 0) {
drawSpeechBubble(g, speech, new Vector2f(x,y));
}
}
public void say (String str) {
speech = Translator.convert(str);
timer = 4000;
}
public void say (String str, int duration) {
speech = Translator.convert(str);
timer = duration;
}
public static void drawSpeechBubble(Graphics g, String str, Vector2f origin) {
//g.setFont(GraphicsManager.getSpeechFont());
ArrayList<String> lines = new ArrayList<String>();
while (str.length() > 0) {
int marker = 0;
while (g.getFont().getWidth(str.substring(0, marker)) < 160 && marker < str.length()) {
marker++;
}
while (marker < str.length() && str.charAt(marker) != ' ') {
marker--;
}
lines.add(str.substring(0, marker));
if (marker < str.length()) {
str = str.substring(marker+1);
} else {
str = "";
}
}
int longestLine = 0;
for (int i=0; i<lines.size(); i++) {
if (g.getFont().getWidth(lines.get(i)) > g.getFont().getWidth(lines.get(longestLine))) {
longestLine = i;
}
}
int sections = g.getFont().getWidth(lines.get(longestLine))+5;
if (g.getFont().getWidth(lines.get(longestLine)) % 16 > 0) {
sections /= 16;
sections++;
} else {
sections /= 16;
}
boolean flipTailOnY = false;
if (origin.y < 40+(lines.size()*10)) {
flipTailOnY = true;
}
boolean flipTailOnX = true;
int tail = 1;
if (origin.x > 200) {
tail = sections - 2;
flipTailOnX = !flipTailOnX;
}
float x = origin.x-((tail+1)*16);
if (!flipTailOnX) {
x -= 16;
}
float y = origin.y;
if (!flipTailOnY) {
y -= 26 + (lines.size()*10);
} else {
y += 6;
}
for (int i=0; i<sections; i++) {
if (i == tail && i == 0) {
if (flipTailOnY) {
GraphicsManager.getSpeechBubble(5).getFlippedCopy(false, true).draw(x, y-2);
for (int h=0; h<lines.size(); h++) {
GraphicsManager.getSpeechBubble(3).getFlippedCopy(true, false).draw(x, y+8+(h*10));
}
GraphicsManager.getSpeechBubble(1).getFlippedCopy(true, false).draw(x, y+8+(lines.size()*10));
} else {
GraphicsManager.getSpeechBubble(1).getFlippedCopy(true, true).draw(x, y-2);
for (int h=0; h<lines.size(); h++) {
GraphicsManager.getSpeechBubble(3).getFlippedCopy(true, false).draw(x, y+8+(h*10));
}
GraphicsManager.getSpeechBubble(5).draw(x, y+8+(lines.size()*10));
}
} else if (i == tail && i == sections-1) {
if (flipTailOnY) {
GraphicsManager.getSpeechBubble(5).getFlippedCopy(true, true).draw(x+(16*i), y-2);
for (int h=0; h<lines.size(); h++) {
GraphicsManager.getSpeechBubble(3).draw(x+(16*i), y+8+(h*10));
}
GraphicsManager.getSpeechBubble(1).draw(x+(16*i), y+8+(lines.size()*10));
} else {
GraphicsManager.getSpeechBubble(1).getFlippedCopy(false, true).draw(x+(16*i), y-2);
for (int h=0; h<lines.size(); h++) {
GraphicsManager.getSpeechBubble(3).draw(x+(16*i), y+8+(h*10));
}
GraphicsManager.getSpeechBubble(5).getFlippedCopy(true, false).draw(x+(16*i), y+8+(lines.size()*10));
}
} else if (i == tail) {
if (flipTailOnY) {
GraphicsManager.getSpeechBubble(0).getFlippedCopy(flipTailOnX, true).draw(x+(16*i), y-2);
for (int h=0; h<lines.size(); h++) {
GraphicsManager.getSpeechBubble(4).draw(x+(16*i), y+8+(h*10));
}
GraphicsManager.getSpeechBubble(2).draw(x+(16*i), y+8+(lines.size()*10));
} else {
GraphicsManager.getSpeechBubble(2).getFlippedCopy(false, true).draw(x+(16*i), y-2);
for (int h=0; h<lines.size(); h++) {
GraphicsManager.getSpeechBubble(4).draw(x+(16*i), y+8+(h*10));
}
GraphicsManager.getSpeechBubble(0).getFlippedCopy(flipTailOnX, false).draw(x+(16*i), y+8+(lines.size()*10));
}
} else if (i == 0) {
GraphicsManager.getSpeechBubble(1).getFlippedCopy(true, true).draw(x, y-2);
for (int h=0; h<lines.size(); h++) {
GraphicsManager.getSpeechBubble(3).getFlippedCopy(true, false).draw(x, y+8+(h*10));
}
GraphicsManager.getSpeechBubble(1).getFlippedCopy(true, false).draw(x, y+8+(lines.size()*10));
} else if (i == sections-1) {
GraphicsManager.getSpeechBubble(1).getFlippedCopy(false, true).draw(x+(16*i), y-2);
for (int h=0; h<lines.size(); h++) {
GraphicsManager.getSpeechBubble(3).draw(x+(16*i), y+8+(h*10));
}
GraphicsManager.getSpeechBubble(1).draw(x+(16*i), y+8+(lines.size()*10));
} else {
GraphicsManager.getSpeechBubble(2).getFlippedCopy(false, true).draw(x+(16*i), y-2);
for (int h=0; h<lines.size(); h++) {
GraphicsManager.getSpeechBubble(4).draw(x+(16*i), y+8+(h*10));
}
GraphicsManager.getSpeechBubble(2).draw(x+(16*i), y+8+(lines.size()*10));
}
}
g.setColor(Color.black);
for (int i=0; i<lines.size(); i++) {
g.drawString(lines.get(i), x+3, y+8+(i*10));
}
g.setColor(Color.white);
}
}