-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathGameObject.cs
More file actions
612 lines (473 loc) · 14.7 KB
/
Copy pathGameObject.cs
File metadata and controls
612 lines (473 loc) · 14.7 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
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended;
using SharpDX.Direct2D1;
using SimplexResources.Objects;
using SimplexIde;
using Color = System.Drawing.Color;
using static SimplexCore.Sgml;
using SpriteBatch = Microsoft.Xna.Framework.Graphics.SpriteBatch;
namespace SimplexCore
{
[Serializable]
public class GameObject : IDisposable
{
public void Reset()
{
_position = __DefaultPosition;
Position = __DefaultPosition;
PositionPrevious = __DefaultPosition;
ImageAngle = __DefaultImageAngle;
ImageScale = __DefaultImageScale;
Speed = 0;
Direction = __DefaultDirection;
ImageIndex = __DefaultImageIndex;
ImageAlpha = __DefaultImageAlpha;
Colliders.Clear();
}
public struct AlarmStruct
{
public int Steps;
public bool Running;
}
public AlarmStruct[] Alarms = new AlarmStruct[16];
[XmlIgnore]
public Sprite Sprite;
private Vector2 _position;
public float X
{
get => _position.X;
set
{
_position.X = value;
if (ImageAngle == 0)
{
UpdateRectangle();
}
else
{
UpdateRotatedRectangle();
}
PositionPrevious = Position;
}
}
public float Y
{
get => _position.Y;
set
{
_position.Y = value;
if (ImageAngle == 0)
{
UpdateRectangle();
}
else
{
UpdateRotatedRectangle();
}
PositionPrevious = Position;
}
}
public void __Store()
{
__DefaultImageAngle = ImageAngle;
__DefaultImageScale = ImageScale;
__DefaultPosition = Position;
__DefaultDirection = Direction;
__DefaultImageIndex = ImageIndex;
__DefaultImageAlpha = ImageAlpha;
}
public Vector2 Position
{
get => _position;
set
{
_position = value;
if (ImageAngle == 0)
{
UpdateRectangle();
}
else
{
UpdateRotatedRectangle();
}
PositionPrevious = Position;
}
}
[XmlIgnore]
public Type OriginalType;
[XmlIgnore]
public string[] EditorOptions = {"Send backward", "Send forward", "Delete"};
[XmlIgnore]
public Rectangle CollisionContainer = Rectangle.Empty;
[XmlIgnore]
public Control[] EditorProperties;
/*
* magic constants:
* [magic_separator]
*/
public string TypeString;
public float ImageIndex
{
get => _imageIndex;
set
{
_imageIndex = value;
if (_imageIndex > FramesCount)
{
_imageIndex = 0;
}
_imageIndex = _imageIndex.Clamp(0, FramesCount);
}
}
public float ImageAngle;
public float ImageAngleTarget;
public float TransformSpeed;
public Vector2 ImageScale;
public Vector2 ImageScaleTarget;
public float ImageAlpha;
public string LayerName;
public float ImageSpeed;
public float Direction;
public Vector2 PositionStart;
public Vector2 PositionPrevious;
public float Speed;
public float Friction;
public Vector2 Gravity;
public Vector2 Velocity;
public float Mass;
public bool Persistent;
public string PersistentLayer;
public Vector2 ImageOrigin;
public Vector2 __DefaultPosition;
public float __DefaultImageAngle;
public float __DefaultDirection;
public Vector2 __DefaultImageScale;
public float __DefaultImageIndex;
public float __DefaultImageAlpha;
[XmlIgnore]
public RotatedRectangle rr = null;
[XmlIgnore]
private List<ColliderBase> _colliders = null;
[XmlIgnore]
public List<ColliderBase> Colliders
{
get
{
return _colliders ?? (_colliders = new List<ColliderBase>());
}
}
[XmlIgnore]
public ObjectLayer Layer;
[XmlIgnore]
public string EditorPath;
[XmlIgnore]
public int FramesCount;
[XmlIgnore]
public Guid Id;
[XmlIgnore]
private List<ColliderDescriptor> _descriptors;
[XmlIgnore]
public List<ColliderDescriptor> CollidersActive
{
get
{
return _descriptors ?? (_descriptors = new List<ColliderDescriptor>());
}
}
private float _imageIndex;
[XmlIgnore]
public Vector2 TempPosition = Vector2.Zero;
public void UpdateRectangle()
{
CollisionContainer.X = (int)(_position.X - (ImageOrigin.X * ImageScale.X));
CollisionContainer.Y = (int)(_position.Y - (ImageOrigin.Y * ImageScale.Y));
CollisionContainer.Width = (int) (Sprite.ImageRectangle.Width * ImageScale.X);
CollisionContainer.Height = (int)(Sprite.ImageRectangle.Height * ImageScale.Y);
}
public bool CollidingWithPoint(Vector2 point)
{
if (ImageAngle == 0)
{
return CollisionContainer.Contains(point);
}
return rr.Contains(point);
}
void UpdateRotatedRectangle()
{
if (rr == null)
{
rr = new RotatedRectangle(Vector2.One, Vector2.One, Vector2.One, Vector2.One);
}
float xdif = Position.X - PositionPrevious.X;
float ydif = Position.Y - PositionPrevious.Y;
rr.Point1 = new Vector2(rr.Point1.X + xdif, rr.Point1.Y + ydif);
rr.Point2 = new Vector2(rr.Point2.X + xdif, rr.Point2.Y + ydif);
rr.Point3 = new Vector2(rr.Point3.X + xdif, rr.Point3.Y + ydif);
rr.Point4 = new Vector2(rr.Point4.X + xdif, rr.Point4.Y + ydif);
}
public GameObject()
{
ImageScale = Vector2.One;
ImageScaleTarget = Vector2.One;
ImageAngle = 0;
ImageAngleTarget = 0;
ImageIndex = 0;
TransformSpeed = 0.2f;
Sprite = new Sprite();
Sprite.TextureSource = "unknown";
ImageAlpha = 1;
ImageSpeed = 1;
Gravity = new Vector2(0, 270);
Speed = 0;
Velocity = Vector2.Zero;
Friction = 0;
Direction = 0;
Id = Guid.NewGuid();
if (ImageAngle == 0)
{
UpdateRectangle();
}
else
{
UpdateRotatedRectangle();
}
}
public void UpdateImageAngle()
{
ImageAngle = SimplexMath.Lerp(ImageAngle, ImageAngleTarget, TransformSpeed);
}
public void UpdateImageScale()
{
ImageScale = new Vector2(SimplexMath.Lerp(ImageScale.X, ImageScaleTarget.X, TransformSpeed), SimplexMath.Lerp(ImageScale.Y, ImageScaleTarget.Y, TransformSpeed));
}
public void UpdateColliders()
{
foreach (ColliderBase c in Colliders)
{
if (c is ColliderRectangle)
{
ColliderRectangle cr = c as ColliderRectangle;
if (c.AttachToRoot && cr.Root != null)
{
cr.Collision.X = c.Root.X;
cr.Collision.Y = c.Root.Y;
}
cr.CollisionTransformed = new RectangleF(Position.X + cr.Collision.X, Position.Y + cr.Collision.Y, cr.Collision.Width, cr.Collision.Height);
}
if (c is ColliderCircle)
{
ColliderCircle cr = c as ColliderCircle;
if (c.AttachToRoot && cr.Root != null)
{
cr.Position.X = c.Root.X;
cr.Position.Y = c.Root.Y;
}
cr.Position = new Vector2(Position.X, Position.Y);
}
}
}
public void RegisterCollider(string c, Type cT, string n, Action<GameObject, GameObject> method)
{
CollisionPairExtended cp1 = new CollisionPairExtended();
CollisionPair cp2 = new CollisionPair();
cp1.ColliderName = c;
cp1.Object = GetType();
cp1.CollisionAction = method;
cp2.ColliderName = n;
cp2.Object = cT;
CollisionsTree.DefinedCollisionPairs.Add(cp1, cp2);
}
public void DrawStart(SpriteBatch s, DynamicVertexBuffer vb, BasicEffect be, Matrix m, GameObject currentObject)
{
Sgml.sb = s;
Sgml.vb = vb;
Sgml.be = be;
Sgml.m = m;
Sgml.currentObject = currentObject;
}
public virtual void EvtSetup()
{
}
// Editor events
public virtual void EvtRegisterCollisions()
{
}
public virtual void EvtDraw()
{
//s.Draw(objectTexture, Position, Microsoft.Xna.Framework.Color.White * ImageAlpha);
}
public virtual void EvtCreate()
{
}
public virtual void EvtCreateEnd()
{
}
public virtual void EvtLoad()
{
}
public virtual void EvtSave()
{
}
public virtual void EvtDelete()
{
}
public virtual void EvtStep()
{
}
public virtual void EvtAlarm0()
{
}
public virtual void EvtAlarm1()
{
}
public virtual void EvtAlarm2()
{
}
public virtual void EvtAlarm3()
{
}
public virtual void EvtAlarm4()
{
}
public virtual void EvtAlarm5()
{
}
public virtual void EvtAlarm6()
{
}
public virtual void EvtAlarm7()
{
}
public virtual void EvtAlarm8()
{
}
public virtual void EvtAlarm9()
{
}
public virtual void EvtAlarm10()
{
}
public virtual void EvtAlarm11()
{
}
public virtual void EvtAlarm12()
{
}
public virtual void EvtAlarm13()
{
}
public virtual void EvtAlarm14()
{
}
public virtual void EvtAlarm15()
{
}
public virtual void EvtAlarm16()
{
}
public virtual void EvtContextMenuSelected(ToolStripItem e)
{
}
public virtual void EvtDrawToSurfaces()
{
}
// Public state events
public void UpdateState()
{
// Update timelines
/* foreach (TimeLines line in all_time_lines)
{
line.Count++;
}*/
// Update alarms
for (var i = 0; i < 16; i++)
{
if (Alarms[i].Running)
{
Alarms[i].Steps--;
if (Alarms[i].Steps <= 0)
{
MethodInfo alarmMethod = OriginalType.GetMethod("EvtAlarm" + i);
alarmMethod.Invoke(this, null);
Alarms[i].Steps = -1;
Alarms[i].Running = false;
}
}
}
UpdateImageScale();
UpdateImageAngle();
ImageIndex += (float)ImageSpeed;
// Apply friction
if (abs(Speed) > 0)
{
if (Speed > 0)
{
Speed -= Friction;
}
else
{
Speed += Friction;
}
if (abs(Speed) < Friction)
{
Speed = 0;
}
}
// Update velocity
Velocity = new Vector2(Velocity.X + (float)lengthdir_x(-Gravity.X, Gravity.Y), Velocity.Y + (float)lengthdir_y(-Gravity.X, Gravity.Y));
Position = new Vector2(Position.X + Velocity.X + (float)lengthdir_x(Speed, Direction), Position.Y + Velocity.Y + (float)lengthdir_y(Speed, Direction));
}
// Events
public virtual void OnCreate()
{
}
public virtual void OnStep()
{
}
public virtual void OnDraw(SpriteBatch sb)
{
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects).
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
disposedValue = true;
}
}
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
// ~GameObject() {
// // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
// Dispose(false);
// }
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
// GC.SuppressFinalize(this);
}
#endregion
}
}