Package net.zurvanlabs.math

Source Code of net.zurvanlabs.math.Source

package net.zurvanlabs.math;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import net.zurvanlabs.math.configuration.InvalidConfigurationException;
import net.zurvanlabs.math.configuration.file.YamlConfiguration;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

public class Source extends BasicGame {
  public static int yamlGridX = 20;
  public static int yamlGridY = 16;
  public static int lastID = 0;
 
  private boolean bInitRender = true;
 
  private Input input;
 
  private ArrayList<GuiElement> guiElements = new ArrayList<GuiElement>();
  public int[][] iGridValues = new int[yamlGridX][yamlGridY];
 
  private static YamlConfiguration yamlElements;
  private static YamlConfiguration yamlSettings;

  public Source(String title) {
    super(title);
  }
 
  @Override
  public void init(GameContainer gc)
      throws SlickException {
    // Set all elements to -1
    for (int x = 0; x < yamlGridX; x++) {
      for (int y = 0; y < yamlGridY; y++) {
        iGridValues[x][y] = -1;
      }
    }
   
    input = gc.getInput();
   
    try {
      yamlElements.load(GuiElement.yamlGuiFilename);
     
      int count = yamlElements.getInt(GuiElement.yamlLastID);
     
      for (int i = 0; i < count; i++) {
        if (yamlElements.isSet(GuiElement.yamlPrefix + i)) {
          insertGuiElement(new GuiTextBox(yamlElements.getInt(GuiElement.yamlPrefix + i + ".x"), yamlElements.getInt(GuiElement.yamlPrefix + i + ".y"), "Hello world!"));
        }
      }
     
      yamlElements.save(GuiElement.yamlGuiFilename);
      yamlElements.load(GuiElement.yamlGuiFilename);
    } catch (Exception e) {
      System.out.println("Cannot load YML init");
    }
   
    /*// Insert GUI
    insertGuiElement(new GuiTextBox(0, 0, "Hello world!"));
    insertGuiElement(new GuiTextBox(4, 1, "Hello world!"));
    insertGuiElement(new GuiTextBox(3, 1, "Hello world!"));*/
   
    //yamlWorker.saveToFile();
   
    // Will run only once
    for (int i = 0; i < guiElements.size(); i++) {
      GuiElement ge = guiElements.get(i);
     
      ge.init(gc);
    }
  }

  @Override
  public void update(GameContainer gc, int d)
      throws SlickException {
    if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) == true) {
      insertGuiElement(new GuiTextBox(input.getMouseX() / GuiElement.yamlGridW, input.getMouseY() / GuiElement.yamlGridH, "Hello world!"));
    }
   
    // Update ticks
    for (int i = 0; i < guiElements.size(); i++) {
      GuiElement ge = guiElements.get(i);
     
      ge.update(gc, d);
    }
  }

  @Override
  public void render(GameContainer gc, Graphics g)
      throws SlickException {
    // Will run only once
    initRender(gc, g);
   
    // Render ticks
    for (int i = 0; i < guiElements.size(); i++) {
      GuiElement ge = guiElements.get(i);
     
      ge.render(gc, g);
    }
  }
 
  private boolean insertGuiElement(GuiElement guiElement) {
    int ge_x = guiElement.x;
    int ge_y = guiElement.y;
   
    int ge_w = guiElement.w;
    int ge_h = guiElement.h;
   
    for (int _w = 0; _w < ge_w; _w++) {
      for (int _h = 0; _h < ge_h; _h++) {
        try {
          if (iGridValues[_w + ge_x][_h + ge_y] != -1) {
            System.out.println("Grid Element [" + (_w + ge_x) + "][" + (_h + ge_y) + "] is occupied by " + iGridValues[_w + ge_x][_h + ge_y]);
            return false;
          }
        } catch (ArrayIndexOutOfBoundsException e) {
          System.out.println("Grid Element [" + (_w + ge_x) + "][" + (_h + ge_y) + "] is on wrong position");
         
          return false;
        }
      }
    }
   
    for (int _w = 0; _w < ge_w; _w++) {
      for (int _h = 0; _h < ge_h; _h++) {
        iGridValues[_w + ge_x][_h + ge_y] = guiElement.id;
      }
    }
   
    guiElements.add(guiElement);
   
    try {
      yamlElements.set(GuiElement.yamlLastID, lastID);
      yamlElements.set(GuiElement.yamlPrefix + guiElement.id + ".x", guiElement.x);
      yamlElements.set(GuiElement.yamlPrefix + guiElement.id + ".y", guiElement.y);
      yamlElements.set(GuiElement.yamlPrefix + guiElement.id + ".w", guiElement.w);
      yamlElements.set(GuiElement.yamlPrefix + guiElement.id + ".h", guiElement.h);
      yamlElements.save(GuiElement.yamlGuiFilename);
      yamlElements.load(GuiElement.yamlGuiFilename);
    } catch (Exception e) {
      System.out.println("Cannot save YML");
    }
   
    return true;
  }
 
  private void initRender(GameContainer gc, Graphics g)
      throws SlickException {
    if (bInitRender == true) {
      bInitRender = false;
     
      g.setBackground(Color.white);
    }
  }
 
  public static void main(String[] args)
      throws SlickException, FileNotFoundException, IOException, InvalidConfigurationException {
    yamlSettings = new YamlConfiguration();
   
    try {
      yamlSettings.load("settings.yml");
    } catch (FileNotFoundException e) {
      yamlSettings.set("settings.yamlGridX", 20);
      yamlSettings.set("settings.yamlGridY", 20);
      yamlSettings.set("settings.yamlGridW", 20);
      yamlSettings.set("settings.yamlGridH", 20);
      yamlSettings.set("settings.title", "Title");
      yamlSettings.set("settings.gui.yamlGuiFilename", "grid.yml");
      yamlSettings.set("settings.gui.yamlElementPrefix", "e_");
      yamlSettings.set("settings.gui.yamlLastID", "lid");
     
      yamlSettings.save("settings.yml");
      yamlSettings.load("settings.yml");
    }
   
    Source.yamlGridX = yamlSettings.getInt("settings.yamlGridX");
    Source.yamlGridY = yamlSettings.getInt("settings.yamlGridY");
    GuiElement.yamlGridW = yamlSettings.getInt("settings.yamlGridW");
    GuiElement.yamlGridH = yamlSettings.getInt("settings.yamlGridH");
    GuiElement.yamlGuiFilename = yamlSettings.getString("settings.gui.yamlGuiFilename");
    GuiElement.yamlPrefix = yamlSettings.getString("settings.gui.yamlElementPrefix");
    GuiElement.yamlLastID = yamlSettings.getString("settings.gui.yamlLastID");
    String title = yamlSettings.getString("settings.title");
   
    yamlElements = new YamlConfiguration();
   
    try {
      yamlElements.load(GuiElement.yamlGuiFilename);
    } catch (FileNotFoundException e) {
      yamlElements.set(GuiElement.yamlLastID, 0);
      yamlElements.save(GuiElement.yamlGuiFilename);
      yamlElements.load(GuiElement.yamlGuiFilename);
    }
   
    AppGameContainer agc = new AppGameContainer(new Source(title));
    agc.setVSync(false);
    agc.setDisplayMode(yamlGridX * GuiElement.yamlGridW, yamlGridY * GuiElement.yamlGridH, false);
    agc.setUpdateOnlyWhenVisible(false);
    agc.setShowFPS(false);
    agc.setResizable(false);
    agc.start();
  }
}
TOP

Related Classes of net.zurvanlabs.math.Source

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.