Android Open Source - DolphinOES Pathfinding Node






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.//  ww  w.  j a va 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.Arrays;
import com.badlogic.gdx.math.Vector2;

/** A node in a {@link PathfindingMap}.
 * 
 * @author Claudiu Bele */
public class PathfindingNode
{

  // region fields

  public int x;
  public int y;
  @SuppressWarnings("unused")
  private PathfindingMap map;
  private Vector2 position;
  private Vector2 nodePosition;

  /** Whether or not if, for example, you can't access the left node, with
   * mirroraccess = true, the left node's access to this one will be false as
   * well */

  /** left, right, bottom, top access to keys */
  public boolean[] access;

  /** Connected nodes values to the right, left, top, bottom or the values on
   * the adjacent nodes to the left, right, bottom or top to the object */
  public boolean[] adjacent;

  /** Whether or not to handle adjancent nodes. */
  public boolean[] handleAdjacent;

  public static int LEFT_ACCESS = 0;
  public static int RIGHT_ACCESS = 1;
  public static int BOTTOM_ACCESS = 2;
  public static int TOP_ACCESS = 3;

  // endregion fields

  // region constructors

  public PathfindingNode(PathfindingMap map, int nodeX, int nodeY,
      boolean access)
  {

    this(map, nodeX, nodeY,
        new boolean[] { access, access, access, access });
  }

  public PathfindingNode(PathfindingMap map, int nodeX, int nodeY,
      boolean[] access)
  {

    this(map, nodeX, nodeY, access, false);
  }

  public PathfindingNode(PathfindingMap map, int nodeX, int nodeY,
      boolean[] access, boolean mirrorAccess)
  {

    if (nodeX < 0 || nodeX > map.nodesX - 1)
      throw new RuntimeException("Node X is outside of bounds");
    if (nodeY < 0 || nodeY > map.nodesY - 1)
      throw new RuntimeException("Node X is outside of bounds");
    if (access.length != 4)
      throw new RuntimeException("Node is being passed " + access.length
          + " values");

    this.access = access;
    if (nodeX == 0)
      this.access[LEFT_ACCESS] = false;
    if (nodeX == map.nodesX - 1)
      this.access[RIGHT_ACCESS] = false;
    if (nodeY == 0)
      this.access[BOTTOM_ACCESS] = false;
    if (nodeY == map.nodesY - 1)
      this.access[TOP_ACCESS] = false;

    this.position = new Vector2(nodeX * map.getNodeSize().x, nodeY
        * map.getNodeSize().y);
    this.nodePosition = new Vector2(x, y);

    this.x = nodeX;
    this.y = nodeY;
    this.map = map;

    if (mirrorAccess)
    {
      if (nodeX != 0)
      {
        map.nodes[nodeX - 1][nodeY].access[RIGHT_ACCESS] = access[LEFT_ACCESS];
      }

      if (nodeX != map.nodesX - 1)
      {
        map.nodes[nodeX + 1][nodeY].access[LEFT_ACCESS] = access[RIGHT_ACCESS];
      }

      if (nodeY != 0)
      {
        map.nodes[nodeX][nodeY - 1].access[TOP_ACCESS] = access[BOTTOM_ACCESS];
      }

      if (nodeY != map.nodesY - 1)
      {
        map.nodes[nodeX][nodeY + 1].access[BOTTOM_ACCESS] = access[TOP_ACCESS];
      }
    }

  }

  public PathfindingNode(PathfindingMap map, int x, int y, boolean[] access,
      boolean[] adjacentNodesData, boolean[] handleAdjacent)
  {

    this(map, x, y, access);
    if (adjacentNodesData.length != 4)
      throw new RuntimeException("Adjacent node is being passed "
          + access.length + " values");
    if (handleAdjacent.length != 4)
      throw new RuntimeException("Adjacent node is being passed "
          + access.length + " values");

    this.adjacent = adjacentNodesData;
    this.handleAdjacent = handleAdjacent;

    this.position = new Vector2(x * map.getNodeSize().x, y
        * map.getNodeSize().y);
    this.nodePosition = new Vector2(x, y);

    if (x != 0 && handleAdjacent[LEFT_ACCESS])
    {
      map.nodes[x - 1][y].access[RIGHT_ACCESS] = adjacent[LEFT_ACCESS];
    }

    if (x != map.nodesX - 1 && handleAdjacent[RIGHT_ACCESS])
    {
      map.nodes[x + 1][y].access[LEFT_ACCESS] = adjacent[RIGHT_ACCESS];
    }

    if (y != 0 && handleAdjacent[BOTTOM_ACCESS])
    {
      map.nodes[x][y - 1].access[TOP_ACCESS] = adjacent[BOTTOM_ACCESS];
    }

    if (y != map.nodesY - 1 && handleAdjacent[TOP_ACCESS])
    {
      map.nodes[x][y + 1].access[BOTTOM_ACCESS] = adjacent[TOP_ACCESS];
    }
  }

