This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
/
Anima.cs
1229 lines (1116 loc) · 43.5 KB
/
Anima.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
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
The MIT License (MIT)
Copyright (c) 2016 JustBurn
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using Sandbox.ModAPI;
using Sandbox.Game.Entities;
using VRage.ObjectBuilders;
using VRageMath;
using VRage.ModAPI;
using VRage.Game.Entity;
using VRage.Library.Utils;
//using VRage.FileSystem;
namespace AnimaScript
{
/// <summary>
/// Anima main class.
/// </summary>
public class Anima
{
private MyEntity m_entity;
private string m_modelsFolder;
private List<AnimaPart> m_partList;
private bool m_inRange;
private MyGameTimer m_gameTimer;
private double m_lastElapse;
private static int m_instances = 0;
/// <summary>
/// Playback mode.
/// </summary>
public enum Playback
{
/// <summary>Halt playback.</summary>
/// <remarks>When used in Play() it will be the same as Stop().</remarks>
HALT,
/// <summary>Play once.</summary>
/// <remarks>Playback is stopped once it hit the outside of animation.</remarks>
ONCE,
/// <summary>Loop play.</summary>
/// <remarks>Loop infinitely, to stop call Stop().</remarks>
LOOP,
/// <summary>Ping-pong play.</summary>
/// <remarks>Loop infinitely, invert part speed in each border, to stop call Stop().</remarks>
PINGPONG,
}
/// <summary>
/// Construct Anima class.
/// </summary>
/// <remarks>Don't forget to call Init(MyEntity, string, string) to initialize!</remarks>
public Anima()
{
m_entity = null;
m_modelsFolder = null;
m_partList = new List<AnimaPart>();
m_inRange = true;
m_gameTimer = new MyGameTimer();
m_lastElapse = m_gameTimer.Elapsed.Seconds;
}
/// <summary>
/// Script version.
/// </summary>
public struct Version
{
/// <summary>Major version.</summary>
public const int Major = 0;
/// <summary>Minor version.</summary>
public const int Minor = 5;
}
/// <summary>
/// Return true if is a dedicated server (no game graphics)
/// </summary>
public static bool DedicatedServer
{
get { return MyAPIGateway.Utilities.IsDedicated; }
}
/// <summary>
/// Return root entity.
/// </summary>
public MyEntity Entity
{
get { return m_entity; }
}
/// <summary>
/// Enable Anima parts, if disabled all parts won't be updated and will be invisible. Useful when used together with TestPlayerDistance(double).
/// </summary>
public bool Enable
{
get { return m_inRange; }
set
{
if (value != m_inRange)
{
m_inRange = value;
foreach (var part in m_partList)
{
part.Enable_Recv(value);
}
}
}
}
/// <summary>
/// Return models folder (FOR DEBUGGING)
/// </summary>
public string ModelsFolder
{
get { return m_modelsFolder; }
}
/// <summary>
/// Return number of instrances (FOR DEBUGGING)
/// </summary>
public int Instances
{
get { return m_instances; }
}
/// <summary>
/// Return elapsed time in seconds since last call to GetElapsed().
/// </summary>
public double GetElapsed()
{
double elapse = m_gameTimer.Elapsed.Seconds;
double value = elapse - m_lastElapse;
m_lastElapse = elapse;
return value;
}
/// <summary>
/// Initialize Anima class.
/// </summary>
/// <param name="entity">root block entity.</param>
/// <param name="modName">mod name in workshop.</param>
/// <param name="altModName">alternative mod name, for local mod.</param>
/// <returns>true on success.</returns>
/// <remarks>Current game version doesn't allow to check directories, if model show in-game like a black cube make sure the modName and/or altModName are correct.</remarks>
public bool Init(MyEntity entity, string modName, string altModName)
{
if (entity == null) return false;
if (m_entity != null) return false;
// Scan for publisher ID
ulong publishID = 0;
var mods = MyAPIGateway.Session.GetCheckpoint("null").Mods;
foreach (var mod in mods)
{
if (modName == mod.FriendlyName) publishID = mod.PublishedFileId;
}
// Figure out mod's directory
// A better way was if MyAPIGateway reported the current mod path
if (publishID != 0)
{
m_modelsFolder = Path.GetFullPath(string.Format(@"{0}\{1}.sbm\Models\", MyAPIGateway.Utilities.GamePaths.ModsPath, publishID.ToString()));
/*if (!Directory.Exists(m_modelsFolder))
{
// Extracted sbm into local copy
m_modelsFolder = Path.GetFullPath(string.Format(@"{0}\{1}\Models\", MyAPIGateway.Utilities.GamePaths.ModsPath, publishID.ToString()));
}*/
}
else m_modelsFolder = Path.GetFullPath(string.Format(@"{0}\{1}\Models\", MyAPIGateway.Utilities.GamePaths.ModsPath, altModName));
/*if (!MyFileSystem.DirectoryExists(m_modelsFolder) && !string.IsNullOrEmpty(modName))
{
m_modelsFolder = Path.GetFullPath(string.Format(@"{0}\{1}\Models\", MyAPIGateway.Utilities.GamePaths.ModsPath, modName));
}
if (!MyFileSystem.DirectoryExists(m_modelsFolder) && !string.IsNullOrEmpty(altModName))
{
m_modelsFolder = Path.GetFullPath(string.Format(@"{0}\{1}\Models\", MyAPIGateway.Utilities.GamePaths.ModsPath, altModName));
}
if (!MyFileSystem.DirectoryExists(m_modelsFolder))
{
return false;
}*/
// Done!
m_entity = entity;
m_instances++;
return true;
}
/// <summary>
/// Dispose unused resources (mostly animation sequences)
/// </summary>
public void Dispose()
{
if (m_entity == null) return;
m_instances--;
if (m_instances <= 0) AnimaSeqManager.DiscardAll();
}
/// <summary>
/// Add anima part for animation.
/// </summary>
/// <param name="parent">parent part, null for root.</param>
/// <param name="modelFile">Model file name inside Models folder, excluding extension.</param>
/// <param name="smoothAnim">smooth animation, linear lerp between keyframes.</param>
/// <param name="visible">initialize it visible?</param>
/// <returns>part or null for failure.</returns>
public AnimaPart AddPart(AnimaPart parent, string modelFile, bool smoothAnim = false, bool visible = true)
{
if (m_entity == null) return null;
// Create part
MyEntity parentEntity = (parent != null) ? parent.Entity : m_entity;
MyEntity part_entity = new MyEntity();
part_entity.Save = false;
part_entity.IsPreview = true;
part_entity.Init(null, m_modelsFolder + modelFile + ".mwm", parentEntity, null, null);
part_entity.Render.EnableColorMaskHsv = true;
part_entity.Render.ColorMaskHsv = m_entity.Render.ColorMaskHsv;
part_entity.Render.PersistentFlags = MyPersistentEntityFlags2.CastShadows;
part_entity.PositionComp.LocalMatrix = Matrix.Identity;
part_entity.Flags = EntityFlags.Visible | EntityFlags.NeedsDraw | EntityFlags.NeedsDrawFromParent | EntityFlags.InvalidateOnMove;
part_entity.OnAddedToScene(parentEntity);
AnimaPart part = new AnimaPart(this, parent, part_entity);
if (!visible) part.Visible = false;
m_partList.Add(part);
return part;
}
/// <summary>
/// Add anima part for animation, parent will be one of root's subpart.
/// </summary>
/// <param name="subpartName">root subpart name.</param>
/// <param name="modelFile">>Model file name inside Models folder, excluding extension.</param>
/// <param name="smoothAnim">smooth animation, linear lerp between keyframes.</param>
/// <param name="visible">initialize it visible?</param>
/// <returns>part or null for failure.</returns>
public AnimaPart AddPartToSubpart(string subpartName, string modelFile, bool smoothAnim = false, bool visible = true)
{
if (m_entity == null) return null;
if (!m_entity.Subparts.ContainsKey(subpartName)) return null;
// Create part
MyEntity parentEntity = m_entity.Subparts[subpartName];
MyEntity part_entity = new MyEntity();
part_entity.Save = false;
part_entity.IsPreview = true;
part_entity.Init(null, m_modelsFolder + modelFile + ".mwm", parentEntity, null, null);
part_entity.Render.EnableColorMaskHsv = true;
part_entity.Render.ColorMaskHsv = m_entity.Render.ColorMaskHsv;
part_entity.Render.PersistentFlags = MyPersistentEntityFlags2.CastShadows;
part_entity.PositionComp.LocalMatrix = Matrix.Identity;
part_entity.Flags = EntityFlags.Visible | EntityFlags.NeedsDraw | EntityFlags.NeedsDrawFromParent | EntityFlags.InvalidateOnMove;
part_entity.OnAddedToScene(parentEntity);
AnimaPart part = new AnimaPart(this, null, part_entity);
if (!visible) part.Visible = false;
m_partList.Add(part);
return part;
}
// Hmm... can't exactly remove subpart easily because of parenting (will need to invalidate all childs MyEntity/AnimaParts/List<AnimaParts>)...
/// <summary>
/// Test player distance with block.
/// </summary>
/// <param name="maxDistance">distance in meters.</param>
/// <returns>true if inside range, false if outside.</returns>
public bool TestPlayerDistance(double maxDistance)
{
return Vector3D.DistanceSquared(m_entity.WorldMatrix.Translation, MyAPIGateway.Session.Camera.WorldMatrix.Translation) < (maxDistance * maxDistance);
}
/// <summary>
/// Set smooth animation on all parts.
/// </summary>
/// <param name="smoothAnim">Enable smooth animation, linear lerp between keyframes.</param>
public void SetSmoothAnimation(bool smoothAnim)
{
if (m_entity == null) return;
foreach (var part in m_partList)
{
part.SmoothAnimation = smoothAnim;
}
}
// AUTOMATIC ANIMATIONS
/// <summary>
/// Start playback on all parts.
/// </summary>
/// <param name="playMode">any of playback mode. Enum of Anima.Playback.</param>
/// <param name="cursorPos">starting cursor position (in keyframes).</param>
/// <remarks>cursor position will be clamped to part's animation range.</remarks>
public void Play(Playback playMode, float cursorPos = 0f)
{
if (m_entity == null) return;
foreach (var part in m_partList)
{
part.Play(playMode, cursorPos);
}
}
/// <summary>
/// Start playback on all parts.
/// </summary>
/// <param name="playMode">any of playback mode. Enum of Anima.Playback.</param>
/// <param name="cursorPos">starting cursor position (normalized between 0.0f to 1.0f).</param>
/// <remarks>cursor position will be clamped to part's animation range.</remarks>
public void PlayNormal(Playback playMode, float cursorPos = 0f)
{
if (m_entity == null) return;
foreach (var part in m_partList)
{
part.PlayNormal(playMode, cursorPos);
}
}
/// <summary>
/// Stop playback on all parts.
/// </summary>
public void Stop()
{
if (m_entity == null) return;
foreach (var part in m_partList)
{
part.Stop();
}
}
/// <summary>
/// Resume playback on all parts.
/// </summary>
public void Resume()
{
if (m_entity == null) return;
foreach (var part in m_partList)
{
part.Resume();
}
}
/// <summary>
/// Pause playback on all parts.
/// </summary>
public void Pause()
{
if (m_entity == null) return;
foreach (var part in m_partList)
{
part.Pause();
}
}
/// <summary>
/// Process the animation on all parts.
/// </summary>
/// <param name="elapsedTime">period elapsed since last call (in seconds).</param>
public void Update(double elapsedTime)
{
if (m_entity == null) return;
foreach (var part in m_partList)
{
part.Update(elapsedTime);
}
}
/// <summary>
/// Apply transformations on all parts.
/// </summary>
/// <param name="invokeCallback">if false, OnTransform callback won't be invoked.</param>
public void ApplyTransformations(bool invokeCallback)
{
if (m_entity == null) return;
foreach (var part in m_partList)
{
part.ApplyTransformations(invokeCallback);
}
}
}
/// <summary>
/// Anima part class.
/// </summary>
public class AnimaPart
{
private Anima m_anima;
private MyEntity m_entity;
private AnimaPart m_parent;
private bool m_smoothAnim;
private bool m_enabled;
private bool m_visible;
// Callbacks
private Action<AnimaPart> m_onTransform;
private Action<AnimaPart> m_onComplete;
// Automatic play
private AnimaSeqBase m_seq;
private Anima.Playback m_playMode;
private Anima.Playback m_playMode2;
private float m_cursorPos;
private float m_speed;
private int m_lastCursorPos;
// Coloring
private bool m_disableRootColor;
// Transformations
private Vector3 m_position;
private Quaternion m_rotation;
private Vector3 m_scale;
/// <summary>
/// Anima main class constructor.
/// </summary>
public AnimaPart(Anima anima, AnimaPart parent, MyEntity entity)
{
m_anima = anima;
m_entity = entity;
m_parent = parent;
m_smoothAnim = false;
m_enabled = true;
m_visible = true;
m_onTransform = null;
m_onComplete = null;
m_seq = null;
m_playMode = Anima.Playback.HALT;
m_playMode2 = Anima.Playback.HALT;
m_cursorPos = 0f;
m_speed = 1f;
m_lastCursorPos = 0;
m_disableRootColor = false;
m_position = Vector3.Zero;
m_rotation = Quaternion.Identity;
m_scale = Vector3.One;
}
/// <summary>
/// Get game's entity.
/// </summary>
public MyEntity Entity
{
get { return m_entity; }
}
/// <summary>
/// Get parent part.
/// </summary>
/// <remarks>returns null if parent is root or subpart.</remarks>
public AnimaPart Parent
{
get { return m_parent; }
}
/// <summary>
/// Enable smooth animation, linear lerp between keyframes.
/// </summary>
public bool SmoothAnimation
{
get { return m_smoothAnim; }
set { m_smoothAnim = value; }
}
/// <summary>
/// Is part active?
/// </summary>
/// <remarks>While this is false, Update() and transformations aren't applied.</remarks>
public bool Active
{
get { return m_enabled; }
}
/// <summary>
/// Visible object? Writing will affect all child's parts!
/// </summary>
/// <remarks>will return false if not active!</remarks>
public bool Visible
{
get { return m_entity.Render.Visible; }
set
{
if (m_visible != value)
{
m_visible = value;
m_entity.Render.Visible = m_visible && m_enabled;
}
}
}
/// <summary>
/// Set/get sequence for playback. Use (sequence_class_name).Adquire().
/// </summary>
public AnimaSeqBase Sequence
{
get { return m_seq; }
set { m_seq = value; }
}
/// <summary>
/// Get playback mode.
/// </summary>
public Anima.Playback Playback
{
get { return m_playMode; }
}
/// <summary>
/// Get cursor position in keyframes.
/// </summary>
public float Cursor
{
get { return m_cursorPos; }
}
/// <summary>
/// Get cursor position in normalized range.
/// </summary>
public float CursorNormal
{
get
{
if (m_seq == null) return 0f;
if (m_cursorPos <= m_seq.FrameStart) return 0f;
if (m_cursorPos >= m_seq.FrameEnd) return 1f;
return (m_cursorPos - m_seq.FrameStart) / m_seq.FramePeriod;
}
}
/// <summary>
/// Set/get playback speed (0.5 = half speed).
/// </summary>
public float Speed
{
get { return m_speed; }
set { m_speed = value; }
}
/// <summary>
/// Enable/disable custom color.
/// </summary>
/// <remarks>If disabled, color is matched to parent in Update().</remarks>
public bool EnableColor
{
get { return m_disableRootColor; }
set { m_disableRootColor = value; }
}
/// <summary>
/// Set/get part color, in Hue/Saturation/Value.
/// </summary>
/// <remarks>Color can only be changed if EnableColor is true.</remarks>
public Vector3 ColorHsv
{
get
{
if (m_entity == null) return Vector3.Zero;
return m_entity.Render.ColorMaskHsv;
}
set
{
if (m_entity == null || !m_disableRootColor) return;
m_entity.Render.ColorMaskHsv = value;
}
}
/// <summary>
/// Set/get position. (local matrix)
/// </summary>
public Vector3 Position
{
get { return m_position; }
set { m_position = value; }
}
/// <summary>
/// Set/get rotation. (local matrix)
/// </summary>
public Quaternion Rotation
{
get { return m_rotation; }
set { m_rotation = value; }
}
/// <summary>
/// Set/get scale. (local matrix)
/// </summary>
public Vector3 Scale
{
get { return m_scale; }
set { m_scale = value; }
}
/// <summary>
/// Set/get callback that is called just before sending transform.
/// </summary>
/// <remarks>This can be useful to perform procedual tranformations together with animation.</remarks>
public Action<AnimaPart> OnTransform
{
get { return m_onTransform; }
set { m_onTransform = value; }
}
/// <summary>
/// Set/get callback that is called when the cursor goes outside the animation range.
/// </summary>
/// <remarks>Called when the animation ends or loops.</remarks>
public Action<AnimaPart> OnComplete
{
get { return m_onComplete; }
set { m_onComplete = value; }
}
/// <summary>
/// Start playback.
/// </summary>
/// <param name="playMode">any of playback mode, enum of Anima.Playback.</param>
/// <param name="cursorPos">starting cursor position (in keyframes).</param>
/// <remarks>cursor position will be clamped to animation range.</remarks>
public void Play(Anima.Playback playMode, float cursorPos = 0f)
{
if (m_entity == null || m_seq == null) return;
m_playMode = playMode;
m_playMode2 = playMode;
if (cursorPos < m_seq.FrameStart) cursorPos = m_seq.FrameStart;
else if (cursorPos > m_seq.FrameEnd) cursorPos = m_seq.FrameEnd;
m_cursorPos = cursorPos;
ProcAutoAnimation();
}
/// <summary>
/// Start playback with normalized cursor.
/// </summary>
/// <param name="playMode">any of playback mode, enum of Anima.Playback.</param>
/// <param name="cursorPos">starting cursor position (normalized between 0 to 1).</param>
/// <remarks>cursor position will be clamped to animation range.</remarks>
public void PlayNormal(Anima.Playback playMode, float cursorPos = 0f)
{
if (m_entity == null || m_seq == null) return;
m_playMode = playMode;
m_playMode2 = playMode;
if (cursorPos < 0f) cursorPos = 0f;
else if (cursorPos > 1f) cursorPos = 1f;
m_cursorPos = cursorPos * m_seq.FramePeriod + m_seq.FrameStart;
ProcAutoAnimation();
}
/// <summary>
/// Stop playback.
/// </summary>
public void Stop()
{
if (m_entity == null) return;
m_playMode = Anima.Playback.HALT;
m_playMode2 = Anima.Playback.HALT;
}
/// <summary>
/// Resume playback.
/// </summary>
public void Resume()
{
if (m_entity == null) return;
if (m_playMode != Anima.Playback.HALT) return;
m_playMode = m_playMode2;
if (m_playMode != Anima.Playback.HALT) ProcAutoAnimation();
}
/// <summary>
/// Pause playback.
/// </summary>
public void Pause()
{
if (m_entity == null) return;
if (m_playMode == Anima.Playback.HALT) return;
m_playMode2 = m_playMode;
m_playMode = Anima.Playback.HALT;
}
/// <summary>
/// Set "Emissive" material.
/// </summary>
/// <param name="emissivity">Emissivity between 0.0f to 1.0f, being 1.0f full emissive.</param>
/// <param name="emissivePartColor">Emissive color, multiplied with diffuse texture.</param>
public void SetEmissive(float emissivity, Color emissivePartColor)
{
if (m_entity == null) return;
MyCubeBlockEmissive.SetEmissiveParts(m_entity, emissivity, emissivePartColor, Color.White);
}
/// <summary>
/// Set "Emissive" and "Display" material.
/// </summary>
/// <param name="emissivity">Emissivity between 0.0f to 1.0f, being 1.0f full emissive.</param>
/// <param name="emissivePartColor">"Emissive" color, multiplied with diffuse texture.</param>
/// <param name="displayPartColor">"Display" color, multiplied with diffuse texture.</param>
/// <remarks>Both materials are affected by emissivity, since Keen never uses "Display" material they may remove it in the future, use it at your own risk.</remarks>
public void SetEmissive(float emissivity, Color emissivePartColor, Color displayPartColor)
{
if (m_entity == null) return;
MyCubeBlockEmissive.SetEmissiveParts(m_entity, emissivity, emissivePartColor, displayPartColor);
}
/// <summary>
/// Process the animation.
/// </summary>
/// <param name="elapsedTime">period elapsed since last call (in seconds).</param>
/// <returns>true on success</returns>
public void Update(double elapsedTime)
{
if (m_entity == null) return;
float elapsed = (float)elapsedTime;
if (elapsed == 0f || !m_enabled) return;
// Apply root color
if (!m_disableRootColor)
{
m_entity.Render.ColorMaskHsv = m_anima.Entity.Render.ColorMaskHsv;
}
// Apply animation
if (m_playMode != Anima.Playback.HALT)
{
if (m_seq == null)
{
m_playMode = Anima.Playback.HALT;
m_playMode2 = Anima.Playback.HALT;
return;
}
// Timelapse
bool callLoop = false;
m_cursorPos += elapsed * m_seq.FrameRate * m_speed;
if (m_cursorPos < m_seq.FrameStart || m_cursorPos >= (m_seq.FrameEnd + 1f))
{
if (m_playMode == Anima.Playback.ONCE)
{
m_playMode = Anima.Playback.HALT;
m_playMode2 = Anima.Playback.HALT;
}
else if (m_playMode == Anima.Playback.PINGPONG)
{
m_speed = -m_speed;
}
float newPos = (m_cursorPos - m_seq.FrameStart) % m_seq.FramePeriod;
if (newPos < 0f) newPos = m_seq.FramePeriod + newPos;
m_cursorPos = newPos + m_seq.FrameStart;
callLoop = true;
}
// Call callback
if (callLoop && m_onComplete != null) m_onComplete(this);
// Get keyframe and apply transformation if needed
if (m_smoothAnim || ((int)m_cursorPos != m_lastCursorPos)) ProcAutoAnimation();
}
}
/// <summary>
/// Apply transformations.
/// </summary>
/// <param name="invokeCallback">if false, OnTransform callback won't be invoked.</param>
public void ApplyTransformations(bool invokeCallback)
{
if (m_entity == null || !m_enabled) return;
if (m_onTransform != null && invokeCallback) m_onTransform(this);
m_entity.PositionComp.LocalMatrix = Matrix.CreateFromTransformScale(m_rotation, m_position, m_scale);
}
/// <summary>
/// Apply local matrix.
/// </summary>
/// <param name="matrix">local matrix</param>
/// <param name="invokeCallback">if false, OnTransform callback won't be invoked.</param>
/// <remarks>transformations won't be extracted from the matrix.</remarks>
public void ApplyLocalMatrix(ref Matrix matrix, bool invokeCallback)
{
if (m_entity == null || !m_enabled) return;
if (m_onTransform != null && invokeCallback) m_onTransform(this);
m_entity.PositionComp.LocalMatrix = matrix;
}
private void ProcAutoAnimation()
{
if (!m_enabled) return;
if (m_seq == null)
{
m_playMode = Anima.Playback.HALT;
m_playMode2 = Anima.Playback.HALT;
return;
}
// Get keyframe and apply transformation
AnimaSeqBase.Keyframe sframe;
m_lastCursorPos = (int)m_cursorPos;
if (!m_seq.GetKeyframeData(m_lastCursorPos, out sframe))
{
m_playMode = Anima.Playback.HALT;
m_playMode2 = Anima.Playback.HALT;
return;
}
// Smooth animation
if (m_smoothAnim)
{
AnimaSeqBase.Keyframe sframe2;
if (!m_seq.GetKeyframeData(m_lastCursorPos + 1, out sframe2))
{
m_playMode = Anima.Playback.HALT;
m_playMode2 = Anima.Playback.HALT;
return;
}
float lerpValue = m_cursorPos - (float)m_lastCursorPos;
sframe.position = Vector3.Lerp(sframe.position, sframe2.position, lerpValue);
sframe.rotation = Quaternion.Lerp(sframe.rotation, sframe2.rotation, lerpValue);
sframe.scale = Vector3.Lerp(sframe.scale, sframe2.scale, lerpValue);
}
// Apply trasformation
m_position = sframe.position;
m_rotation = sframe.rotation;
m_scale = sframe.scale;
if (m_onTransform != null) m_onTransform(this);
m_entity.PositionComp.LocalMatrix = Matrix.CreateFromTransformScale(m_rotation, m_position, m_scale);
}
/// <summary>
/// Used by Anima.Enable, shouldn't be called directly.
/// </summary>
public void Enable_Recv(bool enable)
{
if (m_enabled != enable)
{
m_enabled = enable;
m_entity.Render.Visible = m_visible && m_enabled;
}
}
}
/// <summary>
/// Workaround class to expose UpdateEmissiveParts() to public, useful for changing "Emissive" material.
/// </summary>
public class MyCubeBlockEmissive : MyCubeBlock
{
/// <summary>
/// Set emissive parts
/// </summary>
/// <param name="entity">entity to apply changes</param>
/// <param name="emissivity">Emissivity between 0.0f to 1.0f, being 1.0f full emissive.</param>
/// <param name="emissivePartColor">"Emissive" color, multiplied with diffuse texture.</param>
/// <param name="displayPartColor">"Display" color, multiplied with diffuse texture.</param>
/// <remarks>Both materials are affected by emissivity, since Keen never uses "Display" material they may remove it in the future, use it at your own risk.</remarks>
public static void SetEmissiveParts(MyEntity entity, float emissivity, Color emissivePartColor, Color displayPartColor)
{
if (entity != null) UpdateEmissiveParts(entity.Render.RenderObjectIDs[0], emissivity, emissivePartColor, displayPartColor);
}
}
/// <summary>
/// Anima sequence base.
/// </summary>
public abstract class AnimaSeqBase
{
private List<Keyframe> m_keyframes = null;
private int m_gStart = 0, m_gEnd = 0;
private float m_rate = 0f;
private int m_kStart = 0, m_kEnd = 0;
private string m_name = null;
/// <summary>
/// Keyframe data.
/// </summary>
public struct Keyframe
{
/// <summary>Position.</summary>
public Vector3 position;
/// <summary>Rotation (Quaternion).</summary>
public Quaternion rotation;
/// <summary>Scale.</summary>
public Vector3 scale;
}
/// <summary>
/// Get starting frame.
/// </summary>
public float FrameStart
{
get { return (float)(m_gStart); }
}
/// <summary>
/// Get ending frame.
/// </summary>
public float FrameEnd
{
get { return (float)(m_gEnd); }
}
/// <summary>
/// Get period in frames.
/// </summary>
public float FramePeriod
{
get { return (float)(m_gEnd - m_gStart + 1); }
}
/// <summary>
/// Get frame rate.
/// </summary>
public float FrameRate
{
get { return (float)m_rate; }
}
/// <summary>
/// Get sequence class name as string.
/// </summary>
public string Name
{
get { return m_name; }
}
/// <summary>
/// Anima sequence base constructor, isn't to be called directly, Use (your_class_name).Adquire().
/// </summary>
protected AnimaSeqBase()
{
PData();
}
/// <summary>
/// Adquire sequence, data is only loaded once and shared between multiple instances.
/// </summary>
/// <returns>static copy of the sequence</returns>
public static AnimaSeqBase Adquire()
{
throw new ArgumentException("Cannot Adquire from sequence base!");
}
/// <summary>
/// Discard stored data, call this on game unload to free memory.
/// </summary>
public abstract void DiscardStatic();
/// <summary>
/// Discard stored data, call this on game unload to free memory.
/// </summary>
public static void Discard()
{
throw new ArgumentException("Cannot Discard from sequence base!");
}
/// <summary>
/// Get data associated to keyframe.
/// </summary>
/// <param name="keyframe">keyframe</param>
/// <param name="data">output of data</param>
/// <returns>true on success</returns>
public bool GetKeyframeData(int keyframe, out AnimaSeqBase.Keyframe data)
{
if (m_kEnd == 0)
{
// No data! Shouldn't happen!
data = new AnimaSeqBase.Keyframe();
return false;
}
if (keyframe < m_kStart) keyframe = m_kStart;
if (keyframe > m_kEnd) keyframe = m_kEnd;
data = m_keyframes[keyframe - m_kStart];
return true;
}
/// <summary>