Package Hexel.generation.terrainGenerator.originalPlateGenerator.plate

Source Code of Hexel.generation.terrainGenerator.originalPlateGenerator.plate.PlateChunks

package Hexel.generation.terrainGenerator.originalPlateGenerator.plate;

import java.util.concurrent.ConcurrentHashMap;

import Hexel.math.Vector2i;

public class PlateChunks {
  private ConcurrentHashMap<Vector2i, PlateChunk> inMemoryPCs = new ConcurrentHashMap<Vector2i, PlateChunk>();
  private Object[] locks = new Object[100];

  PlateChunkGenerator pcg;

  public PlateChunks() {
    this.pcg = new PlateChunkGenerator();
    for (int i = 0; i < this.locks.length; i++) {
      this.locks[i] = new Object();
    }
  }

  public PlateChunk get(int x, int y) {
    return this.get(new Vector2i(x, y));
  }

  public PlateChunk get(Vector2i ppos) {
    if (hasPCInMemory(ppos)) {
      return this.inMemoryPCs.get(ppos);
    }
    maybeGenPC(ppos);
    return this.inMemoryPCs.get(ppos);
  }

  private void maybeGenPC(Vector2i ppos) {
    int hash = Math.abs(ppos.hashCode() % this.locks.length);
    synchronized (this.locks[hash]) {
      if (hasPCInMemory(ppos))
        return;

      PlateChunk pc = this.pcg.gen(ppos.x, ppos.y);
      this.inMemoryPCs.put(ppos, pc);
    }
  }

  private boolean hasPCInMemory(Vector2i ppos) {
    return this.inMemoryPCs.containsKey(ppos);
  }

  public void unloadPC(Vector2i ppos) {
    this.inMemoryPCs.remove(ppos);
  }

  public Plate getPlate(int x, int y) {
    int px = (int) Math.floor(x * 1.0 / PlateChunk.WIDTH);
    int py = (int) Math.floor(y * 1.0 / PlateChunk.HEIGHT);

    x -= px * PlateChunk.WIDTH;
    y -= py * PlateChunk.HEIGHT;

    return this.get(px, py).plates[x][y];
  }

}
TOP

Related Classes of Hexel.generation.terrainGenerator.originalPlateGenerator.plate.PlateChunks

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.