package ru.vagrant_ai.questionmarkgame.obj;
import java.util.Random;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Line;
import org.newdawn.slick.geom.Point;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.tests.xml.Entity;
import ru.vagrant_ai.questionmarkgame.main.Game;
import ru.vagrant_ai.questionmarkgame.main.GameplayState;
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.MS;
public class Projectile extends Entity {
Line bullet;
Color col;
Graphics g;
int alpha = 0;
float x;
float y;
float curr_x;
float curr_y;
float n_x;
float n_y;
int rule;
Vector2f vector_pl;
Vector2f vector_mouse;
Vector2f vector_normal;
int bullet_iter_max = 12;
int bullet_iter = bullet_iter_max;
public Projectile(int rule, Point proj_start) throws SlickException
{
switch(rule)
{
case 1: //player's bullet
this.x = (int) proj_start.getX();
this.y = (int) proj_start.getY();
curr_x = this.x;
curr_y = this.y;
if (curr_x == 0 && curr_y == 0) //game freeze prevention
{
curr_x = 1;
curr_y = 1;
}
this.rule = rule;
/* Searching for player-mouse line angle and setting bullet fly vector */
vector_pl = new Vector2f(this.x, this.y);
vector_mouse = new Vector2f(Util.MouseX(), Util.MouseY());
vector_mouse.sub(vector_pl);
vector_mouse.normalise();
float recoil = GameplayState.player.gun.opt_recoil;
vector_mouse.setTheta(vector_mouse.getTheta()+new Random().nextFloat()*5-recoil/2);
n_x = vector_mouse.x;
n_y = vector_mouse.y;
g = Game.app.getGraphics();
bullet = new Line(this.x+GameplayState.player.gun.gun_x, this.y+GameplayState.player.gun.gun_y, this.x, this.y);
while(true)
{
curr_x += n_x; curr_y += n_y;
drawLine();
if (checkCollision())
break;
}
draw();
break;
}
}
private void drawLine()
{
switch(rule)
{
case 1: bullet.set(x, y, curr_x, curr_y); break;
}
}
public void draw() throws SlickException
{
switch(rule)
{
case 1:
col = new Color(255, 255, 170, alpha);
g.setLineWidth(1);
g.setColor(col);
g.drawLine(this.x+GameplayState.player.gun.gun_x, this.y+GameplayState.player.gun.gun_y, curr_x, curr_y);
break;
}
}
public void update()
{
switch(rule)
{
case 1:
bullet_iter--;
if (bullet_iter > 0)
{
alpha = 255/bullet_iter_max*bullet_iter;
}
break;
}
}
private boolean checkCollision()
{
if (bullet.intersects(GameplayState.border)) return true;
for (int i = 0; i < MonsterHandler.Monster_array.size(); ++i)
{
Monster monster = MonsterHandler.Monster_array.get(i);
if (bullet.intersects(monster.hitbox) && monster.state != MS.NULL && monster.state != MS.DEAD)
{
monster.onDamage(GameplayState.player.gun.opt_power);
return true;
}
}
return false;
}
/**
* Checks, if projectile is out of the screen, or hits the ground or the mob
* @return true if out of bounds
*/
boolean checkActive()
{
return (bullet_iter != 0);
}
}