⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inventario.java

📁 一个完全的j2me斜视角地图的rpg游戏
💻 JAVA
字号:
/*        
        GAEM - Graphical Adventure Engine for Mobiles (version 0.1)
        Copyright (C) 2005 Victor Borrull
        
        This file is part of GAEM.
        GAEM is free software; you can redistribute it and/or modify it under the terms
        of the GNU General Public License as published by the Free Software Foundation; either
        version 2 of the License, or (at your option) any later version.
        GAEM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
        without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
        See the GNU General Public License for more details.
        You should have received a copy of the GNU General Public License along with this program;
        if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

import javax.microedition.lcdui.*;

/**     La clase Inventario recrea un inventario virtual, con capacidad limitada a 5 Objetos.<br>
*       Hay metodos para moverse adelante y atras en la lista, son llamados por el metodo teclas()<br>
*       de CanvasJuego.<br>
*       Otro metodo importante es {@link #anyadirObjeto(Obj, CanvasJuego, int) anyadirObjeto(Obj, CanvasJuego, int)} con el cual Se a馻den Objetos al final del array lista<br>
*       y devuelve el metodo {@link Obj#getFraseCogido() Obj.getFraseCogido()} o Devuelve un mensaje de error si esta lleno el array.<br>
*/
public class Inventario {
        /**     Lista de Objs en inventario.*/
	private Obj [] lista;
        /**     Cantidad de objetos en inventario.*/
	private int numeroObjetos;
        /**     Obj actualmente seleccionado.*/
        private int actual = -1;
        /**     Constructor que inicializa la lista y el numero de Objs en inventario*/
	public Inventario() {
		lista = new Obj[5];
		numeroObjetos = 0;
	}
        /**     @return Cantidad de Objs en inventario*/
	public int getNumeroObjetos() {
		return numeroObjetos;
	}
        /**     @return N鷐ero de Obj actualmente seleccionado.*/
	public int getActual() {
		return actual;
	}
        /**     @param i N鷐ero de Obj a recuperar.
        *       @return Obj en la posici髇 indicada.
        */
	public Obj getObjeto(int i) {
                Obj retorno;
                try {
                        retorno = lista[i];
                } catch (ArrayIndexOutOfBoundsException e) {
                        System.out.println("Excepcion en getObjeto() al acceder a una posicion no valida del array. Devuelve null");
                        retorno = null;
                }
		return retorno;
	}
        /**     Devuelve primer Obj.
        *       @return Primer Obj en inventario.
        */
	public Obj primero() {
		return lista[0];
	}
        /**     Devuelve siguiente Obj (primero si es el 鷏timo).
        *       @return Siguiente Obj (primero si es el 鷏timo).
        */
	public Obj siguiente() {
		if (numeroObjetos > actual + 1)
			actual++;
		else
			actual = 0;
		return lista[actual];

	}
        /**     Devuelve Obj anterior (鷏timo si es el primero).
        *       @return Siguiente Obj (鷏timo si es el primero).
        */
	public Obj anterior() {
		if (actual == 0)
			actual = numeroObjetos - 1;
		else
			actual--;
		return lista[actual];
	}
        /**     A馻de Obj al inventario y devuelve su frase de cogido; cambia a <code>true</code> la variable {@link CanvasJuego#mapaCambiado 
        *       CanvasJuego.mapaCambiado}.
        *       @param obj Obj a coger.
        *       @param canvas CanvasJuego para actualizar mapaCambiado.
        *       @param posic Posici髇 en inventario del Obj. Si es 6, se a馻de como nuevo, si no, se a馻de en esta posicion ya que es un Obj cargado que ya estaba en inventario.
        *       @return Frase de cogido del Obj o Frase de inventario lleno.
        */
	public String anyadirObjeto(Obj obj, CanvasJuego canvas, int posic) {
        String returno = "";
        if (posic == 6) {
		if (numeroObjetos < lista.length) {
			lista[numeroObjetos] = obj;
			returno = obj.getFraseCogido();
                        obj.setPosicionInventario(numeroObjetos);
                        System.out.println("Objeto a馻dido en inventario en posicion: " + numeroObjetos);
			numeroObjetos++;
                        canvas.mapaCambiado = true;
		} else {
			returno = "No puedo coger m醩 objetos";
		}

        } else {        //Objetos cargados de inventario
                        obj.defineReferencePixel(obj.getWidth()/2, obj.getHeight()/2);     //centro pivot de cada objeto
			lista[posic] = obj;
                        obj.setPosicionInventario(numeroObjetos);
                        System.out.println("Objeto a馻dido en inventario en posicion: " + posic);
                        returno = obj.getFraseCogido();
                        numeroObjetos++;
        }
        obj.setVisible(false);
        obj.setCogible(false,"");
        return returno;
        }
        /**     A馻de Obj a inventario.*/
	public String anyadirObjeto(Obj obj, CanvasJuego canvas) {
                return anyadirObjeto(obj, canvas, 6);
	}
        /**     Quita Obj en la posici髇 indicada del inventario.
        *       @param posicion Posici髇 en lista de inventario del Obj a eliminar.
        */
        public void quitarObjeto(int posicion) {
/*              
                Borra objeto de lista[posicion] y mueve los objetos posteriores en la lista.<br>
                Decrementa la variable numeroObjetos.
*/
                lista[posicion].setPosicionInventario(6);
                numeroObjetos--;
                for (int i = posicion; i < numeroObjetos; i++) {
                        if (i == lista.length-1) {
                                lista[i] = null;
                                numeroObjetos = 4;
                        } else {
                                lista[i] = lista[i+1];
                        }
                }
        }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -