Android Open Source - CircleWorldGDX Tilemap Circle View






From Project

Back to project page CircleWorldGDX.

License

The source code is released under:

MIT License

If you think the Android project CircleWorldGDX listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.fdangelo.circleworld.universeview.tilemap;
/* www. j  a  v a  2s.com*/
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.fdangelo.circleworld.GameLogic;
import com.fdangelo.circleworld.universeengine.tilemap.ITilemapCircleListener;
import com.fdangelo.circleworld.universeengine.tilemap.TilemapCircle;

public class TilemapCircleView extends Actor implements ITilemapCircleListener {
  private int lastHeight;
  private int lastWidth;

  private TilemapCircleViewBackgroundRenderer backgroundRenderer;

  private TilemapCircleViewRenderer[] renderers;

  private TilemapCircle tilemapCircle;

  private Texture tilesetTexture;

  public final Texture getTilesetTexture() {
    return tilesetTexture;
  }

  public final TilemapCircle getTilemapCircle() {
    return tilemapCircle;
  }

  public TilemapCircleView() {
  }

  public final void init(final TilemapCircle tilemapCircle) {
    this.tilemapCircle = tilemapCircle;

    tilemapCircle.setListener(this);

    // Use the first texture in the atlas as the tileset texture
    final TextureAtlas atlas = GameLogic.getInstace().assetManager.get("atlas/tilemap.atlas", TextureAtlas.class);
    tilesetTexture = atlas.getTextures().iterator().next();

    updatePosition();

    initRenderers();

    updateMesh();
  }

  private final void updateMesh() {
    for (int i = 0; i < renderers.length; i++) {
      renderers[i].updateMesh();
    }
  }

  private final void initRenderers() {
    final int renderersAmount = MathUtils.clamp(MathUtils.ceil((tilemapCircle.getWidth() * tilemapCircle.getHeight()) / (32 * 32)), 1, 256);

    if (renderers == null || renderers.length != renderersAmount || lastWidth != tilemapCircle.getWidth() || lastHeight != tilemapCircle.getHeight()) {
      // Destroy existing renderers
      if (renderers != null) {
        for (int i = 0; i < renderers.length; i++) {
          renderers[i].dispose();
        }
      }

      if (backgroundRenderer != null) {
        backgroundRenderer.dispose();
      }

      // Add tile map circle renderers
      renderers = new TilemapCircleViewRenderer[renderersAmount];

      lastWidth = tilemapCircle.getWidth();
      lastHeight = tilemapCircle.getHeight();

      final int sizeX = MathUtils.ceil((float) tilemapCircle.getWidth() / (float) renderers.length);

      int fromX = 0;
      int toX = sizeX;

      for (int i = 0; i < renderers.length; i++) {
        renderers[i] = new TilemapCircleViewRenderer();
        renderers[i].init(this, fromX, toX);

        fromX += sizeX;
        toX += sizeX;

        if (toX >= tilemapCircle.getWidth()) {
          toX = tilemapCircle.getWidth();
        }

        if (fromX > toX) {
          fromX = toX;
        }
      }

      // Add background renderer
      backgroundRenderer = new TilemapCircleViewBackgroundRenderer();
    }

    backgroundRenderer.init(this);

    for (int i = 0; i < renderers.length; i++) {
      renderers[i].setDirty();
    }
  }

  private final TilemapCircleViewRenderer getRenderer(final int tileX, final int tileY) {
    if (renderers != null && renderers.length > 0) {
      final int sizeX = MathUtils.ceil((float) tilemapCircle.getWidth() / (float) renderers.length);

      final int rendererIndex = tileX / sizeX;

      return renderers[rendererIndex];
    }

    return null;
  }

  @Override
  public void onTilemapTileChanged(final int tileX, final int tileY) {
    getRenderer(tileX, tileY).setDirty();
  }

  @Override
  public void onTilemapParentChanged(final float deltaTime) {
    updatePosition();

    updateMesh();
  }

  static private Matrix4 tmpMatrix4 = new Matrix4();

  @Override
  public void draw(final Batch batch, final float parentAlpha) {
    batch.end();

    tmpMatrix4.set(batch.getProjectionMatrix());
    tmpMatrix4.mul(batch.getTransformMatrix());
    tmpMatrix4.translate(getX(), getY(), 0);

    backgroundRenderer.draw(tmpMatrix4);

    TilemapCircleViewRenderer.beginDraw(tmpMatrix4, tilesetTexture);
    for (int i = 0; i < renderers.length; i++) {
      renderers[i].draw();
    }
    TilemapCircleViewRenderer.endDraw();

    batch.begin();
  }

  private final void updatePosition() {
    setPosition(tilemapCircle.getPositionX(), tilemapCircle.getPositionY());
    setRotation(tilemapCircle.getRotation() * MathUtils.radiansToDegrees);
  }

  public void recycle() {
    tilemapCircle = null;
  }
}




