Exemplo n.º 1
0
        public override SpellVar Select(SpellCursor cursor)
        {
            Camera  mainCam  = Scene.Current.FindComponent <Camera>();
            Vector3 worldPos = mainCam.GetSpaceCoord(DualityApp.Mouse.Pos);

            var pickedObjects = RigidBody.PickShapesGlobal(worldPos.Xy)
                                .Select(s => new {
                Interactor = s.Parent.GameObj.GetComponent <ICmpSpellInteractor>(),
                Distance   = (s.Parent.GameObj.Transform.Pos.Xy - worldPos.Xy).Length
            })
                                .Where(e => e.Interactor != null)
                                .ToArray();

            float[] scores = Spell.WeightByProximity(pickedObjects.Length, i => pickedObjects[i].Distance);

            if (pickedObjects.Length > 0)
            {
                ObjectVar objVar = new ObjectVar();
                for (int i = 0; i < pickedObjects.Length; i++)
                {
                    objVar.Add(pickedObjects[i].Interactor, scores[i]);
                }
                return(objVar);
            }
            else
            {
                return(new PositionVar
                {
                    Position = worldPos.Xy
                });
            }
        }
Exemplo n.º 2
0
        public override SpellVar Select(SpellCursor cursor)
        {
            // Determine additional selection parameters
            List <Func <ICmpSpellInteractor, float> > predicateList = new List <Func <ICmpSpellInteractor, float> >();

            foreach (SpellGlyph param in cursor.Parameters)
            {
                if (param is ICreationGlyph)
                {
                    predicateList.Add(i => (param as ICreationGlyph).GetRessemblance(i));
                }
            }

            // Select objects
            Vector2 myPos         = cursor.BoundTo.Pos;
            var     pickedObjects = Scene.Current.FindComponents <ICmpSpellInteractor>()
                                    .Select(i => new {
                Interactor = i,
                Distance   = (myPos - i.Pos).Length,
                MatchRate  = predicateList.Aggregate(1.0f, (r, p) => r * p(i))
            })
                                    .Where(e => e.MatchRate > 0.01f && Spell.GetEfficiency(e.Distance) > 0.01f && (e.Interactor as Component).GameObj != (cursor.BoundTo as Component).GameObj)
                                    .ToArray();

            ObjectVar objVar = new ObjectVar();

            float[] scores = Spell.WeightByProximity(pickedObjects.Length, i => pickedObjects[i].Distance, i => pickedObjects[i].MatchRate);
            for (int i = 0; i < pickedObjects.Length; i++)
            {
                objVar.Add(pickedObjects[i].Interactor, scores[i] * pickedObjects[i].MatchRate);
            }
            return(objVar);
        }
