示例#1
0
文件: Actor.java 项目: newisso/libgdx
  /**
   * Notifies this actor's listeners of the event. The event is not propagated to any parents.
   * Before notifying the listeners, this actor is set as the {@link Event#getListenerActor()
   * listener actor}. The event {@link Event#setTarget(Actor) target} must be set before calling
   * this method. If this actor is not in the stage, the stage must be set before calling this
   * method.
   *
   * @param capture If true, the capture listeners will be notified instead of the regular
   *     listeners.
   * @return true of the event was {@link Event#cancel() cancelled}.
   */
  public boolean notify(Event event, boolean capture) {
    if (event.getTarget() == null)
      throw new IllegalArgumentException("The event target cannot be null.");

    DelayedRemovalArray<EventListener> listeners = capture ? captureListeners : this.listeners;
    if (listeners.size == 0) return event.isCancelled();

    event.setListenerActor(this);
    event.setCapture(capture);
    if (event.getStage() == null) event.setStage(stage);

    listeners.begin();
    for (int i = 0, n = listeners.size; i < n; i++) {
      EventListener listener = listeners.get(i);
      if (listener.handle(event)) {
        event.handle();
        if (event instanceof InputEvent) {
          InputEvent inputEvent = (InputEvent) event;
          if (inputEvent.getType() == Type.touchDown) {
            event
                .getStage()
                .addTouchFocus(
                    listener,
                    this,
                    inputEvent.getTarget(),
                    inputEvent.getPointer(),
                    inputEvent.getButton());
          }
        }
      }
    }
    listeners.end();

    return event.isCancelled();
  }
  public void update(float delta) {
    if (MathUtils.random() < delta * Constants.ICICLE_SPAWNS_PER_SECOND) {

      Vector2 newIciclePosition =
          new Vector2(MathUtils.random() * viewport.getWorldWidth(), viewport.getWorldHeight());
      Icicle newIcicle = new Icicle(newIciclePosition);
      icicleList.add(newIcicle);
    }

    for (Icicle icicle : icicleList) {
      icicle.update(delta);
    }

    // TODO: begin a removal session
    icicleList.begin();

    // TODO: Remove any icicle completely off the bottom of the screen
    for (int i = 0; i < icicleList.size; i++) {
      if (icicleList.get(i).position.y < -Constants.ICICLES_HEIGHT) {
        icicleList.removeIndex(i);
      }
    }

    // TODO: End removal session
    icicleList.end();
  }
示例#3
0
文件: Actor.java 项目: newisso/libgdx
 public boolean addListener(EventListener listener) {
   if (!listeners.contains(listener, true)) {
     listeners.add(listener);
     return true;
   }
   return false;
 }
示例#4
0
文件: Group.java 项目: btdiehr/libgdx
 public void act(float delta) {
   super.act(delta);
   DelayedRemovalArray<Actor> children = this.children;
   children.begin();
   for (int i = 0, n = children.size; i < n; i++) children.get(i).act(delta);
   children.end();
 }
示例#5
0
文件: Group.java 项目: btdiehr/libgdx
 /**
  * Swaps two actors. Returns false if the swap did not occur because the actors are not children
  * of this group.
  */
 public boolean swapActor(Actor first, Actor second) {
   int firstIndex = children.indexOf(first, true);
   int secondIndex = children.indexOf(second, true);
   if (firstIndex == -1 || secondIndex == -1) return false;
   children.swap(firstIndex, secondIndex);
   return true;
 }
示例#6
0
文件: Group.java 项目: btdiehr/libgdx
 /**
  * Adds an actor as a child of this group, immediately before another child actor. The actor is
  * first removed from its parent group, if any.
  */
 public void addActorBefore(Actor actorBefore, Actor actor) {
   actor.remove();
   int index = children.indexOf(actorBefore, true);
   children.insert(index, actor);
   actor.setParent(this);
   actor.setStage(getStage());
   childrenChanged();
 }
示例#7
0
文件: Group.java 项目: btdiehr/libgdx
 /**
  * Adds an actor as a child of this group, immediately after another child actor. The actor is
  * first removed from its parent group, if any.
  */
 public void addActorAfter(Actor actorAfter, Actor actor) {
   actor.remove();
   int index = children.indexOf(actorAfter, true);
   if (index == children.size) children.add(actor);
   else children.insert(index + 1, actor);
   actor.setParent(this);
   actor.setStage(getStage());
   childrenChanged();
 }
示例#8
0
文件: Group.java 项目: btdiehr/libgdx
 /**
  * Swaps two actors by index. Returns false if the swap did not occur because the indexes were out
  * of bounds.
  */
 public boolean swapActor(int first, int second) {
   int maxIndex = children.size;
   if (first < 0 || first >= maxIndex) return false;
   if (second < 0 || second >= maxIndex) return false;
   children.swap(first, second);
   return true;
 }
