Exemplo n.º 1
0
        public void GenNewComponent()
        {
            Random r    = new Random();
            var    a    = GraphicsEngine.camera.VisibleRectangle;
            int    type = r.Next(3); //0 = component, 1,2 = wire

            if (type == 0)           //component
            {
                int ci = r.Next(0, GUIEngine.s_componentSelector.components.Count);
                GUIEngine.s_componentSelector.components[ci].avalable = 1;
                var c = GUIEngine.s_componentSelector.components[ci].GetNewInstance() as Components.Component;
                if (c == null || c is Components.Cursor || c is Components.Wire || c is Components.Properties.IDragDropPlacable ||
                    c is Components.Properties.ICore)
                {
                    return;
                }
                var rots = c.Graphics.GetPossibleRotations();
                if (rots != null && rots.Length > 0)
                {
                    int rot = r.Next(0, rots.Length);
                    c.ComponentRotation = rots[rot];
                }
                int x = r.Next(a.X - 100, a.X + a.Width + 100);
                int y = r.Next(a.Y - 100, a.Y + a.Height + 100);
                if (!GraphicsEngine.CanDrawGhostComponent(x - 16, y - 16, (int)c.Graphics.GetSize().X + 32, (int)c.Graphics.GetSize().Y + 32))
                {
                    return;
                }
                c.Graphics.Position = new Vector2(x, y);
                //components.Add(c);
                c.Initialize();
                c.InitAddChildComponents();
                c.Tag = 0;
                Components.ComponentsManager.Add(c);
            }
            else//wire
            {
                if (Components.ComponentsManager.Components.Count == 0)
                {
                    return;
                }
                var c1 = Components.ComponentsManager.GetComponent(r.Next(0, Components.ComponentsManager.Components.Count));
                var c2 = Components.ComponentsManager.GetComponent(r.Next(0, Components.ComponentsManager.Components.Count));
                if (c1 == null || c2 == null)
                {
                    return;
                }
                var cj1 = c1.getJoints();
                var cj2 = c2.getJoints();
                if (cj1 == null || cj2 == null || cj1.Length == 0 || cj2.Length == 0)
                {
                    return;
                }
                var j1 = Components.ComponentsManager.GetComponent(cj1[r.Next(cj1.Length)]);
                var j2 = Components.ComponentsManager.GetComponent(cj2[r.Next(cj2.Length)]);
                if (j1 == null || j2 == null || !(j1 is Components.Joint) || !(j2 is Components.Joint) ||
                    !(j1 as Components.Joint).Graphics.Visible || !(j2 as Components.Joint).Graphics.Visible)
                {
                    return;
                }
                Components.Wire w = new Components.Wire(j1 as Components.Joint, j2 as Components.Joint);
                w.Initialize();
                w.InitAddChildComponents();
                w.Tag = 0;
                Components.ComponentsManager.Add(w);
            }
            ticksSinceLastComponentAdd = 0;
        }
