Android Open Source - DolphinOES Pathfinding Handler






From Project

Back to project page DolphinOES.

License

The source code is released under:

Apache License

If you think the Android project DolphinOES 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

/*******************************************************************************
 * Copyright 2014 See AUTHORS file.//from w w w  .  j  av  a 2 s .  co  m
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.sidereal.dolphinoes.behaviors.pathfinding;

import java.util.HashMap;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.sidereal.dolphinoes.architecture.DolphinOES;
import com.sidereal.dolphinoes.architecture.GameBehavior;
import com.sidereal.dolphinoes.architecture.GameObject;

/** Handles the creation of {@link PathfindingMap} objects.
 * 
 * @author Claudiu Bele */
public class PathfindingHandler extends GameBehavior
{

  // region fields

  private static HashMap<String, PathfindingMap> maps;
  public PathfindingMap map;
  public static Sprite debugClosed;
  public static Sprite debugOpen;
  public static float nodeSizeDebug = 0.9f;
  public static float lineThickness = 10;
  public static BitmapFont font;

  // endregion fields

  // region constructors

  public PathfindingHandler(GameObject obj)
  {

    super(obj);

    GameBehavior.setDebugKeys(getClass(), new Integer[] {
        Keys.CONTROL_LEFT, Keys.X });
  }

  @Override
  protected void initialiseClass()
  {

    if (maps == null)
    {
      maps = new HashMap<String, PathfindingMap>();
    }

    if (!DolphinOES.debug.isEnabled())
      return;
    if (font == null)
    {
      font = DolphinOES.assets.get(
          DolphinOES.assets.frameworkAssetsFolder + "Blocks.fnt",
          BitmapFont.class);
      font.setColor(Color.BLACK);
      font.setScale(2);
    }
    DolphinOES.assets.load(
        DolphinOES.assets.frameworkAssetsFolder + "White.png", Texture.class);

    if (debugOpen == null)
    {
      debugOpen = new Sprite(DolphinOES.assets.get(
          DolphinOES.assets.frameworkAssetsFolder + "White.png",
          Texture.class));
      debugOpen.setColor(new Color(0, 1, 0, 0.5f));
    }

    if (debugClosed == null)
    {
      debugClosed = new Sprite(
          DolphinOES.assets.get(
              DolphinOES.assets.frameworkAssetsFolder + "White.png",
              Texture.class));
      debugClosed.setColor(new Color(1, 0, 0, 0.5f));
    }
  }

  // endregion constructors

  // region methods

  @Override
  public void update()
  {

  }

  public void addMap(String name, int width, int height)
  {

    map = new PathfindingMap(width, height);
    maps.put(name, map);
  }

