2 using FarseerPhysics.Dynamics;
3 using Microsoft.Xna.Framework;
5 using System.Collections.Generic;
9 using Microsoft.Xna.Framework.Graphics;
16 public List<VoronoiCell>
Cells {
get;
private set; }
22 private readonly List<Vector2[]> triangles;
23 private readonly Color color;
25 private float moveState;
26 private float moveLength;
28 private Vector2 moveAmount;
31 get {
return moveAmount; }
35 moveLength = moveAmount.Length();
39 private float wallDamageOnTouch;
42 get {
return wallDamageOnTouch; }
45 Cells.ForEach(c => c.DoesDamage = !MathUtils.NearlyEqual(value, 0.0f));
46 wallDamageOnTouch = value;
52 private Vector2? originalPos;
56 get {
return moveState; }
57 set { moveState = MathHelper.Clamp(value, 0.0f, MathHelper.TwoPi); }
60 public LevelWall(List<Vector2> vertices, Color color,
Level level,
bool giftWrap =
false,
bool createBody =
true)
64 List<Vector2> originalVertices =
new List<Vector2>(vertices);
65 if (giftWrap) { vertices = MathUtils.GiftWrap(vertices); }
66 if (vertices.Count < 3)
68 throw new ArgumentException(
"Failed to generate a wall (not enough vertices). Original vertices: " +
string.Join(
", ", originalVertices.Select(v => v.ToString())));
71 for (
int i = 0; i < wallCell.
Edges.Count; i++)
73 wallCell.
Edges[i].Cell1 = wallCell;
74 wallCell.
Edges[i].IsSolid =
true;
76 Cells =
new List<VoronoiCell>() { wallCell };
79 Body = CaveGenerator.GeneratePolygons(
Cells,
level, out triangles);
80 if (triangles.Count == 0)
82 throw new ArgumentException(
"Failed to generate a wall (not enough triangles). Original vertices: " +
string.Join(
", ", originalVertices.Select(v => v.ToString())));
94 Cells =
new List<VoronoiCell>();
95 for (
int i = 0; i < edgePositions.Count - 1; i++)
97 Vector2[] vertices =
new Vector2[4];
98 vertices[0] = edgePositions[i];
99 vertices[1] = edgePositions[i + 1];
100 vertices[2] = vertices[1] + extendAmount;
101 vertices[3] = vertices[0] + extendAmount;
107 wallCell.
Edges[0].Cell1 = wallCell;
108 wallCell.
Edges[1].Cell1 = wallCell;
109 wallCell.
Edges[2].Cell1 = wallCell;
110 wallCell.
Edges[3].Cell1 = wallCell;
111 wallCell.
Edges[0].IsSolid =
true;
116 Cells[i - 1].Edges[1].Cell2 = wallCell;
122 Body = CaveGenerator.GeneratePolygons(
Cells,
level, out triangles);
123 Body.CollisionCategories = Physics.CollisionLevel;
129 public virtual void Update(
float deltaTime)
131 if (
Body.BodyType == BodyType.Static) {
return; }
133 Vector2 bodyPos = ConvertUnits.ToDisplayUnits(
Body.Position);
134 Cells.ForEach(c => c.Translation = bodyPos);
136 if (!originalPos.HasValue) { originalPos = bodyPos; }
138 if (moveLength > 0.0f &&
MoveSpeed > 0.0f)
140 moveState +=
MoveSpeed / moveLength * deltaTime;
141 moveState %= MathHelper.TwoPi;
143 Vector2 targetPos = ConvertUnits.ToSimUnits(originalPos.Value + moveAmount * (
float)Math.Sin(moveState));
144 Body.ApplyForce((targetPos -
Body.Position).ClampLength(1.0f) *
Body.Mass);
150 return Cells.Any(c => c.IsPointInside(point));
156 VertexBuffer?.Dispose();
List< VoronoiCell > Cells
LevelWall(List< Vector2 > edgePositions, Vector2 extendAmount, Color color, Level level)
bool IsPointInside(Vector2 point)
LevelWall(List< Vector2 > vertices, Color color, Level level, bool giftWrap=false, bool createBody=true)
virtual void Update(float deltaTime)