Commit ff2aa1bb by 李进峰

add follow player

parent a0a9bad4
......@@ -19,13 +19,13 @@ public class GameEnv implements Serializable{
private static final long serialVersionUID = 1L;
public Map<String, SuperClass> npcClassIndex;
public Map<String, NpcInstance> npcInstanceIndex;
public Map<String, NpcInstance> followNpcIndex;
public Map<String, SuperClass> goodsClassIndex;
public Map<String, GoodsInstance> goodsInstanceIndex;
public Map<String, Place> placeIndex;
public byte[][] mapGraph;
public Map<String, Integer>mapNames;
public String[] mapIndexName;
public GameEnv(String botName) throws Exception {
// 通过文件来初始化游戏环境
// String fullName = "test.json";
......@@ -35,6 +35,7 @@ public class GameEnv implements Serializable{
String jsonString = DBUtil.queryEnvJsonByBotName(botName);
Map<String, Object> gameProjectMap = JSON.parseObject(jsonString);
System.out.println(gameProjectMap);
this.followNpcIndex = new HashMap<String, NpcInstance>();
if(!gameProjectMap.isEmpty()) {
// 地图
Map<String, Object> map = (Map<String, Object>)gameProjectMap.get("map");
......@@ -147,6 +148,9 @@ public class GameEnv implements Serializable{
attr.setName(instanceAttrKey);
attr.setValue((String)instanceAttrMap.get(instanceAttrKey));
npcInstanceAttrs.add(attr);
if(instanceAttrKey.equals("跟随玩家")&&((String)instanceAttrMap.get(instanceAttrKey)).equals("是")) {
this.followNpcIndex.put(npcInstance.getName(), npcInstance);
}
}
}
npcInstance.setAttrs(npcInstanceAttrs);
......@@ -299,20 +303,28 @@ public class GameEnv implements Serializable{
}
public Integer isNeighbour(String placeA, String placeB) {
if(placeA.equals(placeB)) {
return 0;
}else {
try {
int x = this.mapNames.get(placeA);
int y = this.mapNames.get(placeB);
if(this.mapGraph[x][y] == 1) {
try {
int x = this.mapNames.get(placeA);
if(placeA.equals(placeB) || this.placeIndex.get(placeA).getNickname().indexOf(placeB)>=0) {
return 0;
}else {
int flag = 0;
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;
}else {
return 2;
}
}catch(Exception e) {
return -1;
}
}catch(Exception e) {
return 2;
}
}
// 两点之间的路径 // 使用广度优先算法单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm。
......@@ -339,7 +351,7 @@ public class GameEnv implements Serializable{
placeLinkNodeSecond.setIndex(i);
placeLinkNodeSecond.setPlaceLink(placeLinkNode);
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) {
paths.add(this.mapIndexName[placeLinkNodeSecond.getIndex()]);
placeLinkNodeSecond = placeLinkNodeSecond.getPlaceLink();
......@@ -358,6 +370,7 @@ public class GameEnv implements Serializable{
}
public List<String> queryPathBySite(String placeA, String placeB){
List<String> paths = this.getAllPath(placeA, placeB);
System.out.println(paths);
List<String> path = new ArrayList<String>();
if(paths.size()>1) {
for(int i=0; i<paths.size(); i++) {
......@@ -610,6 +623,15 @@ public class GameEnv implements Serializable{
List<Attr> attrs = npcInstance.getAttrs();
for(int j=0; j<attrs.size(); j++) {
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);
return true;
}
......@@ -634,4 +656,13 @@ public class GameEnv implements Serializable{
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