Пример #1
0
        public static Node ContructLineWall(Room room, Vector2R start, Vector2R end, int thickness,
                                            Dictionary <dynamic, dynamic> props = null, bool addToWallGroup = true)
        {
            float dist       = Vector2R.Distance(start, end);
            int   halfheight = (int)(dist / 2);
            int   halfwidth  = thickness / 2;
            float angle      = VMath.VectorToAngle(start - end);

            Node    n = new Node(room, props, ShapeType.Polygon);
            Polygon p = (Polygon)n.body.shape;

            n.body.orient = angle;
            p.SetBox(halfwidth, halfheight, false);
            n.body.pos = (start + end) / 2;
            n.body.DrawPolygonCenter = false;


            n.body.SetStatic();
            if (addToWallGroup)
            {
                room.MasterGroup.childGroups["Wall Group"].IncludeEntity(n);
                n.OnSpawn();
            }
            return(n);
        }
Пример #2
0
        public Node SelectNodeAt(Vector2R pos)
        {
            Node  found           = null;
            float shortedDistance = Int32.MaxValue;

            for (int i = MasterGroup.fullSet.Count - 1; i >= 0; i--)
            {
                Node n = MasterGroup.fullSet.ElementAt(i);
                // find node that has been clicked, starting from the most recently placed nodes
                float distsquared = Vector2R.DistanceSquared(n.body.pos, pos);
                if (distsquared < n.body.radius * n.body.radius)
                {
                    if (distsquared < shortedDistance)
                    {
                        found           = n;
                        shortedDistance = distsquared;
                    }
                }
            }
            return(found);
        }
Пример #3
0
        internal void Resize(Vector2R resizeVect, bool fillWithGrid = false)
        {
            _pendingRoomResize = () => {
                WorldWidth  = (int)resizeVect.X;
                WorldHeight = (int)resizeVect.Y;
                int newCellsX  = WorldWidth / GridsystemAffect.cellWidth;
                int gridHeight = fillWithGrid ? WorldHeight : OrbIt.ScreenHeight;
                GridsystemAffect = new GridSystem(this, newCellsX, new Vector2R(0, WorldHeight - gridHeight),
                                                  WorldWidth,
                                                  gridHeight);
                Level = new Level(this, newCellsX, newCellsX, GridsystemAffect.cellWidth,
                                  GridsystemAffect.cellHeight);
                //roomRenderTarget = new RenderTarget2D(game.GraphicsDevice, worldWidth, worldHeight);
                CollisionManager.gridsystemCollision = new GridSystem(this, newCellsX,
                                                                      new Vector2R(0,
                                                                                   WorldHeight - gridHeight),
                                                                      WorldWidth, gridHeight);
                fillWithGrid = false;

                Camera.pos = new Vector2R(WorldWidth / 2f, WorldHeight / 2f);
            };
        }
Пример #4
0
        public virtual void Update()
        {
            if (IsPlayer)
            {
                body.angularVelocity = 0;
            }

            if (!movement.pushable && tempPosition != new Vector2R(0, 0))
            {
                body.pos      = tempPosition;
                body.velocity = Vector2R.Zero;
            }
            previousFramePosition = tempPosition;
            body.effvelocity      = body.pos - tempPosition;
            tempPosition          = body.pos;

            //collision.ClearCollisionList();
            collision.ClearCollisionLists();
            if (nodeState == state.off || nodeState == state.drawOnly)
            {
                return;
            }

            if (aOtherProps.Count > 0)
            {
                //AffectAlgorithm #2 See Souce History in this file for AffectAlgorithm 1
                if (meta.IgnoreAffectGrid)
                {
                    foreach (Node n in room.MasterGroup.fullSet)
                    {
                        affectAction(body, n.body);
                    }
                }
                else
                {
                    room.GridsystemAffect.retrieveOffsetArraysAffect(body, affectAction, affectionReach);
                }
            }
            if (OnAffectOthers != null)
            {
                OnAffectOthers.Invoke(this, null);
            }

            foreach (Component component in comps.Values)
            {
                component.CaluclateDecay();
                Type t = component.GetType();
                if (aSelfProps.Contains(t))
                {
                    component.AffectSelf();
                }
            }

            if (IsPlayer)
            {
                //player.controller.UpdateNewState();
                player.input.SetNewState();
                foreach (Type c in playerProps)
                {
                    comps[c].PlayerControl(player.input);
                }
                //player.controller.UpdateOldState();
                player.input.SetOldState();
            }
            //AI execution
            if (IsAI)
            {
                foreach (Type c in aiProps)
                {
                    comps[c].AIControl(AIMode.Agro);
                }
            }

            if (movement.active)
            {
                movement.AffectSelf();            //temporary until make movement list to update at the correct time
            }
            if (triggerSortComponentsUpdate)
            {
                SortComponentListsUpdate();
                triggerSortComponentsUpdate = false;
            }

            if (triggerRemoveComponent)
            {
                RemoveComponentTriggered();
            }
        }
