package ru.vagrant_ai.questionmarkgame.obj;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import ru.vagrant_ai.questionmarkgame.main.Game;
import ru.vagrant_ai.questionmarkgame.main.GameplayState;
import ru.vagrant_ai.questionmarkgame.obj.add.BonusItem;
import ru.vagrant_ai.questionmarkgame.util.Text;
import ru.vagrant_ai.questionmarkgame.util.Util;
import ru.vagrant_ai.questionmarkgame.util.list.IS;
import ru.vagrant_ai.questionmarkgame.util.list.ITEM;
public class Dispenser {
private Image util_img_main = Util.loadImage("particle/item_gui");
private Image util_img = Util.loadImage("particle/item_gui_extra");
private List<ITEM> pre_item_list = new ArrayList<ITEM>(); //items that aren't spawned yet
public List<ITEM> pre_upg_list = new ArrayList<ITEM>(); //upgrades that aren't spawned yet
private byte item_list_delay = 15;
private List<BonusItem> item_array = new ArrayList<BonusItem>(); //items that currently in its box form
public List<BonusItem> upgrade_array = new ArrayList<BonusItem>(); //upgrades that currently in its box form
private List<String[]> showup_queue = new ArrayList<String[]>(); //banners with item description
private List<Image> showup_icon = new ArrayList<Image>(); //TODO banners with item icon
private List<String[]> showup_queue_upg = new ArrayList<String[]>(); //popup description for upgrades
public List<ITEM> used_items = new ArrayList<ITEM>(); //items already used
public void dispenseItem(ITEM id)
{
pre_item_list.add(id);
}
public void dispenseUpgrade(ITEM id)
{
pre_upg_list.add(id);
}
public void spawnItem(ITEM id)
{
float opt = 0;
BonusItem item = null;
switch(id)
{
/* PERMANENT */
/* GUN */
/* DELAY */
case P_GUN_DELAY_HIGH:
opt += 2;
case P_GUN_DELAY_MED:
opt += 2;
case P_GUN_DELAY_LOW:
opt += 2;
break;
/* RECOIL */
case P_GUN_SPREADING_HIGH:
opt += 0.3f;
case P_GUN_SPREADING_MED:
opt += 0.3f;
case P_GUN_SPREADING_LOW:
opt += 0.3f;
break;
/* POWER */
case P_GUN_POWER_HIGH:
opt += 5;
case P_GUN_POWER_MED:
opt += 5;
case P_GUN_POWER_LOW:
opt += 5;
break;
/* RELOAD */
case P_GUN_RELOAD_HIGH:
opt += 1.25;
case P_GUN_RELOAD_MED:
opt += 1.25;
case P_GUN_RELOAD_LOW:
opt += 1.25;
break;
/* UPGRADES */
/* JUMP UP */
case P_UPG_FREE_JUMP_LOW:
opt++;
case P_UPG_FREE_JUMP_MAX:
break;
/* HOLY LIGHT */
case P_UPG_STRONG_LIGHT:
opt++;
break;
case P_UPG_LIGHT:
opt++;
break;
case P_UPG_SMALL_LIGHT:
opt++;
break;
/* OTHERS */
/* REPAIR */
case P_OTHER_REPAIR_ADD_HIGH:
opt += 0.0009;
case P_OTHER_REPAIR_ADD_MED:
opt += 0.0009;
case P_OTHER_REPAIR_ADD_LOW:
opt += 0.0009;
break;
/* HP UP */
case P_OTHER_HPUP_HIGH:
opt += 20;
case P_OTHER_HPUP_MED:
opt += 20;
case P_OTHER_HPUP_LOW:
opt += 20;
break;
/* INSTANT */
/* HEAL */
case I_HEAL_PERC:
opt = 33;
break;
default: break;
}
item = new BonusItem(opt, false);
item.setID(id);
item_array.add(item);
}
public void spawnUpgrade(ITEM id)
{
BonusItem item = new BonusItem(0, true);
item.setID(id);
upgrade_array.add(item);
}
public void update()
{
if (item_list_delay > 0) //spawn delayed items
item_list_delay--;
if (item_list_delay < 1)
{
if (pre_item_list.size() > 0) //check for items first
{
spawnItem(pre_item_list.get(0));
pre_item_list.remove(0);
}
else if (pre_upg_list.size() > 0) //then for upgrades
{
spawnUpgrade(pre_upg_list.get(0));
pre_upg_list.remove(0);
}
item_list_delay = (byte) (new Random().nextInt(8)+15);
}
for (int i = 0; i < item_array.size(); ++i) //update items
item_array.get(i).update();
for (int i = 0; i < upgrade_array.size(); ++i) //update upgrades
upgrade_array.get(i).update();
}
public void render(Graphics g)
{
/*RENDER BOX*/
for (int i = 0; i < item_array.size(); ++i)
item_array.get(i).render(g);
for (int i = 0; i < item_array.size(); ++i)
{
if (item_array.get(i).state == IS.NULL)
item_array.remove(i);
}
for (int i = 0; i < upgrade_array.size(); ++i)
upgrade_array.get(i).render(g);
for (int i = 0; i < upgrade_array.size(); ++i)
{
if (upgrade_array.get(i).state == IS.NULL)
upgrade_array.remove(i);
}
/*RENDER PARTICLE*/
if (showup_queue.size() < 1)
offset_state = 0;
else
{
for (int i = 0; i < showup_queue.size() && i < 8; ++i)
{
if (i == 0)
{
if (!GameplayState.gamePaused())
update_leading();
render_leading(g);
}
else render_other(i,g);
}
}
}
private final byte text_offset_main = 75;
private final byte text_offset_secondary = 45;
private byte offset_state = 0;
short offset_main = 0;
short util_alpha = 0;
private void update_leading()
{
switch(offset_state)
{
case 0:
offset_main = 0;
offset_state++;
break;
case 1:
offset_main += 19;
if (offset_main >= 665)
{
offset_main = 0;
offset_state++;
}
break;
case 2:
offset_main++;
if (offset_main > 150)
{
offset_main = 0;
offset_state++;
}
break;
case 3:
offset_main += 2;
util_img_main.setAlpha(1f-(float)offset_main/100);
showup_icon.get(0).setAlpha(1f-(float)offset_main/100);
if (offset_main > 100)
{
offset_main = 0;
offset_state = 0;
util_img_main.setAlpha(1);
showup_queue.remove(0);
showup_icon.remove(0);
}
break;
}
}
private void render_leading(Graphics g)
{
String[] str = null;
if (offset_state > 0)
str = showup_queue.get(0);
switch(offset_state)
{
case 1:
util_img_main.draw(815-offset_main, Game.getAppY()-90);
showup_icon.get(0).draw(Text.extractLength(""+str[0], 60)-717+offset_main, Game.getAppY()-text_offset_main-8);
Text.drawString(60, Text.extractLength(""+str[0], 60)-670+offset_main, Game.getAppY()-text_offset_main, ""+str[0], Color.red);
Text.drawString(45, Text.extractLength(""+str[1], 45)-670+offset_main, Game.getAppY()-text_offset_secondary, ""+str[1], Color.red);
break;
case 2:
util_img_main.draw(150, Game.getAppY()-90);
showup_icon.get(0).draw(Text.extractLength(""+str[0], 60)-52, Game.getAppY()-text_offset_main-8);
Text.drawString(60, Text.extractLength(""+str[0], 60)-5, Game.getAppY()-text_offset_main, ""+str[0], Color.red);
Text.drawString(45, Text.extractLength(""+str[1], 45)-5, Game.getAppY()-text_offset_secondary, ""+str[1], Color.red);
break;
case 3:
util_img_main.draw(150, Game.getAppY()-90);
showup_icon.get(0).draw(Text.extractLength(""+str[0], 60)-52, Game.getAppY()-text_offset_main-8);
Text.drawString(60, Text.extractLength(""+str[0], 60)-5, Game.getAppY()-text_offset_main, ""+str[0], new Color(255,0,0,255/100*(100-offset_main)));
Text.drawString(45, Text.extractLength(""+str[1], 45)-5, Game.getAppY()-text_offset_secondary, ""+str[1], new Color(255,0,0,255/100*(100-offset_main)));
default:
break;
}
}
private void render_other(int i, Graphics g)
{
String[] str = showup_queue.get(i);
byte offset;
switch(i)
{
case 2:
offset = -3;
break;
case 3:
offset = -1;
break;
case 4:
offset = 4;
break;
case 5:
offset = 12;
break;
case 6:
offset = 26;
break;
case 7:
offset = 42;
break;
default:
offset = 0;
break;
}
float offset_x = util_img.getWidth()*(1f-0.6f/8*i)/2;
Image img = util_img.getScaledCopy(1f-0.6f/8*i);
img.setAlpha(1f-(0.5f/8*i));
img.draw(400-offset_x, Game.getAppY()-94-i*38+offset);
Text.drawString(40-3*i, Text.extractLength(""+str[0], 40-3*i), Game.getAppY()-84-i*39+offset, ""+str[0], new Color(255, 0, 0, 255-102/8*i));
}
public void addToQueue(ITEM id, String[] str)
{
showup_queue.add(str);
Image img = id.getIcon();
img = img.getScaledCopy(2f);
img.setFilter(Image.FILTER_NEAREST);
showup_icon.add(img);
}
public void addToQueue(String[] str)
{
showup_queue_upg.add(str);
}
public void addToUsed(ITEM item)
{
used_items.add(item);
}
}