PongFE/PongFE/Engines/Spawners/BallSpawner.cs

39 lines
1.4 KiB
C#

using System.Numerics;
using Encompass;
using Microsoft.Xna.Framework.Graphics;
using PongFE.Components;
using PongFE.Messages;
using PongFE.Extensions;
namespace PongFE.Spawners
{
public class BallSpawner : Spawner<BallSpawnMessage>
{
private Texture2D WhitePixel { get; }
public BallSpawner(Texture2D whitePixel)
{
WhitePixel = whitePixel;
}
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(velocity));
AddComponent(ball, new ScaleComponent(message.Width, message.Height));
AddComponent(ball, new CollisionComponent(new MoonTools.Bonk.Rectangle(0, 0, 16, 16)));
AddComponent(ball, new Texture2DComponent(WhitePixel, 0));
AddComponent(ball, new CanBeBouncedComponent());
AddComponent(ball, new BounceResponseComponent());
AddComponent(ball, new CanBeTrackedComponent());
AddComponent(ball, new CanBeDestroyedComponent());
AddComponent(ball, new IncreaseScoreAfterDestroyComponent());
}
}
}