GameObject.java :  » Game » galatichero » net » narusas » galaticheroproto » model » Java Open Source

Java Open Source » Game » galatichero 
galatichero » net » narusas » galaticheroproto » model » GameObject.java
package net.narusas.galaticheroproto.model;

import java.util.ArrayList;

import net.narusas.galaticheroproto.control.MessageListener;

public abstract class GameObject implements MessageListener {
  protected long id = ObjectManager.getNextId();

  ArrayList<Action> actionList;

  protected boolean died = false;

  public Action nextAction;

  protected int state;

  public GameObject() {
    ObjectManager.add(this);
  }

  public long getId() {
    return id;
  }

  public void die() {
    died = true;
    ObjectManager.removeDied(id);
  }

  public boolean isDied() {
    return died;
  }

  public int getState() {
    return state;
  }

  public void setState(int state) {
    this.state = state;
  }

  public void addAction(Action action) {
    // System.out.println("Add Action:"+action);
    if (actionList == null) {
      actionList = new ArrayList<Action>();
    }
    actionList.add(action);
    if (nextAction == null) {
      setNextAction(action);
    }
  }

  public void forceAction(Action action) {
    // System.out.println("Force action :" + action);
    if (actionList != null) {
      actionList.clear();
    }

    nextAction = null;
    addAction(action);
  }

  protected void setNextAction(Action action) {
    // System.out.println("Set next action:"+action);
    nextAction = action;
    nextAction.setCollaborator(this);

  }

  protected void handleActionLogic() {
    if (nextAction != null) {
      nextAction.logic();
      if (nextAction.isOver()) {
        nextAction();
      }
    }
  }

  protected void nextAction() {
    actionList.remove(0);
    if (actionList.size() == 0) {
      nextAction = null;
      return;
    }
    setNextAction(actionList.get(0));
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.