Пример #5
0
        public static void cloneNode(Node sourceNode, Node destNode)
        {
            List <FieldInfo> fields = sourceNode.GetType().GetFields().ToList();

            fields.AddRange(sourceNode.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).ToList());
            List <PropertyInfo> properties = sourceNode.GetType().GetProperties().ToList();

            /*
             *    foreach (PropertyInfo property in properties)
             *    {
             *        //if (property.Name.Equals("compsProp")) continue;
             *        property.SetValue(destNode, property.GetValue(sourceNode, null), null);
             *
             *    }
             *    //*/
            //do not copy parent field
            foreach (FieldInfo field in fields)
            {
                //Debug.Assert(!(field.Name == "_name" && field.GetValue(sourceNode).ToString() == "shovel2"));
                if (field.Name.Equals("_comps"))
                {
                    Dictionary <Type, Component> dict = sourceNode.comps;
                    foreach (Type key in dict.Keys)
                    {
                        if (key == typeof(Movement) || key == typeof(Collision))
                        {
                            continue;
                        }
                        destNode.addComponent(key, sourceNode.comps[key].active);
                        Component.CloneComponent(dict[key], destNode.comps[key]);
                        destNode.comps[key].Initialize(destNode);
                    }
                    foreach (Type key in destNode.comps.Keys.ToList())
                    {
                        if (key == typeof(Movement) || key == typeof(Collision))
                        {
                            continue;
                        }
                        Component  component = destNode.comps[key];
                        MethodInfo mInfo     = component.GetType().GetMethod("AfterCloning");
                        if (mInfo != null &&
                            mInfo.DeclaringType == component.GetType())
                        {
                            component.AfterCloning();
                        }
                    }
                }
                else if ((field.FieldType == typeof(int)) ||
                         (field.FieldType == typeof(Single)) ||
                         (field.FieldType == typeof(bool)) ||
                         (field.FieldType == typeof(string)))
                {
                    if (!field.Name.Equals("IsDefault"))
                    {
                        field.SetValue(destNode, field.GetValue(sourceNode));
                    }
                }
                else if (field.FieldType == typeof(Vector2R))
                {
                    Vector2R vect    = (Vector2R)field.GetValue(sourceNode);
                    Vector2R newvect = new Vector2R(vect.X, vect.Y);
                    field.SetValue(destNode, newvect);
                }
                else if (field.FieldType == typeof(Color))
                {
                    Color col    = (Color)field.GetValue(sourceNode);
                    Color newcol = new Color(col.R, col.G, col.B, col.A);
                    field.SetValue(destNode, newcol);
                }
                else if (field.FieldType == (typeof(Collision)))
                {
                    Component.CloneComponent(sourceNode.collision, destNode.collision);
                    destNode.collision.parent = destNode;
                    destNode.collision.AfterCloning();
                }
                else if (field.FieldType == (typeof(Movement)))
                {
                    Component.CloneComponent(sourceNode.movement, destNode.movement);
                    destNode.movement.parent = destNode;
                    destNode.movement.AfterCloning();
                }
                else if (field.FieldType == (typeof(Body)))
                {
                    //Component.CloneComponent(sourceNode.body, destNode.body);

                    Component.CloneObject(sourceNode.body, destNode.body);
                    destNode.body.parent     = destNode;
                    destNode.body.shape.body = destNode.body;
                    destNode.body.AfterCloning();
                }
            }
        }
