Line data Source code
1 : function Pack() {}
2 :
3 0 : const PACKING_INTERVAL = 250;
4 :
5 0 : Pack.prototype.Schema =
6 : "<element name='Entity' a:help='Entity to transform into'>" +
7 : "<text/>" +
8 : "</element>" +
9 : "<element name='Time' a:help='Time required to transform this entity, in milliseconds'>" +
10 : "<data type='nonNegativeInteger'/>" +
11 : "</element>" +
12 : "<element name='State' a:help='Whether this entity is packed or unpacked'>" +
13 : "<choice>" +
14 : "<value>packed</value>" +
15 : "<value>unpacked</value>" +
16 : "</choice>" +
17 : "</element>";
18 :
19 0 : Pack.prototype.Init = function()
20 : {
21 0 : this.packed = this.template.State == "packed";
22 0 : this.packing = false;
23 0 : this.elapsedTime = 0;
24 0 : this.timer = undefined;
25 : };
26 :
27 0 : Pack.prototype.OnDestroy = function()
28 : {
29 0 : this.CancelTimer();
30 : };
31 :
32 0 : Pack.prototype.CancelTimer = function()
33 : {
34 0 : if (this.timer)
35 : {
36 0 : let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
37 0 : cmpTimer.CancelTimer(this.timer);
38 0 : this.timer = undefined;
39 : }
40 : };
41 :
42 0 : Pack.prototype.IsPacked = function()
43 : {
44 0 : return this.packed;
45 : };
46 :
47 0 : Pack.prototype.IsPacking = function()
48 : {
49 0 : return this.packing;
50 : };
51 :
52 0 : Pack.prototype.CanPack = function()
53 : {
54 0 : return !this.packing && !this.packed;
55 : };
56 :
57 0 : Pack.prototype.CanUnpack = function()
58 : {
59 0 : return !this.packing && this.packed;
60 : };
61 :
62 0 : Pack.prototype.Pack = function()
63 : {
64 0 : if (this.IsPacked() || this.IsPacking())
65 0 : return;
66 :
67 0 : this.packing = true;
68 :
69 0 : let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
70 0 : this.timer = cmpTimer.SetInterval(this.entity, IID_Pack, "PackProgress", 0, PACKING_INTERVAL, null);
71 :
72 0 : let cmpVisual = Engine.QueryInterface(this.entity, IID_Visual);
73 0 : if (cmpVisual)
74 0 : cmpVisual.SelectAnimation("packing", true, 1.0);
75 : };
76 :
77 0 : Pack.prototype.Unpack = function()
78 : {
79 0 : if (!this.IsPacked() || this.IsPacking())
80 0 : return;
81 :
82 0 : this.packing = true;
83 :
84 0 : let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
85 0 : this.timer = cmpTimer.SetInterval(this.entity, IID_Pack, "PackProgress", 0, PACKING_INTERVAL, null);
86 :
87 0 : let cmpVisual = Engine.QueryInterface(this.entity, IID_Visual);
88 0 : if (cmpVisual)
89 0 : cmpVisual.SelectAnimation("unpacking", true, 1.0);
90 : };
91 :
92 0 : Pack.prototype.CancelPack = function()
93 : {
94 0 : if (!this.IsPacking())
95 0 : return;
96 :
97 0 : this.CancelTimer();
98 0 : this.packing = false;
99 0 : this.SetElapsedTime(0);
100 :
101 : // Clear animation
102 0 : let cmpVisual = Engine.QueryInterface(this.entity, IID_Visual);
103 0 : if (cmpVisual)
104 0 : cmpVisual.SelectAnimation("idle", false, 1.0);
105 : };
106 :
107 0 : Pack.prototype.GetPackTime = function()
108 : {
109 0 : return ApplyValueModificationsToEntity("Pack/Time", +this.template.Time, this.entity);
110 : };
111 :
112 0 : Pack.prototype.GetElapsedTime = function()
113 : {
114 0 : return this.elapsedTime;
115 : };
116 :
117 0 : Pack.prototype.GetProgress = function()
118 : {
119 0 : return Math.min(this.elapsedTime / this.GetPackTime(), 1);
120 : };
121 :
122 0 : Pack.prototype.SetElapsedTime = function(time)
123 : {
124 0 : this.elapsedTime = time;
125 0 : Engine.PostMessage(this.entity, MT_PackProgressUpdate, { "progress": this.elapsedTime });
126 : };
127 :
128 0 : Pack.prototype.PackProgress = function(data, lateness)
129 : {
130 0 : if (this.elapsedTime < this.GetPackTime())
131 : {
132 0 : this.SetElapsedTime(this.GetElapsedTime() + PACKING_INTERVAL + lateness);
133 0 : return;
134 : }
135 :
136 0 : this.CancelTimer();
137 0 : this.packed = !this.packed;
138 0 : this.packing = false;
139 :
140 0 : Engine.PostMessage(this.entity, MT_PackFinished, { "packed": this.packed });
141 :
142 0 : let newEntity = ChangeEntityTemplate(this.entity, this.template.Entity);
143 :
144 0 : if (newEntity)
145 0 : PlaySound(this.packed ? "packed" : "unpacked", newEntity);
146 :
147 : };
148 :
149 0 : Engine.RegisterComponentType(IID_Pack, "Pack", Pack);
|