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

📄 restore.java

📁 这是我自己写的一个RPG游戏 基于MIDP2.0开发,开发环境为Eclipse+EclipseMe+WTK2.2,主要是用于学习,实现了RPG游戏的基本功能,望能发布
💻 JAVA
字号:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

class Restore
{
	private RecordStore rs;
	private Map map;
	private ItemManager iMgr;
	private Creature main;
	private boolean used[];
	private String name[];
	private int level[];
	private int onRecord;
	private ByteArrayOutputStream bos;
	private DataOutputStream dos;
	private ByteArrayInputStream bis;
	private DataInputStream dis;
	private byte data[];
	
	public Restore()
	{
		open();
		refresh();
		close();
	}
	
	void open()
	{
		try {
			rs=RecordStore.openRecordStore("RpgGame",true);
		} catch (RecordStoreFullException e) {
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			e.printStackTrace();
		} catch (RecordStoreException e) {
			e.printStackTrace();
		}	
	}
	
	private void refresh()
	{
		used=new boolean[3];
		name=new String[3];
		level=new int[3];
		try {
			if(rs.getNumRecords()==0)
			{
				bos=new ByteArrayOutputStream();
				dos=new DataOutputStream(bos);
				dos.writeBoolean(false);
				data=bos.toByteArray();
				for(int i=0;i<3;i++)
				{
					rs.addRecord(data,0,data.length);
					used[i]=false;
				}
				bos.close();
				dos.close();
			}
			else
			{
				for(int i=1;i<=3;i++)
				{
					data=rs.getRecord(i);
					bis=new ByteArrayInputStream(data);
					dis=new DataInputStream(bis);
					used[i-1]=dis.readBoolean();
					if(used[i-1])
					{
						name[i-1]=dis.readUTF();
						level[i-1]=dis.readInt();
					}
					bis.close();
					dis.close();
				}
			}
		} catch (RecordStoreNotOpenException e1) {
			e1.printStackTrace();
		} catch (RecordStoreFullException e1) {
			e1.printStackTrace();
		} catch (InvalidRecordIDException e1) {
			e1.printStackTrace();
		} catch (RecordStoreException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		} 
		
	}
	
	boolean[] getRecordUsed()
	{
		return used;
	}
	
	String[] getName()
	{
		return name;
	}
	
