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

📄 xmlscriptparser.java

📁 j2me游戏引擎 j2me游戏引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	/**
	 * 使用逗号分割字符串为数组
	 * @param s 被分割的字符串
	 * @return 分割后的数组
	 */
	private String[] splitByComma(String s){
		String[] result=StringExtension.objectArrayBatchToStringArray(
				StringExtension.split(new StringBuffer(s),
						",",StringExtension.STRING_ARRAY,false));
		return result;
	}
	

	/**
	 * 取出指定元素的属性,构造相应的对象,以列表的方式返回
	 * @param elementName 元素名称
	 * @param elementAttributes 元素属性名称数组
	 * @param elementType 元素所说明的游戏对象的类型 
	 * @param associatedTableArray 与本游戏对象关联的游戏对象列表,供本游戏对象查找并作关联
	 * @param isOrdinal 是否按顺序读取xml
	 * @return 装载游戏对象列表(包括游戏对象或者作为游戏对象列表-如消息队列)
	 */
	public GameObjectQueue readGameObjectConfigure(String elementName,
			String[] elementAttributes,int elementType,
			GameObjectQueue[] associatedTableArray,boolean isOrdinal){
		
		GameObjectQueue resultTable=null;
		GameObject go=null;
		GameObject findGo=null;
		GameObjectQueue findGq=null;
		String id=null;
		String objectIDList=null;
		
		//读取元素属性值,返回包含多个属性的值集合的Vector
		Vector AttributesValueSet=readElement(elementName,elementAttributes,isOrdinal);
		
		if ((AttributesValueSet!=null)&&(AttributesValueSet.size()>0)){
			//处理每个属性值集合
			for(int i=0;i<AttributesValueSet.size();i++){
				Vector attrValue=(Vector)AttributesValueSet.elementAt(i);
				switch(elementType){
				//对于没有关联子列表的属性值集合,直接构造对象
				case EVENT_OBJECT:
					if (resultTable==null){
						resultTable=new GameObjectQueue();
					}
					go=new Event();
					go.loadProperties(attrValue);
					System.out.println("被装载的游戏对象:"+go);
					resultTable.put(go.getId(),go);
					break;
				case MESSAGE_OBJECT:
					if (resultTable==null){
						resultTable=new GameObjectQueue();
					}
					go=new Message();
					go.loadProperties(attrValue);
					System.out.println("被装载的游戏对象:"+go);
					resultTable.put(go.getId(),go);
					break;
				case PROPERTY_OBJECT:
					if (resultTable==null){
						resultTable=new GameObjectQueue();
					}
					go=new Property();
					go.loadProperties(attrValue);
					System.out.println("被装载的游戏对象:"+go);
					resultTable.put(go.getId(),go);
					break;
				case LAYER_OBJECT:
					if (resultTable==null){
						resultTable=new GameObjectQueue();
					}
					go=new SimpleLayer();
					go.loadProperties(attrValue);
					System.out.println("被装载的游戏对象:"+go);
					resultTable.put(go.getId(),go);
					break;
				case TRANSFORMER_OBJECT:
					if (resultTable==null){
						resultTable=new GameObjectQueue();
					}
					go=new MapTransformer();
					go.loadProperties(attrValue);
					System.out.println("被装载的游戏对象:"+go);
					resultTable.put(go.getId(),go);
					break;
				case CAMERA_OBJECT:
					if (resultTable==null){
						resultTable=new GameObjectQueue();
					}
					go=new Camera();
					go.loadProperties(attrValue);
					System.out.println("被装载的游戏对象:"+go);
					resultTable.put(go.getId(),go);
					break;
				case MUSIC_OBJECT:
					if (resultTable==null){
						resultTable=new Musician();
					}
					go=new Music();
					go.loadProperties(attrValue);
					System.out.println("被装载的游戏对象:"+go);
					resultTable.put(go.getId(),go);
					break;
				//对于带有关联子列表的属性值集合,需要进行关联
				case EVENTQUEUE_OBJECT:
					if (resultTable==null){
						resultTable=new EventQueue();
					}
				case MESSAGEQUEUE_OBJECT:
					if (resultTable==null){
						resultTable=new MessageQueue();
					}
				case PROPERTYBOX_OBJECT:
					if (resultTable==null){
						resultTable=new PropertyManager();
					}
					//获得ID
					id=(String)attrValue.elementAt(0);
					//获得其关联的对象的ID列表
					objectIDList=(String)attrValue.elementAt(1);
					//对其关联的对象进行关联
					if (objectIDList.length()>0){
						GameObjectQueue gq=new GameObjectQueue();
						//将关联的对象的ID列表分离为数组
						String[] objectIDSet=splitByComma(objectIDList);
						//在关联对象的列表中查找与关联的对象的ID对应的对象,并作关联
						for(int j=0;j<objectIDSet.length;j++){
							findGo=(GameObject)associatedTableArray[0].find(objectIDSet[j]);
							if (findGo!=null){
								gq.put(objectIDSet[j],findGo);
								gq.setId(id);
								resultTable.put(id,gq);
							}
						}
					}
					break;
				case NPC_OBJECT:
					if (resultTable==null){
						resultTable=new GameObjectQueue();
					}
					go=new NPC();
					go.loadProperties(attrValue);
					NPC npc=(NPC)go;
					//查找关联对象列表中与关联对象ID相对应的对象,即与本NPC对应的事件列表
					findGq=(GameObjectQueue)associatedTableArray[0].find((String)attrValue.elementAt(15));
					if (findGo!=null){
						npc.setEventQueue((EventQueue)findGq);
					}
					else{
						npc.setEventQueue(new EventQueue());
					}
					//查找关联对象列表中与关联对象ID相对应的对象,即与本NPC对应的道具列表
					findGq=(GameObjectQueue)associatedTableArray[1].find((String)attrValue.elementAt(16));
					if (findGq!=null){
						npc.setPropertyBox((PropertyManager)findGq);
					}
					else{
						npc.setPropertyBox(new PropertyManager());
					}
					npc.setType(Integer.parseInt((String)attrValue.elementAt(17)));
					System.out.println("被装载的游戏对象:"+go);
					resultTable.put(npc.getId(),npc);
					break;
				case ACTOR_OBJECT:
					if (resultTable==null){
						resultTable=new GameObjectQueue();
					}
					go=new Actor();
					go.loadProperties(attrValue);
					Actor actor=(Actor)go;
					//查找关联对象列表中与关联对象ID相对应的对象,即与本Actor对应的道具列表
					findGq=(GameObjectQueue)associatedTableArray[0].find((String)attrValue.elementAt(14));
					if (findGq!=null){
						actor.setPropertyBox((PropertyManager)findGq);
					}
					else{
						actor.setPropertyBox(new PropertyManager());
					}
					System.out.println("被装载的游戏对象:"+go);
					resultTable.put(actor.getId(),actor);
					break;
				case MAP_OBJECT:
					if (resultTable==null){
						resultTable=new GameObjectQueue();
					}
					go=new SimpleMap();
					go.loadProperties(attrValue);
					SimpleMap simpleMap=(SimpleMap)go;
					
					int attrID=4;
					int associatedTableID=0;
					
					for(int m=attrID;m<attrValue.size();m++){
						//获得Layer和npc、maptransformer
						//获得其关联的对象的ID列表
						objectIDList=(String)attrValue.elementAt(attrID);
						//对其关联的对象进行关联
						if (objectIDList.length()>0){
							GameObjectQueue gq=new GameObjectQueue();
							//将关联的对象的ID列表分离为数组
							String[] objectIDSet=splitByComma(objectIDList);
							//在关联对象的列表中查找与关联的对象的ID对应的对象,并作关联
							for(int j=0;j<objectIDSet.length;j++){
								findGo=(GameObject)associatedTableArray[associatedTableID].find(objectIDSet[j]);
								if (findGo!=null){
									gq.put(objectIDSet[j],findGo);
									gq.setId(id);
								}
							}
							switch(attrID){
							case 4:
								simpleMap.setLayerSet(gq);
								break;
							case 5:
								simpleMap.setNpcSet(gq);
								break;
							case 6:
								simpleMap.setMapLink(gq);
								break;
							}
						}
						attrID++;
						associatedTableID++;
					}
					System.out.println("被装载的游戏对象:"+go);
					resultTable.put(go.getId(),go);
					break;
				case LEVEL_OBJECT:
					if (resultTable==null){
						resultTable=new GameObjectQueue();
					}
					go=new SimpleLevel();
					go.loadProperties(attrValue);
					SimpleLevel level=(SimpleLevel)go;
					//获得map队列
					GameObjectQueue gq=new GameObjectQueue();
					//将关联的对象的ID列表分离为数组
					String[] objectIDSet=splitByComma((String)attrValue.elementAt(3));
					//在关联对象的列表中查找与关联的对象的ID对应的对象,并作关联
					for(int j=0;j<objectIDSet.length;j++){
						findGo=(GameObject)associatedTableArray[0].find(objectIDSet[j]);
						if (findGo!=null){
							gq.put(objectIDSet[j],findGo);
						}
					}
					level.setMapSet(gq);
					System.out.println("被装载的游戏对象:"+go);
					resultTable.put(go.getId(),go);
					break;
				}
				
					
				
			}
			return resultTable;
		}
		else{
			return new GameObjectQueue();
		}
	}
	
	/**
	 * 取出全局配置元素的属性,返回游乐场要使用的运行时间间隔
	 * @param elementName 元素名称
	 * @param elementAttributes 元素属性名称数组
	 * @param isOrdinal 是否按顺序读取xml
	 * @return 游乐场要使用的运行时间间隔
	 */
	public int readGlobalConfigureForCarnieRunInterval(String elementName,
			String[] elementAttributes,boolean isOrdinal){
		
		//读取元素属性值,返回包含多个属性的值集合的Vector
		Vector AttributesValueSet=readElement(elementName,elementAttributes,isOrdinal);
		int result=10;
		if ((AttributesValueSet!=null)&&(AttributesValueSet.size()>0)){
			//处理每个属性值集合
			Vector attrValue=(Vector)AttributesValueSet.elementAt(0);
			result= Integer.valueOf((String)attrValue.elementAt(0)).intValue();	
		}
		return result;
	}
	
	public GameObjectQueue readMsgConfigure(boolean isOrdinal){
		return readGameObjectConfigure(msgName,msgAttributes,MESSAGE_OBJECT,null,isOrdinal);
	}
	
	public GameObjectQueue readMsgQueueConfigure(GameObjectQueue[] associatedTableArray,boolean isOrdinal){
		return readGameObjectConfigure(msgQueueName,msgQueueAttributes,MESSAGEQUEUE_OBJECT,associatedTableArray,isOrdinal);
	}
	
	public GameObjectQueue readEventConfigure(boolean isOrdinal){
		return readGameObjectConfigure(eventName,eventAttributes,EVENT_OBJECT,null,isOrdinal);
	}
	
	public GameObjectQueue readEventQueueConfigure(GameObjectQueue[] associatedTableArray,boolean isOrdinal){
		return readGameObjectConfigure(eventQueueName,eventQueueAttributes,EVENTQUEUE_OBJECT,associatedTableArray,isOrdinal);
	}
	
	public GameObjectQueue readPropertyConfigure(boolean isOrdinal){
		return readGameObjectConfigure(propertyName,propertyAttributes,PROPERTY_OBJECT,null,isOrdinal);
	}
	
	public GameObjectQueue readPropertyBoxConfigure(GameObjectQueue[] associatedTableArray,boolean isOrdinal){
		return readGameObjectConfigure(propBoxName,propBoxAttributes,PROPERTYBOX_OBJECT,associatedTableArray,isOrdinal);
	}
	
	public GameObjectQueue readNpcConfigure(GameObjectQueue[] associatedTableArray,boolean isOrdinal){
		return readGameObjectConfigure(npcName,npcAttributes,NPC_OBJECT,associatedTableArray,isOrdinal);
	}
	
	public GameObjectQueue readActorConfigure(GameObjectQueue[] associatedTableArray,boolean isOrdinal){
		return readGameObjectConfigure(actorName,actorAttributes,ACTOR_OBJECT,associatedTableArray,isOrdinal);
	}
	
	public GameObjectQueue readLayerConfigure(boolean isOrdinal){
		return readGameObjectConfigure(layerName,layerAttributes,LAYER_OBJECT,null,isOrdinal);
	}
	
	public GameObjectQueue readTransformerConfigure(boolean isOrdinal){
		return readGameObjectConfigure(transformerName,transformerAttributes,TRANSFORMER_OBJECT,null,isOrdinal);
	}
	
	public GameObjectQueue readMapConfigure(GameObjectQueue[] associatedTableArray,boolean isOrdinal){
		return readGameObjectConfigure(mapName,mapAttributes,MAP_OBJECT,associatedTableArray,isOrdinal);
	}
	
	public GameObjectQueue readLevelConfigure(GameObjectQueue[] associatedTableArray,boolean isOrdinal){
		return readGameObjectConfigure(levelName,levelAttributes,LEVEL_OBJECT,associatedTableArray,isOrdinal);
	}
	
	public GameObjectQueue readCameraConfigure(boolean isOrdinal){
		return readGameObjectConfigure(cameraName,cameraAttributes,CAMERA_OBJECT,null,isOrdinal);
	}
	
	public GameObjectQueue readMusicConfigure(boolean isOrdinal){
		return readGameObjectConfigure(musicName,musicAttributes,MUSIC_OBJECT,null,isOrdinal);
	}
	
	public int readCarnieRunInterval(boolean isOrdinal){
		return readGlobalConfigureForCarnieRunInterval(globalName,globalAttributes,isOrdinal);
	}
	
	public static void main(String[] argv){
		XmlScriptParser x=new XmlScriptParser();
		x.openConfigure("/configure/config.xml");
		GameObjectQueue msgTable=x.readMsgConfigure(true);
		System.out.println("msg数量="+msgTable.size());
		GameObjectQueue mqTable=x.readMsgQueueConfigure(new GameObjectQueue[]{msgTable},true);
		System.out.println("msg Queue数量="+mqTable.size());
		
		GameObjectQueue eventTable=x.readEventConfigure(true);
		System.out.println("event数量="+eventTable.size());
		GameObjectQueue eqTable=x.readEventQueueConfigure(new GameObjectQueue[]{eventTable},true);
		System.out.println("event Queue数量="+eqTable.size());
		
		GameObjectQueue propTable=x.readPropertyConfigure(true);
		System.out.println("property数量="+propTable.size());
		GameObjectQueue propBoxTable=x.readPropertyBoxConfigure(new GameObjectQueue[]{propTable},true);
		System.out.println("propertyBox数量="+propBoxTable.size());
		
		GameObjectQueue npcTable=x.readNpcConfigure(new GameObjectQueue[]{propTable,eventTable},true);
		System.out.println("npc数量="+npcTable.size()+" npc[npc01].name="+(NPC)npcTable.get("npc01"));
		
		GameObjectQueue actorTable=x.readActorConfigure(new GameObjectQueue[]{propTable},true);
		System.out.println("actor数量="+actorTable.size());
		
		GameObjectQueue layerTable=x.readLayerConfigure(true);
		System.out.println("layer数量="+layerTable.size());
		
		GameObjectQueue transformerTable=x.readTransformerConfigure(true);
		System.out.println("transformer数量="+transformerTable.size());
		
		GameObjectQueue mapTable=x.readMapConfigure(new GameObjectQueue[]{layerTable,npcTable,transformerTable},true);
		System.out.println("map数量="+mapTable.size());
		
		GameObjectQueue levelTable=x.readLevelConfigure(new GameObjectQueue[]{mapTable},true);
		System.out.println("level数量="+levelTable.size());
		
		GameObjectQueue cameraTable=x.readCameraConfigure(true);
		System.out.println("camera数量="+cameraTable.size());
		
		GameObjectQueue musicTable=x.readMusicConfigure(true);
		System.out.println("music数量="+musicTable.size());
		System.out.println("Carnie运行间隔="+x.readCarnieRunInterval(true));
//		//地图元素名称
//		String mapName="map";
//		//地图元素属性
//		String[] mapAttributes={"id","width","height",
//				"layerREF","npcREF","maptransformerREF"};
//		Vector AttributesValueSet=x.readElement(mapName,mapAttributes,false);
//		System.out.println(AttributesValueSet.size());
//		//音乐元素名称
//		String musicName="music";
//		//音乐元素属性
//		String[] musicAttributes={"id","resURL","musicType",
//				"playModel","loopNumber"};
//		AttributesValueSet=x.readElement(musicName,musicAttributes,false);
	}
}

⌨️ 快捷键说明

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