Android Open Source - LucyTheMoocher Cinematic






From Project

Back to project page LucyTheMoocher.

License

The source code is released under:

MIT License

If you think the Android project LucyTheMoocher 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.lucythemoocher.physics;
//from w  w  w. j av  a  2 s .c  om
import java.util.ArrayList;

import com.lucythemoocher.Globals.Globals;
import com.lucythemoocher.util.MathUtil;

public class Cinematic {
  protected static final float GRAVITY = 0.004f;
  protected static final float MOVESPEED = 0.8f;
  protected static final float JUMPSPEED = 1.5f;
  protected static final float ATTACKSPEED = 2f;
  
  
  private float posx_;
  private float posy_;
  protected float speedx_;
  protected float speedy_;
  private float offsetx_;
  private float offsety_;
  private float normalSpeed_;
  private boolean withGravity_;
  
  private ArrayList<Box> boundingBoxes_;
  
  /**
   * Constructor
   */
  public Cinematic() {
    boundingBoxes_ = new ArrayList<Box>();
    normalSpeed_ = MOVESPEED;
    offsetx_ = 0.0f;
    offsety_ = 0.0f;
    withGravity_ = true;
  }
  
  /**
   * Constructor with default speed
   * @param speed: default speed for walking
   */
  public Cinematic(float speed) {
    boundingBoxes_ = new ArrayList<Box>();
    normalSpeed_ = speed;
    offsetx_ = 0.0f;
    offsety_ = 0.0f;
    withGravity_ = true;
  }
  
  /**
   * Constructor with default speed
   * @param speed: default speed for walking
   * @param withGravity: take gravity or not
   */
  public Cinematic(float speed, boolean withGravity) {
    boundingBoxes_ = new ArrayList<Box>();
    normalSpeed_ = speed;
    offsetx_ = 0.0f;
    offsety_ = 0.0f;
    withGravity_ = withGravity;
  }
  
  public Cinematic(Box box) {
    this();
    box.setCin(this);
    boundingBoxes_.add(box);
  }
  
  public Cinematic(float x, float y, float h, float w) {
    this();
    addBox(x, y, h, w);
  }
  
  public void addBox(float x, float y, float h, float w) {
    posx_ = x;
    posy_ = y;
    Box box = new Box(x, y, h, w);
    box.setCin(this);
    boundingBoxes_.add(box);
  }
  
  public void update() {
    if (withGravity_) {
      updateNormalSpeed();
    }
    updatePos();
  }
  

  private void updatePos() {
    float tempx = speedx() * Globals.getInstance().getGame().getDt() + offsetx_;
    float tempy = speedy() * Globals.getInstance().getGame().getDt() + offsety_;
    int movex = (int)tempx;
    int movey = (int)tempy;
    if ( tempx > 0 ) {
      offsetx_ = tempx - (float)Math.floor((double)tempx);
    } else {
      offsetx_ = tempx - (float)Math.ceil((double)tempx); 
    }
    for ( int i=0; i< Math.abs(movex) && !Globals.getInstance().getGame().getMap().hasCollision(boundingBoxes_) ; i++ ) {
      posx_ += MathUtil.sign(movex);
    }
    if ( Globals.getInstance().getGame().getMap().hasCollision(boundingBoxes_) ) {
      posx_ -= MathUtil.sign(movex);
      speedx_ = 0;
    }
    for ( int i=0; i< (int)Math.abs(movey) && !Globals.getInstance().getGame().getMap().hasCollision(boundingBoxes_) ; i++ ) {
      posy_ += MathUtil.sign(movey);
    }
    if ( Globals.getInstance().getGame().getMap().hasCollision(boundingBoxes_) ) {
      posy_ -= MathUtil.sign(movey);
      speedy_ = 0;
    }
  }
  
  public float x() {
    return posx_ + offsetx_;
  }
  
  public float y() {
    return posy_ + offsety_;
  }
  
  public float speedx() {
    return speedx_;
  }
  
  public float speedy() {
    return speedy_;
  }
  
  public ArrayList<Box> boundingBoxes() {
    return boundingBoxes_;
  }

  public void moveStop() {
    speedx_ = 0;
  }
  
  public void stay() {
    speedx_ = 0;
    speedy_ = 0;
  }

  public void moveLeft() {
    speedx_ = -normalSpeed_;
  }

  public void moveRight() {
    speedx_ = normalSpeed_;
  }

  public void moveFastUp() {
    speedy_ = -JUMPSPEED;
  }
  
  public void moveUp() {
    speedy_ = -MOVESPEED;
  }
  
  public void moveDown() {
    speedy_ = ATTACKSPEED;
  }

  private void updateNormalSpeed() {
    speedy_ += GRAVITY * Globals.getInstance().getGame().getDt();
  }
  
  /**
   * Get the aimed position (x)
   * @return
   */
  public float getTargetX() {
    return posx_ + 500 * speedx_;
  }
  
  /**
   * Get the aimed position (y)
   * @return
   */
  public float getTargetY() {
    return posy_ + 100 * speedy_;
  }
  