示例#9
0
文件: Group.java 项目: btdiehr/libgdx
 /**
  * Adds an actor as a child of this group, at a specific index. The actor is first removed from
  * its parent group, if any.
  */
 public void addActorAt(int index, Actor actor) {
   actor.remove();
   children.insert(index, actor);
   actor.setParent(this);
   actor.setStage(getStage());
   childrenChanged();
 }
示例#10
0
文件: Group.java 项目: btdiehr/libgdx
 /**
  * Adds an actor as a child of this group. The actor is first removed from its parent group, if
  * any.
  */
 public void addActor(Actor actor) {
   actor.remove();
   children.add(actor);
   actor.setParent(this);
   actor.setStage(getStage());
   childrenChanged();
 }
示例#11
0
文件: Group.java 项目: btdiehr/libgdx
 /**
  * Removes an actor from this group. If the actor will not be used again and has actions, they
  * should be {@link Actor#clearActions() cleared} so the actions will be returned to their {@link
  * Action#setPool(com.badlogic.gdx.utils.Pool) pool}, if any. This is not done automatically.
  */
 public boolean removeActor(Actor actor) {
   if (!children.removeValue(actor, true)) return false;
   Stage stage = getStage();
   if (stage != null) stage.unfocus(actor);
   actor.setParent(null);
   actor.setStage(null);
   childrenChanged();
   return true;
 }
  public void update(float delta) {
    if (MathUtils.random() < delta * Constants.ICICLE_SPAWNS_PER_SECOND) {
      Vector2 newIciclePosition =
          new Vector2(MathUtils.random() * viewport.getWorldWidth(), viewport.getWorldHeight());
      Icicle newIcicle = new Icicle(newIciclePosition);
      icicleList.add(newIcicle);
    }

    for (Icicle icicle : icicleList) {
      icicle.update(delta);
    }

    icicleList.begin();
    for (int i = 0; i < icicleList.size; i++) {
      if (icicleList.get(i).position.y < -Constants.ICICLES_HEIGHT) {
        // TODO: Increment count of icicles dodged
        iciclesDodged++;
        icicleList.removeIndex(i);
      }
    }
    icicleList.end();
  }
示例#13
0
  public void update(float delta, Array<Platform> platforms) {

    lastFramePosition.set(position);
    velocity.y -= Constants.GRAVITY;
    position.mulAdd(velocity, delta);

    if (position.y < Constants.KILL_PLANE) {
      init();
    }

    // Land on/fall off platforms
    if (jumpState != Enums.JumpState.JUMPING) {
      if (jumpState != JumpState.RECOILING) {
        jumpState = Enums.JumpState.FALLING;
      }

      for (Platform platform : platforms) {
        if (landedOnPlatform(platform)) {
          jumpState = Enums.JumpState.GROUNDED;
          velocity.y = 0;
          velocity.x = 0;
          position.y = platform.top + Constants.GIGAGAL_EYE_HEIGHT;
        }
      }
    }

    // Collide with enemies

    Rectangle gigaGalBounds =
        new Rectangle(
            position.x - Constants.GIGAGAL_STANCE_WIDTH / 2,
            position.y - Constants.GIGAGAL_EYE_HEIGHT,
            Constants.GIGAGAL_STANCE_WIDTH,
            Constants.GIGAGAL_HEIGHT);

    for (Enemy enemy : level.getEnemies()) {
      Rectangle enemyBounds =
          new Rectangle(
              enemy.position.x - Constants.ENEMY_COLLISION_RADIUS,
              enemy.position.y - Constants.ENEMY_COLLISION_RADIUS,
              2 * Constants.ENEMY_COLLISION_RADIUS,
              2 * Constants.ENEMY_COLLISION_RADIUS);
      if (gigaGalBounds.overlaps(enemyBounds)) {

        if (position.x < enemy.position.x) {
          recoilFromEnemy(Direction.LEFT);
        } else {
          recoilFromEnemy(Direction.RIGHT);
        }
      }
    }

    // Move left/right
    if (jumpState != JumpState.RECOILING) {
      if (Gdx.input.isKeyPressed(Keys.LEFT)) {
        moveLeft(delta);
      } else if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
        moveRight(delta);
      } else {
        walkState = Enums.WalkState.NOT_WALKING;
      }
    }

    // Jump
    if (Gdx.input.isKeyPressed(Keys.Z)) {
      switch (jumpState) {
        case GROUNDED:
          startJump();
          break;
        case JUMPING:
          continueJump();
      }
    } else {
      endJump();
    }

    // TODO: Check if GigaGal should pick up a powerup
    // This is a tough one. Check for overlaps similar to how we detect collisions with enemies,
    // then remove any picked up powerups (and update GigaGal's ammo count)
    // Remember to check out the solution project if you run into trouble!
    DelayedRemovalArray<Powerup> powerups = level.getPowerups();
    powerups.begin();
    for (int i = 0; i < powerups.size; i++) {
      Powerup powerup = powerups.get(i);
      Rectangle powerupBounds =
          new Rectangle(
              powerup.position.x - Constants.POWERUP_CENTER.x,
              powerup.position.y - Constants.POWERUP_CENTER.y,
              Assets.instance.powerupAssets.powerup.getRegionWidth(),
              Assets.instance.powerupAssets.powerup.getRegionHeight());
      if (gigaGalBounds.overlaps(powerupBounds)) {
        ammo += Constants.POWERUP_AMMO;
        powerups.removeIndex(i);
      }
    }
    powerups.end();

    // Shoot
    // TODO: Check if GigaGal has any ammo left
    if (Gdx.input.isKeyJustPressed(Keys.X) && ammo > 0) {

      // TODO: Decrement ammo
      ammo--;
      Vector2 bulletPosition;

      if (facing == Direction.RIGHT) {
        bulletPosition =
            new Vector2(
                position.x + Constants.GIGAGAL_CANNON_OFFSET.x,
                position.y + Constants.GIGAGAL_CANNON_OFFSET.y);
      } else {
        bulletPosition =
            new Vector2(
                position.x - Constants.GIGAGAL_CANNON_OFFSET.x,
                position.y + Constants.GIGAGAL_CANNON_OFFSET.y);
      }
      level.spawnBullet(bulletPosition, facing);
    }
  }