  public PathfindingNode(PathfindingMap map, int nodeX, int nodeY,
      NodePrefab prefab)
  {

    this(map, nodeX, nodeY, Arrays.copyOf(prefab.access, 4), Arrays.copyOf(
        prefab.adjacent, 4), Arrays.copyOf(prefab.handleAdjacent, 4));
  }

  // endregion constructors

  // region methods

  @Override
  public String toString()
  {

    return "(" + x + "," + y + ")";
  }

  public void setPosition(float x, float y)
  {

    this.position.set(x, y);
  }

  public Vector2 getPosition()
  {

    return this.position;
  }

  public Vector2 getNodePosition()
  {

    return nodePosition;
  }

  public static class NodePrefab
  {

    public static NodePrefab RIGHT_WALL = new NodePrefab(new boolean[] {
        true, false, true, true }, new boolean[] { false, true, false,
        false }, new boolean[] { false, false, false, false },
        new boolean[] { false, true, false, false });

    public static NodePrefab LEFT_WALL = new NodePrefab(new boolean[] {
        false, true, true, true }, new boolean[] { true, false, false,
        false }, new boolean[] { false, false, false, false },
        new boolean[] { true, false, false, false });

    public static NodePrefab BOTTOM_WALL = new NodePrefab(new boolean[] {
        true, true, false, true }, new boolean[] { false, false, true,
        false }, new boolean[] { false, false, false, false },
        new boolean[] { false, false, true, false });

    public static NodePrefab TOP_WALL = new NodePrefab(new boolean[] {
        true, true, true, false }, new boolean[] { false, false, false,
        true }, new boolean[] { false, false, false, false },
        new boolean[] { false, false, false, true });

    public static NodePrefab RIGHT_LEDGE = new NodePrefab(new boolean[] {
        true, false, true, true }, new boolean[] { false, true, false,
        false }, new boolean[] { false, false, false, false },
        new boolean[] { false, false, false, false });

    public static NodePrefab LEFT_LEDGE = new NodePrefab(new boolean[] {
        false, true, true, true }, new boolean[] { true, false, false,
        false }, new boolean[] { false, false, false, false },
        new boolean[] { false, false, false, false });

    public static NodePrefab BOTTOM_LEDGE = new NodePrefab(new boolean[] {
        true, true, false, true }, new boolean[] { false, false, true,
        false }, new boolean[] { false, false, false, false },
        new boolean[] { false, false, false, false });

    public static NodePrefab TOP_LEDGE = new NodePrefab(new boolean[] {
        true, true, true, false }, new boolean[] { false, false, false,
        true }, new boolean[] { false, false, false, false },
        new boolean[] { false, false, false, false });

    public static NodePrefab WALL = new NodePrefab(new boolean[] { false,
        false, false, false },
        new boolean[] { true, true, true, true }, new boolean[] {
            false, false, false, false }, new boolean[] { true,
            true, true, true });

    public static NodePrefab INNER_WALL = new NodePrefab(new boolean[] {
        false, false, false, false }, new boolean[] { true, true, true,
        true }, new boolean[] { false, false, false, false },
        new boolean[] { false, false, false, false });

    public boolean access[];
    public boolean handleAccess[];
    public boolean adjacent[];
    public boolean handleAdjacent[];

    public NodePrefab(boolean[] access, boolean[] handleAccess,
        boolean[] adjacent, boolean handleAdjacent[])
    {

      if (access.length != 4)
        throw new RuntimeException("Node is being passed "
            + access.length + " values");
      if (adjacent.length != 4)
        throw new RuntimeException("Node is being passed "
            + access.length + " values");
      if (handleAdjacent.length != 4)
        throw new RuntimeException("Node is being passed "
            + access.length + " values");

      this.access = access;
      this.handleAccess = handleAccess;
      this.adjacent = adjacent;
      this.handleAdjacent = handleAdjacent;
    }
  }

  // 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