Пример #1
0
        //  Picking item in inventory
        // Что быстрее: передавать больше данных через аргументы или производить более долгий поиск?
        public void PickItem(SceneObject sceneObject, SceneModel scene, Interaction interaction, List <Responce> responces)//(int id,int sceneId, List<Responce> responces)
        {
            //var interacton = level.Interactions.FirstOrDefault(i => i.TargetObjectId == sceneObject.Id);
            int inventoryItemId = interaction.TargetInventoryObjectId.Value;
            var inventoryObj    = level.InventoryObjects.FirstOrDefault(io => io.Id == inventoryItemId);

            if (userInventory.FirstOrDefault(ui => ui.Id == inventoryItemId) == null)
            {
                userInventory.Add(level.InventoryObjects.FirstOrDefault(io => io.Id == inventoryItemId));
            }
            else
            {
                responces.Add(new Responce {
                    interactionType = -1
                });                                                   // изменил 1 на -1 потому что ничего же не произойдет
                return;
            }
            var responce = new Responce()
            {
                interactionType = 0,
                oldId           = sceneObject.Id,
                newId           = inventoryItemId,
                Name            = inventoryObj.Name,
                ImageUrl        = inventoryObj.ImageUrl
            };

            if (!inventoryObj.IsInfinite)
            {
                scene.SceneObjects.Remove(sceneObject);
                responce.Action = 0;
            }
            else
            {
                responce.Action = 2;
            }
            responces.Add(responce);
        }
Пример #2
0
        //Do action or sequence of actions
        public void DoActions(SceneObject sceneObject, SceneModel scene, Interaction interacton, List <Responce> responces)
        {
            var associatedSceneObject = scene.SpawnedSceneObjects.FirstOrDefault(sso => sso.Id == interacton.InteractingObjectId);

            if (interacton.InteractingObjectId != sceneObject.Id)
            {
                //and interacting objects exists
                if (interacton.InteractingObjectId.HasValue)
                {
                    var responce = new Responce()
                    {
                        interactionType = 2,
                        oldId           = sceneObject.Id,
                        newId           = associatedSceneObject.Id,
                        Name            = associatedSceneObject.Name,
                        Description     = associatedSceneObject.Description,
                        x            = associatedSceneObject.x,
                        y            = associatedSceneObject.y,
                        z            = associatedSceneObject.z,
                        ImageUrl     = associatedSceneObject.ImageUrl,
                        Action       = 1,
                        isValid      = true,
                        isSpawn      = interacton.IsSpawn,
                        SpecialEvent = interacton.SpecialEvent
                    };
                    responces.Add(responce);
                    scene.SpawnedSceneObjects.Add(sceneObject);
                    scene.SceneObjects.Add(associatedSceneObject);
                    if (!interacton.IsSpawn)
                    {
                        scene.SceneObjects.Remove(sceneObject);
                    }
                }
                else
                {
                    var responce = new Responce()
                    {
                        interactionType = 2,
                        Action          = 0,
                        isValid         = true
                    };
                    responces.Add(responce);
                    scene.SceneObjects.Remove(sceneObject);
                }
            }
            else
            //if interaction does not change object, but does some events
            if (interacton.SpecialEvent != null)
            {
                var responce = new Responce()
                {
                    interactionType = 2,
                    Action          = 1,
                    isValid         = true,
                    isSpawn         = interacton.IsSpawn,
                    SpecialEvent    = interacton.SpecialEvent
                };
                responces.Add(responce);
            }
            if (interacton.NextInteractionId.HasValue)
            {
                interacton = level.Interactions.FirstOrDefault(i => i.Id == interacton.NextInteractionId);
                int newId = interacton.TargetObjectId ?? default(int);
                sceneObject = scene.SceneObjects.FirstOrDefault(so => so.Id == newId);
                ; DoActions(sceneObject, scene, interacton, responces);
            }
        }