-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBall.cs
More file actions
69 lines (57 loc) · 2.28 KB
/
Ball.cs
File metadata and controls
69 lines (57 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using nkast.Aether.Physics2D.Dynamics;
using Vector2 = nkast.Aether.Physics2D.Common.Vector2;
namespace PeggleAI
{
public class Ball
{
// Sprites
private static Texture2D ballTexture;
private static Vector2 ballTextureSize;
private static Vector2 ballTextureOrigin;
private static Vector2 ballTextureScale;
// Physics
private static float ballRadius = 0.125f;
public Body ballBody { get; private set; }
private const float BALL_SPEED = 0.4f;
private const float BALL_BOUNCINESS = 0.6f;
private const float BALL_FRICTION = 0.1f;
public Ball(float x, float y, float rotation, World world)
{
Vector2 ballPosition = new Vector2(x, y);
ballBody = world.CreateBody(ballPosition, 0, BodyType.Dynamic);
var ballFixture = ballBody.CreateCircle(ballRadius, 1f);
ballFixture.Restitution = BALL_BOUNCINESS;
ballFixture.Friction = BALL_FRICTION;
// When the ball is spawned, give it some velocity in the direction the player is aiming
Vector2 ballVelocity = new Vector2((float)Math.Sin(rotation) * -1, (float)Math.Cos(rotation));
ballBody.ApplyLinearImpulse( ballVelocity * BALL_SPEED);
}
public static void loadContent(Texture2D ballTexture)
{
Ball.ballTexture = ballTexture;
Ball.ballTextureSize = new Vector2(ballTexture.Width, ballTexture.Height);
Ball.ballTextureScale = new Vector2(
(ballRadius * 2f) / ballTextureSize.X,
(ballRadius * 2f) / ballTextureSize.Y
);
Ball.ballTextureOrigin = ballTextureSize / 2f;
}
public void draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(
ballTexture,
LevelComponent.convertVec(ballBody.Position),
null,
Color.White,
ballBody.Rotation,
LevelComponent.convertVec(ballTextureOrigin),
LevelComponent.convertVec(ballTextureScale),
SpriteEffects.FlipVertically,
0f
);
}
}
}