  @Override
  public void updateDebug()
  {

    if (map != null)
    {
      for (int i = 0; i < map.nodesX; i++)
      {
        for (int j = 0; j < map.nodesY; j++)
        {

          float nodeX = map.getNodeX(i);
          float nodeY = map.getNodeY(j);
          font.draw(object.gameBatch.spriteBatch, i + " , " + j,
              (int) nodeX - font.getBounds(i + " , " + j).width
                  / 2f,
              (int) nodeY + font.getBounds(i + " , " + j).height
                  / 2f);

          if (map.nodes[i][j].access[0] == false)
          {

            debugClosed.setBounds(nodeX - map.getNodeSize().x / 2
                + 1, nodeY
                - (map.getNodeSize().y * nodeSizeDebug) / 2,
                lineThickness, map.getNodeSize().y
                    * nodeSizeDebug);
            debugClosed.draw(object.gameBatch.spriteBatch);
          } else
          {
            debugOpen.setBounds(
                nodeX - map.getNodeSize().x / 2 + 1, nodeY
                    - (map.getNodeSize().y * nodeSizeDebug)
                    / 2, lineThickness, map.getNodeSize().y
                    * nodeSizeDebug);
            debugOpen.draw(object.gameBatch.spriteBatch);
          }

          if (map.nodes[i][j].access[1] == false)
          {

            debugClosed.setBounds(nodeX + map.getNodeSize().x / 2
                - lineThickness - 1, nodeY
                - (map.getNodeSize().y * nodeSizeDebug) / 2,
                lineThickness, map.getNodeSize().y
                    * nodeSizeDebug);
            debugClosed.draw(object.gameBatch.spriteBatch);
          } else
          {
            debugOpen.setBounds(nodeX + map.getNodeSize().x / 2
                - lineThickness - 1, nodeY
                - (map.getNodeSize().y * nodeSizeDebug) / 2,
                lineThickness, map.getNodeSize().y
                    * nodeSizeDebug);
            debugOpen.draw(object.gameBatch.spriteBatch);
          }

          if (map.nodes[i][j].access[2] == false)
          {

            debugClosed.setBounds(nodeX
                - (map.getNodeSize().x * nodeSizeDebug) / 2,
                nodeY - map.getNodeSize().y / 2 + 1,
                map.getNodeSize().x * nodeSizeDebug,
                lineThickness);
            debugClosed.draw(object.gameBatch.spriteBatch);
          } else
          {
            debugOpen.setBounds(nodeX
                - (map.getNodeSize().x * nodeSizeDebug) / 2,
                nodeY - map.getNodeSize().y / 2 + 1,
                map.getNodeSize().x * nodeSizeDebug,
                lineThickness);
            debugOpen.draw(object.gameBatch.spriteBatch);
          }

          if (map.nodes[i][j].access[3] == false)
          {

            debugClosed.setBounds(nodeX
                - (map.getNodeSize().x * nodeSizeDebug) / 2,
                nodeY + map.getNodeSize().y / 2 - lineThickness
                    - 1, map.getNodeSize().x
                    * nodeSizeDebug, lineThickness);
            debugClosed.draw(object.gameBatch.spriteBatch);
          } else
          {
            debugOpen.setBounds(nodeX
                - (map.getNodeSize().x * nodeSizeDebug) / 2,
                nodeY + map.getNodeSize().y / 2 - lineThickness
                    - 1, map.getNodeSize().x
                    * nodeSizeDebug, lineThickness);
            debugOpen.draw(object.gameBatch.spriteBatch);
          }

        }
      }
    }

  }

  public static PathfindingMap getMap(String mapName)
  {

    if (!maps.containsKey(mapName))
      return null;

    return maps.get(mapName);
  }

  // endregion methods
}




Java Source Code List

