예제 #1
0
        private void Collide(ISceneObject s1, ISceneObject s2)
        {
            //TODO: Destroyed meteor must generate tiny meteors
            if (s1.GetType() == typeof(Bullet) && s2.GetType() == typeof(Meteor))
            {
                if (s1.Circle.Contains(s2.Circle))
                {
                    RemoveSceneObject(s1);
                    RemoveSceneObject(s2);
                }
            }
            else if (s1.GetType() == typeof(Meteor) && s2.GetType() == typeof(Bullet))
            {
                if (s1.Circle.Contains(s2.Circle))
                {
                    RemoveSceneObject(s1);
                    RemoveSceneObject(s2);
                }
            }

            if ((s1.GetType() == typeof(Ship) && s2.GetType() == typeof(Meteor)) || (s1.GetType() == typeof(Meteor) && s2.GetType() == typeof(Ship)))
            {
                if (s1.Circle.Contains(s2.Circle))
                {
                    Game.Exit();
                }
            }
        }
 public void DoCollideWith(ISceneObject other, float tpf)
 {
     if (!other.GetType().IsAssignableFrom(typeof(SingularityBullet)))
     {
         (me as MinorAsteroid).ResetLastHit();
     }
 }
예제 #3
0
        private void OnEnable()
        {
            ISceneObject sceneObject  = target as ISceneObject;
            FieldInfo    fieldInfoObj = sceneObject.GetType().GetField("uniqueName", BindingFlags.NonPublic | BindingFlags.Instance);
            string       uniqueName   = fieldInfoObj.GetValue(sceneObject) as string;

            if (string.IsNullOrEmpty(uniqueName))
            {
                sceneObject.SetSuitableName();
            }
        }
        public static void SendNewObject(ServerMgr serverMgr, ISceneObject obj)
        {
            if (!(obj is ISendable))
            {
                Logger.Error("Trying to send " + obj.GetType().Name + " but it is not ISendable");
                return;
            }

            NetOutgoingMessage msg = serverMgr.CreateNetMessage();

            (obj as ISendable).WriteObject(msg);
            serverMgr.BroadcastMessage(msg);
        }
예제 #5
0
        /// <summary>
        /// ihned prida objekt do sceny
        /// </summary>
        private void DirectAttachToScene(ISceneObject obj)
        {
            if (obj.Id == -1)
            {
                Logger.Error("Trying to add object " + obj.GetType().Name + " to scene, but it has uninitialized id -> skipped!");
                return;
            }

            for (int i = idsToRemove.Count - 1; i >= 0; i--)
            {
                if (obj.Id == idsToRemove[i])
                {
                    idsToRemove.RemoveAt(i);
                    return;
                }
            }

            if (obj.GetGeometry() == null && !(obj is IEmpty))
            {
                Logger.Warn("Trying to add geometry object to scene, but it is null -> skipped!");
                return;
            }

            objects.Add(obj);

            BeginInvoke(new Action(() =>
            {
                if (obj is IHeavyWeightSceneObject)
                {
                    GetCanvas().Children.Add((obj as IHeavyWeightSceneObject).HeavyWeightGeometry);
                }
                else if (obj is ParticleEmmitor)
                {
                    (obj as ParticleEmmitor).Init(GetParticleArea());
                }
                else if (!(obj is IEmpty))
                {
                    area.Add(obj.GetGeometry(), obj.Category);
                }
            }));
        }
예제 #6
0
        /// <summary>
        /// Serializes a scene object.
        /// </summary>
        /// <param name="obj"></param>
        public static string SerializeObject(ISceneObject obj)
        {
            string result  = "";
            Type   objType = obj.GetType();

            result += "o " + obj.Id + " " + objType.FullName + "\n";

            FieldInfo[]    fields     = objType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Where(f => f.IsDefined(typeof(SceneData), false)).ToArray();
            PropertyInfo[] properties = objType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Where(p => p.IsDefined(typeof(SceneData), false)).ToArray();
            foreach (PropertyInfo property in properties)
            {
                if (property.PropertyType.IsGenericType)
                {
                    IList list = (IList)property.GetValue(obj);
                    foreach (object item in list)
                    {
                        result += "p e " + property.Name + " " + ToString(item) + "\n";
                    }
                    continue;
                }
                result += "p " + property.Name + " " + ToString(property.GetValue(obj)) + "\n";
            }
            foreach (FieldInfo field in fields)
            {
                if (field.FieldType.IsGenericType)
                {
                    IList list = (IList)field.GetValue(obj);
                    foreach (object item in list)
                    {
                        result += "f e " + field.Name + " " + ToString(item) + "\n";
                    }
                    continue;
                }
                result += "f " + field.Name + " " + ToString(field.GetValue(obj)) + "\n";
            }

            return(result);
        }
