📄 gamestore.java
字号:
package org.gamecollege.j2me.rpg;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Vector;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotFoundException;
/**
* 游戏存储类
*
*/
public class GameStore {
MyGameCanvas mgc;
public GameStore(MyGameCanvas _mgc){
mgc=_mgc;
}
/**
* 是否有存盘
* @return
*/
public boolean canLoad(){
try {
String[] ds=RecordStore.listRecordStores();
if(ds==null||ds.length==0){
return false;
}else{
for (int i = 0; i < ds.length; i++) {
if(ds[i].equals("TXWW")){
return true;
}
}
return false;
}
} catch (Exception e) {
return false;
}
}
/**
* 删除存盘
*
*/
public void deleteRecord(){
try {
RecordStore.deleteRecordStore("TXWW");
} catch (RecordStoreNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 游戏存盘
*
*/
public void saveGame(){
try {
RecordStore rs=RecordStore.openRecordStore("TXWW",true);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(baos);
//关卡号
dos.writeInt(mgc.curLevelNo);
//地图号
dos.writeInt(mgc.curLevel.curMap.mapNo);
//英雄位置
dos.writeInt(mgc.myPlayer.col);
dos.writeInt(mgc.myPlayer.row);
//速度
dos.writeInt(mgc.myPlayer.rate);
//money
dos.writeInt(mgc.myPlayer.money);
//hp
dos.writeInt(mgc.myPlayer.hp);
//mp
dos.writeInt(mgc.myPlayer.mp);
//exp
dos.writeInt(mgc.myPlayer.exp);
//rank
dos.writeInt(mgc.myPlayer.rank);
//nextExp
dos.writeInt(mgc.myPlayer.nextExp);
//attackPoint
dos.writeInt(mgc.myPlayer.attackPoint);
//defendPoint
dos.writeInt(mgc.myPlayer.defendPoint);
//当前武器
dos.writeInt(mgc.myPlayer.curWeaponID);
//当前护具
dos.writeInt(mgc.myPlayer.curDefendID);
//道具和技能
StringBuffer sb=new StringBuffer();
Vector v=mgc.myPlayer.weaponVec;
for (int i = 0; i < v.size(); i++) {
RPGObject ro=(RPGObject)v.elementAt(i);
sb.append(ro.ID+"|");
}
v=mgc.myPlayer.jacketVec;
for (int i = 0; i < v.size(); i++) {
RPGObject ro=(RPGObject)v.elementAt(i);
sb.append(ro.ID+"|");
}
v=mgc.myPlayer.medicVec;
for (int i = 0; i < v.size(); i++) {
RPGObject ro=(RPGObject)v.elementAt(i);
sb.append(ro.ID+"|");
}
dos.writeUTF(sb.toString());
sb.delete(0,sb.length());
v=mgc.myPlayer.skillVec;
for (int i = 0; i < v.size(); i++) {
RPGObject ro=(RPGObject)v.elementAt(i);
sb.append(ro.ID+"|");
}
dos.writeUTF(sb.toString());
dos.close();
byte[] data=baos.toByteArray();
if(rs.getNumRecords()>1){
rs.setRecord(1,data,0,data.length);
}else{
rs.addRecord(data,0,data.length);
}
rs.closeRecordStore();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读取存盘
*
*/
public void loadGame(){
try {
RecordStore rs=RecordStore.openRecordStore("TXWW",false);
byte[] data=rs.getRecord(1);
ByteArrayInputStream bais=new ByteArrayInputStream(data);
DataInputStream dis=new DataInputStream(bais);
if(mgc.curLevel==null){
mgc.curLevel=new Level();
}
mgc.curLevel.levelNo=dis.readInt();
mgc.curLevel.curMapNo=dis.readInt();
mgc.myPlayer.col=dis.readInt();
mgc.myPlayer.row=dis.readInt();
mgc.myPlayer.rate=dis.readInt();
mgc.myPlayer.money=dis.readInt();
mgc.myPlayer.hp=dis.readInt();
mgc.myPlayer.mp=dis.readInt();
mgc.myPlayer.exp=dis.readInt();
mgc.myPlayer.rank=dis.readInt();
mgc.myPlayer.nextExp=dis.readInt();
mgc.myPlayer.attackPoint=dis.readInt();
mgc.myPlayer.defendPoint=dis.readInt();
mgc.myPlayer.curWeaponID=dis.readInt();
mgc.myPlayer.curDefendID=dis.readInt();
mgc.myPlayer.propertyIDs=dis.readUTF();
mgc.myPlayer.skillIDs=dis.readUTF();
dis.close();
rs.closeRecordStore();
LevelLoader.instance.loadLevel(mgc.curLevel);
} catch (Exception e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -