package ru.vagrant_ai.questionmarkgame.obj.add;
import java.util.Random;
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.Elements;
import ru.vagrant_ai.questionmarkgame.obj.mob.Monster;
import ru.vagrant_ai.questionmarkgame.util.MonsterHandler;
import ru.vagrant_ai.questionmarkgame.util.Util;
import ru.vagrant_ai.questionmarkgame.util.list.ITEM;
import ru.vagrant_ai.questionmarkgame.util.list.MS;
public class Sniper {
private Image util_img, rifle, shoot;
private short x;
private short opt_firerate;
private final short shoot_moment = 155;
private boolean rotation = false;
public Sniper(int x)
{
this.x = (short)x;
util_img = Util.loadImage("particle/bush");
rifle = Util.loadImage("particle/bush_gun");
shoot = Util.loadImage("particle/bush_shoot");
opt_firerate = 0;
}
public void render()
{
util_img.getFlippedCopy(rotation, false).draw(x, GameplayState.ground_level-53);
rifle.getFlippedCopy(rotation, false).draw((rotation?x-35:x+25), GameplayState.ground_level-28);
if (opt_firerate > shoot_moment-Elements.extractLevel(ITEM.P_UPG_SNIPER)*30)
{
if (rotation)
{
Image shoot_;
shoot_ = shoot.getFlippedCopy(true, false);
shoot_.setAlpha(1-(opt_firerate-(shoot_moment-Elements.extractLevel(ITEM.P_UPG_SNIPER)*30))*0.18f);
shoot_.draw(x-53, GameplayState.ground_level-26);
}
else
{
shoot.setAlpha(1-(opt_firerate-(shoot_moment-Elements.extractLevel(ITEM.P_UPG_SNIPER)*30))*0.18f);
shoot.draw(x+76, GameplayState.ground_level-26);
}
}
}
public void update()
{
opt_firerate++;
if (opt_firerate%20 == 0)
{
chooseNearest(); //to flip or not to flip
}
if (opt_firerate == shoot_moment-Elements.extractLevel(ITEM.P_UPG_SNIPER)*30)
{
Monster mob = chooseNearest();
if (mob == null || mob.state == MS.DEAD || mob.state == MS.NULL)
opt_firerate--;
else
{
if (new Random().nextInt(50) > 30-Elements.extractLevel(ITEM.P_UPG_SNIPER)*8)
mob.onDamage(40+Elements.extractLevel(ITEM.P_UPG_SNIPER)*12, true, false);
else
mob.onDamage(120+Elements.extractLevel(ITEM.P_UPG_SNIPER)*30, true, true);
}
}
if (opt_firerate > shoot_moment+5-Elements.extractLevel(ITEM.P_UPG_SNIPER)*30)
{
opt_firerate = -5;
shoot.setAlpha(1);
}
}
private Monster chooseNearest()
{
short index = -1;
float min_x = 0;
short curr_x = (short) (x+util_img.getWidth()/2);
if (MonsterHandler.monster_array.size() == 0)
return null;
for (short i = 0; i < MonsterHandler.monster_array.size(); ++i)
{
float mob_x = MonsterHandler.monster_array.get(i).getX();
if (mob_x > curr_x)
mob_x = mob_x-curr_x;
else
mob_x = curr_x-mob_x;
if ((min_x == 0 || min_x > mob_x) && MonsterHandler.monster_array.get(i).getX() < Game.getAppX() && MonsterHandler.monster_array.get(i).getX() > 20)
{
min_x = mob_x;
index = i;
}
}
if (index == -1)
return null;
if (curr_x > MonsterHandler.monster_array.get(index).getX())
rotation = true;
else
rotation = false;
return MonsterHandler.monster_array.get(index);
}
}