Package Hexel.generation.terrainGenerator.typeMaps

Source Code of Hexel.generation.terrainGenerator.typeMaps.DrawTypeMap

package Hexel.generation.terrainGenerator.typeMaps;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.WindowConstants;

import Hexel.generation.terrainGenerator.heightMaps.FractalHeightMapGenerator;
import Hexel.generation.terrainGenerator.heightMaps.FractalHexagonalTypeHeightMap;
import Hexel.generation.terrainGenerator.heightMaps.HeightMap;
import Hexel.generation.terrainGenerator.heightMaps.TypeFractalHeightMapGenerator;
import Hexel.math.HexGeometry;
import Hexel.math.Vector2d;
import Hexel.math.Vector2i;
import Hexel.math.Vector3i;
import Hexel.rendering.Camera;
import Hexel.rendering.Renderer;

public class DrawTypeMap {
  public static void main(String[] args) {
    new DrawTypeMap(new FractalHexagonalTypeHeightMap(0));
  }

  public DrawTypeMap(ContinentMapGenerator cmg) {
    testContinentMap(cmg);
  }

  public DrawTypeMap(TypeHeightMapGenerator tmg) {
    testTypeHeightMap(tmg);
  }

  public DrawTypeMap(FractalHexagonalTypeHeightMap tmg) {
    testFractalHexTypeHeightMap(tmg);
  }

  private int xshift = 0;
  private int yshift = 0;

  public void testContinentMap(final ContinentMapGenerator cmg) {
    draw(new Canvas() {
      @Override
      public void paint(Graphics page) {
        page.setColor(Color.white);
        page.fillRect(0, 0, this.getWidth(), this.getHeight());
        Vector2i tmp = new Vector2i();
        int s = 1;
        for (int x = 0; x < this.getWidth()/s; x++) {
          for (int y = 0; y < this.getHeight()/s; y++) {
            if (cmg.isLand(x-this.getWidth()/s/2+xshift, y-this.getHeight()/s/2+yshift)){
              page.setColor(new Color(0, 150, 0));
            }
            else {
              page.setColor(Color.BLUE);
            }
            page.fillRect(x*s, y*s, s, s);
          }
        }
      }
    });
  }

  public void testTypeHeightMap(final TypeHeightMapGenerator tmg) {
    draw(new Canvas() {
      @Override
      public void paint(Graphics page) {
        page.setColor(Color.white);
        page.fillRect(0, 0, this.getWidth(), this.getHeight());
        Vector2i tmp = new Vector2i();
        int s = 1;
        long t0 = System.currentTimeMillis();
        for (int x = 0; x < this.getWidth()/s; x++) {
          for (int y = 0; y < this.getHeight()/s; y++) {
            int h = tmg.getHeight(x, y, tmp);
            int wh = tmg.getWaterHeight(x, y, tmp);
            if (wh > h){
              page.setColor(Color.blue);
            }
            else {
              int c = 0+h/10;
              if (c < 0)
                c = 0;
              if (c > 255)
                c = 255;
              page.setColor(new Color(c, c, c));
            }
            page.fillRect(x*s, y*s, s, s);
          }
        }
//        System.out.println(System.currentTimeMillis() - t0);
      }
    });
  }

  public void testFractalHexTypeHeightMap(final FractalHexagonalTypeHeightMap tmg) {
    draw(new Canvas() {
      @Override
      public void paint(Graphics page) {
        page.setColor(Color.white);
        page.fillRect(0, 0, this.getWidth(), this.getHeight());
        Vector2i tmp = new Vector2i();
        int s = 1;
        long t0 = System.currentTimeMillis();
        HeightMap hm = null;
        int cx = this.getWidth()/2;
        int cy = this.getHeight()/2;
        int rx = this.getWidth()/s/2;
        int ry = this.getHeight()/s/2;
        for (int x = -rx; x < rx; x++) {
          for (int y = -ry; y < ry; y++) {
            int hx = x+xshift;
            int hy = y+yshift;
            if (hm == null || !hm.containsPoint(hx, hy)){
              hm = tmg.getHeightMapAtXY(hx, hy, tmp, null);
            }
            int h = tmg.getHeight(hx, hy, tmp, hm);
            int wh = tmg.getWaterHeight(hx, hy, tmp);
            if (wh > h){
              page.setColor(Color.blue);
            }
            else {
              int c = h;
              if (c < 0)
                c = 0;
              if (c > 255)
                c = 255;
              page.setColor(new Color(c, c, c));
            }
            page.fillRect(x*s+cx, -y*s+cy, s, s);
          }
        }

        Camera camera = Renderer.dbgCamera;
        if (camera != null){
          page.setColor(Color.yellow);
          page.fillRect(cx-2, cy-2, s*4, s*4);
        }
        System.out.println(System.currentTimeMillis() - t0);
      }
    });
  }


  private void repaint(Canvas canvas){
    Rectangle box = canvas.getBounds();
    Image img = new BufferedImage(box.width, box.height, BufferedImage.TYPE_INT_ARGB);
    Graphics page = img.getGraphics();
    canvas.paint(page);
    canvas.getGraphics().drawImage(img, 0, 0, null);
  }

  public void draw(final Canvas canvas) {
    JFrame f = new JFrame();
    final int W = 1000;
    final int H = 1000;
    f.add(canvas);
    f.setSize(W, H);
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.setVisible(true);
    canvas.createBufferStrategy(2);
    canvas.addKeyListener(new KeyListener(){
      @Override
      public void keyTyped(KeyEvent e) {
        if (e.getKeyChar() == 'a')
          xshift += 10;
        else if (e.getKeyChar() == 's')
          xshift -= 10;
        if (e.getKeyChar() == 'w')
          yshift += 10;
        else if (e.getKeyChar() == 'r')
          yshift -= 10;
        repaint(canvas);
      }
      @Override public void keyPressed(KeyEvent e) {}
      @Override public void keyReleased(KeyEvent e) {}
    });
    new Timer(1000, new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        Camera camera = Renderer.dbgCamera;
        if (camera != null){
          int x = (int) camera.getCameraX();
          int y = (int) camera.getCameraY();
          Vector2d tmp = new Vector2d();
          Vector3i p = new Vector3i();
          HexGeometry.cartesianToBlock(x, y, 0, tmp, p);
          xshift = p.x;
          yshift = p.y;
        }
        repaint(canvas);
      }
    }).start();
  }

}
TOP

Related Classes of Hexel.generation.terrainGenerator.typeMaps.DrawTypeMap

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.