-
Notifications
You must be signed in to change notification settings - Fork 1
/
Object.cs
438 lines (395 loc) · 15.9 KB
/
Object.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using FarseerPhysics;
using FarseerPhysics.Common;
using FarseerPhysics.Common.Decomposition;
using FarseerPhysics.Factories;
using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;
namespace gameBungee
{
public class Object
{
//protected Vector2 Position;
protected List<Fixture> ObjectPhysic = new List<Fixture>();
protected AnimationObjet A_Animation;
public AnimationCharacter A_AnimationChar;
protected const Category PolygonCategory = Category.Cat2; //a changer avec un enum
protected const Category RectangleCategory = Category.Cat3; //a changer avec un enum
protected const Category CircleCategory = Category.Cat4; //a changer avec un enum
protected const Category PointCategory = Category.Cat5; //a changer avec un enum
protected Vector2 position;
private Color color = new Color(0,0,0,255);
public enum ObjectType {Cercle, Rectangle, Polygon, Point, Edge};
protected ObjectType objType;
public ObjectType objectType
{
get { return objType; }
}
public Color Color
{
get { return color; }
set { color = value; }
}
public Vector2 PositionInitial
{
get { return position; }
set { position = value; }
}
public int FixtureID
{
get { return FixtureObject.FixtureId; }
}
public Vector2 Position
{
get { return ObjectPhysic.First().Body.Position; }
}
public Fixture FixtureObject
{
get { return ObjectPhysic.First(); }
}
public BodyType Body_Type
{
get { return FixtureObject.Body.BodyType; }
set { foreach (Fixture fix in ObjectPhysic) fix.Body.BodyType = value;}
}
public void addDisplayer()
{
A_Animation = new AnimationObjet();
A_Animation.PlayAnimation(null, 0.0f, false);
}
public void addTexture(Texture2D tex, float frameTime)
{
if (A_AnimationChar == null)
A_AnimationChar = new AnimationCharacter();
if (A_AnimationChar.Animation == null)
{
Animation temp = new Animation(tex, frameTime, true);
A_AnimationChar.PlayAnimation(tex, frameTime, true);
}
else
{
A_AnimationChar.Animation.effectPlayer.Texture = tex;
A_AnimationChar.Animation.Texture = tex;
A_AnimationChar.Animation.FrameTime = frameTime;
A_AnimationChar.update();
}
}
public void addTexture(TextureImg texImg, float frameTime)
{
Texture2D img = null;
switch (texImg)
{
case TextureImg.Null:
break;
case TextureImg.tree1:
img = EnvironmentVariable.content.Load<Texture2D>("textures/tree1");
break;
case TextureImg.tree2:
img = EnvironmentVariable.content.Load<Texture2D>("textures/tree2");
break;
case TextureImg.tree3:
img = EnvironmentVariable.content.Load<Texture2D>("textures/tree3");
break;
case TextureImg.tree4:
img = EnvironmentVariable.content.Load<Texture2D>("textures/tree4");
break;
case TextureImg.tree5:
img = EnvironmentVariable.content.Load<Texture2D>("textures/tree5");
break;
case TextureImg.tree6:
img = EnvironmentVariable.content.Load<Texture2D>("textures/tree6");
break;
case TextureImg.tree7:
img = EnvironmentVariable.content.Load<Texture2D>("textures/tree7");
break;
case TextureImg.tree8:
img = EnvironmentVariable.content.Load<Texture2D>("textures/tree8");
break;
case TextureImg.tree9:
img = EnvironmentVariable.content.Load<Texture2D>("textures/tree9");
break;
case TextureImg.tree10:
img = EnvironmentVariable.content.Load<Texture2D>("textures/tree10");
break;
case TextureImg.grass:
img = EnvironmentVariable.content.Load<Texture2D>("textures/grass");
break;
case TextureImg.Idle:
img = EnvironmentVariable.content.Load<Texture2D>("textures/Idle");
break;
case TextureImg.Run:
img = EnvironmentVariable.content.Load<Texture2D>("textures/Run");
break;
}
if (A_AnimationChar == null)
A_AnimationChar = new AnimationCharacter();
if (A_AnimationChar.Animation == null)
{
Animation temp = new Animation(img, frameTime, true);
A_AnimationChar.PlayAnimation(img, frameTime, true);
}
else
{
A_AnimationChar.Animation.effectPlayer.Texture = img;
A_AnimationChar.Animation.Texture = img;
A_AnimationChar.Animation.FrameTime = frameTime;
A_AnimationChar.update();
}
}
public void delete()
{
EnvironmentVariable.worldPhysic.RemoveBody(ObjectPhysic.Last().Body);
}
}
public class ObjCercle : Object
{
private float radius;
public float Radius
{
get { return radius; }
set { radius = value; }
}
public ObjCercle(Vector2 InitialPosition, float InitialRadius, float InitialAngle, World worldPhysic, Color color, BodyType bodyType = BodyType.Static)
{
objType = ObjectType.Cercle;
this.ObjectPhysic.Add(FixtureFactory.CreateCircle(worldPhysic, InitialRadius, 1.0f, InitialPosition));
ObjectPhysic.Last().Body.Rotation = InitialAngle;
Body_Type = bodyType;
//Give it some bounce and friction
ObjectPhysic.Last().Restitution = 0.5f;
ObjectPhysic.Last().Friction = 1.0f;
if(bodyType != BodyType.Kinematic)
ObjectPhysic.Last().CollisionFilter.CollisionCategories = CircleCategory;
else
ObjectPhysic.Last().CollisionFilter.CollidesWith = PointCategory;
radius = InitialRadius;
this.Body_Type = bodyType;
PositionInitial = InitialPosition;
this.Color = color;
}
public ObjCercle(Vector2 InitialPosition, float InitialRadius, float InitialAngle, World worldPhysic, BodyType bodyType = BodyType.Static)
{
objType = ObjectType.Cercle;
this.ObjectPhysic.Add(FixtureFactory.CreateCircle(worldPhysic, InitialRadius, 1.0f, InitialPosition));
ObjectPhysic.Last().Body.Rotation = InitialAngle;
ObjectPhysic.Last().Body.BodyType = bodyType;
//Give it some bounce and friction
ObjectPhysic.Last().Restitution = 0.5f;
ObjectPhysic.Last().Friction = 1.0f;
if (bodyType != BodyType.Kinematic)
ObjectPhysic.Last().CollisionFilter.CollisionCategories = CircleCategory;
else
ObjectPhysic.Last().CollisionFilter.CollidesWith = PointCategory;
radius = InitialRadius;
Body_Type = bodyType;
PositionInitial = InitialPosition;
this.Color = Color.Black;
}
public ObjCercle(Vector2 InitialPosition, float InitialRadius, float InitialAngle, World worldPhysic, Body body)
{
objType = ObjectType.Cercle;
ObjectPhysic.Add(FixtureFactory.CreateCircle(InitialRadius, 1.0f, body, InitialPosition));
ObjectPhysic.First().Body.Rotation = InitialAngle;
ObjectPhysic.First().Body.BodyType = BodyType.Dynamic;
Body_Type = BodyType.Static;
//Give it some bounce and friction
ObjectPhysic.First().Restitution = 0.5f;
ObjectPhysic.First().Friction = 1.0f;
ObjectPhysic.First().CollisionFilter.CollisionCategories = CircleCategory;
//Position = InitialPosition;
radius = InitialRadius;
}
public void Draw()
{
A_Animation.DrawCircle(ObjectPhysic.First(), this.Color);
}
}
public class ObjRectange : Object
{
Vector2 size;
float angle;
float angleInit;
private TextureImg img = TextureImg.Null;
public TextureImg Texture
{
get { return img; }
set { img = value; }
}
new public Vector2 PositionInitial
{
get { return position-Size/2; }
set { position = value + Size/2; }
}
public float Angle
{
get { return angleInit; }
set { angleInit = value;
ObjectPhysic.Last().Body.Rotation = value * (float)Math.PI / 180.0f; }
}
public Vector2 Size
{
get { return size; }
set { size = value; }
}
public ObjRectange(Vector2 InitialPosition, float width, float height, float InitialAngle, BodyType bodyType)
{
objType = ObjectType.Rectangle;
angle = InitialAngle;
ObjectPhysic.Add(FixtureFactory.CreateRectangle(EnvironmentVariable.worldPhysic, width, height, 1.0f, InitialPosition));
ObjectPhysic.Last().Body.Rotation = InitialAngle * (float)Math.PI / 180.0f;
//Give it some bounce and friction
ObjectPhysic.Last().Restitution = 0.0f;
ObjectPhysic.Last().Friction = 1.0f;
Color = new Color(0, 0, 0, 255);
if (bodyType != BodyType.Kinematic)
ObjectPhysic.Last().CollisionFilter.CollisionCategories = RectangleCategory;
else
{
ObjectPhysic.Last().CollisionFilter.CollidesWith = PointCategory;
}
ObjectPhysic.Last().Body.BodyType = bodyType;
Body_Type = bodyType;
ObjectPhysic.Last().CollisionFilter.CollisionCategories = RectangleCategory;
PositionInitial = InitialPosition;
angleInit = InitialAngle;
size.X = width;
size.Y = height;
}
public ObjRectange(Vector2 InitialPosition, float width, float height, float InitialAngle, BodyType BodyType, Body body)
{
objType = ObjectType.Rectangle;
ObjectPhysic.Add(FixtureFactory.CreateRectangle(width, height, 1.0f, InitialPosition, body));
ObjectPhysic.First().Body.Rotation = InitialAngle * (float)Math.PI / 180.0f;
//Give it some bounce and friction
ObjectPhysic.First().Restitution = 0.0f;
ObjectPhysic.First().Friction = 1.0f;
Body_Type = BodyType;
ObjectPhysic.First().CollisionFilter.CollisionCategories = RectangleCategory;
angleInit = InitialAngle;
size.X = width;
size.Y = height;
}
public void Draw()
{
A_Animation.Draw(ObjectPhysic,Color);
}
public void Draw(GameTime gameTime, Boolean Flip)
{
if (A_AnimationChar != null)
A_AnimationChar.Draw(gameTime, ObjectPhysic.Last(), size, Flip);
}
}
public class ObjPolygon : Object
{
List<Vector2> vertlist;
public List<Vector2> VerticesList
{
get { return vertlist; }
}
public List<Fixture> FixtureList
{
get { return ObjectPhysic; }
}
public ObjPolygon(List<Vector2> vertlist, BodyType bodyType, World worldPhysic)
{
objType = ObjectType.Polygon;
this.vertlist = vertlist;
FarseerPhysics.Common.Vertices vert = new FarseerPhysics.Common.Vertices(vertlist);
List<FarseerPhysics.Common.Vertices> verts = EarclipDecomposer.ConvexPartition(vert);
ObjectPhysic = FixtureFactory.CreateCompoundPolygon(worldPhysic, verts, 1.0f);
Body_Type = bodyType;
foreach (Fixture fixt in ObjectPhysic)
{
fixt.Body.BodyType = bodyType;
if (bodyType != BodyType.Kinematic)
fixt.CollisionFilter.CollisionCategories = PolygonCategory;
else
{
fixt.CollisionFilter.CollisionCategories = PolygonCategory;
fixt.CollisionFilter.CollidesWith = PointCategory;
}
}
}
public void setFriction(float friction)
{
foreach (Fixture fix in ObjectPhysic)
{
fix.Friction = friction;
}
}
public void Draw()
{
A_Animation.Draw(ObjectPhysic, Color);
}
}
public class ObjEdge : Object
{
public ObjEdge(Vector2 start, Vector2 end, World worldPhysic)
{
objType = ObjectType.Edge;
ObjectPhysic.Add(FixtureFactory.CreateEdge(worldPhysic, start, end));
ObjectPhysic.First().Body.BodyType = BodyType.Kinematic;
}
public void Draw()
{
A_Animation.DrawEdge(ObjectPhysic);
}
}
public class ObjPoint : Object
{
private Fixture fixureCollidesWith;
public Object objCollidesWith;
public ObjPoint(Vector2 InitialPosition, float InitialRadius)
{
objType = ObjectType.Point;
this.ObjectPhysic.Add(FixtureFactory.CreateCircle(EnvironmentVariable.worldPhysic, InitialRadius, 1.0f, InitialPosition));
ObjectPhysic.Last().Body.BodyType = BodyType.Dynamic;
ObjectPhysic.Last().CollisionFilter.AddCollisionCategory(PointCategory);
ObjectPhysic.Last().OnCollision += new OnCollisionEventHandler(CollisionPointMouse);
}
public bool CollisionPointMouse(Fixture obj1, Fixture obj2, Contact contact)
{
if (fixureCollidesWith == null || fixureCollidesWith.ShapeType == ShapeType.Circle)
{
if (contact.FixtureA.FixtureId == this.FixtureObject.FixtureId)
fixureCollidesWith = contact.FixtureB;
else
fixureCollidesWith = contact.FixtureA;
foreach (ObjCercle c in EnvironmentVariable.lCercle)
{
if (c.FixtureObject.FixtureId == fixureCollidesWith.FixtureId)
{
objCollidesWith = c;
return false;
}
}
foreach (ObjRectange r in EnvironmentVariable.lRectangle)
{
if (r.FixtureObject.FixtureId == fixureCollidesWith.FixtureId)
{
objCollidesWith = r;
return false;
}
}
foreach (ObjPolygon p in EnvironmentVariable.lPolygon)
{
foreach (Fixture fix in p.FixtureList)
{
if (fix.FixtureId == fixureCollidesWith.FixtureId)
{
objCollidesWith = p;
return false;
}
}
}
}
return false;
}
}
}