Пример #6
0
        private Node ConstructWallPoly(Dictionary <dynamic, dynamic> props, float hw, float hh, Vector2R pos)
        {
            Node n = new Node(this, props);

            n.Comp <BasicDraw>().active = false;
            Polygon poly = new Polygon {
                body = n.body
            };

            poly.body.pos = pos;
            poly.SetBox(hw, hh);
            //poly.SetOrient(0f);

            n.body.shape = poly;
            n.body.SetStatic();
            n.body.orient = (0);
            //n.body.restitution = 1f;

            //n.movement.pushable = false;

            MasterGroup.childGroups["Wall Group"].entities.Add(n);
            return(n);
        }
Пример #7
0
        //this is NOT clone component
        public static void CloneObject(object sourceObject, object destObject)
        {
            List <FieldInfo> fields = sourceObject.GetType().GetFields().ToList();

            fields.AddRange(sourceObject.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).ToList());
            List <PropertyInfo> properties = sourceObject.GetType().GetProperties().ToList();

            foreach (PropertyInfo property in properties)
            {
                if (property.PropertyType == typeof(ModifierInfo))
                {
                    continue;
                }
                if (property.PropertyType == typeof(Node))
                {
                    continue;
                }
                if (property.GetSetMethod() != null)
                {
                    property.SetValue(destObject, property.GetValue(sourceObject, null), null);
                }
            }
            foreach (FieldInfo field in fields)
            {
                if (field.Name.Equals("shape"))
                {
                    continue;
                }
                //no longer checking for dictionaries, parent(Node)
                if ((field.FieldType == typeof(int)) ||
                    (field.FieldType == typeof(Single)) ||
                    (field.FieldType == typeof(bool)) ||
                    (field.FieldType == typeof(string)))
                {
                    field.SetValue(destObject, field.GetValue(sourceObject));
                }
                else if (field.FieldType == typeof(Vector2R))
                {
                    Vector2R vect    = (Vector2R)field.GetValue(sourceObject);
                    Vector2R newvect = new Vector2R(vect.X, vect.Y);
                    field.SetValue(destObject, newvect);
                }
                else if (field.FieldType == typeof(Color))
                {
                    Color col    = (Color)field.GetValue(sourceObject);
                    Color newcol = new Color(col.R, col.G, col.B, col.A);
                    field.SetValue(destObject, newcol);
                }
                else if (field.FieldType == typeof(Room))
                {
                    field.SetValue(destObject, field.GetValue(sourceObject));
                }
            }

            MethodInfo mInfo = destObject.GetType().GetMethod("InitializeLists");

            if (mInfo != null)
            {
                mInfo.Invoke(destObject, null);
            }
            mInfo = destObject.GetType().GetMethod("AfterCloning");
            if (mInfo != null)
            {
                mInfo.Invoke(destObject, null);
            }

            //destObject.InitializeLists();
            //destObject.AfterCloning();
        }
