示例#1
0
 /// <summary>
 /// 获取与碰撞器的关系:-1无关 0相交或包括碰撞器 1在碰撞器内部
 /// </summary>
 /// <param name="self"></param>
 /// <param name="trigger"></param>
 /// <param name="position"></param>
 /// <param name="rotation"></param>
 /// <returns></returns>
 public static int GetRelationshipWithTrigger(this AOICell self, AOITriggerComponent trigger, Vector3 position, Quaternion rotation)
 {
     if (trigger.TriggerType == TriggerShapeType.Cube)
     {
         var obb = trigger.GetComponent <OBBComponent>();
         return(AOIHelper.GetGridRelationshipWithOBB(position, rotation, obb.Scale, self.xMax - self.xMin, self.posx, self.posy));
     }
     else
     {
         return(AOIHelper.GetGridRelationshipWithSphere(position, trigger.Radius, self.xMax - self.xMin, self.posx, self.posy));
     }
 }
示例#2
0
        public static async ETTask Init(this AOISceneViewComponent self)
        {
            #region 从XML初始化场景物体信息
            self.DynamicSceneMap = new Dictionary <string, AOISceneViewComponent.DynamicScene>();
            string xmlPath = "GameAssets/Config/Map.xml";
            var    content = await ResourcesComponent.Instance.LoadAsync <TextAsset>(xmlPath);

            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(content.text);
            // 使用 XPATH 获取所有 gameObject 节点
            XmlNodeList xmlNodeList = xmlDocument.LastChild.ChildNodes;
            foreach (XmlNode scene in xmlNodeList)
            {
                var sceneName = scene.Attributes["sceneName"].Value;
                AOISceneViewComponent.DynamicScene item = new AOISceneViewComponent.DynamicScene();
                self.DynamicSceneMap.Add(sceneName, item);
                item.Objects        = new List <AOISceneViewComponent.DynamicSceneObject>();
                item.GridMapObjects = new Dictionary <long, List <AOISceneViewComponent.DynamicSceneObject> >();
                foreach (XmlNode xmlNode in scene.ChildNodes)
                {
                    AOISceneViewComponent.DynamicSceneObject sceneObject = new AOISceneViewComponent.DynamicSceneObject();
                    string gameObjectName = xmlNode.Attributes["objectName"].Value;
                    string objectPath     = xmlNode.Attributes["objectPath"].Value;
                    sceneObject.Path = objectPath;
                    XmlNode positionXmlNode = xmlNode.SelectSingleNode("descendant::position");
                    XmlNode rotationXmlNode = xmlNode.SelectSingleNode("descendant::rotation");
                    XmlNode scaleXmlNode    = xmlNode.SelectSingleNode("descendant::scale");

                    if (positionXmlNode != null && rotationXmlNode != null && scaleXmlNode != null)
                    {
                        sceneObject.Position = new Vector3(float.Parse(positionXmlNode.Attributes["x"].Value),
                                                           float.Parse(positionXmlNode.Attributes["y"].Value), float.Parse(positionXmlNode.Attributes["z"].Value));
                        sceneObject.Rotation = Quaternion.Euler(new Vector3(float.Parse(rotationXmlNode.Attributes["x"].Value),
                                                                            float.Parse(rotationXmlNode.Attributes["y"].Value), float.Parse(rotationXmlNode.Attributes["z"].Value)));
                        sceneObject.Scale = new Vector3(float.Parse(scaleXmlNode.Attributes["x"].Value),
                                                        float.Parse(scaleXmlNode.Attributes["y"].Value), float.Parse(scaleXmlNode.Attributes["z"].Value));
                    }
                    item.Objects.Add(sceneObject);
                    int   x      = (int)Math.Floor(sceneObject.Position.x / self.GridLen);
                    int   y      = (int)Math.Floor(sceneObject.Position.z / self.GridLen);
                    float radius = Mathf.Sqrt(sceneObject.Scale.x * sceneObject.Scale.x + sceneObject.Scale.y * sceneObject.Scale.y + sceneObject.Scale.z * sceneObject.Scale.z) / 2;
                    int   count  = (int)Math.Ceiling(radius / self.GridLen);//环境多加一格
                    for (int i = x - count; i <= x + count; i++)
                    {
                        for (int j = y - count; j <= y + count; j++)
                        {
                            var res = AOIHelper.GetGridRelationshipWithOBB(sceneObject.Position, sceneObject.Rotation,
                                                                           sceneObject.Scale, self.GridLen, i, j);
                            if (res >= 0)
                            {
                                var id = AOIHelper.CreateCellId(i, j);
                                if (!item.GridMapObjects.ContainsKey(id))
                                {
                                    item.GridMapObjects.Add(id, new List <AOISceneViewComponent.DynamicSceneObject>());
                                }
                                item.GridMapObjects[id].Add(sceneObject);
                            }
                        }
                    }
                }
            }
            xmlDocument = null;
            #endregion
        }