Exemplo n.º 2
0
        public override void Draw(Renderer renderer)
        {
            if (Main.curState.StartsWith("GAME"))
            {
                //Grid
                #region Grid
                MicroWorld.Graphics.GUI.GridDraw.DrawGrid();
                #endregion


                //AoE-s
                #region AoE-s
                renderer.End();
                renderer.BeginUnscaled(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.AnisotropicClamp, DepthStencilState.None, RasterizerState.CullNone);
                LightAOE.Draw(renderer);
                MagnetAOE.Draw(renderer);
                renderer.End();
                renderer.Begin();
                #endregion


                //Components
                #region Components
                Components.ComponentsManager.Draw();
                Components.ComponentsManager.PostDraw();
                #endregion


                //SelectedGhost
                #region SelectedGhost
                if (MicroWorld.Graphics.GUI.GUIEngine.s_componentSelector.SelectedComponent.Text != "Cursor" &&
                    InputEngine.curMouse.X > GUI.Scene.ComponentSelector.CSTile.SIZE_X && !Logics.GameInputHandler.isLine)// &&
                //!MicroWorld.Graphics.GUI.GUIEngine.s_componentSelector.SelectedComponent.IsComponentOfType<Components.Properties.IDragDropPlacable>())
                {
                    int x = (int)((InputEngine.curMouse.X) / Settings.GameScale - Settings.GameOffset.X);
                    int y = (int)((InputEngine.curMouse.Y) / Settings.GameScale - Settings.GameOffset.Y);
                    Components.Component.Rotation rot = Logics.GameInputHandler.GhostRotation;
                    Vector2 size = MicroWorld.Graphics.GUI.GUIEngine.s_componentSelector.SelectedComponentGraphics.GetSizeRotated(rot);
                    Logics.GridHelper.GridCoords(ref x, ref y);
                    Logics.PlacableAreasManager.MakePlacable(ref x, ref y, (int)size.X, (int)size.Y);
                    Logics.GridHelper.GridCoords(ref x, ref y);
                    Vector2 so = MicroWorld.Graphics.GUI.GUIEngine.s_componentSelector.SelectedComponentGraphics.GetCenter(rot);
                    int     nx = x - (int)so.X, ny = y - (int)so.Y;

                    bool b = GraphicsEngine.CanDrawGhostComponent(ref nx, ref ny, (int)size.X, (int)size.Y);

                    Color c = Main.renderer.Overlay;
                    if (!b)
                    {
                        renderer.Overlay = Color.Red * 0.5f;
                    }
                    if (InputEngine.curMouse.LeftButton == ButtonState.Pressed &&
                        GUI.GUIEngine.s_componentSelector.SelectedComponent.Avalable == 0)
                    {
                        Main.renderer.Overlay = Main.Ticks % 40 < 20 ? Color.Red : c;
                    }
                    MicroWorld.Graphics.GUI.GUIEngine.s_componentSelector.SelectedComponentGraphics.DrawGhost(
                        nx,
                        ny,
                        Main.renderer, Logics.GameInputHandler.GhostRotation);
                    Main.renderer.Overlay = c;

                    #region Dotted Lines
                    MicroWorld.Graphics.GUI.GUIEngine.s_componentSelector.SelectedComponentGraphics.DrawBorder(
                        nx, ny, Logics.GameInputHandler.GhostRotation, renderer);
                    #endregion
                }
                #endregion


                //RemovingComponentsVisuals
                #region RemovingComponentsVisuals
                MicroWorld.Graphics.Effects.Effects.DrawRemovingVisuals();
                #endregion


                //MouseOverComponent
                #region MouseOverComponent
                if (Logics.GameInputHandler.MouseOverComponent != null)
                {
                    Logics.GameInputHandler.MouseOverComponent.Graphics.DrawBorder(renderer);
                }
                #endregion


                //wire
                #region Wire
                if (Logics.GameInputHandler.isLine)
                {
                    lock (Logics.GameInputHandler.pendingWirePath)
                        Components.Graphics.WireGraphics.DrawWire(Logics.GameInputHandler.pendingWirePath);

                    #region Dotted Lines
                    if (Logics.GameInputHandler.pendingWirePath != null && Logics.GameInputHandler.pendingWirePath.Count > 0)
                    {
                        //optimization
                        List <Vector2> l = new List <Vector2>();
                        Vector2        v1 = new Vector2(), v2 = new Vector2();
                        lock (Logics.GameInputHandler.pendingWirePath)
                        {
                            l.Add(new Vector2(Logics.GameInputHandler.pendingWirePath[0].X, Logics.GameInputHandler.pendingWirePath[0].Y));
                            for (int i = 1; i < Logics.GameInputHandler.pendingWirePath.Count - 1; i++)
                            {
                                v1 = new Vector2(Logics.GameInputHandler.pendingWirePath[i].X - Logics.GameInputHandler.pendingWirePath[i - 1].X,
                                                 Logics.GameInputHandler.pendingWirePath[i].Y - Logics.GameInputHandler.pendingWirePath[i - 1].Y);
                                v2 = new Vector2(Logics.GameInputHandler.pendingWirePath[i + 1].X - Logics.GameInputHandler.pendingWirePath[i].X,
                                                 Logics.GameInputHandler.pendingWirePath[i + 1].Y - Logics.GameInputHandler.pendingWirePath[i].Y);

                                if (((v1.X != 0) != (v2.X != 0)) ||
                                    (v1.Y != 0) != (v2.Y != 0))
                                {
                                    l.Add(new Vector2(Logics.GameInputHandler.pendingWirePath[i].X, Logics.GameInputHandler.pendingWirePath[i].Y));
                                }
                            }
                            l.Add(new Vector2(Logics.GameInputHandler.pendingWirePath[Logics.GameInputHandler.pendingWirePath.Count - 1].X,
                                              Logics.GameInputHandler.pendingWirePath[Logics.GameInputHandler.pendingWirePath.Count - 1].Y));
                        }
                        //draw
                        Components.Graphics.WireGraphics.DrawBorder(l, renderer);
                    }
                    #endregion
                }
                #endregion


                //dnd
                #region DnD
                if (Logics.GameInputHandler.isComponentDnD)
                {
                    int x = (int)((InputEngine.curMouse.X) / Settings.GameScale - Settings.GameOffset.X);
                    int y = (int)((InputEngine.curMouse.Y) / Settings.GameScale - Settings.GameOffset.Y);
                    Logics.PlacableAreasManager.MakePlacable(ref x, ref y);
                    MicroWorld.Logics.GridHelper.GridCoords(ref x, ref y);
                    Logics.GameInputHandler.DnDComponent.DrawGhost(Main.renderer,
                                                                   (int)Logics.GameInputHandler.pendingWireP1.X, (int)Logics.GameInputHandler.pendingWireP1.Y, x, y);

                    #region Dotted Lines
                    (Logics.GameInputHandler.DnDComponent as Components.Component).Graphics.DrawBorder(renderer);
                    #endregion
                }
                #endregion


                //Pending placable area
                #region PlacableAreas
                if (PlacableAreasCreator.create)
                {
                    int x = InputEngine.curMouse.X, y = InputEngine.curMouse.Y;
                    Utilities.Tools.ScreenToGameCoords(ref x, ref y);
                    Logics.GridHelper.GridCoords(ref x, ref y);
                    RenderHelper.DrawDottedLinesToBorders(new Point[] { new Point(x, y) }, Color.White, renderer, true);
                }

                if (Logics.GameInputHandler.isPlacableAreaPending)
                {
                    int x = (int)Logics.GameInputHandler.pendingWireP1.X;
                    int y = (int)Logics.GameInputHandler.pendingWireP1.Y;
                    int w = InputEngine.curMouse.X;
                    int h = InputEngine.curMouse.Y;
                    Utilities.Tools.ScreenToGameCoords(ref w, ref h);
                    Logics.GridHelper.GridCoords(ref w, ref h);
                    if (w < x)
                    {
                        int t = w;
                        w = x;
                        x = t;
                    }
                    if (h < y)
                    {
                        int t = h;
                        h = y;
                        y = t;
                    }
                    w -= x;
                    h -= y;
                    Main.renderer.Draw(GraphicsEngine.pixel, new Rectangle((int)x, (int)y, (int)w, (int)h), Color.Yellow * 0.4f);
                    RenderHelper.DrawDottedLinesToBorders(
                        new Point[] {
                        new Point(x, y),
                        new Point(x + w, y),
                        new Point(x, y + h),
                        new Point(x + w, y + h)
                    },
                        Color.White, renderer, true);
                }
                else
                {
                    //highlight deleting area
                    if (Graphics.GUI.Scene.PlacableAreasCreator.delete)
                    {
                        Rectangle r;
                        for (int i = 0; i < Logics.PlacableAreasManager.areas.Count; i++)
                        {
                            r = Logics.PlacableAreasManager.areas[i];
                            int x = InputEngine.curMouse.X;
                            int y = InputEngine.curMouse.Y;
                            Utilities.Tools.ScreenToGameCoords(ref x, ref y);

                            if (r.Contains(x, y))
                            {
                                Main.renderer.Draw(GraphicsEngine.pixel, new Rectangle(r.X, r.Y, r.Width, r.Height), Color.Yellow * 0.4f);
                                RenderHelper.DrawDottedLinesToBorders(
                                    new Point[] {
                                    new Point(r.X, r.Y),
                                    new Point(r.X + r.Width, r.Y),
                                    new Point(r.X, r.Y + r.Height),
                                    new Point(r.X + r.Width, r.Y + r.Height)
                                },
                                    Color.White, renderer, true);
                                break;
                            }
                        }
                    }
                }
                #endregion


                //Particles
                #region Particles
                ParticleManager.Draw();
                #endregion
            }
        }