random serve

main
Evan Hemsley 2020-07-15 18:31:49 -07:00
parent 12e08e284c
commit dccab83a00
6 changed files with 46 additions and 10 deletions

View File

@ -20,7 +20,7 @@ namespace PongFE.Engines
SendMessage( SendMessage(
new BallSpawnMessage( new BallSpawnMessage(
new MoonTools.Structs.Position2D(640, 360), new MoonTools.Structs.Position2D(640, 360),
new System.Numerics.Vector2(-200, 100), 300,
16, 16,
16 16
), ),

View File

@ -1,7 +1,9 @@
using System.Numerics;
using Encompass; using Encompass;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using PongFE.Components; using PongFE.Components;
using PongFE.Messages; using PongFE.Messages;
using PongFE.Extensions;
namespace PongFE.Spawners namespace PongFE.Spawners
{ {
@ -17,10 +19,14 @@ namespace PongFE.Spawners
protected override void Spawn(BallSpawnMessage message) protected override void Spawn(BallSpawnMessage message)
{ {
var ball = CreateEntity(); 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 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 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 CanBeBouncedComponent());
AddComponent(ball, new BounceResponseComponent()); AddComponent(ball, new BounceResponseComponent());
AddComponent(ball, new CanBeTrackedComponent()); AddComponent(ball, new CanBeTrackedComponent());

View File

@ -1,12 +1,17 @@
using MoonTools.Structs; using System.Numerics;
namespace PongFE.Extensions namespace PongFE.Extensions
{ {
public static class Vector2Extensions 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); 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));
}
} }
} }

View File

@ -1,4 +1,3 @@
using System.Numerics;
using Encompass; using Encompass;
using MoonTools.Structs; using MoonTools.Structs;
@ -7,14 +6,14 @@ namespace PongFE.Messages
public struct BallSpawnMessage : IMessage public struct BallSpawnMessage : IMessage
{ {
public Position2D Position { get; } public Position2D Position { get; }
public Vector2 Velocity { get; } public float Speed { get; }
public int Width { get; } public int Width { get; }
public int Height { 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; Position = position;
Velocity = velocity; Speed = speed;
Width = width; Width = width;
Height = height; Height = height;
} }

View File

@ -80,7 +80,7 @@ namespace PongFE
WorldBuilder.SendMessage( WorldBuilder.SendMessage(
new BallSpawnMessage( new BallSpawnMessage(
new MoonTools.Structs.Position2D(640, 360), new MoonTools.Structs.Position2D(640, 360),
new System.Numerics.Vector2(-300, 200), 300,
16, 16,
16 16
) )

View File

@ -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;
}
}