Commit ff2aa1bb by 李进峰

add follow player

parent a0a9bad4
...@@ -19,13 +19,13 @@ public class GameEnv implements Serializable{ ...@@ -19,13 +19,13 @@ public class GameEnv implements Serializable{
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public Map<String, SuperClass> npcClassIndex; public Map<String, SuperClass> npcClassIndex;
public Map<String, NpcInstance> npcInstanceIndex; public Map<String, NpcInstance> npcInstanceIndex;
public Map<String, NpcInstance> followNpcIndex;
public Map<String, SuperClass> goodsClassIndex; public Map<String, SuperClass> goodsClassIndex;
public Map<String, GoodsInstance> goodsInstanceIndex; public Map<String, GoodsInstance> goodsInstanceIndex;
public Map<String, Place> placeIndex; public Map<String, Place> placeIndex;
public byte[][] mapGraph; public byte[][] mapGraph;
public Map<String, Integer>mapNames; public Map<String, Integer>mapNames;
public String[] mapIndexName; public String[] mapIndexName;
public GameEnv(String botName) throws Exception { public GameEnv(String botName) throws Exception {
// 通过文件来初始化游戏环境 // 通过文件来初始化游戏环境
// String fullName = "test.json"; // String fullName = "test.json";
...@@ -35,6 +35,7 @@ public class GameEnv implements Serializable{ ...@@ -35,6 +35,7 @@ public class GameEnv implements Serializable{
String jsonString = DBUtil.queryEnvJsonByBotName(botName); String jsonString = DBUtil.queryEnvJsonByBotName(botName);
Map<String, Object> gameProjectMap = JSON.parseObject(jsonString); Map<String, Object> gameProjectMap = JSON.parseObject(jsonString);
System.out.println(gameProjectMap); System.out.println(gameProjectMap);
this.followNpcIndex = new HashMap<String, NpcInstance>();
if(!gameProjectMap.isEmpty()) { if(!gameProjectMap.isEmpty()) {
// 地图 // 地图
Map<String, Object> map = (Map<String, Object>)gameProjectMap.get("map"); Map<String, Object> map = (Map<String, Object>)gameProjectMap.get("map");
...@@ -147,6 +148,9 @@ public class GameEnv implements Serializable{ ...@@ -147,6 +148,9 @@ public class GameEnv implements Serializable{
attr.setName(instanceAttrKey); attr.setName(instanceAttrKey);
attr.setValue((String)instanceAttrMap.get(instanceAttrKey)); attr.setValue((String)instanceAttrMap.get(instanceAttrKey));
npcInstanceAttrs.add(attr); npcInstanceAttrs.add(attr);
if(instanceAttrKey.equals("跟随玩家")&&((String)instanceAttrMap.get(instanceAttrKey)).equals("是")) {
this.followNpcIndex.put(npcInstance.getName(), npcInstance);
}
} }
} }
npcInstance.setAttrs(npcInstanceAttrs); npcInstance.setAttrs(npcInstanceAttrs);
...@@ -299,20 +303,28 @@ public class GameEnv implements Serializable{ ...@@ -299,20 +303,28 @@ public class GameEnv implements Serializable{
} }
public Integer isNeighbour(String placeA, String placeB) { public Integer isNeighbour(String placeA, String placeB) {
if(placeA.equals(placeB)) { try {
return 0; int x = this.mapNames.get(placeA);
}else { if(placeA.equals(placeB) || this.placeIndex.get(placeA).getNickname().indexOf(placeB)>=0) {
try { return 0;
int x = this.mapNames.get(placeA); }else {
int y = this.mapNames.get(placeB); int flag = 0;
if(this.mapGraph[x][y] == 1) { for(int j=0;j<this.mapGraph[x].length;j++) {
if(this.mapGraph[x][j] ==1) {
if(this.mapIndexName[j].equals(placeB) || this.placeIndex.get(this.mapIndexName[j]).getNickname().indexOf(placeB)>=0) {
flag = 1;
break;
}
}
}
if(flag == 1) {
return 1; return 1;
}else { }else {
return 2; return 2;
} }
}catch(Exception e) {
return -1;
} }
}catch(Exception e) {
return 2;
} }
} }
// 两点之间的路径 // 使用广度优先算法单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm。 // 两点之间的路径 // 使用广度优先算法单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm。
...@@ -339,7 +351,7 @@ public class GameEnv implements Serializable{ ...@@ -339,7 +351,7 @@ public class GameEnv implements Serializable{
placeLinkNodeSecond.setIndex(i); placeLinkNodeSecond.setIndex(i);
placeLinkNodeSecond.setPlaceLink(placeLinkNode); placeLinkNodeSecond.setPlaceLink(placeLinkNode);
List<String> nickname = this.placeIndex.get(this.mapIndexName[i]).getNickname(); List<String> nickname = this.placeIndex.get(this.mapIndexName[i]).getNickname();
if(this.mapIndexName[i].equals(placeB) || (nickname!=null && nickname.indexOf(placeB)>0)) { if(this.mapIndexName[i].equals(placeB) || (nickname!=null && nickname.indexOf(placeB)>=0)) {
while(placeLinkNodeSecond.getPlaceLink() != null) { while(placeLinkNodeSecond.getPlaceLink() != null) {
paths.add(this.mapIndexName[placeLinkNodeSecond.getIndex()]); paths.add(this.mapIndexName[placeLinkNodeSecond.getIndex()]);
placeLinkNodeSecond = placeLinkNodeSecond.getPlaceLink(); placeLinkNodeSecond = placeLinkNodeSecond.getPlaceLink();
...@@ -358,6 +370,7 @@ public class GameEnv implements Serializable{ ...@@ -358,6 +370,7 @@ public class GameEnv implements Serializable{
} }
public List<String> queryPathBySite(String placeA, String placeB){ public List<String> queryPathBySite(String placeA, String placeB){
List<String> paths = this.getAllPath(placeA, placeB); List<String> paths = this.getAllPath(placeA, placeB);
System.out.println(paths);
List<String> path = new ArrayList<String>(); List<String> path = new ArrayList<String>();
if(paths.size()>1) { if(paths.size()>1) {
for(int i=0; i<paths.size(); i++) { for(int i=0; i<paths.size(); i++) {
...@@ -610,6 +623,15 @@ public class GameEnv implements Serializable{ ...@@ -610,6 +623,15 @@ public class GameEnv implements Serializable{
List<Attr> attrs = npcInstance.getAttrs(); List<Attr> attrs = npcInstance.getAttrs();
for(int j=0; j<attrs.size(); j++) { for(int j=0; j<attrs.size(); j++) {
if(names[3].equals(attrs.get(j).getName())) { if(names[3].equals(attrs.get(j).getName())) {
if(names[3].equals("跟随玩家")) {
if(value.equals("是")) {
this.followNpcIndex.put(npcInstance.getName(), npcInstance);
}else if(value.equals("否")) {
this.followNpcIndex.remove(npcInstance.getName());
}else {
continue;
}
}
attrs.get(j).setValue(value); attrs.get(j).setValue(value);
return true; return true;
} }
...@@ -634,4 +656,13 @@ public class GameEnv implements Serializable{ ...@@ -634,4 +656,13 @@ public class GameEnv implements Serializable{
return false; return false;
} }
} }
// 更新npc的位置
public boolean updateFollowerLocation(String location) {
Place place = this.placeIndex.get(location);
for(String instanceKey:this.followNpcIndex.keySet()) {
this.followNpcIndex.get(instanceKey).setPlace(place);
}
return true;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment