Package catchemrpg.threads

Source Code of catchemrpg.threads.BattleThread

package catchemrpg.threads;

import catchemrpg.base.*;
import catchemrpg.drawers.EventLog;
import catchemrpg.enums.WindowState;
import catchemrpg.functions.MouseInputMap;
import catchemrpg.gameobjects.Battle;
import catchemrpg.gameobjects.GameImage;
import catchemrpg.gameobjects.Monster;
import catchemrpg.gameobjects.MonsterTypeMatchup;
import java.awt.Color;
import java.awt.Graphics2D;

/**
*
* @author Toby Pinder (Gigitrix)
*/
public class BattleThread
  {

    /**
     * Initialise the battleThread
     */
    public void initialise()
      {
        //nothing to initialise!
      }
    /**
     * BOX 1X Position
     */
    public static final int BOX1X = 30;
    /**
     * BOX 1Y Position
     */
    public static final int BOX1Y = 320;
    /**
     * BOX 1 Width
     */
    public static final int BOX1W = 100;
    /**
     * BOX 1 Height
     */
    public static final int BOX1H = 25;
    /**
     * BOX 1 Color
     */
    public static final Color BOX1C = Color.RED;
    /**
     * HP Image Icon.
     */
    public static GameImage HPImage = new GameImage("/catchemrpg/assets/graphics/healthicon.png");
    /**
     * Attack Image Icon
     */
    public static GameImage attImage = new GameImage("/catchemrpg/assets/graphics/attackicon.png");
    /**
     * Defensive Image Icon
     */
    public static GameImage defImage = new GameImage("/catchemrpg/assets/graphics/defenceicon.png");

    /**
     * Render the battle thread, as well as the game logic.
     * @param window The graphics to draw on.
     * @return painted Graphics2D.
     */
    public Graphics2D renderToScreen(Graphics2D window)
      {

        if (window != null)
          {
            window.setColor(Color.BLACK);
            window.fillRect(0, 0, 800, 600);
            if (Main.temp_useNewBattle)
              {
                //Ghost-protoype the areas.
                window.setColor(Color.RED);
                window.drawRect(0,0,425, 75); //Enemy Leader
                window.setColor(Color.YELLOW);
                window.drawRect(425, 0, 75, 75); //Enemy Leader Image
                window.setColor(Color.GREEN);
                window.fillRect(0,75,500, 15); //HP bar
                window.setColor(Color.CYAN);
                window.fillRect(0,90,500, 10); //MP bar


                for(int i=0;i<5;i++)
                {
                    for(int j=0;j<2;j++)
                    {
                    //card
                    window.setColor(Color.BLUE);
                    window.drawRect(0+100*i, 125+200*j, 100, 150); //outline
                    window.setColor(Color.CYAN);
                    window.drawRect(0+100*i, 125+200*j, 100, 25); //name pane
                    window.setColor(Color.MAGENTA);
                    window.drawRect(0+100*i, 150+200*j, 75, 100); //image area.
                    window.setColor(Color.GREEN);
                    window.fillRect(75+100*i, 150+200*j, 15, 125); //HP Meter
                    window.setColor(Color.CYAN);
                    window.fillRect(90+100*i, 150+200*j, 10, 125); //HP Meter
                    window.setColor(Color.ORANGE);
                    window.drawRect(0+100*i, 250+200*j, 25, 25); //Icon 1
                    window.setColor(Color.ORANGE);
                    window.drawRect(25+100*i, 250+200*j, 25, 25); //Icon 2
                    window.setColor(Color.ORANGE);
                    window.drawRect(50+100*i, 250+200*j, 25, 25); //Icon 3
                    }
                }
                window.setColor(Color.GREEN);
                window.fillRect(0,500,500, 15); //HP bar
                window.setColor(Color.CYAN);
                window.fillRect(0,515,500, 10); //MP bar
                window.setColor(Color.RED);
                window.drawRect(0,525,425, 75); //Enemy Leader
                window.setColor(Color.YELLOW);
                window.drawRect(425, 525, 75, 75); //Enemy Leader Image
                //side pane
                window.setColor(Color.WHITE);
                window.drawRect(500, 0, 150, 200); //big picture
                try
             {
           
            GameImage monsterImage = new GameImage(BaseVars.aiTeam.yourMonsters[0].imagePath);
            monsterImage.draw(window, 500, 0);
          } catch (IllegalArgumentException ex)
          {
            System.out.println("ERROR LOADING IMAGE @" + BaseVars.aiTeam.yourMonsters[0].imagePath);
          }
                for(int i=0;i<3;i++)
                {
                window.setColor(new Color(0x999999));
                window.drawRect(500, 200+(50*i), 150, 25); //stat
                window.setColor(new Color(0x666666));
                window.drawRect(500, 225+(50*i), 150, 25); //stat
                }
                window.setColor(new Color(0x999999));
                window.drawRect(500, 350, 150, 25); //stat
               
                window.setColor(Color.YELLOW);
                window.drawRect(650, 0, 75, 75); //atk slot 1
                window.setColor(Color.ORANGE);
                window.drawRect(650, 75, 75, 75); //atk slot 2
                window.setColor(Color.RED);
                window.drawRect(650, 150, 75, 75); //atk slot 3
                window.setColor(Color.YELLOW);
                window.drawRect(650, 225, 75, 75); //atk slot 4
                window.setColor(Color.ORANGE);
                window.drawRect(650, 300, 75, 75); //atk slot 5
                window.setColor(Color.RED);
                window.drawRect(650, 375, 75, 75); //atk slot 6
                window.setColor(Color.BLUE);
                window.drawRect(575, 375, 75, 75); //atk scroll down
                window.setColor(new Color(0xFF00FF));
                window.drawRect(500, 375, 75, 75); //atk scroll up
                window.setColor(new Color(0x333333));
                for(int i=0;i<6;i++)
                {
                window.drawRect(500, 450+(25*i),225,25);
                }
              }
            else
              {
                drawMonsterCard(window, BaseVars.aiTeam.yourMonsters[BaseVars.aiTeam.yourMonsterID], 30, 35);
                drawMonsterCard(window, BaseVars.aiTeam.enemyMonsters[BaseVars.aiTeam.enemyMonsterID], 300, 35);

                getLastClick();
                window.setColor(BOX1C);
                window.draw3DRect(BOX1X, BOX1Y, BOX1W, BOX1H, true);
                window.drawString("Attack", BOX1X + 32, BOX1Y + BOX1H - 8); //scalars are highly guesswork requiring presicion, trial, improvement etc... Sucks doesn't it.

                //draw chatwindow
                window.setColor(Color.WHITE);
                window.drawLine(0, 499, 800, 499);
                window = EventLog.draw(window);

                if (BaseVars.aiTeam.turn == 2)
                  {
                    doEnemyTurn();
                  }
              }


          }
        return window;
      }

    /**
     * Get the last click, and parse it.
     */
    private static void getLastClick()
      {

        //if we have a new click....
        if (MouseInputMap.dealtWith == false)
          {
            if (MouseInputMap.clickX > BOX1X && MouseInputMap.clickY >= BOX1Y && MouseInputMap.clickY <= BOX1Y + BOX1H && MouseInputMap.clickX <= BOX1X + BOX1W)
              {
                //call a neat-o battle function.  
                buttonBattlePressed();
              }
            else
              {
              }
            MouseInputMap.dealtWith = true;
          }
      }

    /**
     * The attack button has been pressed.
     *
     */
    private static void buttonBattlePressed()
      {
        //check current state of the player.
        if (BaseVars.aiTeam.turn == 1)
          {
            //Attack! call fight function and put the injured creatures back in as normal creatures.
            Monster[] monsters = Battle.YourTurn(BaseVars.aiTeam.yourMonsters[BaseVars.aiTeam.yourMonsterID], BaseVars.aiTeam.enemyMonsters[BaseVars.aiTeam.enemyMonsterID]);
            BaseVars.aiTeam.yourMonsters[BaseVars.aiTeam.yourMonsterID] = monsters[0];
            BaseVars.aiTeam.enemyMonsters[BaseVars.aiTeam.enemyMonsterID] = monsters[1];
            //make sure everything is still alive
            checkCreatureDeath();

            monsters = null;

          }
        else if (BaseVars.aiTeam.turn == 2)
          {
            System.err.println("USER CANNOT MOVE: CPU THINKING/ATTACKING.");
          }
        else
          {
            System.err.println("Malformed battle turn identfier.");
            System.err.println("Current Value: " + BaseVars.aiTeam.turn);
          }
      }

    /**
     * Run the enemy's basic attack
     */
    private static void doEnemyTurn()
      {
        //basic attack for now...

        //if monster is capable of attack...
        if (BaseVars.aiTeam.enemyMonsters[BaseVars.aiTeam.enemyMonsterID].HP > 0)
          {
            Monster[] monsters = Battle.TheirTurn(BaseVars.aiTeam.enemyMonsters[BaseVars.aiTeam.enemyMonsterID], BaseVars.aiTeam.yourMonsters[BaseVars.aiTeam.yourMonsterID]);
            BaseVars.aiTeam.enemyMonsters[BaseVars.aiTeam.enemyMonsterID] = monsters[0];
            BaseVars.aiTeam.yourMonsters[BaseVars.aiTeam.yourMonsterID] = monsters[1];
          }

        checkCreatureDeath();
        BaseVars.aiTeam.turn = 1;
      }

    /**
     * Iterate through the Creature list, choosing one that isn't dead. If none is
     * found, returns -1.
     * @param monsterList An array of Monsters you want to sort though and find
     * an alive one
     * @return the integer index of the monster to next fight with.
     */
    public static int creatureReplacement(Monster[] monsterList)
      {
        int selectedCreature = -1;
        //for each creature in the list.
        for (int i = 0; i < monsterList.length; i++)
          {
            //if this creature has more than zero HP
            try
              {
                if (monsterList[i].HP > 0)
                  {
                    selectedCreature = i;
                    break;
                  }
              } catch (NullPointerException npe)
              {
                break;
              }
          }
        return selectedCreature;
      }

    /**
     * Checks if anything is dead, and if it is, checks if the user is out of useable creatures. Then, if not, swaps in a useable creature. If the user/enemy has lost, it ends the battle.
     *
     */
    public static void checkCreatureDeath()
      {

        if (BaseVars.aiTeam.yourMonsters[BaseVars.aiTeam.yourMonsterID].HP <= 0)
          {
            BaseVars.aiTeam.yourMonsterID = creatureReplacement(BaseVars.aiTeam.yourMonsters);
            if (BaseVars.aiTeam.yourMonsterID == -1)
              {
                //TODO: So java doesn't go phycho...
                BaseVars.aiTeam.yourMonsterID = 0;
                //you lost, as there are no more creatures for you to use. You will
                //already be notified of game over, so there is no need for a message
                //in the log here
                BaseVars.state = WindowState.gameover;
              }
          }
        //not else if for future proofing
        if (BaseVars.aiTeam.enemyMonsters[BaseVars.aiTeam.enemyMonsterID].HP <= 0)
          {
            //THEY DIED
            BaseVars.aiTeam.enemyMonsterID = creatureReplacement(BaseVars.aiTeam.enemyMonsters);
            if (BaseVars.aiTeam.enemyMonsterID == -1)
              {
                //TODO: So java doesn't go phycho...
                BaseVars.aiTeam.enemyMonsterID = 0;
                //they lost, as there are no more creatures for them to use.
                EventLog.append(BaseVars.lang.ui$wonBattle);
                BaseVars.state = WindowState.map;
              }

          }
      }

    /**
     * Draws a monster stat card to the specified window, using the specified monster,
     * at specified location
     *
     * @param window the graphics to draw the card on
     * @param monster the monster's data for the card
     * @param x the xPosition to draw the card at
     * @param y the yPosition to draw the card at
     * @return drawnon graphics
     */
    public Graphics2D drawMonsterCard(Graphics2D window, Monster monster, int x, int y)
      {
        //choose the colour of the card based on it's type.
        switch (monster.TYPE)
          {
            case MonsterTypeMatchup.TYPE_NOTYPE:
                window.setColor(MonsterTypeMatchup.TYPE_COLOR_NOTYPE);
                break;
            case MonsterTypeMatchup.TYPE_UNDEAD:
                window.setColor(MonsterTypeMatchup.TYPE_COLOR_UNDEAD);
                break;
            case MonsterTypeMatchup.TYPE_HOLY:
                window.setColor(MonsterTypeMatchup.TYPE_COLOR_HOLY);
                break;
            case MonsterTypeMatchup.TYPE_FIRE:
                window.setColor(MonsterTypeMatchup.TYPE_COLOR_FIRE);
                break;
            case MonsterTypeMatchup.TYPE_WIND:
                window.setColor(MonsterTypeMatchup.TYPE_COLOR_WIND);
                break;
            case MonsterTypeMatchup.TYPE_WATER:
                window.setColor(MonsterTypeMatchup.TYPE_COLOR_WATER);
                break;
            case MonsterTypeMatchup.TYPE_EARTH:
                window.setColor(MonsterTypeMatchup.TYPE_COLOR_EARTH);
                break;
          }

        window.fillRect(x, y, 180, 180);


        //mp. Checks to see if maxed out and uses a different color
        if (monster.MP == monster.MPmax)
          {
            window.setColor(Color.CYAN);
          }
        else
          {
            window.setColor(Color.BLUE);
          }
        window.fillRect(x + 5, y + 5, (int) Math.ceil(((double) monster.MP / (double) monster.MPmax) * 170), 15);



        //hp base
        window.setColor(Color.RED);
        window.fillRect(x + 5, y + 25, 170, 15);
        //hp
        window.setColor(Color.GREEN);
        window.fillRect(x + 5, y + 25, (int) Math.ceil(((double) monster.HP / (double) monster.HPmax) * 170), 15);

        //HP icon and number
        HPImage.draw(window, x + 128, y + 43);
        window.setColor(Color.WHITE);
        window.drawString("" + monster.HP, x + 145, y + 55);

        //name
        window.setColor(Color.WHITE);
        //POSSIBILITY: EMPHASISE FONT::::    window.setFont(new Font("Cooper Black",0,10));
        window.drawString(monster.NAME, x + 15, y + 75);


        //TODO! MAJOR PERFORMANCE LEAK: 2 IO READS PER FRAME! CHANGE TO SPRITE!
        //Could draw these images to a static Monster1 and Monster2 then just copy in the new Images.
        //then ask for new images on faint/change
        try
          {
            GameImage monsterImage = new GameImage(monster.imagePath);
            monsterImage.draw(window, x + 40, y + 65);
          } catch (IllegalArgumentException ex)
          {
            System.out.println("ERROR LOADING IMAGE @" + monster.imagePath);
            window.setColor(Color.LIGHT_GRAY);
            window.fillRect(x + 40, y + 65, 100, 100);
          }


        attImage.draw(window, x + 5, y + 163);
        window.drawString("" + monster.att, x + 21, y + 175);
        defImage.draw(window, x + 40, y + 163);
        window.drawString("" + monster.def, x + 56, y + 175);


        //give back
        return window;
      }
  }
TOP

Related Classes of catchemrpg.threads.BattleThread

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.