Package kku.cs.fgl.transition

Source Code of kku.cs.fgl.transition.TransitionBase

package kku.cs.fgl.transition;

import kku.cs.fgl.actor.ImageActor;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.GameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.Transition;

abstract public class TransitionBase implements Transition {
  private GameState state1, state2;

  protected int duration = 2000;

  protected int time = 0;

  protected Image img1, img2;

  protected ImageActor scene1, scene2;

  private boolean inited = false;

  public TransitionBase() {

  }

  public TransitionBase(int duration) {
    this.duration = duration;
  }


  /**
   * �ж١���¡��͹��� render �����á
   * @param game
   * @param container
   * @param g
   */
  abstract void init(StateBasedGame game, GameContainer container);

  public void init(GameState firstState, GameState secondState) {
    state1 = firstState;
    state2 = secondState;
    if (state1 != null)
      scene1 = new ImageActor(img1);
    if (state2 != null)
      scene2 = new ImageActor(img2);
  }

  public boolean isComplete() {
    if (time > duration) {
      try {
        if (img1 != null)
          img1.destroy();
        if (img2 != null)
          img2.destroy();
      } catch (SlickException e) {
      }
      return true;
    }
    return false;
  }

  public void postRender(StateBasedGame game, GameContainer container,
      Graphics g) throws SlickException {
    Graphics.setCurrent(g);
    if (scene2 != null)
      scene2.render(g);
    if (scene1 != null){
      scene1.render(g);
    }
  }

  public void preRender(StateBasedGame game, GameContainer container,
      Graphics g) throws SlickException {
  }

  public void update(StateBasedGame game, GameContainer container, int delta)
      throws SlickException {
    time += delta;
    int w = container.getWidth();
    int h = container.getHeight();
    if (state1 != null) {
      if (img1 == null) {
        img1 = new Image(container.getWidth(), container.getHeight());
        scene1.setImg(img1);
      }
      Graphics g1 = img1.getGraphics();
      Graphics.setCurrent(g1);
      g1.setColor(Color.black);
      g1.fillRect(0, 0, w, h);
      state1.render(container, game, g1);
      g1.flush();
    }
    if (state2 != null) {
      if (img2 == null) {
        img2 = new Image(container.getWidth(), container.getHeight());
        scene2.setImg(img2);
      }
      Graphics g2 = img2.getGraphics();
      Graphics.setCurrent(g2);
      g2.setColor(Color.black);
      g2.fillRect(0, 0, w, h);
      state2.render(container, game, g2);
      g2.flush();
    }
    if (!inited) {
      init(game,container);
      inited = true;
    }
  }

  public int getTime() {
    return time;
  }

  public int getDuration() {
    return duration;
  }

  public void setDuration(int duration) {
    this.duration = duration;
  }

}
TOP

Related Classes of kku.cs.fgl.transition.TransitionBase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.