Java Source Code List

.AssetsUpdater.java
com.fdangelo.circleworld.GameLogicState.java
com.fdangelo.circleworld.GameLogic.java
com.fdangelo.circleworld.MainActivity.java
com.fdangelo.circleworld.Main.java
com.fdangelo.circleworld.MyGdxGame.java
com.fdangelo.circleworld.RobovmLauncher.java
com.fdangelo.circleworld.client.GwtLauncher.java
com.fdangelo.circleworld.gui.AvatarEditControlScreen.java
com.fdangelo.circleworld.gui.AvatarMoveControlScreen.java
com.fdangelo.circleworld.gui.HudScreen.java
com.fdangelo.circleworld.gui.core.Gui.java
com.fdangelo.circleworld.gui.core.ScreenTable.java
com.fdangelo.circleworld.gui.core.Screen.java
com.fdangelo.circleworld.universeengine.IUniverseListener.java
com.fdangelo.circleworld.universeengine.ThingPosition.java
com.fdangelo.circleworld.universeengine.ThingType.java
com.fdangelo.circleworld.universeengine.Thing.java
com.fdangelo.circleworld.universeengine.UniverseFactory.java
com.fdangelo.circleworld.universeengine.UniverseGeneratorDefault.java
com.fdangelo.circleworld.universeengine.UniverseGenerator.java
com.fdangelo.circleworld.universeengine.Universe.java
com.fdangelo.circleworld.universeengine.objects.AvatarInput.java
com.fdangelo.circleworld.universeengine.objects.Avatar.java
com.fdangelo.circleworld.universeengine.objects.FollowParentParameters.java
com.fdangelo.circleworld.universeengine.objects.IUniverseObjectListener.java
com.fdangelo.circleworld.universeengine.objects.ShipInput.java
com.fdangelo.circleworld.universeengine.objects.Ship.java
com.fdangelo.circleworld.universeengine.objects.UniverseObject.java
com.fdangelo.circleworld.universeengine.tilemap.ITilemapCircleListener.java
com.fdangelo.circleworld.universeengine.tilemap.PlanetType.java
com.fdangelo.circleworld.universeengine.tilemap.PlanetTypes.java
com.fdangelo.circleworld.universeengine.tilemap.Planet.java
com.fdangelo.circleworld.universeengine.tilemap.TileDirection.java
com.fdangelo.circleworld.universeengine.tilemap.TileHitFlags.java
com.fdangelo.circleworld.universeengine.tilemap.TileHitInfo.java
com.fdangelo.circleworld.universeengine.tilemap.TileSubtype.java
com.fdangelo.circleworld.universeengine.tilemap.TileType.java
com.fdangelo.circleworld.universeengine.tilemap.TileTypes.java
com.fdangelo.circleworld.universeengine.tilemap.TilemapCircle.java
com.fdangelo.circleworld.universeengine.utils.DataPools.java
com.fdangelo.circleworld.universeengine.utils.PoolByte.java
com.fdangelo.circleworld.universeengine.utils.PoolColor.java
com.fdangelo.circleworld.universeengine.utils.PoolFloat.java
com.fdangelo.circleworld.universeengine.utils.PoolInt.java
com.fdangelo.circleworld.universeengine.utils.PoolVector2.java
com.fdangelo.circleworld.universeengine.utils.PoolVector3.java
com.fdangelo.circleworld.universeengine.utils.UEProfilerSample.java
com.fdangelo.circleworld.universeengine.utils.UEProfiler.java
com.fdangelo.circleworld.universeview.FollowCameraParameters.java
com.fdangelo.circleworld.universeview.UniverseViewCamera.java
com.fdangelo.circleworld.universeview.UniverseViewFactory.java
com.fdangelo.circleworld.universeview.UniverseView.java
com.fdangelo.circleworld.universeview.objects.AvatarInputEditTool.java
com.fdangelo.circleworld.universeview.objects.AvatarInputMode.java
com.fdangelo.circleworld.universeview.objects.AvatarViewInput.java
com.fdangelo.circleworld.universeview.objects.AvatarView.java
com.fdangelo.circleworld.universeview.objects.InputAreas.java
com.fdangelo.circleworld.universeview.objects.ShipInputMode.java
com.fdangelo.circleworld.universeview.objects.ShipViewInput.java
com.fdangelo.circleworld.universeview.objects.ShipView.java
com.fdangelo.circleworld.universeview.objects.UniverseObjectView.java
com.fdangelo.circleworld.universeview.tilemap.PlanetView.java
com.fdangelo.circleworld.universeview.tilemap.TilemapCircleViewBackgroundRenderer.java
com.fdangelo.circleworld.universeview.tilemap.TilemapCircleViewRenderer.java
com.fdangelo.circleworld.universeview.tilemap.TilemapCircleView.java
com.fdangelo.circleworld.utils.Mathf.java
com.fdangelo.circleworld.utils.Vector2I.java