2020-02-25 13:29:07 -08:00
|
|
|
class AutoBuildable
|
|
|
|
|
{
|
|
|
|
|
Init()
|
|
|
|
|
{
|
2021-01-09 23:25:52 -08:00
|
|
|
this.UpdateRate();
|
2020-02-25 13:29:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return {number} - The rate with technologies and aura modification applied.
|
|
|
|
|
*/
|
|
|
|
|
GetRate()
|
|
|
|
|
{
|
|
|
|
|
return this.rate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UpdateRate()
|
|
|
|
|
{
|
2020-06-14 02:49:32 -07:00
|
|
|
this.rate = ApplyValueModificationsToEntity("AutoBuildable/Rate", +this.template.Rate, this.entity);
|
2020-02-25 13:29:07 -08:00
|
|
|
if (this.rate)
|
|
|
|
|
this.StartTimer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StartTimer()
|
|
|
|
|
{
|
|
|
|
|
if (this.timer || !this.rate)
|
|
|
|
|
return;
|
|
|
|
|
|
2025-05-11 01:39:08 -07:00
|
|
|
const cmpFoundation = Engine.QueryInterface(this.entity, IID_Foundation);
|
2020-02-25 13:29:07 -08:00
|
|
|
if (!cmpFoundation)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
cmpFoundation.AddBuilder(this.entity);
|
2025-05-11 01:39:08 -07:00
|
|
|
const cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
|
2020-02-25 13:29:07 -08:00
|
|
|
this.timer = cmpTimer.SetInterval(this.entity, IID_AutoBuildable, "AutoBuild", 0, 1000, undefined);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CancelTimer()
|
|
|
|
|
{
|
|
|
|
|
if (!this.timer)
|
|
|
|
|
return;
|
|
|
|
|
|
2025-05-11 01:39:08 -07:00
|
|
|
const cmpFoundation = Engine.QueryInterface(this.entity, IID_Foundation);
|
2020-02-25 13:29:07 -08:00
|
|
|
if (cmpFoundation)
|
|
|
|
|
cmpFoundation.RemoveBuilder(this.entity);
|
|
|
|
|
|
2025-05-11 01:39:08 -07:00
|
|
|
const cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
|
2020-02-25 13:29:07 -08:00
|
|
|
cmpTimer.CancelTimer(this.timer);
|
|
|
|
|
delete this.timer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AutoBuild()
|
|
|
|
|
{
|
|
|
|
|
if (!this.rate)
|
|
|
|
|
{
|
|
|
|
|
this.CancelTimer();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-05-11 01:39:08 -07:00
|
|
|
const cmpFoundation = Engine.QueryInterface(this.entity, IID_Foundation);
|
2020-02-25 13:29:07 -08:00
|
|
|
if (!cmpFoundation)
|
|
|
|
|
{
|
|
|
|
|
this.CancelTimer();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmpFoundation.Build(this.entity, this.rate);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-14 02:49:32 -07:00
|
|
|
OnValueModification(msg)
|
|
|
|
|
{
|
|
|
|
|
if (msg.component != "AutoBuildable")
|
|
|
|
|
return;
|
2020-02-25 13:29:07 -08:00
|
|
|
|
2020-06-14 02:49:32 -07:00
|
|
|
this.UpdateRate();
|
|
|
|
|
}
|
2020-02-25 13:29:07 -08:00
|
|
|
|
2020-06-14 02:49:32 -07:00
|
|
|
OnOwnershipChanged(msg)
|
|
|
|
|
{
|
|
|
|
|
if (msg.to == INVALID_PLAYER)
|
|
|
|
|
return;
|
2020-02-25 13:29:07 -08:00
|
|
|
|
2020-06-14 02:49:32 -07:00
|
|
|
this.UpdateRate();
|
|
|
|
|
}
|
2020-02-25 13:29:07 -08:00
|
|
|
}
|
|
|
|
|
|
2020-06-14 02:49:32 -07:00
|
|
|
AutoBuildable.prototype.Schema =
|
|
|
|
|
"<a:help>Defines whether the entity can be built by itself.</a:help>" +
|
|
|
|
|
"<a:example>" +
|
|
|
|
|
"<Rate>1.0</Rate>" +
|
|
|
|
|
"</a:example>" +
|
|
|
|
|
"<element name='Rate' a:help='The rate at which the building autobuilds.'>" +
|
|
|
|
|
"<ref name='nonNegativeDecimal'/>" +
|
|
|
|
|
"</element>";
|
|
|
|
|
|
2020-02-25 13:29:07 -08:00
|
|
|
Engine.RegisterComponentType(IID_AutoBuildable, "AutoBuildable", AutoBuildable);
|