Esempio n. 1
0
        // The public builder method
        public static List <EventSubject <GameEntityModel> > Build(Storage.Character charData, int[] subjectIds)
        {
            List <EventSubject <GameEntityModel> > subjects = new List <EventSubject <GameEntityModel> >(subjectIds == null ? 1 : subjectIds.Length);

            // Build default subjects
            subjects.Add(BuildDefaultSubject(GetSelf));
            if (subjectIds == null)
            {
                return(subjects);
            }
            int numDefaultSubjects = subjects.Count;

            // Build custom subjects
            EventSubject <GameEntityModel> subject;

            foreach (int subjectId in subjectIds)
            {
                subject = BuildFromParameter(charData.genericParameters[subjectId], numDefaultSubjects);
                if (subject != null)
                {
                    subjects.Add(subject);
                }
            }
            return(subjects);
        }
Esempio n. 2
0
        private static ConditionalEvent <GameEntityModel> ReadEvent(Storage.Character charData, Storage.GenericEvent storageEvent, out int keyFrame, Storage.CharacterAnimation animation)
        {
            // Build event
            List <EventSubject <GameEntityModel> >   subjects;
            List <EventCondition <GameEntityModel> > conditions;
            List <EventAction <GameEntityModel> >    actions;

            subjects   = CharacterSubjectsBuilder.Build(charData, storageEvent.subjectIds);
            conditions = CharacterConditionsBuilder.Build(charData, storageEvent.conditionIds, out keyFrame, animation);
            actions    = CharacterActionsBuilder.Build(charData, storageEvent.eventIds);
            return(new ConditionalEvent <GameEntityModel>(subjects, conditions, actions));
        }
Esempio n. 3
0
        // Save
        public void SaveCharacter()
        {
            // Convert to storage format
            Storage.Character storageCharacter = character.SaveToStorage();

            // Open file stream and serialize it
            FileInfo            charFile   = new FileInfo(charactersDataPath + storageCharacter.name + dataExtension);
            FileStream          charStream = charFile.Open(FileMode.Create, FileAccess.Write);
            RbStorageSerializer serializer = new RbStorageSerializer();

            serializer.Serialize(charStream, storageCharacter);
            charStream.Close();
        }
Esempio n. 4
0
        // Load
        public void LoadCharacter(string characterName)
        {
            // Open file stream and deserialize it
            FileInfo            charFile   = new FileInfo(charactersDataPath + characterName + dataExtension);
            FileStream          charStream = charFile.OpenRead();
            RbStorageSerializer serializer = new RbStorageSerializer();

            Storage.Character storageCharacter = serializer.Deserialize(charStream, null, typeof(Storage.Character)) as Storage.Character;
            charStream.Close();
            // Load character into editor character format
            character = Character.LoadFromStorage(storageCharacter);
            Reset();
        }
Esempio n. 5
0
        // The public builder method
        public static List <EventAction <GameEntityModel> > Build(Storage.Character charData, int[] eventIds)
        {
            if (eventIds == null || eventIds.Length == 0)
            {
                return(new List <EventAction <GameEntityModel> >());
            }
            List <EventAction <GameEntityModel> > actions = new List <EventAction <GameEntityModel> >(eventIds.Length);
            EventAction <GameEntityModel>         action;

            foreach (int eventId in eventIds)
            {
                action = BuildFromParameter(charData.genericParameters[eventId]);
                if (action != null)
                {
                    actions.Add(action);
                }
            }
            return(actions);
        }
        // The public builder method
        public static List <EventCondition <GameEntityModel> > Build(Storage.Character charData, int[] conditionIds, out int keyFrame, Storage.CharacterAnimation animation)
        {
            List <EventCondition <GameEntityModel> > conditions = new List <EventCondition <GameEntityModel> >(conditionIds.Length);
            EventCondition <GameEntityModel>         condition;

            keyFrame = InvalidKeyframe;
            int conditionKeyFrame;

            foreach (int conditionId in conditionIds)
            {
                condition = BuildFromParameter(charData.genericParameters[conditionId], out conditionKeyFrame, animation);
                if (condition != null)
                {
                    conditions.Add(condition);
                }
                else if (conditionKeyFrame != InvalidKeyframe)
                {
                    keyFrame = conditionKeyFrame;
                }
            }
            return(conditions);
        }
