Package ru.vagrant_ai.questionmarkgame.obj

Source Code of ru.vagrant_ai.questionmarkgame.obj.Elements

package ru.vagrant_ai.questionmarkgame.obj;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Circle;
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.add.Light;
import ru.vagrant_ai.questionmarkgame.obj.add.Money;
import ru.vagrant_ai.questionmarkgame.obj.add.Sniper;
import ru.vagrant_ai.questionmarkgame.obj.mob.Monster;
import ru.vagrant_ai.questionmarkgame.util.MonsterHandler;
import ru.vagrant_ai.questionmarkgame.util.Particle;
import ru.vagrant_ai.questionmarkgame.util.list.ITEM;
import ru.vagrant_ai.questionmarkgame.util.list.MS;
import ru.vagrant_ai.questionmarkgame.util.list.PT;
import ru.vagrant_ai.questionmarkgame.util.list.SCENE;
import ru.vagrant_ai.questionmarkgame.util.list.WS;

/*
* Light bonus
* Kamikaze bonus
* Explosion
*/

public class Elements {
 
  public static HashMap<ITEM, Byte> upgrade_array = new HashMap<ITEM, Byte>(); //upgrade levels
  private static List<Money>   money_array = new ArrayList<Money>();
  private static List<Light>   light_array = new ArrayList<Light>();
  public static List<Sniper>  sniper_array = new ArrayList<Sniper>();
  public static byte sniper_quantity;
  public static byte[] light_quantity = new byte[3];
  public static boolean kamikaze = false;
  public static boolean knife = false;
  private static byte util_knife = 40;
 
  public Elements()
  {
    money_array.clear();
    light_array.clear();
    sniper_array.clear();
    light_quantity[0] = 0;
    light_quantity[1] = 0;
    light_quantity[2] = 0;
    sniper_quantity = 0;
  }

  public void update()
  {
    for (int i = 0; i < light_array.size(); ++i) //light update
    {
      Light element = light_array.get(i);
      element.update();
    }
    for (int i = 0; i < money_array.size(); ++i) //money update
    {
      Money element = money_array.get(i);
      element.update();
    }
    for (int i = 0; i < money_array.size(); ++i) //money remove
    {
      Money element = money_array.get(i);
      if (element.state == 0)
        money_array.remove(i);
    }
    for (int i = 0; i < sniper_array.size(); ++i) //sniper update
    {
      Sniper element = sniper_array.get(i);
      element.update();
    }
    if (knife)
      knifeUpdate();
    if (GameplayState.monster_controller.getState() == WS.BREAK)
    {
      Input inp = Game.app.getInput();
      if ((inp.isKeyPressed(Input.KEY_DOWN) || inp.isKeyPressed(Input.KEY_S)) && GameplayState.player.pl_center.getX() > 590 &&  GameplayState.player.pl_center.getX() < 640)
        GameplayState.scene_handler.activateScene(SCENE.SHOP);
    }
  }
 
  public void render(Graphics g)
  {
    for (int i = 0; i < light_array.size(); ++i)
    {
      Light element = light_array.get(i);
      element.render(g);
    }
    for (int i = 0; i < money_array.size(); ++i)
    {
      Money element = money_array.get(i);
      element.render();
    }
    //shop render in Background
    //sniper render in Background   
  }
 
  public static void addMoney(int x, int y, byte quantity)
  {
    Money money = new Money(x, y, quantity);
    money_array.add(money);
    return;
  }
 
  public static void addLight(int x, float width, byte damage, float speed, int type)
  {
    light_quantity[type-1]++;
    Light light = new Light(x, type);
    light_array.add(light);
    return;
  }
 
  public static void addLight(float width, byte damage, float speed, int type)
  {
    int rand = 400;
    while(rand > 300 && rand < 500)
      rand = new Random().nextInt(Game.getAppX())+(int)width/2+5-(int)width-10;
    addLight(rand, width, damage, speed, type);
    return;
  }
 
  public static void addLight(int type)
  {
    switch(type)
    {
    case 1:
      Elements.addLight(55, (byte) 4, 30, type);
      break;
    case 2:
      Elements.addLight(95, (byte) 7, 25, type);
      break;
    case 3:
      Elements.addLight(155, (byte) 10, 20, type);
      break;
    default:
      break;
    }
  }
 
  public static void addExplosion(int x, int y)
  {
    Particle.addNew(PT.EXPLOSION, x, y);
    addKnockback(x, y, 1, 1, x, (byte) 50);
  }
 
  public static void addSniper()
  {
    int rand = 351;
    while (rand > 330 && rand < 540)
      rand = new Random().nextInt(Game.getAppX()-85)+20;
    Sniper sniper = new Sniper(rand);
    sniper_array.add(sniper);
    sniper_quantity++;
  }
 
