Ejemplo n.º 1
0
        private static Actor ConvertFromActorJson(ActorJson source)
        {
            Actor result = null;

            if (source.ActorType == "Monster")
            {
                var breed           = BreedFactory.Instance.Breeds[source.Properties["BreedName"].ToString()];
                var positionJObject = (JObject)source.Properties["Position"];
                var position        = new VectorBase((int)positionJObject["x"], (int)positionJObject["y"]);

                var healthJObject = (JObject)source.Properties["Health"];
                var health        = new Stat((int)healthJObject["Current"], (int)healthJObject["Max"]);

                var generation = int.Parse(source.Properties["Generation"].ToString());

                var monster = new Monster(null, breed, position.x, position.y, (int)health.Max, generation);

                monster.Health = health;

                result = monster;
            }
            else
            {
                throw new ApplicationException("Have not implemented peristance for actor type: " + source.ActorType);
            }

            return(result);
        }
Ejemplo n.º 2
0
        private static ActorJson ConvertToActorJson(Actor source)
        {
            var result = new ActorJson();

            result.Properties = new Dictionary <string, object>();

            if (source is Monster)
            {
                var monster = source as Monster;

                result.ActorType = "Monster";
                result.Properties.Add("BreedName", monster.Breed.Name);
                result.Properties.Add("Position", source.Position);
                result.Properties.Add("Health", monster.Health);
                result.Properties.Add("Generation", monster.Generation);
                result.Properties.Add("Fear", monster.Fear);
                result.Properties.Add("MonsterState", monster.State.GetType().Name);
            }
            else if (source is Hero)
            {
                var hero = source as Hero;
                result.ActorType = "Hero";

                var ignoreProperties = new List <string> {
                    "Equipment", "Inventory", "HeroClass", "Behavior", "Game", "Appearance"
                };

                foreach (var propInfo in hero.GetType().GetProperties())
                {
                    if (ignoreProperties.Contains(propInfo.Name) == false)
                    {
                        if (propInfo.CanWrite && propInfo.GetSetMethod(/*nonPublic*/ true).IsPublic)
                        {
                            // The setter exists and is public.
                            result.Properties.Add(propInfo.Name, propInfo.GetValue(hero, null));
                        }
                    }
                }
            }
            else
            {
                throw new ApplicationException("Have not implemented peristance for actor type: " + source.GetType());
            }

            return(result);
        }