Android Open Source - mobius Linked






From Project

Back to project page mobius.

License

The source code is released under:

MIT License

If you think the Android project mobius 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.sgtcodfish.mobiusListing.components;
/*from w  w w .  ja  v  a2s .  c o m*/
import com.artemis.Component;
import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.World;

/**
 * @author Ashley Davis (SgtCoDFish)
 */
public class Linked implements Component {
  /**
   * <p>
   * Holds a performer which can be used to apply linking attributes to linked
   * child entities.
   * </p>
   * 
   * @author Ashley Davis (SgtCoDFish)
   */
  public interface LinkPerformer {
    /**
     * <p>
     * Performs some function that links the child entity to the parent. An
     * example might be moving the child to the same position as the parent,
     * or to a position with some function applied to it.
     * </p>
     * 
     * @param parent
     *        The parent entity of linkedEntity.
     */
    public void perform(Entity parent);
  }

  public Entity      child    = null;
  public LinkPerformer  performer  = null;

  public Linked() {
  }

  @Override
  public void reset() {
    child = null;
    performer = null;
  }

  /**
   * <p>
   * Creates a new {@link PositionOpacityLinkPerformer} between the parent and
   * child entities.
   * </p>
   * 
   * @param world
   *        The world in which the entities reside.
   * @param parent
   *        The parent entity.
   * @param child
   *        The child entity; see {@link PositionOpacityLinkPerformer} for
   *        changes which performer.perform(parent) will carry out.
   * @param xOffset
   *        How far in the x direction the child should be offset.
   * @param yFlip
   *        The way to "flip" the position of the child entity when mirrored.
   */
  public static void makePositionOpacityLink(World world, Entity parent, Entity child, float xOffset, float yFlip) {
    Linked link = world.createComponent(Linked.class);
    link.child = child;
    link.performer = link.new PositionOpacityLinkPerformer(world, xOffset, yFlip);

    parent.addComponent(link);
  }

  /**
   * <p>
   * Does nothing to the child entity. Useful for linking a child's lifecycle
   * to its parent's (i.e. the child is destroyed when the parent is).
   * </p>
   * 
   * @author Ashley Davis (SgtCoDFish)
   */
  public static class PassLink implements LinkPerformer {
    @Override
    public void perform(Entity parent) {
    }
  }

  /**
   * <p>
   * Contains a {@link LinkPerformer} which maps a child's position to the
   * parent's mirrored position, and maps a child's {@link Opacity} to match
   * its parent's opacity.
   * </p>
   * 
   * @author Ashley Davis (SgtCoDFish)
   */
  public class PositionOpacityLinkPerformer implements LinkPerformer {
    private ComponentMapper<Position>  positionMapper  = null;
    private ComponentMapper<Opacity>  opacityMapper  = null;

    private float            xOffset      = 0.0f;
    private float            yFlip      = 0.0f;

    public PositionOpacityLinkPerformer(World world, float xOffset, float yFlip) {
      this.positionMapper = world.getMapper(Position.class);
      this.opacityMapper = world.getMapper(Opacity.class);

      this.xOffset = xOffset;
      this.yFlip = yFlip;
    }

    @Override
    public void perform(Entity parent) {
      if (parent == null || child == null) {
        return;
      }

      Position thisPos = positionMapper.get(parent);
      Position otherPos = positionMapper.get(child);

      if (thisPos != null && otherPos != null) {
        otherPos.position.y = yFlip - thisPos.position.y;
        otherPos.position.x = xOffset + thisPos.position.x;
      }

      Opacity thisOpacity = opacityMapper.get(parent);
      Opacity otherOpacity = opacityMapper.get(child);

      if (thisOpacity != null && otherOpacity != null) {
        otherOpacity.opacity = thisOpacity.opacity;
      }
    }
  }
}




Java Source Code List

com.sgtcodfish.mobiusListing.Item.java
com.sgtcodfish.mobiusListing.MobiusListingGame.java
com.sgtcodfish.mobiusListing.TerrainCollisionMap.java
com.sgtcodfish.mobiusListing.WorldConstants.java
com.sgtcodfish.mobiusListing.android.AndroidLauncher.java
com.sgtcodfish.mobiusListing.components.ChildLinked.java
com.sgtcodfish.mobiusListing.components.Collectable.java
com.sgtcodfish.mobiusListing.components.DxLayer.java
com.sgtcodfish.mobiusListing.components.DyLayer.java
com.sgtcodfish.mobiusListing.components.FadableLayer.java
com.sgtcodfish.mobiusListing.components.FocusTaker.java
com.sgtcodfish.mobiusListing.components.InteractableLayer.java
com.sgtcodfish.mobiusListing.components.Interactable.java
com.sgtcodfish.mobiusListing.components.Inventory.java
com.sgtcodfish.mobiusListing.components.Linked.java
com.sgtcodfish.mobiusListing.components.MobiusSprite.java
com.sgtcodfish.mobiusListing.components.MovingLayer.java
com.sgtcodfish.mobiusListing.components.Opacity.java
com.sgtcodfish.mobiusListing.components.PlatformInputListener.java
com.sgtcodfish.mobiusListing.components.PlatformSprite.java
com.sgtcodfish.mobiusListing.components.PlayerInputListener.java
com.sgtcodfish.mobiusListing.components.PlayerSprite.java
com.sgtcodfish.mobiusListing.components.PlayerState.java
com.sgtcodfish.mobiusListing.components.Position.java
com.sgtcodfish.mobiusListing.components.Solid.java
com.sgtcodfish.mobiusListing.components.StaticSprite.java
com.sgtcodfish.mobiusListing.components.TiledRenderable.java
com.sgtcodfish.mobiusListing.components.Velocity.java
com.sgtcodfish.mobiusListing.desktop.DesktopLauncher.java
com.sgtcodfish.mobiusListing.levels.LevelEntityFactory.java
com.sgtcodfish.mobiusListing.player.HumanoidAnimationState.java
com.sgtcodfish.mobiusListing.player.PlayerConstants.java
com.sgtcodfish.mobiusListing.player.PlayerEntityFactory.java
com.sgtcodfish.mobiusListing.systems.AudioSystem.java
com.sgtcodfish.mobiusListing.systems.CollisionBoxRenderingDebugSystem.java
com.sgtcodfish.mobiusListing.systems.FocusTakerSystem.java
com.sgtcodfish.mobiusListing.systems.LevelAdvanceSystem.java
com.sgtcodfish.mobiusListing.systems.LinkingSystem.java
com.sgtcodfish.mobiusListing.systems.MovementSystem.java
com.sgtcodfish.mobiusListing.systems.PlatformInputSystem.java
com.sgtcodfish.mobiusListing.systems.PlayerInputSystem.java
com.sgtcodfish.mobiusListing.systems.SolidProcessingSystem.java
com.sgtcodfish.mobiusListing.systems.SpriteRenderingSystem.java
com.sgtcodfish.mobiusListing.systems.TerrainCollisionBoxRenderingDebugSystem.java
com.sgtcodfish.mobiusListing.systems.TerrainCollisionSystem.java
com.sgtcodfish.mobiusListing.systems.TiledRenderingSystem.java