Package ru.vagrant_ai.questionmarkgame.obj.proj

Source Code of ru.vagrant_ai.questionmarkgame.obj.proj.ShotgunProjectile

package ru.vagrant_ai.questionmarkgame.obj.proj;

import java.util.Random;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Line;
import org.newdawn.slick.geom.Point;
import org.newdawn.slick.geom.Vector2f;

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 ShotgunProjectile extends Projectile {

  private Line bullet;
  private Color col, col2;
  private Graphics g;
  private short alpha = 0;
  private byte pellet_quantity = 6;
  private float x;
  private float y;
  private float[] curr_x = new float[pellet_quantity];
  private float[] curr_y = new float[pellet_quantity];
  private float n_x;
  private float n_y;
  private int length;
  private Vector2f vector_pl;
  private Vector2f vector_mouse;
 
  public ShotgunProjectile(Point gun_point)
  {
    this.x = (int) gun_point.getX();
    this.y = (int) gun_point.getY();
    bullet_iter = 12;
   
    /* Searching for player-mouse line angle and setting bullet fly vector */
    for (int i = 0; i < pellet_quantity; i++)
    {
      length = new Random().nextInt(22)+96;
      curr_x[i] = this.x;
      curr_y[i] = this.y;
      if (curr_x[i] == 0 && curr_y[i] == 0) //game freeze prevention
      {
        curr_x[i] = 1;
        curr_y[i] = 1;
      }
      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_spreading;
      float rand = new Random().nextFloat()*22-11; //[-11..11]
      vector_mouse.setTheta(vector_mouse.getTheta()+rand/7*(recoil+2f));
      n_x = vector_mouse.x;
      n_y = vector_mouse.y;
      g = Game.app.getGraphics();
      bullet = new Line(this.x, this.y, curr_x[i], curr_y[i]);
      while(true)
      {
        curr_x[i] += n_x; curr_y[i] += n_y;
        drawLine(i);
        if (checkCollision() || bullet.length() > length)
          break;
      }
    }
    render();
  }
 
  private void drawLine(int pellet)
  {
    bullet.set(x, y, curr_x[pellet], curr_y[pellet]);
  }
 
  public void update()
  {
    bullet_iter--;
    if (bullet_iter > 0)
    {
      alpha = (short) (255/12*bullet_iter)
    }
  }
 
  public void render()
  {
    int col_offset = new Random().nextInt(20)-10;
    col = new Color(207+col_offset, 207+col_offset, 207+col_offset, alpha);
    col2 = new Color(207+col_offset, 207+col_offset, 207+col_offset, (alpha>100?alpha-100:0));
    g.setLineWidth(1);
    g.setColor(col);
    for (int i = 0; i < pellet_quantity; i++)
      g.drawGradientLine(x, y, col, curr_x[i], curr_y[i], col2);
  }
 
  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 (monster.state != MS.NULL && monster.state != MS.DEAD)
      {
        if (bullet.intersects(monster.hitbox_crit))
        {
          monster.onDamage((int) ((float)GameplayState.player.gun.opt_power/6*2.8f), true);     
          return true;
        }
        else if (bullet.intersects(monster.hitbox))
          {
            monster.onDamage((int)((float)(GameplayState.player.gun.opt_power/6)*1.6f), false);     
            return true;
          }
      }
    }
    return false;
  }
}
TOP

Related Classes of ru.vagrant_ai.questionmarkgame.obj.proj.ShotgunProjectile

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.