package ru.vagrant_ai.questionmarkgame.obj;
import java.util.Random;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import ru.vagrant_ai.questionmarkgame.main.GameplayState;
import ru.vagrant_ai.questionmarkgame.util.Particle;
import ru.vagrant_ai.questionmarkgame.util.Text;
import ru.vagrant_ai.questionmarkgame.util.Util;
import ru.vagrant_ai.questionmarkgame.util.list.PT;
public class BrokenObject {
float repair_percent = 0;
public float opt_repair_add = 0f; //[0..0.01f]
public float opt_repair_add_add = 0f; //[0..0.0012f]
private byte util_particle_delay = 0;
boolean is_repairing = false;
Image[][] image = new Image[4][4];
private byte current_slide = 0;
private byte util_delay = 0;
private byte current_level = 0;
private byte util_changing = 0;
private final short x = 375;
public BrokenObject()
{
for (int i = 0; i < 4; i++)
{
for (int x = 0; x < 4; x++)
{
image[i][x] = Util.loadImage("capsule/capsule_lvl_"+(i+1)+"000"+x+".png");
}
}
}
public void update(GameContainer gc)
{
checkIsRepairing(gc);
checkParticle();
}
private void checkParticle()
{
if (is_repairing)
{
util_particle_delay--;
if (util_particle_delay < 1)
{
util_particle_delay = 35;
if (new Random().nextBoolean())
Particle.addNew(PT.SMOKE, new Random().nextInt(2)+1, (float)new Random().nextInt(110)+x, (float)new Random().nextInt(65)+GameplayState.ground_level-33);
else
Particle.addNew(PT.WRENCH, new Random().nextInt(110)+x, new Random().nextInt(65)+GameplayState.ground_level-33);
}
}
else
util_particle_delay = 35;
}
private void checkIsRepairing(GameContainer gc)
{
Input input = gc.getInput();
if (input.isKeyDown(Input.KEY_S) || input.isKeyDown(Input.KEY_DOWN) && repair_percent < 100 && !is_repairing)
{
int pl_x = (int)GameplayState.player.pl_x;
if (pl_x >= x && pl_x <= x+160)
is_repairing = true;
}
if (is_repairing)
{
int pl_x = (int)GameplayState.player.pl_x;
if ((pl_x >= x && pl_x <= x+160) && !Util.MouseIsPressed(1) && repair_percent < 100)
{
repair_percent += 0.004f+opt_repair_add+opt_repair_add_add;
if (repair_percent > 100) repair_percent = 100;
}
else is_repairing = false;
}
}
public void render(Graphics g)
{
if (
(repaired() > 75 && current_level == 2
|| repaired() > 50 && current_level == 1
|| repaired() > 25 && current_level == 0)
&& util_changing == 0
)
util_changing = 10;
if (util_changing > 0)
{
for (int i = 0; i < 4; i++)
Particle.addNew(PT.SMOKE, 3, (float)new Random().nextInt(110)+x, (float)new Random().nextInt(65)+GameplayState.ground_level+33);
util_changing--;
if (util_changing == 5) current_level++;
}
util_delay--;
if (util_delay < 1)
{
util_delay = 10;
current_slide++;
if (current_slide > 3)
current_slide = 0;
}
image[current_level][current_slide].draw(x, GameplayState.ground_level+GameplayState.player.height-102);
Text.drawString(24, x+72, GameplayState.ground_level+GameplayState.player.height-115, String.format("%.2f", repair_percent)+"%", Color.red);
}
/**
* @return repair percent
*/
public float repaired()
{
return repair_percent;
}
}