  private void knifeUpdate()
  {
    if (!GameplayState.gamePaused())
      util_knife--;
    if (util_knife < 1)
    {
      util_knife = 0;
      Rectangle rect = new Rectangle(GameplayState.player.pl_center.getX()+(GameplayState.player.util_facing?20:-55), GameplayState.player.pl_center.getY()-30, 35, 60);
      Graphics g = Game.app.getGraphics();
      g.setLineWidth(1);
      g.setColor(Color.red);
      g.draw(rect);
      for (int i = 0; i < MonsterHandler.monster_array.size(); ++i)
      {
        Monster mob = MonsterHandler.monster_array.get(i);
        if ((rect.intersects(mob.hitbox) || rect.contains(mob.hitbox)) && mob.state != MS.DEAD && mob.state != MS.NULL)
        {
          util_knife = (byte) (40-Elements.extractLevel(ITEM.P_UPG_KNIFE)*5);
          Particle.addNew(PT.KNIFE, (int) GameplayState.player.pl_center.getX(), (int) GameplayState.player.pl_center.getY());
          mob.onDamage(25+Elements.extractLevel(ITEM.P_UPG_KNIFE)*8, true, false);
          break;
        }
      }
    }
  }
 
  public static byte extractLevel(ITEM item)
  {
    if (upgrade_array.get(item) == null)
      return 0;
    return upgrade_array.get(item);
  }
 
  public static void addLevel(ITEM item)
  {
    byte i = 0;
    if (extractLevel(item) < item.getMaxLevel())
    {
      if (upgrade_array.get(item) != null)
      {
        i = upgrade_array.get(item);
        upgrade_array.remove(item);
      }
      i++;
      upgrade_array.put(item, i);
    }
         
  }
 
  /*
   * Thorn
   * Kamikaze
   * Poison cloud
   */
  public static void addKnockback(float x, float y, int power, int hp, int center_x, byte radius)
  {
    List<Monster> list = MonsterHandler.monster_array;
    List<Monster> dmg_list = new ArrayList<Monster>();
    boolean source = (GameplayState.player.pl_center.getX() == x && GameplayState.player.pl_center.getY() == y?true:false);
    x = (int) x;
    y = (int) y;
    byte pc_lvl = 0;
    boolean pc = false;
    byte th_lvl = 0;
    boolean th = false;
    byte km_lvl = 0;
    if (source)
    {
      pc_lvl = Elements.extractLevel(ITEM.P_UPG_POISON_CLOUD);
      pc = GameplayState.player.util_poison_cloud;
      th_lvl = Elements.extractLevel(ITEM.P_UPG_THORNY);
      th = GameplayState.player.util_thorny;
      radius += (pc_lvl+th_lvl>2?(pc_lvl+th_lvl>4?20:10):0);
    }
    else
    {
      km_lvl = Elements.extractLevel(ITEM.P_UPG_KAMIKAZE);
      radius += km_lvl*4;
    }
    for (int i = 0; i < list.size(); ++i)
    {
      MS mob_state = list.get(i).state;
      if (mob_state != MS.DEAD && mob_state != MS.NULL)
      {
        Rectangle mob_hitbox = list.get(i).hitbox; 
        for (float j = 0; j < radius; j += 3)
        {
          if (mob_hitbox.intersects(new Circle(x, y, j)) && mobNotDamaged(list.get(i), dmg_list))
          {
            float knockback = power; //calculate knockback
            knockback += (pc?2+pc_lvl:0);
            knockback += (th?4+th_lvl*0.8f:0);
            knockback += km_lvl*0.8f;
            knockback = knockback*(j/radius*0.4f+0.8f);
            int damage = 0; //calculate damage
            damage += (pc?5+pc_lvl:0);
            damage += (th?30+th_lvl*5:0);
            damage += (!source?26+km_lvl*4:0);
            damage = (int) ((float)damage*(j/radius*0.4f+0.8f));
            list.get(i).onSpecialDamage(knockback, damage, center_x);
            if ((pc && source) || (Elements.extractLevel(ITEM.P_GUN_POISON_BULLET)+Elements.extractLevel(ITEM.P_UPG_POISON_CLOUD) >= 2 && !source)) //check for poison
              list.get(i).addPoison((byte) (12+pc_lvl*2), (byte) (hp*5+pc_lvl)); //TODO check all items and upgrades
            dmg_list.add(list.get(i));
          }
        }
      }
    }
  }
 
    private static boolean mobNotDamaged(Monster mob, List<Monster> list)
    {
      if (list.size() == 0)
          return true;
      for (int i = 0; i < list.size(); ++i)
        if (list.get(i) == mob)
          return false;
      return true;
    }
 
}
TOP

Related Classes of ru.vagrant_ai.questionmarkgame.obj.Elements

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.