Esempio n. 7
0
        private static void SetupCharacter(Storage.Character charData)
        {
            string charName = charData.name;

            // Register animation view for this character
            AnimationView view = new AnimationView();

            AnimationsVCPool.Instance.SetDefaultView(charName, view);

            // Setup each animation
            AnimationController controller;
            ConditionalEvent <GameEntityModel> animEvent;
            int keyFrame;

            foreach (Storage.CharacterAnimation animation in charData.animations)
            {
                // Register controller for this animation
                controller = new AnimationController();
                AnimationsVCPool.Instance.RegisterController(charName, animation.name, controller);

                // Setup animation events
                if (animation.events != null)
                {
                    foreach (Storage.GenericEvent e in animation.events)
                    {
                        animEvent = ReadEvent(charData, e, out keyFrame, animation);
                        if (keyFrame != CharacterConditionsBuilder.InvalidKeyframe)
                        {
                            controller.AddKeyframeEvent((uint)keyFrame, animEvent);
                        }
                        else
                        {
                            controller.AddGeneralEvent(animEvent);
                        }
                    }
                }


                // Setup frame data
                // WARNING TODO: pool of boxes and pool of HitData
                FrameData[] framesData = new FrameData[animation.numFrames];
                FrameData   frameData;

                // Collisions
                if (animation.collisionBoxes != null)
                {
                    Storage.Box storageBox;
                    int         boxIndex;
                    // For each box
                    Storage.CollisionBox storageCollisionBox;
                    for (int collisionId = 0; collisionId < animation.collisionBoxes.Length; ++collisionId)
                    {
                        storageCollisionBox = animation.collisionBoxes[collisionId];
                        // for each frame of each box
                        for (int frame = 0; frame < storageCollisionBox.boxIds.Length; ++frame)
                        {
                            boxIndex = storageCollisionBox.boxIds[frame];
                            if (boxIndex != invalidBoxId)
                            {
                                storageBox = charData.boxes[boxIndex];
                                frameData  = GetFrameData(framesData, frame);
                                frameData.collisions.Add(new CollisionBox(new Box(storageBox.pointOne, storageBox.pointTwo), collisionId));
                            }
                        }         // each frame
                    }             // each storageCollisionBox
                }

                // Hits
                if (animation.hitBoxes != null)
                {
                    Storage.Box storageBox;
                    Storage.GenericParameter param;
                    HitData hitData;
                    int     boxIndex;
                    // For each box
                    Storage.HitBox storageHitBox;
                    for (int hitId = 0; hitId < animation.hitBoxes.Length; ++hitId)
                    {
                        storageHitBox = animation.hitBoxes[hitId];
                        param         = charData.genericParameters[storageHitBox.paramId];
                        hitData       = CharacterHitsBuilder.Build(param);
                        if (hitData == null)
                        {
                            continue;
                        }
                        hitData.hitboxID = hitId;
                        // for each frame of each box
                        for (int i = 0; i < storageHitBox.boxIds.Length; ++i)
                        {
                            boxIndex = storageHitBox.boxIds[i];
                            if (boxIndex != invalidBoxId)
                            {
                                storageBox = charData.boxes[boxIndex];
                                frameData  = GetFrameData(framesData, i);
                                frameData.hits.Add(new HitBox(new Box(storageBox.pointOne, storageBox.pointTwo), hitData));
                            }
                        }         // each frame
                    }             // each storageHitBox
                }

                // Precompute bounding boxes
                foreach (FrameData finalFramedata in framesData)
                {
                    if (finalFramedata != null)
                    {
                        finalFramedata.ComputeBoundingBoxes();
                    }
                }
                // Store frames data on animation controller
                controller.SetFramesData(framesData);
            }

            // Note: for now storage character data is stored locally,
            // Skins and anchor names accessed through storage data
            // TODO: Perhaps store skins and anchor names separatedly, maybe somewhere else,
            // instead of keeping storage data in memory..
        }
Esempio n. 8
0
        // Load Character view model, and portrait
        public static GameObject LoadViewModel(string characterName, string prefabName)
        {
            string[]          pathItems        = prefabName.Split(prefabDelimiter.ToCharArray());
            Storage.Character storageCharacter = null;
            if (characterName != null)
            {
                loadedCharacters.TryGetValue(characterName, out storageCharacter);
            }
            string skinName;

            if (pathItems != null && pathItems.Length > 1)
            {
                string url = "file://" + charactersModelsPath + pathItems[0];
                WWW    www = WWW.LoadFromCacheOrDownload(url, 1);
                if (www.assetBundle == null)
                {
                    Debug.LogError("Failed to load character bundle at " + url);
                }

                // TODO: HAMMER TIME: workaround to Unity bug on textures
                // Refresh all material shaders
                Material[] materials = www.assetBundle.LoadAllAssets <Material>();
                foreach (Material m in materials)
                {
                    string shaderName = m.shader.name;
                    Shader newShader  = Shader.Find(shaderName);
                    if (newShader != null)
                    {
                        m.shader = newShader;
                    }
                }

                // Load model prefab from bundle
                GameObject prefab = www.assetBundle.LoadAsset <GameObject>(pathItems[1]);

                // Load portrait from bundle
                // TODO: refactor: duplicate code
                if (storageCharacter != null)
                {
                    int index = Array.FindIndex <string>(storageCharacter.viewModels, x => x.Equals(prefabName));
                    if (index >= 0)
                    {
                        string spriteName = storageCharacter.portraits[index];
                        if (!loadedPortraitSprites.ContainsKey(spriteName))
                        {
                            Sprite sprite = www.assetBundle.LoadAsset <Sprite>(spriteName);
                            loadedPortraitSprites[spriteName] = sprite;
                        }
                    }
                }

                www.assetBundle.Unload(false);
                if (prefab != null)
                {
                    return(prefab);
                }
                skinName = pathItems[1];
            }
            else
            {
                skinName = prefabName;
            }

            // Load portrait from resources
            // TODO: refactor: duplicate code
            if (storageCharacter != null)
            {
                int index = Array.FindIndex <string>(storageCharacter.viewModels, x => x.Equals(pathItems[1]));
                if (index >= 0)
                {
                    string spriteName = storageCharacter.portraits[index];
                    if (!loadedPortraitSprites.ContainsKey(spriteName))
                    {
                        Sprite sprite = Resources.Load <Sprite>(spriteName);
                        loadedPortraitSprites[spriteName] = sprite;
                    }
                }
            }

            // Load skin from resources
            return(Resources.Load <GameObject>(skinName));
        }