-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeg.cs
More file actions
116 lines (93 loc) · 3.62 KB
/
Peg.cs
File metadata and controls
116 lines (93 loc) · 3.62 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using nkast.Aether.Physics2D.Dynamics;
using nkast.Aether.Physics2D.Dynamics.Contacts;
using Vector2 = nkast.Aether.Physics2D.Common.Vector2;
namespace PeggleAI
{
public class Peg
{
// Keep a reference to the level that this peg belongs to.
// This will be helpful when we have multiple instances of the game running at once
// during the algorithm
private LevelComponent level;
// Sprites
private static Texture2D bluePegTexture;
private static Texture2D orangePegTexture;
private static Vector2 pegTextureSize;
private static Vector2 pegTextureOrigin;
private static Vector2 pegTextureScale;
public bool isOrange { get; private set; }
// Physics
private static float pegRadius = 0.15f;
public Body pegBody { get; private set; }
private Fixture pegFixture;
private const float PEG_BOUNCINESS = 0.6f;
private const float PEG_FRICTION = 0.1f;
public float x { get; private set; }
public float y { get; private set; }
public Peg(float x, float y, LevelComponent level, bool isOrange)
{
this.level = level;
this.isOrange = isOrange;
this.x = x;
this.y = y;
Vector2 pegPosition = new Vector2(x, y);
pegBody = level.world.CreateBody(pegPosition, 0, BodyType.Static);
// Fixtures are what binds a shape to a body for collision.
pegFixture = pegBody.CreateCircle(pegRadius, 1f);
// Fixtures hold data for bounciness and friction as well
pegFixture.Restitution = PEG_BOUNCINESS;
pegFixture.Friction = PEG_FRICTION;
pegBody.OnCollision += OnCollision;
pegBody.OnSeparation += OnSeparation;
}
public static void loadContent(Texture2D bluePegTexture, Texture2D orangePegTexture)
{
Peg.bluePegTexture = bluePegTexture;
Peg.orangePegTexture = orangePegTexture;
// Scale the texture to the collision body dimensions
Peg.pegTextureSize = new Vector2(bluePegTexture.Width, bluePegTexture.Height);
Peg.pegTextureScale = new Vector2(
(pegRadius * 2f) / pegTextureSize.X,
(pegRadius * 2f) / pegTextureSize.Y
);
// Draw the texture at the center of the shapes
Peg.pegTextureOrigin = pegTextureSize / 2f;
}
public bool OnCollision(Fixture sender, Fixture other, Contact contact)
{
return true;
}
public void OnSeparation(Fixture sender, Fixture other, Contact contact)
{
level.pegsHit.Enqueue(this);
}
public void draw(SpriteBatch spriteBatch)
{
Texture2D currentTexture;
if (isOrange)
{
currentTexture = orangePegTexture;
}
else
{
currentTexture = bluePegTexture;
}
spriteBatch.Draw(
currentTexture,
LevelComponent.convertVec(pegBody.Position),
null,
Color.White,
pegBody.Rotation,
LevelComponent.convertVec(pegTextureOrigin),
LevelComponent.convertVec(pegTextureScale),
SpriteEffects.FlipVertically,
0f
);
}
}
}
// HEavily inspired by Connor "Bozo" "Sprinng uwu" "BigC1109" Langa
// I was held at gunpoint to add this comment