  public boolean hasDownCollision() {
    return Globals.getInstance().getGame().getMap().hasDownCollision(boundingBoxes_);
  }
  
  public boolean hasLeftCollision() {
    return Globals.getInstance().getGame().getMap().hasLeftCollision(boundingBoxes_);
  }
  
  public boolean hasRightCollision() {
    return Globals.getInstance().getGame().getMap().hasRightCollision(boundingBoxes_);
  }
  
  /**
   * Getter
   * @param c 
   * @return True if and only if the cinematic collides with c
   * @see collidesWith(Cinematic c, float ratio)
   */
  public boolean collidesWith(Cinematic c) {
    return collidesWith(c, 1);
  }
  
  /**
   * Getter
   * @param c 
   * @return True if and only if the cinematic collides with c
   */
  public boolean collidesWith(Cinematic c, float ratio) {
    for (Box b1: this.boundingBoxes()) {
      for (Box b2: c.boundingBoxes()) {
        if (b1.collideWith(b2, ratio)) {
          return true;
        }
      }
    }
    return false;
  }
}




Java Source Code List

com.lucythemoocher.LucyTheMoocherActivity.java
com.lucythemoocher.FX.FXManager.java
com.lucythemoocher.FX.FX.java
com.lucythemoocher.Globals.Globals.java
com.lucythemoocher.actors.Actor.java
com.lucythemoocher.actors.ActorsManager.java
com.lucythemoocher.actors.Monster.java
com.lucythemoocher.actors.MonstersManager.java
com.lucythemoocher.actors.PlayerCharacter.java
com.lucythemoocher.actors.Projectile.java
com.lucythemoocher.actors.ProjectilesManager.java
com.lucythemoocher.actors.Tank.java
com.lucythemoocher.actors.TargetCharacter.java
com.lucythemoocher.actors.maincharacter.state.StateAttack.java
com.lucythemoocher.actors.maincharacter.state.StateFalling.java
com.lucythemoocher.actors.maincharacter.state.StateJumping.java
com.lucythemoocher.actors.maincharacter.state.StateNone.java
com.lucythemoocher.actors.maincharacter.state.StateRunning.java
com.lucythemoocher.actors.maincharacter.state.StateWallSliding.java
com.lucythemoocher.actors.maincharacter.state.StateWallWalking.java
com.lucythemoocher.actors.maincharacter.state.State.java
com.lucythemoocher.controls.AIController.java
com.lucythemoocher.controls.ActionController.java
com.lucythemoocher.controls.ButtonListener.java
com.lucythemoocher.controls.Controllable.java
com.lucythemoocher.controls.Controller.java
com.lucythemoocher.controls.GlobalController.java
com.lucythemoocher.controls.KeysListener.java
com.lucythemoocher.controls.TouchListener.java
com.lucythemoocher.events.EventNormal.java
com.lucythemoocher.events.EventSlow.java
com.lucythemoocher.events.Event.java
com.lucythemoocher.game.GameThread.java
com.lucythemoocher.game.Game.java
com.lucythemoocher.game.LevelLoader.java
com.lucythemoocher.graphics.ActorDrawer.java
com.lucythemoocher.graphics.Animation.java
com.lucythemoocher.graphics.Background.java
com.lucythemoocher.graphics.Camera.java
com.lucythemoocher.graphics.Drawable.java
com.lucythemoocher.graphics.Grid.java
com.lucythemoocher.graphics.HUD.java
com.lucythemoocher.graphics.Image.java
com.lucythemoocher.graphics.PersistentEffect.java
com.lucythemoocher.graphics.PersistentPic.java
com.lucythemoocher.graphics.PictureContainer.java
com.lucythemoocher.gui.MenuButtonListener.java
com.lucythemoocher.gui.MenuButtonTouchListener.java
com.lucythemoocher.gui.MenuButton.java
com.lucythemoocher.loops.CreditsLoop.java
com.lucythemoocher.loops.GameOverLoop.java
com.lucythemoocher.loops.InitMenuLoop.java
com.lucythemoocher.loops.LivesMenuLoop.java
com.lucythemoocher.loops.LoopGame.java
com.lucythemoocher.loops.LoopPause.java
com.lucythemoocher.loops.Loop.java
com.lucythemoocher.loops.MasterLoop.java
com.lucythemoocher.physics.Box.java
com.lucythemoocher.physics.Cinematic.java
com.lucythemoocher.physics.Map.java
com.lucythemoocher.sounds.SoundManager.java
com.lucythemoocher.sounds.SoundsState.java
com.lucythemoocher.sounds.StateLevel1.java
com.lucythemoocher.sounds.StateLevel2.java
com.lucythemoocher.sounds.StateLevel3.java
com.lucythemoocher.sounds.StateNormal.java
com.lucythemoocher.util.Direction.java
com.lucythemoocher.util.MathUtil.java
com.lucythemoocher.util.Resources.java
com.lucythemoocher.util.Timer.java