RPG游戏-刷怪系统

news/2024/12/28 18:14:15/

一、数据定义

(一)、刷怪规则定义:

在这里插入图片描述

namespace Common.Data
{public class SpawnRuleDefine{public int ID { get; set; }public int MapID { get; set; }public int SpawnMonID { get; set; }public int SpawnLevel{ get;set; }public SPAWN_TYPE SpawnType { get; set; }public int SpawnPoint { get; set; }public int SpawnPoints { get; set; }public float SpawnPeriod { get; set; }}
}

(二)、DataManger读取EXCEL配置的信息

public class DataManager : Singleton<DataManager>
{public Dictionary<int,Dictionary<int, SpawnPointDefine>> SpawnPoints = null;json = File.ReadAllText(this.DataPath + "SpawnRuleDefine.txt");this.SpawnPoints = JsonConvert.DeserializeObject<Dictionary<int,Dictionary<int, SpawnPointDefine>>>(json);
}

(三)、刷怪点定义

namespace Common.Data
{public class SpawnPointDefine{public int ID { get; set; }//刷怪点IDpublic int MapID { get; set;}public NVector3 Position { get; set; }//位置public NVector3 Direction { get; set; }//朝向}
}

(四)、导出地图中刷怪点的位置信息脚本

 [MenuItem("Map Tools/Export SpawnPoints")]public static void ExportSpawnPoints(){DataManager.Instance.Load();Scene current = EditorSceneManager.GetActiveScene();string currentScene = current.name;if (current.isDirty){EditorUtility.DisplayDialog("提示", "请先保存当前场景", "确定");return;}if (DataManager.Instance.SpawnPoints == null)DataManager.Instance.SpawnPoints = new Dictionary<int, Dictionary<int, SpawnPointDefine>>();foreach (var map in DataManager.Instance.Maps){string sceneFile = "Assets/Scenes/" + map.Value.Resource + ".unity";if (!System.IO.File.Exists(sceneFile)){Debug.LogWarningFormat("Scene {0} not existed!", sceneFile);continue;}EditorSceneManager.OpenScene(sceneFile, OpenSceneMode.Single);SpawnPoint[] SpawnPoints = GameObject.FindObjectsOfType<SpawnPoint>();if (DataManager.Instance.SpawnPoints.ContainsKey(map.Value.ID)){DataManager.Instance.SpawnPoints[map.Value.ID] = new Dictionary<int, SpawnPointDefine>();}foreach (var sp in SpawnPoints){if (!DataManager.Instance.SpawnPoints[map.Value.ID].ContainsKey(sp.ID)){DataManager.Instance.SpawnPoints[map.Value.ID][sp.ID] = new SpawnPointDefine();}SpawnPointDefine def = DataManager.Instance.SpawnPoints[map.Value.ID][sp.ID];def.ID = sp.ID;def.MapID = map.Value.ID;def.Position = GameObjectTool.WorldToLogicN(sp.transform.position);def.Direction = GameObjectTool.WorldToLogicN(sp.transform.forward);}}DataManager.Instance.SaveSpawnPoints();EditorSceneManager.OpenScene("Assets/Scenes/" + currentScene + ".unity");EditorUtility.DisplayDialog("提示", "刷怪点导出完成", "确定");}

二、服务器逻辑

需要有刷怪管理器和怪物管理器,并在Map类中对其进行初始化

1、怪物管理器


namespace GameServer.Managers
{class MonsterManager{private Map Map;public Dictionary<int, Monster> Monsters = new Dictionary<int, Monster>();public void Init(Map map){this.Map = map;}internal Monster Create(int spawnMonID,int spawnLevel,NVector3 position,NVector3 direction){Monster monster = new Monster(spawnMonID, spawnLevel, position, direction);EntityManager.Instance.AddEntity(this.Map.ID, monster);monster.Info.Id = monster.entityId;monster.Info.Mapid = this.Map.ID;Monsters[monster.Id] = monster;this.Map.MonsterEnter(monster);//怪物进入场景 通知客户端刷新怪物return monster;}}
}

2.刷怪管理器

刷怪管理器会根据配置来更新不同地图的刷怪任务

namespace GameServer.Managers
{class SpawnManager{private List<Spawner> Rules = new List<Spawner>();private Map Map;public void Init(Map map){this.Map = map;if (DataManager.Instance.SpawnRules.ContainsKey(map.Define.ID)){foreach(var define in DataManager.Instance.SpawnRules[map.Define.ID].Values){this.Rules.Add(new Spawner(define, this.Map));}}}public void Update(){if (Rules.Count == 0) return;for(int i = 0; i < this.Rules.Count; i++){this.Rules[i].Update();}}}
}

3.刷怪器

    class Spawner{public SpawnRuleDefine Define { get; set; }private Map Map;//刷新时间private float spawnTime = 0;//消失时间private float unspawnTime = 0;private bool spawned = false;//刷怪点位置信息 根据客户端刷怪点信息脚本获取private SpawnPointDefine spawnPoint = null;public Spawner(SpawnRuleDefine define,Map map){this.Define = define;this.Map = map;if (DataManager.Instance.SpawnPoints.ContainsKey(this.Map.ID)){spawnPoint = DataManager.Instance.SpawnPoints[this.Map.ID][this.Define.SpawnPoint];}else{Log.ErrorFormat("SpawnRule[{0}] SpawnPoint[{1}] not existed", this.Define.ID, this.Define.SpawnPoint);}}public void Update(){if (this.CanSpawn()){this.spawn();}}private void spawn(){this.spawned = true;Log.InfoFormat("Map[{0}] Spawn[{1}:Mon:{2},Lv:{3}] at Point:{4}",this.Define.MapID, this.Define.ID, this.Define.SpawnMonID, this.Define.SpawnLevel, this.spawnPoint);//创建怪物this.Map.MonsterManager.Create(this.Define.SpawnMonID, this.Define.SpawnLevel,this.spawnPoint.Position,this.spawnPoint.Direction);}private bool CanSpawn(){if (this.spawned) return false;//上次消失时间+周期 是否大于当前时间if (this.unspawnTime + this.Define.SpawnPeriod > Time.time) return false;return true;}}

4.修改地图管理器

在CharacterEnter方法中加入怪物信息,并通知客户端

 class Map{/// <summary>/// 怪物进入地图/// </summary>/// <param name="character"></param>//广播怪物信息internal void MonsterEnter(Monster monster){Log.InfoFormat("MonsterEnter: Map:{0} monsterId:{1}", this.Define.ID, monster.Id);foreach (var kv in this.MapCharacters){this.AddCharacterEnterMap(kv.Value.connection, monster.Info);}}void AddCharacterEnterMap(NetConnection<NetSession> conn, NCharacterInfo character){if (conn.Session.Response.mapCharacterEnter == null){conn.Session.Response.mapCharacterEnter = new MapCharacterEnterResponse();conn.Session.Response.mapCharacterEnter.mapId = this.Define.ID;}conn.Session.Response.mapCharacterEnter.Characters.Add(character);conn.SendResponse();}//单个角色进入时、也需要添加怪物信息internal void CharacterEnter(NetConnection<NetSession> conn, Character character){//角色进入地图响应conn.Session.Response.mapCharacterEnter = new MapCharacterEnterResponse();conn.Session.Response.mapCharacterEnter.mapId = this.Define.ID;//添加怪物信息foreach (var kv in this.MonsterManager.Monsters){conn.Session.Response.mapCharacterEnter.Characters.Add(kv.Value.Info);}//发送角色进入消息conn.SendResponse();}}

http://www.ppmy.cn/news/578817.html

相关文章

Python之初级RPG小游戏

在国外网站上找到一个练习Python的小游戏感觉不错&#xff0c;自己实现了一下。 通过该练习你能学到&#xff1a; 元组字典简单定义函数和封装条件控制语句 游戏说明 以下是3个房间和1个花园&#xff1a; Hall 客厅 有一把钥匙&#xff0c;Kitchen 厨房 有一只怪物&#xf…

RPG类型游戏—1

现在中国游戏市场出现了好多那种中国类型的RPG类型的游戏&#xff0c;我自己也有几份那样的代码&#xff0c;在看他们代码的时候&#xff0c;觉得他们的代码编写有一个通病&#xff0c;就是写的太过于臃肿不够简洁&#xff0c;代码的移植性也不是很高&#xff0c;如果做同一类型…

RPG小游戏

#导包 import time import random 与学姐的台词 SCRIPT_NPC_SCHOOL_SISTER [你好&#xff01;,你好!,你是新生吗&#xff1f;,是的,想要我教你魔法吗&#xff1f;,\n1、好的 \\n2、不用了吧&#xff0c;我不和学姐学魔法&#xff01;\n]世界里的魔法清单 MAGIC_BOOKS [ [火…

RPG游戏-任务系统

一、前置部分 &#xff08;一&#xff09;、协议部分 enum QUEST_STATUS {IN_PROGRESS 0;//已接受&#xff0c;未完成COMPLATED 1;//已完成&#xff0c;未提交FINISHED 2;//已完成&#xff0c;已提交FAILED 3;//已失败 }enum QUEST_LIST_TYPE {ALL 0;IN_PROGRESS 1;FI…

Java实现RPG游戏

一&#xff0e;实验目的 掌握面向对象程序设计的方法。 明确类与对象的概念&#xff0c;掌握面向对象设计七大原则&#xff1b;掌握常见的设计模式以及类图的描述。 二、UML类图 三、实验要求 1.功能描述几乎所有的RPG游戏&#xff08;一种源自《龙与地下城》的游戏类型&…

RPG

J2ME RPG游戏边学边做&#xff08;一&#xff09;   笔者以前是做j2ee的&#xff0c;一个月前由于兴趣所致开始利用业余时间学习j2me游戏开发。在网上看了一通教程后&#xff0c;便准备动手一边学一边做一个简单的rpg游戏。虽然起点比较高&#xff0c;但感觉游戏难度越大&…

rpg游戏发展史计算机网络,PC Gamer盘点史上最经典RPG游戏TOP15

RPG角色演类游戏往往能带给玩家史诗般的剧情和难忘的战斗体验&#xff0c;今天外媒PC Gamer盘点了史上最经典RPG游戏TOP15&#xff0c;让我们一起来看看吧&#xff01; 第 2 页 史上最经典15大RPG 2 6.《质量效应2(Mass Effect 2)》 发行时间: 2010年 发行商: BioWare 与第一…

堪比端游 欧美十大RPG网页游戏推荐

分享一下我老师大神的人工智能教程&#xff01;零基础&#xff0c;通俗易懂&#xff01;http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章。分享知识&#xff0c;造福人民&#xff0c;实现我们中华民族伟大复兴&#xff01; 你喜欢RPG游戏吗&#xff1f;大多数玩家的…