Пример #8
0
        public static void CloneComponent(Component sourceComp, Component destComp)
        {
            List <FieldInfo> fields = sourceComp.GetType().GetFields().ToList();

            fields.AddRange(sourceComp.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).ToList());
            List <PropertyInfo> properties = sourceComp.GetType().GetProperties().ToList();

            foreach (PropertyInfo property in properties)
            {
                if (property.PropertyType == typeof(ModifierInfo))
                {
                    continue;
                }
                if (property.PropertyType == typeof(Node))
                {
                    var cust = property.GetCustomAttributes(typeof(CopyNodeProperty), false);
                    if (cust.Length > 0)
                    {
                        Node n      = (Node)property.GetValue(sourceComp, null);
                        Node nclone = n.CreateClone(sourceComp.room);
                        property.SetValue(destComp, nclone, null);
                        //Console.WriteLine("CLONING : " + property.Name);
                    }
                    continue;
                }
                if (Utils.isToggle(property.PropertyType))
                {
                    dynamic tog    = property.GetValue(sourceComp, null);
                    dynamic newtog = tog.Clone();
                    property.SetValue(destComp, newtog, null);
                    continue;
                }
                if (property.PropertyType.IsClass)
                {
                    if (!typeof(Delegate).IsAssignableFrom(property.PropertyType) && !(property.PropertyType == typeof(Link)))
                    {
                        //Console.WriteLine("We should be aware of this.");
                    }
                }
                if (property.GetSetMethod() != null)
                {
                    property.SetValue(destComp, property.GetValue(sourceComp, null), null);
                }
            }
            foreach (FieldInfo field in fields)
            {
                if (field.Name.Equals("shape"))
                {
                    continue;
                }
                if (field.FieldType == typeof(Dictionary <string, ModifierInfo>))
                {
                    Modifier mod = (Modifier)sourceComp;

                    Dictionary <string, ModifierInfo> newmodinfos = new Dictionary <string, ModifierInfo>();
                    foreach (KeyValuePair <string, ModifierInfo> kvp in mod.modifierInfos)
                    {
                        string       key          = kvp.Key;
                        ModifierInfo modifierInfo = kvp.Value;
                        Dictionary <string, FPInfo> newFpInfos    = new Dictionary <string, FPInfo>();
                        Dictionary <string, object> newFpInfosObj = new Dictionary <string, object>();
                        foreach (string key2 in modifierInfo.fpInfos.Keys)
                        {
                            FPInfo fpinfo = new FPInfo(modifierInfo.fpInfos[key2]);

                            newFpInfos.Add(key2, fpinfo);
                            newFpInfosObj.Add(key2, null);
                        }
                        Dictionary <string, dynamic> newargs = new Dictionary <string, dynamic>();
                        foreach (string key2 in modifierInfo.args.Keys)
                        {
                            newargs.Add(key2, modifierInfo.args[key2]); //by reference (for now)
                        }

                        ModifierInfo modInfo = new ModifierInfo(newFpInfos, newFpInfosObj, newargs, modifierInfo.modifierDelegate);
                        modInfo.delegateName = modifierInfo.delegateName;
                        newmodinfos.Add(key, modInfo);
                    }
                    field.SetValue(destComp, newmodinfos);
                }
                //no longer checking for dictionaries, parent(Node)
                if ((field.FieldType == typeof(int)) ||
                    (field.FieldType == typeof(Single)) ||
                    (field.FieldType == typeof(bool)) ||
                    (field.FieldType == typeof(string)))
                {
                    field.SetValue(destComp, field.GetValue(sourceComp));
                }
                else if (field.FieldType == typeof(Vector2R))
                {
                    Vector2R vect    = (Vector2R)field.GetValue(sourceComp);
                    Vector2R newvect = new Vector2R(vect.X, vect.Y);
                    field.SetValue(destComp, newvect);
                }
                else if (field.FieldType == typeof(Color))
                {
                    Color col    = (Color)field.GetValue(sourceComp);
                    Color newcol = new Color(col.R, col.G, col.B, col.A);
                    field.SetValue(destComp, newcol);
                }
                else
                {
                    //this would be an object field
                    if (field.Name.Equals("room"))
                    {
                        field.SetValue(destComp, field.GetValue(sourceComp));
                    }
                }
                //field.SetValue(newobj, field.GetValue(obj));
            }
            destComp.InitializeLists();
            destComp.AfterCloning();
        }