com.sidereal.dolphinoes.architecture.AbstractEvent.java
com.sidereal.dolphinoes.architecture.DebugHandler.java
com.sidereal.dolphinoes.architecture.DolphinOES.java
com.sidereal.dolphinoes.architecture.GameBatch.java
com.sidereal.dolphinoes.architecture.GameBehavior.java
com.sidereal.dolphinoes.architecture.GameObject.java
com.sidereal.dolphinoes.architecture.GameScene.java
com.sidereal.dolphinoes.architecture.Module.java
com.sidereal.dolphinoes.architecture.core.Assets.java
com.sidereal.dolphinoes.architecture.core.Configurable.java
com.sidereal.dolphinoes.architecture.core.Debug.java
com.sidereal.dolphinoes.architecture.core.DolphinOESConfiguration.java
com.sidereal.dolphinoes.architecture.core.GameData.java
com.sidereal.dolphinoes.architecture.core.MouseMovedEvent.java
com.sidereal.dolphinoes.architecture.core.Time.java
com.sidereal.dolphinoes.architecture.core.assetload.AssetLoadHandler.java
com.sidereal.dolphinoes.architecture.core.assetload.AssetLoader.java
com.sidereal.dolphinoes.architecture.core.assetload.ClassFileHandleResolver.java
com.sidereal.dolphinoes.architecture.core.assetload.LoadingPercentage.java
com.sidereal.dolphinoes.architecture.core.input.ActionData.java
com.sidereal.dolphinoes.architecture.core.input.ActionEventWrapper.java
com.sidereal.dolphinoes.architecture.core.input.ActionEvent.java
com.sidereal.dolphinoes.architecture.core.input.Input.java
com.sidereal.dolphinoes.architecture.core.input.KeyTypedEvent.java
com.sidereal.dolphinoes.architecture.core.input.ScrollEvent.java
com.sidereal.dolphinoes.architecture.core.input.TouchData.java
com.sidereal.dolphinoes.architecture.core.input.TouchEventWrapper.java
com.sidereal.dolphinoes.architecture.core.input.TouchEvent.java
com.sidereal.dolphinoes.architecture.pos.Position.java
com.sidereal.dolphinoes.architecture.pos.Positions.java
com.sidereal.dolphinoes.backend.ConcreteGametester.java
com.sidereal.dolphinoes.backend.GameTester.java
com.sidereal.dolphinoes.backend.SceneGameTester.java
com.sidereal.dolphinoes.behaviors.audio.AudioListener.java
com.sidereal.dolphinoes.behaviors.audio.AudioPlayer.java
com.sidereal.dolphinoes.behaviors.events.EventTimer.java
com.sidereal.dolphinoes.behaviors.events.RecurringEvent.java
com.sidereal.dolphinoes.behaviors.input.Clickable.java
com.sidereal.dolphinoes.behaviors.particlesystem.ParticleEmitter.java
com.sidereal.dolphinoes.behaviors.particlesystem.ParticleHandler.java
com.sidereal.dolphinoes.behaviors.particlesystem.ParticleSpriteLayout.java
com.sidereal.dolphinoes.behaviors.particlesystem.ParticleSpriteObject.java
com.sidereal.dolphinoes.behaviors.pathfinding.PathfindingHandler.java
com.sidereal.dolphinoes.behaviors.pathfinding.PathfindingMap.java
com.sidereal.dolphinoes.behaviors.pathfinding.PathfindingNode.java
com.sidereal.dolphinoes.behaviors.pathfinding.PathfindingRoute.java
com.sidereal.dolphinoes.behaviors.renderer.DrawerBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.Drawer.java
com.sidereal.dolphinoes.behaviors.renderer.Renderer.java
com.sidereal.dolphinoes.behaviors.renderer.ninepatch.NinepatchBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.ninepatch.NinepatchDrawer.java
com.sidereal.dolphinoes.behaviors.renderer.scml.SCMLBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.scml.SCMLDrawer.java
com.sidereal.dolphinoes.behaviors.renderer.sprite.SpriteBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.sprite.SpriteDrawer.java
com.sidereal.dolphinoes.behaviors.renderer.spritesequence.SpriteSequenceBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.spritesequence.SpriteSequenceDrawer.java
com.sidereal.dolphinoes.behaviors.renderer.spritesequence.SpriteSequencePreference.java
com.sidereal.dolphinoes.behaviors.renderer.texture.TextureBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.texture.TextureDrawer.java
com.sidereal.dolphinoes.behaviors.renderer.tilemap.TileMapBuilder.java
com.sidereal.dolphinoes.behaviors.renderer.tilemap.TileMapDrawer.java
com.sidereal.dolphinoes.behaviors.triggers.Activatable.java
com.sidereal.dolphinoes.behaviors.triggers.Collider.java
com.sidereal.dolphinoes.behaviors.triggers.Hoverable.java
com.sidereal.dolphinoes.ui.MessageBubble.java
com.sidereal.dolphinoes.ui.TextBuilder.java
com.sidereal.dolphinoes.util.BooleanWrapper.java
com.sidereal.dolphinoes.util.DolphinOESException.java
com.sidereal.dolphinoes.util.FloatWrapper.java
com.sidereal.dolphinoes.util.IntWrapper.java
com.sidereal.dolphinoes.util.Utility.java