-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhysicsObject.cs
67 lines (61 loc) · 2.25 KB
/
PhysicsObject.cs
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
using System;
using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Dynamics;
namespace Spacerunner3
{
public abstract class PhysicsObject : IObject, IDrawable
{
private static readonly MyColor _defaultPen = new MyColor(255, 255, 255);
protected static World world;
public abstract void Update(Scene scene, double dt);
public abstract Body? Body { get; }
public virtual void Draw(Graphics graphics, Camera camera)
{
var body = Body;
if (body == null)
{
return;
}
var pen = body.UserData as MyColor ?? _defaultPen;
foreach (var fixture in body.FixtureList)
{
var shape = fixture.Shape;
if (shape is PolygonShape poly)
{
for (var i = 0; i < poly.Vertices.Count; i++)
{
var vert1 = body.GetWorldPoint(poly.Vertices[i]).MyVec();
var vert2 = body.GetWorldPoint(poly.Vertices[(i + 1) % poly.Vertices.Count]).MyVec();
vert1 = camera.Transform(vert1.X, vert1.Y);
vert2 = camera.Transform(vert2.X, vert2.Y);
graphics.Line(vert1.Point, vert2.Point, pen.r, pen.g, pen.b);
}
}
else if (shape is EdgeShape edge)
{
var vert1 = body.GetWorldPoint(edge.Vertex1).MyVec();
var vert2 = body.GetWorldPoint(edge.Vertex2).MyVec();
vert1 = camera.Transform(vert1.X, vert1.Y);
vert2 = camera.Transform(vert2.X, vert2.Y);
graphics.Line(vert1.Point, vert2.Point, pen.r, pen.g, pen.b);
}
else if (shape is null)
{
continue;
}
else
{
throw new Exception("Unknown shape type " + shape.GetType().Name);
}
}
}
public virtual void OnDie(Scene scene)
{
var body = Body;
if (body != null)
{
world.RemoveBody(body);
}
}
}
}