From f161ed3b859e84105be6931811d583e602fc95c9 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Tue, 15 Dec 2020 20:21:44 -0500 Subject: [PATCH] Add project files. --- Program.cs | 47 ++++++++++++++++++++++++++++++ ball.cs | 69 +++++++++++++++++++++++++++++++++++++++++++++ callbacks.cs | 39 +++++++++++++++++++++++++ collisionHandler.cs | 22 +++++++++++++++ game.csproj | 12 ++++++++ game.sln | 25 ++++++++++++++++ globals.cs | 17 +++++++++++ mouse.cs | 23 +++++++++++++++ 8 files changed, 254 insertions(+) create mode 100644 Program.cs create mode 100644 ball.cs create mode 100644 callbacks.cs create mode 100644 collisionHandler.cs create mode 100644 game.csproj create mode 100644 game.sln create mode 100644 globals.cs create mode 100644 mouse.cs diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..2b9dcb9 --- /dev/null +++ b/Program.cs @@ -0,0 +1,47 @@ +using System; +using SFML.Audio; +using SFML.Graphics; +using SFML.Window; +using SFML.System; +using System.Net; +using System.Threading; +using System.Collections.Generic; + +namespace game +{ + class Program + { + static void Main(string[] args) + { + RenderWindow window = new RenderWindow(new VideoMode(1280, 720), "test", Styles.Default); + + callbacks.registerCallbacks(window); + + CircleShape cursor = new CircleShape(25f); + cursor.FillColor = Color.Green; + + ball_t ball = new ball_t(50f); + ball_t ball2 = new ball_t(50f, new Vector2f(100, 100), new Vector2f(1f, .5f)); + + // main loop + while (window.IsOpen) + { + window.DispatchEvents(); + + cursor.Position = new Vector2f(g.mouse.position.X - (cursor.GetLocalBounds().Width / 2), g.mouse.position.Y - (cursor.GetLocalBounds().Height / 2)); + if (g.mouse.button == Mouse.Button.Left && g.mouse.buttonState) + cursor.FillColor = Color.Red; + else + cursor.FillColor = Color.Green; + + window.Clear(); + + // add stuff to drawing surface + window.Draw(cursor); + ball.Run(window); + ball2.Run(window); + window.Display(); + } + } + } +} diff --git a/ball.cs b/ball.cs new file mode 100644 index 0000000..33ab355 --- /dev/null +++ b/ball.cs @@ -0,0 +1,69 @@ +using System; +using SFML.Audio; +using SFML.Graphics; +using SFML.Window; +using SFML.System; +using System.Net; +using System.Threading; + +namespace game +{ + class ball_t + { + public CircleShape shape; + public Vector2f velocity; + public ball_t(float radius) + { + this.shape = new CircleShape(radius); + this.shape.Position = new Vector2f(1280 / 2, 720 / 2); + this.velocity = new Vector2f(.5f, .5f); + collisionHandler.registry.Add(this); + } + public ball_t(float radius, Vector2f position) + { + this.shape = new CircleShape(radius); + this.shape.Position = position; + this.velocity = new Vector2f(.5f, .5f); + collisionHandler.registry.Add(this); + } + public ball_t(float radius, Vector2f position, Vector2f velocity) + { + this.shape = new CircleShape(radius); + this.shape.Position = position; + this.velocity = velocity; + collisionHandler.registry.Add(this); + } + private void RunPhysics(Vector2f forces) + { + //temp place for collision stuff + Vector2f center = new Vector2f(this.shape.Position.X + (this.shape.GetLocalBounds().Width / 2), this.shape.Position.Y + (this.shape.GetLocalBounds().Height / 2)); + bool isValidPosition = ((center.X > (1280 - (this.shape.GetLocalBounds().Width / 2)) || center.Y > (720 - (this.shape.GetLocalBounds().Height / 2))) || (center.X < (0 + (this.shape.GetLocalBounds().Width / 2)) || center.Y < (0 + (this.shape.GetLocalBounds().Height / 2)))); + if (isValidPosition || collisionHandler.isColliding(this)) + { + //this.shape.Position = new Vector2f(1280 / 2, 720 / 2); + this.velocity *= -1; + this.velocity /= 2f; + this.shape.Position += new Vector2f(0, -1F); + this.shape.Position = new Vector2f(Math.Clamp(this.shape.Position.X, 0, 1280), Math.Clamp(this.shape.Position.Y, 0, 720)); + Console.WriteLine("COLLISION: "+this.velocity.Y.ToString()); + } + Vector2f oldPosition = this.shape.Position; + this.shape.Position += this.velocity; + ball_t dummy = this; + dummy.shape.Position = this.shape.Position; + if (collisionHandler.isColliding(dummy)) + { + this.shape.Position = oldPosition; + this.velocity *= -1; + this.velocity /= 2f; + } + if ((Math.Abs(this.velocity.X) < 1 ) && (Math.Abs(this.velocity.Y) < 1)) + this.velocity += forces; + } + public void Run(RenderWindow window) + { + RunPhysics(new Vector2f(0f, 0.000002f * this.shape.Radius)); + window.Draw(this.shape); + } + } +} diff --git a/callbacks.cs b/callbacks.cs new file mode 100644 index 0000000..e771839 --- /dev/null +++ b/callbacks.cs @@ -0,0 +1,39 @@ +using System; +using SFML.Audio; +using SFML.Graphics; +using SFML.Window; +using SFML.System; +using System.Net; +using System.Threading; + +namespace game +{ + class callbacks + { + public static void CallbackClose(object sender, EventArgs e) + { + RenderWindow window = (RenderWindow)sender; + window.Close(); + } + public static void CallbackMouseMoved(object sender, MouseMoveEventArgs e) + { + g.mouse.position = new Vector2f(e.X, e.Y); + } + public static void CallbackMousePressed(object sender, MouseButtonEventArgs e) + { + g.mouse.button = e.Button; + g.mouse.buttonState = true; + } + public static void CallbackMouseReleased(object sender, MouseButtonEventArgs e) + { + g.mouse.buttonState = false; + } + public static void registerCallbacks(RenderWindow window) + { + window.Closed += new EventHandler(callbacks.CallbackClose); + window.MouseMoved += new EventHandler(callbacks.CallbackMouseMoved); + window.MouseButtonPressed += new EventHandler(callbacks.CallbackMousePressed); + window.MouseButtonReleased += new EventHandler(callbacks.CallbackMouseReleased); + } + } +} diff --git a/collisionHandler.cs b/collisionHandler.cs new file mode 100644 index 0000000..2386f0d --- /dev/null +++ b/collisionHandler.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.Text; + +namespace game +{ + static class collisionHandler + { + public static List registry = new List(); + + public static bool isColliding(ball_t ball) + { + foreach (ball_t registeredBall in registry) + { + if (registeredBall != ball && registeredBall.shape.GetGlobalBounds().Intersects(ball.shape.GetGlobalBounds())) + return true; + } + return false; + } + } +} diff --git a/game.csproj b/game.csproj new file mode 100644 index 0000000..c75eb4a --- /dev/null +++ b/game.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.1 + + + + + + + diff --git a/game.sln b/game.sln new file mode 100644 index 0000000..8aaa319 --- /dev/null +++ b/game.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29926.136 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "game", "game.csproj", "{315B7D3F-46D7-4E12-A2A0-32A24994F484}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {315B7D3F-46D7-4E12-A2A0-32A24994F484}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {315B7D3F-46D7-4E12-A2A0-32A24994F484}.Debug|Any CPU.Build.0 = Debug|Any CPU + {315B7D3F-46D7-4E12-A2A0-32A24994F484}.Release|Any CPU.ActiveCfg = Release|Any CPU + {315B7D3F-46D7-4E12-A2A0-32A24994F484}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {6A2AEA8E-88AD-41C2-8550-2ADCE8FD5E98} + EndGlobalSection +EndGlobal diff --git a/globals.cs b/globals.cs new file mode 100644 index 0000000..a234420 --- /dev/null +++ b/globals.cs @@ -0,0 +1,17 @@ +using System; +using SFML.Audio; +using SFML.Graphics; +using SFML.Window; +using SFML.System; +using System.Net; +using System.Threading; + + +namespace game +{ + static class g// a5 moment// ok thats not funny + { + public static mouse_t mouse = new mouse_t(); + public static Random rand = new Random(); + } +} diff --git a/mouse.cs b/mouse.cs new file mode 100644 index 0000000..6bf8920 --- /dev/null +++ b/mouse.cs @@ -0,0 +1,23 @@ +using System; +using SFML.Audio; +using SFML.Graphics; +using SFML.Window; +using SFML.System; +using System.Net; +using System.Threading; + +namespace game +{ + class mouse_t + { + public Vector2f position; + public Mouse.Button button; + public bool buttonState; + public mouse_t() + { + this.position = new Vector2f(0, 0); + this.button = Mouse.Button.Left; + this.buttonState = false; + } + } +}