Package ru.vagrant_ai.questionmarkgame.util

Source Code of ru.vagrant_ai.questionmarkgame.util.MonsterWaveController

package ru.vagrant_ai.questionmarkgame.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;

import ru.vagrant_ai.questionmarkgame.main.Game;
import ru.vagrant_ai.questionmarkgame.util.list.ML;
import ru.vagrant_ai.questionmarkgame.util.list.WS;

public class MonsterWaveController {

  //private WS current_state = WS.START;
  private WS current_state = WS.NULL; //comment-uncomment one of these to turn on or off wave engine
  private short current_wave = 0;
 
  public void update()
  {
    switch(current_state)
    {
    case START: update_start(); break;
    case BREAK: update_break(); break;
    case ANNOUNCE: update_announce(); break;
    case ATTACK: update_attack(); break;
    case FINISH: update_finish(); break;
    case NULL:
    default: return;
    }
   
  }
 
  public void render(Graphics g)
  {
    switch(current_state)
    {
    case START: render_start(g); break;
    case BREAK: render_break(g); break;
    case ANNOUNCE: render_announce(g); break;
    case ATTACK: render_attack(g); break;
    case FINISH: render_finish(g); break;
    case NULL:
    default: return;
    }
  }

/*
* START state
*/
 
  short util_start_timer;
  short util_text_coord;
 
  private void update_start()
  {
    util_start_timer++;
    if (util_start_timer > 880) enterState(WS.ANNOUNCE);
  }

  private void render_start(Graphics g)
  {
    if (util_start_timer < 330)
    {
      if (util_start_timer < 20) util_text_coord = -560;
      else if (util_start_timer >= 100 && util_start_timer < 250) util_text_coord++;
      else util_text_coord += 8;
        Text.drawString(75, util_text_coord-10, Game.getAppY()/4, "Repair by pressing DOWN near", Color.yellow);
        Text.drawString(75, util_text_coord+80, Game.getAppY()/4+35, "the object to win", Color.yellow);
    }
    else if (util_start_timer >= 330 && util_start_timer < 550)
    {
      if (util_start_timer < 340) util_text_coord = -310;
      else if (util_start_timer >= 408 && util_start_timer < 490) util_text_coord++;
      else util_text_coord += 8;
        Text.drawString(75, util_text_coord, Game.getAppY()/4, "Upgrade yourself", Color.yellow);
    }
    else
    {
      if (util_start_timer < 560) util_text_coord = -400;
      else if (util_start_timer >= 635 && util_start_timer < 745) util_text_coord++;
      else util_text_coord += 8;
        Text.drawString(75, util_text_coord, Game.getAppY()/4, "Defend from anomalies", Color.yellow);
    }
  }
 
/*
* BREAK state
*/
  short util_break_alpha;
  short util_alpha_timer;
 
  private void update_break()
  {
    if (util_break_alpha >= 255)
    {
      util_alpha_timer++;
      if (util_alpha_timer > Game.fps*15) enterState(WS.ANNOUNCE);
    }
    else if (util_alpha_timer == 0)
      util_break_alpha += 5;
  }
 
  private void render_break(Graphics g)
  {
    if (util_break_alpha < 255) Text.drawString(150, Text.extractLength("Wave complete!", 150), 40, "Wave complete!", new Color(255,0,0,util_break_alpha));
  }

/*
* ANNOUNCE state
*/
 
  short util_announce_timer;
  short util_alpha;
  boolean util_alpha_dir;
 
  private void update_announce()
  {
    util_announce_timer++;
    if (util_alpha_dir)
    {
      util_alpha += 5;
      if (util_alpha >= 255) util_alpha_dir = false;
    }
    else
    {
      util_alpha -= 5;
      if (util_alpha < 1) util_alpha_dir = true;
    }
    if (util_announce_timer > 408) enterState(WS.ATTACK);
  }
 
  private void render_announce(Graphics g)
  {
    Color col = new Color(255,0,0,util_alpha);
    Text.drawString(200, Text.extractLength("!!INCOMING!!", 200), 40, "!!INCOMING!!", col);
    Text.drawString(104, Text.extractLength("PREPARE FOR WAVE "+current_wave, 104), 120, "PREPARE FOR WAVE "+current_wave, col);
  }

/*
* ATTACK state
*/
 
  boolean util_spawned;
  short util_time_passed;
  boolean util_overtime;
 
  private void update_attack()
  {
    if (!util_spawned)
    {
      int curr_difficulty = 20;
      curr_difficulty += current_wave*4;
      List<ML> mobs = Collections.unmodifiableList(Arrays.asList(ML.values()));
      ArrayList<ML> mob_to_spawn = new ArrayList<ML>();
      for (int i = 0; i < curr_difficulty;)
      {
        int rand = new Random().nextInt(mobs.size());
        mob_to_spawn.add(mobs.get(rand));
        i += mobs.get(rand).getDifficult();
      }
      for (int i = 0; i < mob_to_spawn.size(); ++i)
        MonsterHandler.spawnMob(mob_to_spawn.get(i));
      util_spawned = true;
    }
    util_time_passed++;
    if (!util_overtime && MonsterHandler.monstersActive() < 1)
    {
      enterState(WS.BREAK);
    }
    else if (util_overtime && util_time_passed > Game.fps*20)
    {
      enterState(WS.ANNOUNCE);
    }
    else if (!util_overtime && util_time_passed > Game.fps*60)
    {
      util_overtime = true;
      util_time_passed = 0;
    }
  }
 
  private void render_attack(Graphics g)
  {
   
  }
 
/*
* FINISH state 
*/

  private void update_finish()
  {

  }
 
  private void render_finish(Graphics g)
  {
   
  }

/*
* UTIL
*/
 
  public void enterState(WS state)
  {
    current_state = state;
    switch(state)
    {
    case START: util_start_timer = 0; util_text_coord = 0; break;
    case BREAK: util_break_alpha = 0; util_alpha_timer = 0; break;
    case ANNOUNCE: util_announce_timer = 0; current_wave++; util_alpha_dir = false; util_alpha = 0break;
    case ATTACK: util_spawned = false; util_time_passed = 0; util_overtime = false; break;
    case FINISH: break;
    default:
    }
  }
 
  public int currentWave()
  {
    return current_wave;
  }
 
  public WS getState()
  {
    return current_state;
  }
 
}
TOP

Related Classes of ru.vagrant_ai.questionmarkgame.util.MonsterWaveController

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.