	int[] getLevel()
	{
		return level;
	}
	
	
	void load(int recordID)
	{
		open();
		try {
			data=rs.getRecord(recordID);
		} catch (RecordStoreNotOpenException e) {
			e.printStackTrace();
		} catch (InvalidRecordIDException e) {
			e.printStackTrace();
		} catch (RecordStoreException e) {
			e.printStackTrace();
		}
		bis=new ByteArrayInputStream(data);
		dis=new DataInputStream(bis);
		try {
			boolean used=dis.readBoolean();
			String name=dis.readUTF();
			int level=dis.readInt();
			int exp=dis.readInt();
			int hp=dis.readInt();
			int mp=dis.readInt();
			Item equip[]=new Item[4];
			for(int i=0;i<4;i++)
				equip[i]=new Item(dis.readInt());
			int x=dis.readInt();
			int y=dis.readInt();
			int mapId=dis.readInt();
			int mapStates[]=new int[4];
			for(int i=0;i<4;i++)
				mapStates[i]=dis.readInt();
			Item items[]=new Item[16];
			for(int i=0;i<16;i++)
				items[i]=new Item(dis.readInt());
			int money=dis.readInt();
			int mission=dis.readInt();
			dis.close();
			bis.close();
			iMgr=new ItemManager(money,items);
			map=new Map(mapId,mapStates[mapId],x,y);
			main=new Creature(name,level,exp,hp,mp,mapStates,equip,mission);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		onRecord=recordID;
		close();
	}
	
	void save(Creature main,Map map,ItemManager iMgr)
	{
		open();
		bos=new ByteArrayOutputStream();
		dos=new DataOutputStream(bos);
		try {
			dos.writeBoolean(true);
			dos.writeUTF(main.name);
			dos.writeInt(main.level);//level
			dos.writeInt(main.exp);//exp
			dos.writeInt(main.hp);//hp
			dos.writeInt(main.mp);//mp
			for(int i=0;i<4;i++)
				dos.writeInt(main.items[i].getID());//item1
			dos.writeInt(map.getMe().getX());//x
			dos.writeInt(map.getMe().getY());//y
			dos.writeInt(map.getMapId());//mapId
			for(int i=0;i<4;i++)
				dos.writeInt(main.mapState[i]);//mapState
			for(int i=0;i<16;i++)
				dos.writeInt(iMgr.getItem(i).getID());//items
			dos.writeInt(iMgr.getMoney());//money
			dos.writeInt(main.mission);//mission
			data=bos.toByteArray();
			bos.close();
			dos.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		try {
			rs.setRecord(onRecord,data,0,data.length);
		} catch (RecordStoreNotOpenException e) {
			e.printStackTrace();
		} catch (InvalidRecordIDException e) {
			e.printStackTrace();
		} catch (RecordStoreFullException e) {
			e.printStackTrace();
		} catch (RecordStoreException e) {
			e.printStackTrace();
		}
		refresh();
		close();
	}
	
	void init(int recordID,String name)
	{
		open();
		bos=new ByteArrayOutputStream();
		dos=new DataOutputStream(bos);
		try {
			dos.writeBoolean(true);
			dos.writeUTF(name);
			dos.writeInt(0);//level
			dos.writeInt(0);//exp
			dos.writeInt(60);//hp
			dos.writeInt(10);//mp
			dos.writeInt(0);//item1
			dos.writeInt(0);//item2
			dos.writeInt(0);//item3
			dos.writeInt(0);//item4
			dos.writeInt(64);//x
			dos.writeInt(97);//y
			dos.writeInt(0);//mapId
			for(int i=0;i<4;i++)
				dos.writeInt(0);//mapState
			dos.writeInt(18);
			dos.writeInt(28);
			dos.writeInt(38);
			dos.writeInt(48);
			dos.writeInt(1);
			for(int i=0;i<11;i++)
				dos.writeInt(0);//items
			dos.writeInt(0);//money
			dos.writeInt(0);//mission
			data=bos.toByteArray();
			dos.close();
			bos.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		try {
			rs.setRecord(recordID,data,0,data.length);
		} catch (RecordStoreNotOpenException e) {
			e.printStackTrace();
		} catch (InvalidRecordIDException e) {
			e.printStackTrace();
		} catch (RecordStoreFullException e) {
			e.printStackTrace();
		} catch (RecordStoreException e) {
			e.printStackTrace();
		}
		Item items[]=new Item[16];
		for(int i=0;i<items.length;i++)
			items[i]=new Item(0);
		items[0]=new Item(18);
		items[1]=new Item(28);
		items[2]=new Item(38);
		items[3]=new Item(48);
		items[4]=new Item(1);
		iMgr=new ItemManager(0,items);
		map=new Map(0,0,48,48);
		int mapStates[]=new int[4];
		Item equip[]=new Item[4];
		for(int i=0;i<equip.length;i++)
			equip[i]=new Item(0);
		main=new Creature(name,0,0,60,10,mapStates,equip,0);
		onRecord=recordID;
		refresh();
		close();
	}
	
	Creature getMc()
	{
		return main;
	}
	
	Map getMap()
	{
		return map;
	}
	
	ItemManager getItemManager()
	{
		return iMgr;
	}
	
	void close()
	{
		try {
			rs.closeRecordStore();
		} catch (RecordStoreNotOpenException e) {
			e.printStackTrace();
		} catch (RecordStoreException e) {
			e.printStackTrace();
		}
		System.gc();
	}
}

⌨️ 快捷键说明

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