diff --git a/Gola2/CSDiv.cs b/Gola2/CSDiv.cs index 2c96cf1..239341c 100644 --- a/Gola2/CSDiv.cs +++ b/Gola2/CSDiv.cs @@ -83,7 +83,7 @@ acts = actors.ToArray(); foreach (CSDivActor a in acts) if (a.Active) - a.Step(); + a.DoStep(); } finally { @@ -110,7 +110,7 @@ g.SmoothingMode = SmoothingMode.AntiAlias; g.Clear(Color.Transparent); foreach (CSDivActor a in acts) - if (a.Visible) + if (a.Visible && a.started) { GraphicsState gs = g.Save(); a.Render(g); @@ -157,8 +157,24 @@ CanvasDone(); } + public static T[] GetActors() where T : class + { + List lst = new List(); + foreach (CSDivActor a in actors) + { + if (a.Active) + { + T t = a as T; + if (t != null) + lst.Add(t); + } + } + return lst.ToArray(); + } + public static Rectangle Bounds => canvas.CanvasBounds; public static bool Active => timer != null; + public static CSDivActor[] Actors => actors.ToArray(); #region Extension #endregion @@ -182,6 +198,8 @@ { public static Random Random { get; } = new Random(); + internal bool started = false; + public CSDivActor() { CSDiv.AddActor(this); @@ -192,6 +210,16 @@ CSDiv.RemoveActor(this); } + internal void DoStep() + { + if (!started) + { + Start(); + started = true; + } + Step(); + } + protected internal abstract void Start(); protected internal abstract void Step(); protected internal virtual void Render(Graphics g) diff --git a/Gola2/Gola.cs b/Gola2/Gola.cs index c553b71..c222bd4 100644 --- a/Gola2/Gola.cs +++ b/Gola2/Gola.cs @@ -5,10 +5,12 @@ { class Gola : CSDivActor { - private const float GRAV = 0.6f; - private PointF force = new Point(0, 0); - private float elasticity = 0.8f; - private float nofriction = 0.99f; + private const float GRAV = 0.9f; + + private PointF inertia = new Point(0, 0); + private float elasticity = 0.7f; + private float nofriction = 0.999f; + private float groundfriction = 0.9f; private int radious = 32; private Color color; private Brush fillBrush; @@ -17,13 +19,20 @@ private Vector3 rotation; private Vector3 rforce; private Mini3D content; + private bool collision; public Gola(Creature creature) { + if (creature != null) + content = creature.Get3D(); + } + + protected internal override void Start() + { Sprite = Properties.Resources.Ball2; Position = new PointF(rndf(ScreenLeft, ScreenRight), ScreenBottom); - force.Y = -rndf(40) / 100; - force.X = rndf(-20, 20) / 100; + inertia.Y = -rndf(40); + inertia.X = rndf(-20, 20); do { color = Color.FromArgb(rnd(0, 255), rnd(0, 255), rnd(0, 255)); @@ -33,22 +42,17 @@ fillRectangle = new RectangleF(-radious, -radious, radious * 2, radious * 2); rforce = Vector3.Random() * 0.05f; rotation = Vector3.Random() * 360; - // == CONTENT - if (creature != null) - content = creature.Get3D(); - } - - protected internal override void Start() - { } protected internal override void Step() { - Position = new PointF(Position.X + force.X, Position.Y + force.Y); - force.Y += GRAV; + Position = new PointF(Position.X + inertia.X, Position.Y + inertia.Y); rotation += rforce; - CheckBounds(); + inertia.X *= nofriction; CheckCollision(); + //string frmt = "+0000.00;-0000:00; 0000.00"; + //Console.WriteLine("Position: (" + Position.X.ToString(frmt) + + // "," + Position.Y.ToString(frmt) + ") Force:(" + force.X.ToString(frmt) + "," + force.Y.ToString(frmt) + ")"); } protected internal override void Render(Graphics g) @@ -68,44 +72,59 @@ content.Draw(g); } - private void CheckBounds() + private void CheckCollision() { - if (Y > ScreenBottom) + collision = false; + PointF force = new PointF(0, GRAV); + + Gola[] golas = CSDiv.GetActors(); + foreach (Gola g in golas) { - Y = ScreenBottom; - force.Y = force.Y * -elasticity; - force.X *= nofriction; + if (g == this) + continue; + if (IsNearestTo(g, radious * 2)) + { + PointF ine = inertia; + inertia.X -= g.inertia.X; + inertia.Y -= g.inertia.Y; + g.inertia.X -= ine.X; + g.inertia.Y -= ine.Y; + } + } + + // ================ Bounds + if (Y >= ScreenBottom) + { + force.Y = 0; + inertia.X *= groundfriction; + if (inertia.Y > 0) + if (inertia.Y < elasticity) + { + inertia.Y = 0; + Y = ScreenBottom; + } + else + inertia.Y *= -elasticity; + collision = true; } if (Y < ScreenTop) { Y = ScreenTop; - force.Y = force.Y * -elasticity; + inertia.Y = inertia.Y * -elasticity; } if (X > ScreenRight) { X = ScreenRight; - force.X = force.X * -elasticity; + inertia.X = inertia.X * -elasticity; } if (X < ScreenLeft) { X = ScreenLeft; - force.X = force.X * -elasticity; + inertia.X = inertia.X * -elasticity; } - } - - private void CheckCollision() - { - //Gola[] golas = ActorEngine.GetActors(); - //foreach (Gola g in golas) - //{ - // if (g == this) - // continue; - // if (IsNearestTo(g, size)) - // { - // force.X = rndf(-3, 3); - // force.Y = -5; - // } - //} + // Apply force + inertia.X += force.X; + inertia.Y += force.Y; } private int ScreenLeft => CSDiv.Bounds.Left + radious; diff --git a/Gola2/Gola2.csproj b/Gola2/Gola2.csproj index bd5c8fe..3948f43 100644 --- a/Gola2/Gola2.csproj +++ b/Gola2/Gola2.csproj @@ -33,6 +33,9 @@ Gola2.ico + + + diff --git a/Gola2/Gola2.csproj.user b/Gola2/Gola2.csproj.user index 94c0561..d14eb3b 100644 --- a/Gola2/Gola2.csproj.user +++ b/Gola2/Gola2.csproj.user @@ -1,6 +1,7 @@  - editor + + \ No newline at end of file diff --git a/Gola2/MainForm.Designer.cs b/Gola2/MainForm.Designer.cs index e533139..c79e397 100644 --- a/Gola2/MainForm.Designer.cs +++ b/Gola2/MainForm.Designer.cs @@ -35,13 +35,14 @@ this.addGolaToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.removeGolaToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator(); + this.editorToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); - this.editorToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.doNotClickToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.trayMenuStrip.SuspendLayout(); this.SuspendLayout(); // @@ -59,13 +60,14 @@ this.trayMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.addGolaToolStripMenuItem, this.removeGolaToolStripMenuItem, + this.doNotClickToolStripMenuItem, this.toolStripSeparator2, this.editorToolStripMenuItem, this.aboutToolStripMenuItem, this.toolStripSeparator1, this.exitToolStripMenuItem}); this.trayMenuStrip.Name = "trayMenuStrip"; - this.trayMenuStrip.Size = new System.Drawing.Size(181, 148); + this.trayMenuStrip.Size = new System.Drawing.Size(181, 170); // // addGolaToolStripMenuItem // @@ -86,6 +88,13 @@ this.toolStripSeparator2.Name = "toolStripSeparator2"; this.toolStripSeparator2.Size = new System.Drawing.Size(177, 6); // + // editorToolStripMenuItem + // + this.editorToolStripMenuItem.Name = "editorToolStripMenuItem"; + this.editorToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.editorToolStripMenuItem.Text = "Editor"; + this.editorToolStripMenuItem.Click += new System.EventHandler(this.editorToolStripMenuItem_Click); + // // aboutToolStripMenuItem // this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem"; @@ -133,12 +142,12 @@ this.label3.Text = "USE WITH CAUTION!"; this.label3.TextAlign = System.Drawing.ContentAlignment.BottomCenter; // - // editorToolStripMenuItem + // doNotClickToolStripMenuItem // - this.editorToolStripMenuItem.Name = "editorToolStripMenuItem"; - this.editorToolStripMenuItem.Size = new System.Drawing.Size(180, 22); - this.editorToolStripMenuItem.Text = "Editor"; - this.editorToolStripMenuItem.Click += new System.EventHandler(this.editorToolStripMenuItem_Click); + this.doNotClickToolStripMenuItem.Name = "doNotClickToolStripMenuItem"; + this.doNotClickToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.doNotClickToolStripMenuItem.Text = "Do not click!"; + this.doNotClickToolStripMenuItem.Click += new System.EventHandler(this.doNotClickToolStripMenuItem_Click); // // MainForm // @@ -172,5 +181,6 @@ private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.ToolStripMenuItem editorToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem doNotClickToolStripMenuItem; } } \ No newline at end of file diff --git a/Gola2/MainForm.cs b/Gola2/MainForm.cs index 4c698b6..9f91224 100644 --- a/Gola2/MainForm.cs +++ b/Gola2/MainForm.cs @@ -15,7 +15,7 @@ //CreateGola(); } - public void CreateGola(Creature creature = null ) + public void CreateGola(Creature creature = null) { if (!CSDiv.Active) CSDiv.Start(); @@ -24,7 +24,7 @@ public static void MakeGola(Creature creature) { - if (INSTANCE==null) + if (INSTANCE == null) new MainForm(); INSTANCE.CreateGola(creature); } @@ -83,5 +83,11 @@ { CreatureEditor.Execute(); } + + private void doNotClickToolStripMenuItem_Click(object sender, EventArgs e) + { + for (int i = 0; i < 100; i++) + CreateGola(); + } } } diff --git a/Gola2/Program.cs b/Gola2/Program.cs index 386255e..b2535f2 100644 --- a/Gola2/Program.cs +++ b/Gola2/Program.cs @@ -17,6 +17,7 @@ if (CheckArguments()) { main = new MainForm(); + MainForm.MakeGola(null); Application.Run(); } }