package ru.vagrant_ai.questionmarkgame.obj.add;
import java.util.Random;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Rectangle;
import ru.vagrant_ai.questionmarkgame.main.Game;
import ru.vagrant_ai.questionmarkgame.main.GameplayState;
import ru.vagrant_ai.questionmarkgame.obj.Elements;
import ru.vagrant_ai.questionmarkgame.util.Particle;
import ru.vagrant_ai.questionmarkgame.util.Util;
import ru.vagrant_ai.questionmarkgame.util.list.IS;
import ru.vagrant_ai.questionmarkgame.util.list.ITEM;
import ru.vagrant_ai.questionmarkgame.util.list.PT;
public class BonusItem {
protected float arg;
protected ITEM id;
protected String name[] = {"",""};
protected Rectangle hitbox;
protected short x;
protected short y;
public IS state = IS.FALLING;
protected Image util_icon;
private Image util_img_box, util_img_chute;
private byte box_x_offset_delay = 8, box_x_offset_current = 0, box_x_offset_current_side = 1;
private Image[] util_img_chute_off = new Image[4];
private byte chute_delay = 10, chute_state = 0; //chute state = [0..3]
private boolean chute_reverse = new Random().nextBoolean();
final short gr_lv = GameplayState.ground_level+12;
private boolean upgrade;
int util_activating = 0;
public BonusItem(float arg, boolean upgrade)
{
this.arg = arg;
this.upgrade = upgrade;
x = (short) (new Random().nextInt(Game.getAppX()-60)+30);
y = -120;
util_img_box = Util.loadImage("particle/box"+(upgrade?"_u":""));
util_img_chute = Util.loadImage("particle/parachute.png");
util_img_chute_off[0] = Util.loadImage("particle/parachute_off_1.png");
util_img_chute_off[1] = Util.loadImage("particle/parachute_off_2.png");
util_img_chute_off[2] = Util.loadImage("particle/parachute_off_3.png");
util_img_chute_off[3] = Util.loadImage("particle/parachute_off_4.png");
hitbox = new Rectangle(x,y,30,30);
}
public void update()
{
if (util_icon == null)
util_icon = setIcon();
if ((name[0] == "" || name[1] == "") && !upgrade)
name = setNames();
switch(state)
{
case FALLING:
if (state == IS.FALLING)
{
box_x_offset_delay--;
if (box_x_offset_delay < 1)
{
box_x_offset_delay = 8;
box_x_offset_current += (box_x_offset_current_side>0?1:-1);
if (box_x_offset_current < -5 || box_x_offset_current > 5)
box_x_offset_current_side *= -1;
}
}
if (y != gr_lv) y++;
if (y == gr_lv)
{
state = IS.WAITING;
if (chute_state == 0)
chute_state++;
}
case WAITING:
checkPerform();
checkIntersection();
break;
default: break;
}
}
public void render(Graphics g)
{
if (state == IS.WAITING || state == IS.FALLING)
{
int x = this.x+box_x_offset_current;
util_img_box.draw(x, y);
if (state == IS.WAITING && chute_state < 4)
{
Image img = util_img_chute_off[chute_state].getFlippedCopy((chute_reverse?true:false), false);
img.setAlpha(1-((chute_state+1)*0.15f)); //set alpha depending on chute state
img.draw(x-9, y-59);
chute_delay--;
if (chute_delay < 1)
{
chute_delay = 8;
chute_state++;
}
}
else if (state == IS.FALLING)
util_img_chute.getFlippedCopy((chute_reverse?true:false), false).draw(x-9, y-59);
util_icon.draw(x+6, y+6); //draw icon on it
hitbox.setBounds(x, y, 30, 30);
}
}
private void checkIntersection()
{
if (hitbox.intersects(GameplayState.player.hitbox))
{
if (upgrade)
id.upgrade();
else
id.perform(arg);
state = IS.NULL;
if (id != ITEM.I_HEAL_PERC)
{
if (upgrade)
{
//TODO String description
Elements.addLevel(id);
//GameplayState.dispenser.addToQueue(new String[]);
}
else
{
GameplayState.dispenser.addToQueue(id, name);
GameplayState.dispenser.used_items.add(id);
}
}
}
}
private void checkPerform()
{
if (!upgrade)
{
if (getID().check() && state != IS.NULL)
setID(ITEM.pickRand());
}
else
{
if (Elements.extractLevel(id) >= id.getMaxLevel() && state != IS.NULL)
setID(ITEM.pickRandUpg());
}
}
public byte getChuteState()
{
return chute_state;
}
public short getX()
{
return x;
}
public short getY()
{
return y;
}
public ITEM getID()
{
return id;
}
public void setID(ITEM id)
{
this.id = id;
util_icon = setIcon();
name = setNames();
Particle.addNew(PT.SMOKE, 3, x, y);
}
public Image setIcon()
{
return id.getIcon();
}
private String[] setNames()
{
return id.getNames(arg);
}
}