예제 #7
0
        /// <summary>
        /// Loads a scene file.
        /// </summary>
        /// <param name="path"></param>
        public static void Load(string path)
        {
            sceneResources = new List <ISceneResource>();
            sceneObjects   = new List <ISceneObject>();

            string[] raw = File.ReadAllLines(path);

            string[] res  = raw.Where(line => line[0] == 'r').ToArray();
            string[] objs = raw.Where(line => line[0] == 'o').ToArray();

            for (int i = 0; i < res.Length; i++)
            {
                string[] tokens = res[i].Split(' ');

                Type resType = Type.GetType(tokens[2]);

                if (resType == typeof(Mesh))
                {
                    Mesh mesh = Mesh.Load(tokens[3]);
                    sceneResources.Add(mesh);
                }
                else
                if (resType == typeof(Texture))
                {
                    Texture texture = Texture.LoadFromFile(tokens[3], (TextureFilteringMode)Enum.Parse(typeof(TextureFilteringMode), tokens[4]));
                    sceneResources.Add(texture);
                }
                else
                if (resType == typeof(Shader))
                {
                    Shader shader = new Shader(tokens[3], (ShaderType)Enum.Parse(typeof(ShaderType), tokens[4]));
                    sceneResources.Add(shader);
                }
            }

            for (int i = 0; i < objs.Length; i++)
            {
                string[] tokens = objs[i].Split(' ');

                Type   objType = Type.GetType(tokens[2]);
                object obj     = Activator.CreateInstance(objType);
                sceneObjects.Add((ISceneObject)obj);
            }

            //Interpret file
            ISceneObject currentSceneObject = null;

            for (int i = 0; i < raw.Length; i++)
            {
                string[] tokens = raw[i].Split(' ');

                switch (tokens[0])
                {
                default:
                    break;

                //Set object
                case "o":
                {
                    int id = int.Parse(tokens[1]);
                    currentSceneObject = sceneObjects[id];
                }
                break;

                //Set field
                case "f":
                {
                    if (tokens[1] != "e")
                    {
                        FieldInfo field = currentSceneObject.GetType().GetField(tokens[1], BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                        object    value = GetFieldOrPropertyValue(tokens);
                        field.SetValue(currentSceneObject, value);
                    }
                    else
                    {
                        FieldInfo field = currentSceneObject.GetType().GetField(tokens[2], BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                        IList     list  = (IList)field.GetValue(currentSceneObject);
                        object    value = GetFieldOrPropertyValue(tokens, true);
                        list.Add(value);
                        field.SetValue(currentSceneObject, list);
                    }
                }
                break;

                //Set property
                case "p":
                {
                    if (tokens[1] != "e")
                    {
                        PropertyInfo property = currentSceneObject.GetType().GetProperty(tokens[1], BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                        object       value    = GetFieldOrPropertyValue(tokens);
                        property.SetValue(currentSceneObject, value);
                    }
                    else
                    {
                        PropertyInfo property = currentSceneObject.GetType().GetProperty(tokens[2], BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                        IList        list     = (IList)property.GetValue(currentSceneObject);
                        object       value    = GetFieldOrPropertyValue(tokens, true);
                        list.Add(value);
                        property.SetValue(currentSceneObject, list);
                    }
                }
                break;
                }
            }

            //Finalize
            foreach (ISceneObject obj in sceneObjects)
            {
                if (obj is GameObject gameObject)
                {
                    foreach (Component component in gameObject.components)
                    {
                        component.Start();
                    }
                    GameObject.Add(gameObject);
                }
                if (obj is Drawable drawable)
                {
                    foreach (Shader shader in drawable.usedShaders)
                    {
                        drawable.AddShaderAfterLoad(shader);
                    }
                    drawable.Finish();
                }
            }


            object GetFieldOrPropertyValue(string[] tokens, bool element = false)
            {
                object value = null;
                int    idAdd = 0;

                if (element)
                {
                    idAdd = 1;
                }
                switch (tokens[2 + idAdd][0])
                {
                default:
                {
                    value = ScenePropertyConverter.FromSceneProperty(tokens[2 + idAdd]);
                }
                break;

                case 'o':
                {
                    int id = int.Parse(tokens[2 + idAdd].Substring(1));
                    value = sceneObjects[id];
                }
                break;

                case 'r':
                {
                    int id = int.Parse(tokens[2 + idAdd].Substring(1));
                    value = sceneResources[id];
                }
                break;
                }
                return(value);
            }
        }
예제 #8
0
        /// <summary>
        /// Finds all scene objects and resources in the root object recursively.
        /// </summary>
        /// <param name="root"></param>
        static void AddAllObjectsAndResources(ISceneObject root)
        {
            if (alreadyDefinedObjects.Contains(root))
            {
                return;
            }
            alreadyDefinedObjects.Add(root);
            Type type = root.GetType();

            FieldInfo[]    fields     = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Where(field => field.IsDefined(typeof(SceneData), false)).ToArray();
            PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Where(property => property.IsDefined(typeof(SceneData), false)).ToArray();

            //Search all fields
            foreach (FieldInfo info in fields)
            {
                object value = info.GetValue(root);
                AddObjectFromInfo(value);
            }

            //Search all properties
            foreach (PropertyInfo info in properties)
            {
                object value = info.GetValue(root);
                AddObjectFromInfo(value);
            }

            void AddObjectFromInfo(object value)
            {
                //If the value is a scene object
                if (value is ISceneObject sceneObject)
                {
                    sceneObjects.Add(sceneObject);
                    AddAllObjectsAndResources(sceneObject);
                }
                else

                //If the value is a list of scene objects
                if (value is IEnumerable <ISceneObject> objectList)
                {
                    foreach (ISceneObject so in objectList)
                    {
                        sceneObjects.Add(so);
                        AddAllObjectsAndResources(so);
                    }
                }
                else

                //If the value is a scene resource
                if (value is ISceneResource resource)
                {
                    sceneResources.Add(resource);
                }
                else

                //If the value is list of scene resources
                if (value is IEnumerable <ISceneResource> resourceList)
                {
                    foreach (ISceneResource sr in resourceList)
                    {
                        sceneResources.Add(sr);
                    }
                }
            }
        }
예제 #9
0
    private IEnumerator Run()
    {
        while (true)
        {
            if (State != EState.Stop)
            {
                if (State == EState.Move)
                {
                    OnMove();
                }

                if (Input.GetKeyDown(KeyCode.Space))
                {
                    State = EState.Confire;
                }

                _lastTouch = null;
                if (_touchSceneObjects.Count > 0)
                {
                    _lastTouch = _touchSceneObjects[0];
                }

                if (State == EState.Confire)
                {
                    if (_lastTouch != null)
                    {
                        Anim.Play(MoveIdle, true);
                    }
                    else
                    {
                        State = EState.Move;
                    }
                }

                if (_lastTouch != null)
                {
                    switch (_lastTouch.GetType())
                    {
                    case ESceneObjectType.Link:
                    {
                        if (State == EState.Confire)
                        {
                            var link = _lastTouch as SceneLink;
                            link.ImageSwitch_Link.SetImage(1);
                            yield return(new WaitForSeconds(0.3f));

                            gameObject.transform.position = link.TargeLink.transform.position;
                            yield return(new WaitForSeconds(0.1f));

                            link.ImageSwitch_Link.SetImage(0);
                            SceneSwitchManager.Instance.Mask.DOFade(1, 0.3f);
                            yield return(new WaitForSeconds(0.3f));

                            SceneSwitchManager.Instance.Mask.DOFade(0, 0.3f);
                            SceneSwitchManager.Instance.SetScene(link.TargeScene);
                            SceneSwitchManager.Instance.SceneMap[SceneSwitchManager.Instance.CurrentScene].FollowObj.transform.position = gameObject.transform.position;
                            yield return(new WaitForSeconds(0.3f));
                        }
                    }
                    break;

                    case ESceneObjectType.Item:
                    {
                        var item = _lastTouch as SceneItem;

                        switch (item.TiggerType)
                        {
                        case ESceneTriggerType.Confire:
                        {
                            if (State == EState.Confire)
                            {
                                Show.gameObject.SetActive(!item.IsHideModel);
                                Anim.Play(MoveIdle, true);
                                yield return(item.TriggerPlot.Run());

                                item.Get();
                                Show.gameObject.SetActive(true);
                                CharaterData.CollectItemCount++;
                            }
                        }
                        break;

                        case ESceneTriggerType.Auto:
                        {
                            Show.gameObject.SetActive(!item.IsHideModel);
                            Anim.Play(MoveIdle, true);
                            yield return(item.TriggerPlot.Run());

                            // Show.gameObject.SetActive(true);
                            item.Get();
                        }
                        break;
                        }
                    }
                    break;
                    }

                    if (State == EState.Confire)
                    {
                        State = EState.Move;
                    }
                }
            }



            yield return(null);
        }
    }