Exemplo n.º 3
0
        public AddressModelExtended getItem(int id)
        {
            ObjectVar par = new ObjectVar {
                Name = "?id", Value = id
            };
            DataTable data = dataAccess.ExecuteQuery(
                @"Select a.*, 
							b.name as customer, b.nameInvariant as customerInvariant,
							c.name as country, c.nameInvariant as countryInvariant,
							d.name as city, d.nameInvariant as cityInvariant 
					From `"                     + this.tableName + @"` a 
					LEFT JOIN customers b on b.id = a.customerId
               LEFT JOIN countries c on c.id = a.countryId
               LEFT JOIN cities d on d.id = a.cityId
					Where a.id=?"                    , par);

            if (data.Rows.Count == 0)
            {
                return(null);
            }
            else
            {
                return(ConvertToItem(data.Rows[0]));
            }
        }
Exemplo n.º 4
0
        public override SpellVar Select(SpellCursor cursor)
        {
            Component           host           = cursor.BoundTo as Component;
            ICmpSpellInteractor hostInteractor = (host != null) ? host.GameObj.GetComponent <ICmpSpellInteractor>() : null;

            if (hostInteractor != null)
            {
                ObjectVar obj = new ObjectVar();
                obj.Add(hostInteractor);
                return(obj);
            }
            else
            {
                if (host != null)
                {
                    return(new PositionVar
                    {
                        RelativeTo = (host != null) ? host.GameObj : null,
                        RelativePosition = Vector2.Zero
                    });
                }
                else
                {
                    return(new PositionVar {
                        Position = cursor.BoundTo.Pos
                    });
                }
            }
        }
Exemplo n.º 5
0
        public IEnumerable <PhoneModel> getItems(int customerId)
        {
            ObjectVar par = new ObjectVar {
                Name = "?customerId", Value = customerId
            };
            DataTable data = dataAccess.ExecuteQuery("Select * from `" + this.tableName + "` where customerId=?", par);

            return(ConvertToItems(data));
        }
Exemplo n.º 6
0
        public PhoneModel getItem(int id)
        {
            ObjectVar par = new ObjectVar {
                Name = "?id", Value = id
            };

            DataTable data = dataAccess.ExecuteQuery("Select * from `" + this.tableName + "` where id=?", par);

            if (data.Rows.Count == 0)
            {
                return(null);
            }
            else
            {
                return(ConvertToItem(data.Rows[0]));
            }
        }
Exemplo n.º 7
0
        public IEnumerable <AddressModelExtended> getItems(int customerId)
        {
            ObjectVar par = new ObjectVar {
                Name = "?customerId", Value = customerId
            };
            DataTable data = dataAccess.ExecuteQuery(
                @"Select a.*, 
							b.name as customer, b.nameInvariant as customerInvariant,
							c.name as country, c.nameInvariant as countryInvariant,
							d.name as city, d.nameInvariant as cityInvariant 
					From `"                     + this.tableName + @"` a
					LEFT JOIN customers b on b.id = a.customerId
               LEFT JOIN countries c on c.id = a.countryId
               LEFT JOIN cities d on d.id = a.cityId
					Where customerId=?"                    , par);

            return(ConvertToItems(data));
        }
Exemplo n.º 8
0
        public CityModel getItem(int id)
        {
            ObjectVar par = new ObjectVar {
                Name = "?id", Value = id
            };
            DataTable data = dataAccess.ExecuteQuery(
                @"SELECT a.*,
					 b.name as country, b.nameInvariant as countryInvariant
					FROM `"                     + this.tableName + @"` a
					LEFT JOIN `countries` b ON b.id = a.countryId 
					Where a.id=?"                    , par);

            if (data.Rows.Count == 0)
            {
                return(null);
            }
            else
            {
                return(ConvertToItem(data.Rows[0]));
            }
        }
Exemplo n.º 9
0
        public CustomerModel getItem(int id)
        {
            ObjectVar par = new ObjectVar {
                Name = "?id", Value = id
            };
            DataTable data = dataAccess.ExecuteQuery(
                @"SELECT a.*, 
							c.name as city, c.nameInvariant as cityInvariant,
							b.name as country, b.nameInvariant as countryInvariant
						FROM  "                         + this.tableName + @" a 
						LEFT JOIN countries b on b.id = a.countryId
						LEFT JOIN cities c on c.id = a.cityId
						WHERE id =?"                        , par);

            if (data.Rows.Count == 0)
            {
                return(null);
            }
            else
            {
                return(ConvertToItem(data.Rows[0]));
            }
        }
Exemplo n.º 10
0
        public override bool Activate(SpellCursor cursor)
        {
            float baseEnergy = this.GetEnergyConsumption();
            float usedEnergy = cursor.BoundTo.DrainEnergy(baseEnergy);

            Vector2     boundPos        = cursor.BoundTo.Pos;
            PositionVar posVar          = cursor.GetAnnotation <PositionVar>();
            ObjectVar   objVar          = cursor.GetAnnotation <ObjectVar>();
            ObjectVar   selectedObjects = new ObjectVar();

            if (objVar != null)
            {
                var weightedPos = objVar.Elements.Select(e => new {
                    Weight     = e.Weight,
                    Interactor = e.Interactor,
                    Pos        = e.Interactor.Pos
                });
                foreach (var p in weightedPos)
                {
                    float energyMult = Spell.GetEfficiency((p.Pos - boundPos).Length) * p.Weight / objVar.Count;
                    foreach (T magic in this.Create(p.Pos, usedEnergy * energyMult))
                    {
                        ICmpSpellInteractor interactor = magic.GameObj.GetComponent <ICmpSpellInteractor>();
                        if (interactor != null)
                        {
                            selectedObjects.Add(interactor, energyMult);
                        }
                    }
                }
            }
            else if (posVar != null)
            {
                Vector2 targetPos  = posVar.Position;
                float   energyMult = Spell.GetEfficiency((targetPos - boundPos).Length);
                foreach (T magic in this.Create(posVar.Position, usedEnergy * energyMult))
                {
                    ICmpSpellInteractor interactor = magic.GameObj.GetComponent <ICmpSpellInteractor>();
                    if (interactor != null)
                    {
                        selectedObjects.Add(interactor, energyMult);
                    }
                }
            }
            else
            {
                foreach (T magic in this.Create(boundPos, usedEnergy))
                {
                    ICmpSpellInteractor interactor = magic.GameObj.GetComponent <ICmpSpellInteractor>();
                    if (interactor != null)
                    {
                        selectedObjects.Add(interactor);
                    }
                }
            }

            if (selectedObjects.Count > 0.0f)
            {
                cursor.Annotate(selectedObjects);
            }

            return(true);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Draw an object variable.
        /// <param name="rect">The position to draw the variable.</param>
        /// <param name="objectVar">The object variable to be drawn.</param>
        /// <param name="isAsset">The variable blackboard is an asset?</param>
        /// </summary>
        static void DrawObjectVar(Rect rect, ObjectVar objectVar, bool isAsset)
        {
            // Name
            rect.yMin   += 3f;
            rect.yMax   -= 2f;
            rect.xMin   += 6f;
            rect.xMax   -= 6f;
            rect.height -= c_TwoLinesHeight - c_OneLineHeight;
            DrawName(new Rect(rect.x, rect.y, c_SmallNameWidth, rect.height), objectVar);

            // Value
            rect.xMin += c_SmallNameWidth + c_Space;
            rect.xMax -= c_MinusButtonWidth + c_RightPadding;
            var objectType = objectVar.ObjectType;

            EditorGUI.BeginChangeCheck();
            var newValue = EditorGUI.ObjectField(rect, GUIContent.none, objectVar.Value, objectType, !isAsset);

            if (EditorGUI.EndChangeCheck() && newValue != objectVar.Value)
            {
                // Register undo
                if (objectVar.blackboard != null)
                {
                    #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2
                    Undo.RegisterUndo(objectVar.blackboard, "Variable Value");
                    #else
                    Undo.RecordObject(objectVar.blackboard, "Variable Value");
                    #endif
                }

                // Update variable value
                objectVar.Value = newValue;
                // Set blackboard dirty flag
                if (objectVar.blackboard != null)
                {
                    EditorUtility.SetDirty(objectVar.blackboard);
                }
            }

            // Object Type
            rect.y += rect.height + 3f;
            var objectTypeAsString = objectType.ToString();
            if (GUI.Button(rect, objectTypeAsString, EditorStyles.popup))
            {
                var menu = new GenericMenu();
                // Add None
                menu.AddItem(new GUIContent("None"), string.IsNullOrEmpty(objectTypeAsString), SetObjectType, new SetObjectVarType(objectVar, typeof(UnityEngine.Object)));

                // Add to menu all types that inherites from UnityEngine.Object
                var types = TypeUtility.GetDerivedTypes(typeof(UnityEngine.Object));
                for (int y = 0; y < types.Length; y++)
                {
                    var typeAsString = types[y].ToString();
                    menu.AddItem(new GUIContent(typeAsString.Replace('.', '/')), objectTypeAsString == typeAsString, SetObjectType, new SetObjectVarType(objectVar, types[y]));
                }

                menu.ShowAsContext();
            }

            // Minus button
            rect.x    += rect.width + 2f;
            rect.y    -= rect.height + 3f;
            rect.width = c_MinusButtonWidth;
            rect.yMin -= 2f;
            rect.yMax += 2f;
            if (GUI.Button(rect, s_Styles.iconToolbarMinus, s_Styles.invisbleButton))
            {
                s_VariableToRemove = objectVar;
            }
        }
Exemplo n.º 12
0
 /// <summary>
 /// The class constructor.
 /// <param name= "objectVar">The target ObjectVar.</param>
 /// <param name= "objectType">The new ObjectVar type.</param>
 /// </summary>
 public SetObjectVarType(ObjectVar objectVar, System.Type objectType)
 {
     this.objectVar  = objectVar;
     this.objectType = objectType;
 }
Exemplo n.º 13
0
        public override bool Activate(SpellCursor cursor)
        {
            int baseEnergy = 25;

            // Determine target position
            Vector2 targetPos = cursor.BoundTo.Pos;

            {
                List <SpellVar> selection = new List <SpellVar>();
                foreach (SpellGlyph param in cursor.Parameters)
                {
                    if (param is SelectionGlyph)
                    {
                        SpellVar selectionVar = (param as SelectionGlyph).Select(cursor);
                        if (selectionVar != null)
                        {
                            selection.Add(selectionVar);
                        }
                    }
                }
                PositionVar posVar = selection.OfType <PositionVar>().FirstOrDefault();
                ObjectVar   objVar = selection.OfType <ObjectVar>().FirstOrDefault();
                if (posVar != null)
                {
                    targetPos = posVar.Position;
                }
                else if (objVar != null && objVar.Count > 0.0f)
                {
                    Vector2 acc = Vector2.Zero;
                    foreach (var e in objVar.Elements)
                    {
                        acc += e.Weight * (e.Interactor as Component).GameObj.Transform.Pos.Xy;
                    }
                    targetPos = acc / objVar.Count;
                }
            }


            // Move object
            {
                ObjectVar objVar = cursor.GetAnnotation <ObjectVar>();
                if (objVar != null)
                {
                    var moveObj = objVar.Elements.Select(e => new {
                        Weight     = e.Weight,
                        Interactor = e.Interactor,
                        Transform  = (e.Interactor as Component).GameObj.Transform,
                        Body       = (e.Interactor as Component).GameObj.GetComponent <RigidBody>()
                    });
                    foreach (var p in moveObj)
                    {
                        Vector2 targetDir = (targetPos - p.Transform.Pos.Xy);
                        float   distance  = targetDir.Length;
                        targetDir /= MathF.Max(distance, 1.0f);

                        float efficiency = Spell.GetEfficiency((p.Transform.Pos.Xy - cursor.BoundTo.Pos).Length);

                        bool negativeForce = this.IsNegated;
                        if (p.Weight < 0.0f)
                        {
                            negativeForce = !negativeForce;
                        }
                        float targetSpeed;
                        if (negativeForce)
                        {
                            if (distance <= 1.0f)
                            {
                                distance  = 1.0f;
                                targetDir = MathF.Rnd.NextVector2();
                            }
                            targetSpeed = -15.0f * (1.0f - MathF.Min(distance / 500.0f, 1.0f));
                        }
                        else
                        {
                            targetSpeed = 15.0f * MathF.Min(distance / 500.0f, 1.0f);
                        }

                        Vector2 targetVel         = targetDir * targetSpeed;
                        Vector2 targetVelChange   = (targetVel - p.Body.LinearVelocity);
                        float   targetForceFactor = targetVelChange.Length;
                        Vector2 targetVelDir      = targetVelChange / MathF.Max(0.01f, targetForceFactor);

                        float requestedEnergy =
                            (baseEnergy * MathF.Abs(p.Weight) / (float)objVar.Count) *
                            MathF.Clamp(targetForceFactor * p.Body.Mass / 150.0f, 0.0f, 1.0f);
                        float usedEnergy = cursor.BoundTo.DrainEnergy(requestedEnergy);

                        p.Body.ApplyWorldImpulse(5.0f * efficiency * targetVelDir * usedEnergy);
                    }
                }
            }

            return(true);
        }