From dccab83a009dc175d269d478d58fb0a515c24240 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Wed, 15 Jul 2020 18:31:49 -0700 Subject: [PATCH] random serve --- PongFE/Engines/DestroyEngine.cs | 2 +- PongFE/Engines/Spawners/BallSpawner.cs | 10 ++++++++-- PongFE/Extensions/Vector2Extensions.cs | 9 +++++++-- PongFE/Messages/BallSpawnMessage.cs | 7 +++---- PongFE/PongFEGame.cs | 2 +- PongFE/Utility/MathHelper.cs | 26 ++++++++++++++++++++++++++ 6 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 PongFE/Utility/MathHelper.cs diff --git a/PongFE/Engines/DestroyEngine.cs b/PongFE/Engines/DestroyEngine.cs index cd92c49..6b34131 100644 --- a/PongFE/Engines/DestroyEngine.cs +++ b/PongFE/Engines/DestroyEngine.cs @@ -20,7 +20,7 @@ namespace PongFE.Engines SendMessage( new BallSpawnMessage( new MoonTools.Structs.Position2D(640, 360), - new System.Numerics.Vector2(-200, 100), + 300, 16, 16 ), diff --git a/PongFE/Engines/Spawners/BallSpawner.cs b/PongFE/Engines/Spawners/BallSpawner.cs index d53d132..bc38655 100644 --- a/PongFE/Engines/Spawners/BallSpawner.cs +++ b/PongFE/Engines/Spawners/BallSpawner.cs @@ -1,7 +1,9 @@ +using System.Numerics; using Encompass; using Microsoft.Xna.Framework.Graphics; using PongFE.Components; using PongFE.Messages; +using PongFE.Extensions; namespace PongFE.Spawners { @@ -17,10 +19,14 @@ namespace PongFE.Spawners protected override void Spawn(BallSpawnMessage message) { var ball = CreateEntity(); + + var direction = MathHelper.RandomDouble(-System.Math.PI / 4.0, System.Math.PI / 4.0); + var velocity = new Vector2(message.Speed * (MathHelper.CoinFlip() ? -1 : 1), 0).Rotate((float)direction); + AddComponent(ball, new PositionComponent(message.Position)); - AddComponent(ball, new VelocityComponent(message.Velocity)); + AddComponent(ball, new VelocityComponent(velocity)); AddComponent(ball, new CollisionComponent(new MoonTools.Bonk.Rectangle(0, 0, 16, 16))); - AddComponent(ball, new Texture2DComponent(WhitePixel, 0, new System.Numerics.Vector2(message.Width, message.Height))); + AddComponent(ball, new Texture2DComponent(WhitePixel, 0, new Vector2(message.Width, message.Height))); AddComponent(ball, new CanBeBouncedComponent()); AddComponent(ball, new BounceResponseComponent()); AddComponent(ball, new CanBeTrackedComponent()); diff --git a/PongFE/Extensions/Vector2Extensions.cs b/PongFE/Extensions/Vector2Extensions.cs index bd4b7fa..1cbb3b2 100644 --- a/PongFE/Extensions/Vector2Extensions.cs +++ b/PongFE/Extensions/Vector2Extensions.cs @@ -1,12 +1,17 @@ -using MoonTools.Structs; +using System.Numerics; namespace PongFE.Extensions { public static class Vector2Extensions { - public static Microsoft.Xna.Framework.Vector2 ToXNAVector(this System.Numerics.Vector2 vector) + public static Microsoft.Xna.Framework.Vector2 ToXNAVector(this Vector2 vector) { return new Microsoft.Xna.Framework.Vector2(vector.X, vector.Y); } + + public static Vector2 Rotate(this Vector2 vector, float radians) + { + return Vector2.Transform(vector, Matrix3x2.CreateRotation(radians)); + } } } diff --git a/PongFE/Messages/BallSpawnMessage.cs b/PongFE/Messages/BallSpawnMessage.cs index 3e18eea..24e1247 100644 --- a/PongFE/Messages/BallSpawnMessage.cs +++ b/PongFE/Messages/BallSpawnMessage.cs @@ -1,4 +1,3 @@ -using System.Numerics; using Encompass; using MoonTools.Structs; @@ -7,14 +6,14 @@ namespace PongFE.Messages public struct BallSpawnMessage : IMessage { public Position2D Position { get; } - public Vector2 Velocity { get; } + public float Speed { get; } public int Width { get; } public int Height { get; } - public BallSpawnMessage(Position2D position, Vector2 velocity, int width, int height) + public BallSpawnMessage(Position2D position, float speed, int width, int height) { Position = position; - Velocity = velocity; + Speed = speed; Width = width; Height = height; } diff --git a/PongFE/PongFEGame.cs b/PongFE/PongFEGame.cs index 9dbb674..acf8e0a 100644 --- a/PongFE/PongFEGame.cs +++ b/PongFE/PongFEGame.cs @@ -80,7 +80,7 @@ namespace PongFE WorldBuilder.SendMessage( new BallSpawnMessage( new MoonTools.Structs.Position2D(640, 360), - new System.Numerics.Vector2(-300, 200), + 300, 16, 16 ) diff --git a/PongFE/Utility/MathHelper.cs b/PongFE/Utility/MathHelper.cs new file mode 100644 index 0000000..f8fba42 --- /dev/null +++ b/PongFE/Utility/MathHelper.cs @@ -0,0 +1,26 @@ +using System; + +public static class MathHelper +{ + private readonly static Random s_random = new Random(); + + public static double RandomDouble(double min, double max) + { + return (s_random.NextDouble() * (max - min)) + min; + } + + public static float RandomFloat(float min, float max) + { + return (float)RandomDouble(min, max); + } + + public static int Dice(int n) + { + return (int)Math.Floor(RandomDouble(0, n)); + } + + public static bool CoinFlip() + { + return Dice(2) == 0; + } +}