Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: tom_mai78101 (posted 2014-04-10 04:04:31, viewed 146 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270  
package screen;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import main.Keys;
import main.MainComponent;
import main.NewInputHandler;
import resources.Art;
import abstracts.Tile;

public class Dialogue {
   //According to the width and height of the dialog shown in the original games.
   public static final int WIDTH = 160;
   public static final int HEIGHT = 48;
   //When counting from top of the dialog to top of the text.
   public static final int FIRST_LINE_SPACING_HEIGHT = 16;
   public static final int SECOND_LINE_SPACING_HEIGHT = 32;
   //When counting from left of the dialog to the right of the text's first letter.
   //This is also how far the arrow is from the right of the dialog.
   public static final int TEXT_SPACING_WIDTH = 8;
   
   //Background dialog color is 0xFFF8F8F8, or all RGB values are 248/255.
   //Font size
   public static final int FONT_SIZE = 24;
   
   //Dialogue max string length per line.
   public static final int MAX_STRING_LENGTH = 18;
   
   //Styles
   public static final int DIALOGUE_STYLE_SPEECH = 0xF1;
   
   //TODO: Optimize this, to make way for other types of dialogues to use.
   private String[] tokens;
   private int tokenPointer;
   private Map<Integer, Boolean> dialogs;
   private boolean next;
   private boolean nextTick;
   private byte arrowTickSpeed;
   private boolean showDialog;
   private int firstLineIterator;
   private int secondLineIterator;
   private int dialogKeyID;
   
   private Keys input;
   
   public Dialogue(Keys input) {
      this.input = input;
      this.arrowTickSpeed = 0;
      this.showDialog = false;
      this.next = false;
      this.nextTick = false;
      this.tokenPointer = 0;
      this.dialogs = new HashMap<Integer, Boolean>();
   }
   
   public void renderDialog(BaseScreen output, int x, int y, int centerWidth, int centerHeight) {
      if (this.showDialog) {
         output.blit(Art.dialogue_top_left, x * Tile.WIDTH, y * Tile.HEIGHT);
         for (int i = 0; i < centerWidth - 1; i++) {
            output.blit(Art.dialogue_top, ((x + 1) * Tile.WIDTH) + (i * Tile.WIDTH), y * Tile.HEIGHT);
         }
         output.blit(Art.dialogue_top_right, (x + centerWidth) * Tile.WIDTH, y * Tile.HEIGHT);
         
         for (int j = 0; j < centerHeight - 1; j++) {
            output.blit(Art.dialogue_left, x * Tile.WIDTH, ((y + 1) * Tile.HEIGHT) + j * Tile.HEIGHT);
            for (int i = 0; i < centerWidth - 1; i++) {
               output.blit(Art.dialogue_background, ((x + 1) * Tile.WIDTH) + (i * Tile.WIDTH), ((y + 1) * Tile.HEIGHT) + j * Tile.HEIGHT);
            }
            output.blit(Art.dialogue_right, (x + centerWidth) * Tile.WIDTH, ((y + 1) * Tile.HEIGHT) + j * Tile.HEIGHT);
         }
         
         output.blit(Art.dialogue_bottom_left, x * Tile.WIDTH, ((y + centerHeight) * Tile.HEIGHT));
         for (int i = 0; i < centerWidth - 1; i++) {
            output.blit(Art.dialogue_bottom, ((x + 1) * Tile.WIDTH) + (i * Tile.WIDTH), ((y + centerHeight) * Tile.HEIGHT));
         }
         output.blit(Art.dialogue_bottom_right, (x + centerWidth) * Tile.WIDTH, ((y + centerHeight) * Tile.HEIGHT));
         
         //The boolean "next" is for dialogues that are complete, and the "nextTick" is for displaying the arrow.
         if (this.next && this.nextTick) {
            output.blit(Art.dialogue_next, MainComponent.GAME_WIDTH - 16, MainComponent.GAME_HEIGHT - 8);
         }
      }
   }
   
   /**
    * Hides the dialog by making it disappear from the screen.
    * 
    * This method is used to hide the dialog box and allow back the player's inputs.
    * 
    * @return Nothing.
    * */
   public void hideDialog() {
      this.showDialog = false;
      this.tokenPointer = 0;
      this.setDialogCheckpoint();
      NewInputHandler.unlockInputs();
   }
   
   public void renderText(Graphics g) {
      g.setColor(Color.black);
      g.setFont(Art.font.deriveFont(Font.PLAIN, 24f));
      try {
         g.drawString(this.tokens[this.tokenPointer].substring(0, this.firstLineIterator), Dialogue.getDialogueTextStartingX(), Dialogue.getDialogueTextStartingY());
         g.drawString(this.tokens[this.tokenPointer + 1].substring(0, secondLineIterator), Dialogue.getDialogueTextStartingX(), Dialogue.getDialogueTextSecondLineStartingY());
      }
      catch (Exception e) {
         //Ignore. Silently catch the any sorts of exception, and just let the game flow on.
      }
   }
   
   public void tick() {
      if (this.next) {
         if (input.Z.isPressedDown || input.Z.isTappedDown
               || input.X.isTappedDown || input.X.isPressedDown
               || input.SLASH.isTappedDown || input.SLASH.isPressedDown
               || input.PERIOD.isTappedDown || input.PERIOD.isPressedDown) {
            this.next = false;
            boolean result1 = false, result2 = false;
            try {
               this.tokens[this.tokenPointer].length();
            }
            catch (ArrayIndexOutOfBoundsException e) {
               result1 = true;
            }
            try {
               this.tokens[this.tokenPointer + 1].length();
            }
            catch (ArrayIndexOutOfBoundsException e) {
               result2 = true;
            }
            if (result1 || result2)
               this.hideDialog();
            else {
               this.tokenPointer += 2;
               this.firstLineIterator = this.secondLineIterator = 0;
            }
         }
         this.arrowTickSpeed--;
         if (this.arrowTickSpeed < 0) {
            this.nextTick = !this.nextTick;
            this.arrowTickSpeed = 0x6;
         }
      }
      if (this.showDialog) {
         if (!NewInputHandler.inputsAreLocked())
            NewInputHandler.lockInputs();
         boolean result1 = false, result2 = false;
         try {
            if (this.firstLineIterator < this.tokens[this.tokenPointer].length())
               this.firstLineIterator++;
            else {
               this.tokens[this.tokenPointer + 1].length();
            }
         }
         catch (ArrayIndexOutOfBoundsException e) {
            result1 = true;
         }
         try {
            if (this.secondLineIterator >= this.tokens[this.tokenPointer + 1].length()) {
               this.next = true;
            }
            else {
               if (this.secondLineIterator < this.tokens[this.tokenPointer + 1].length() && this.firstLineIterator >= this.tokens[this.tokenPointer].length())
                  this.secondLineIterator++;
            }
         }
         catch (ArrayIndexOutOfBoundsException e) {
            result2 = true;
         }
         if (result1 && result2 && this.tokenPointer + 1 >= this.tokens.length)
            this.next = true;
      }
      else {
         this.firstLineIterator = this.secondLineIterator = 0;
         if (NewInputHandler.inputsAreLocked())
            NewInputHandler.unlockInputs();
      }
   }
   
   public void createText(String str, int key) {
      this.tokens = toLines(str);
      if (!this.dialogs.isEmpty()) {
         if (!this.getDialogueCheckpoint(this.dialogKeyID))
            this.showDialog = true;
      }
      else {
         this.setDialogKeyID(key);
      }
   }
   
   public void setDialogKeyID(int value) {
      this.dialogKeyID = value;
      this.dialogs.put(value, false);
   }
   
   public void setDialogCheckpoint() {
      this.dialogs.put(this.dialogKeyID, true);
   }
   
   public boolean isDialogCheckpointSet(int key) {
      if (this.dialogs.isEmpty())
         return false;
      return this.dialogs.get(key);
   }
   
   //-------------------------  STATIC FINAL METHODS ONLY -------------------------------
   
   public static final int getDialogueX() {
      return 0;
   }
   
   public static final int getDialogueY() {
      return (MainComponent.GAME_HEIGHT - Dialogue.HEIGHT);
   }
   
   public static final int getDialogueTextStartingX() {
      return Dialogue.TEXT_SPACING_WIDTH * MainComponent.GAME_SCALE;
   }
   
   public static final int getDialogueTextStartingY() {
      return (Dialogue.getDialogueY() * MainComponent.GAME_SCALE) + Dialogue.FIRST_LINE_SPACING_HEIGHT * MainComponent.GAME_SCALE + Dialogue.FONT_SIZE;
   }
   
   public static final int getDialogueTextSecondLineStartingY() {
      return (Dialogue.getDialogueY() * MainComponent.GAME_SCALE) + Dialogue.SECOND_LINE_SPACING_HEIGHT * MainComponent.GAME_SCALE + Dialogue.FONT_SIZE;
   }
   
   public boolean isDisplayingDialogue() {
      return this.showDialog;
   }
   
   public boolean getDialogueCheckpoint(int key) {
      return this.dialogs.get(key);
   }
   
   //----------------------- PRIVATE METHODS ONLY ------------------------------------------
   
   private String[] toLines(String all) {
      ArrayList<String> lines = new ArrayList<>();
      String[] words = all.split("\\s");
      String line = "";
      int length = 0;
      for (String w : words) {
         if (length + w.length() + 1 > MAX_STRING_LENGTH) {
            if (w.length() >= MAX_STRING_LENGTH) {
               line += w;
               lines.add(line);
               line = "";
               continue;
            }
            lines.add(line);
            line = "";
            length = 0;
         }
         if (length > 0) {
            line += " ";
            length += 1;
         }
         line += w;
         length += w.length();
      }
      if (line.length() > 0)
         lines.add(line);
      return lines.toArray(new String[lines.size()]);
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (14 views)
2014-09-21 02:42:18

BurntPizza (13 views)
2014-09-21 01:30:30

moogie (13 views)
2014-09-21 00:26:15

UprightPath (24 views)
2014-09-20 20:14:06

BurntPizza (27 views)
2014-09-19 03:14:18

Dwinin (40 views)
2014-09-12 09:08:26

Norakomi (70 views)
2014-09-10 13:57:51

TehJavaDev (96 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!