示例#14
0
文件: Actor.java 项目: newisso/libgdx
 public boolean removeCaptureListener(EventListener listener) {
   return captureListeners.removeValue(listener, true);
 }
示例#15
0
文件: Actor.java 项目: newisso/libgdx
 /**
  * Adds a listener that is only notified during the capture phase.
  *
  * @see #fire(Event)
  */
 public boolean addCaptureListener(EventListener listener) {
   if (!captureListeners.contains(listener, true)) captureListeners.add(listener);
   return true;
 }
示例#16
0
文件: Group.java 项目: btdiehr/libgdx
 /**
  * Draws all children. {@link #applyTransform(SpriteBatch)} should be called before and {@link
  * #resetTransform(SpriteBatch)} after this method if {@link #setTransform(boolean) transform} is
  * true. If {@link #setTransform(boolean) transform} is false these methods don't need to be
  * called, children positions are temporarily offset by the group position when drawn. This method
  * avoids drawing children completely outside the {@link #setCullingArea(Rectangle) culling area},
  * if set.
  */
 protected void drawChildren(SpriteBatch batch, float parentAlpha) {
   parentAlpha *= getColor().a;
   DelayedRemovalArray<Actor> children = this.children;
   children.begin();
   if (cullingArea != null) {
     // Draw children only if inside culling area.
     if (transform) {
       for (int i = 0, n = children.size; i < n; i++) {
         Actor child = children.get(i);
         if (!child.isVisible()) continue;
         float x = child.getX();
         float y = child.getY();
         if (x <= cullingArea.x + cullingArea.width
             && y <= cullingArea.y + cullingArea.height
             && x + child.getWidth() >= cullingArea.x
             && y + child.getHeight() >= cullingArea.y) {
           child.draw(batch, parentAlpha);
         }
       }
       batch.flush();
     } else {
       // No transform for this group, offset each child.
       float offsetX = getX();
       float offsetY = getY();
       setPosition(0, 0);
       for (int i = 0, n = children.size; i < n; i++) {
         Actor child = children.get(i);
         if (!child.isVisible()) continue;
         float x = child.getX();
         float y = child.getY();
         if (x <= cullingArea.x + cullingArea.width
             && y <= cullingArea.y + cullingArea.height
             && x + child.getWidth() >= cullingArea.x
             && y + child.getHeight() >= cullingArea.y) {
           child.translate(offsetX, offsetY);
           child.draw(batch, parentAlpha);
           child.setPosition(x, y);
         }
       }
       setPosition(offsetX, offsetY);
     }
   } else {
     if (transform) {
       for (int i = 0, n = children.size; i < n; i++) {
         Actor child = children.get(i);
         if (!child.isVisible()) continue;
         child.draw(batch, parentAlpha);
       }
       batch.flush();
     } else {
       // No transform for this group, offset each child.
       float offsetX = getX();
       float offsetY = getY();
       setPosition(0, 0);
       for (int i = 0, n = children.size; i < n; i++) {
         Actor child = children.get(i);
         if (!child.isVisible()) continue;
         float x = child.getX();
         float y = child.getY();
         child.translate(offsetX, offsetY);
         child.draw(batch, parentAlpha);
         child.setPosition(x, y);
       }
       setPosition(offsetX, offsetY);
     }
   }
   children.end();
 }