Differential D4352 Diff 19045 ps/trunk/binaries/data/mods/public/simulation/components/ProductionQueue.js
Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/components/ProductionQueue.js
Show All 14 Lines | |||||
/** | /** | ||||
* @param {number} producer - The entity ID of our producer. | * @param {number} producer - The entity ID of our producer. | ||||
* @param {string} metadata - Optionally any metadata attached to us. | * @param {string} metadata - Optionally any metadata attached to us. | ||||
*/ | */ | ||||
ProductionQueue.prototype.Item.prototype.Init = function(producer, metadata) | ProductionQueue.prototype.Item.prototype.Init = function(producer, metadata) | ||||
{ | { | ||||
this.producer = producer; | this.producer = producer; | ||||
this.metadata = metadata; | this.metadata = metadata; | ||||
}; | |||||
/** | |||||
* @param {string} type - The type of queue to use. | |||||
* @param {string} templateName - The template to queue. | |||||
* @param {number} count - The amount of template to queue. Only applicable for type == "unit". | |||||
* | |||||
* @return {boolean} - Whether the item could be queued. | |||||
*/ | |||||
ProductionQueue.prototype.Item.prototype.Queue = function(type, templateName, count) | |||||
{ | |||||
if (type == "unit") | |||||
return this.QueueEntity(templateName, count); | |||||
if (type == "technology") | |||||
return this.QueueTechnology(templateName); | |||||
warn("Tried to add invalid item of type \"" + type + "\" and template \"" + templateName + "\" to a production queue (entity: " + this.producer + ")."); | |||||
return false; | |||||
}; | }; | ||||
/** | |||||
* @param {string} templateName - The name of the entity to queue. | |||||
* @param {number} count - The number of entities that should be produced. | |||||
* @return {boolean} - Whether the batch was successfully created. | |||||
*/ | |||||
ProductionQueue.prototype.Item.prototype.QueueEntity = function(templateName, count) | ProductionQueue.prototype.Item.prototype.QueueEntity = function(templateName, count) | ||||
{ | { | ||||
const cmpTrainer = Engine.QueryInterface(this.producer, IID_Trainer); | const cmpTrainer = Engine.QueryInterface(this.producer, IID_Trainer); | ||||
if (!cmpTrainer) | if (!cmpTrainer) | ||||
return false; | return false; | ||||
this.entity = cmpTrainer.QueueBatch(templateName, count, this.metadata); | this.entity = cmpTrainer.QueueBatch(templateName, count, this.metadata); | ||||
if (this.entity == -1) | if (this.entity == -1) | ||||
return false; | return false; | ||||
this.originalItem = { | this.originalItem = { | ||||
"templateName": templateName, | "templateName": templateName, | ||||
"count": count, | "count": count, | ||||
"metadata": this.metadata | "metadata": this.metadata | ||||
}; | }; | ||||
return true; | return true; | ||||
}; | }; | ||||
/** | |||||
* @param {string} templateName - The name of the technology to queue. | |||||
* @return {boolean} - Whether the technology was successfully queued. | |||||
*/ | |||||
ProductionQueue.prototype.Item.prototype.QueueTechnology = function(templateName) | ProductionQueue.prototype.Item.prototype.QueueTechnology = function(templateName) | ||||
{ | { | ||||
const cmpResearcher = Engine.QueryInterface(this.producer, IID_Researcher); | const cmpResearcher = Engine.QueryInterface(this.producer, IID_Researcher); | ||||
if (!cmpResearcher) | if (!cmpResearcher) | ||||
return false; | return false; | ||||
this.technology = cmpResearcher.QueueTechnology(templateName, this.metadata); | this.technology = cmpResearcher.QueueTechnology(templateName, this.metadata); | ||||
if (this.technology == -1) | return this.technology != -1; | ||||
return false; | |||||
return true; | |||||
}; | }; | ||||
/** | /** | ||||
* @param {number} id - The id of this item. | * @param {number} id - The id this item needs to get. | ||||
*/ | */ | ||||
ProductionQueue.prototype.Item.prototype.SetID = function(id) | ProductionQueue.prototype.Item.prototype.SetID = function(id) | ||||
{ | { | ||||
this.id = id; | this.id = id; | ||||
}; | }; | ||||
ProductionQueue.prototype.Item.prototype.Stop = function() | ProductionQueue.prototype.Item.prototype.Stop = function() | ||||
{ | { | ||||
if (this.entity) | if (this.entity > 0) | ||||
{ | Engine.QueryInterface(this.producer, IID_Trainer)?.StopBatch(this.entity); | ||||
const cmpTrainer = Engine.QueryInterface(this.producer, IID_Trainer); | |||||
if (cmpTrainer) | |||||
cmpTrainer.StopBatch(this.entity); | |||||
} | |||||
if (this.technology) | if (this.technology > 0) | ||||
{ | Engine.QueryInterface(this.producer, IID_Researcher)?.StopResearching(this.technology); | ||||
const cmpResearcher = Engine.QueryInterface(this.producer, IID_Researcher); | |||||
if (cmpResearcher) | |||||
cmpResearcher.StopResearching(this.technology); | |||||
} | |||||
}; | }; | ||||
/** | /** | ||||
* Called when the first work is performed. | * Called when the first work is performed. | ||||
*/ | */ | ||||
ProductionQueue.prototype.Item.prototype.Start = function() | ProductionQueue.prototype.Item.prototype.Start = function() | ||||
{ | { | ||||
this.started = true; | this.started = true; | ||||
}; | }; | ||||
/** | |||||
* @return {boolean} - Whether there is work done on the item. | |||||
*/ | |||||
ProductionQueue.prototype.Item.prototype.IsStarted = function() | ProductionQueue.prototype.Item.prototype.IsStarted = function() | ||||
{ | { | ||||
return !!this.started; | return !!this.started; | ||||
}; | }; | ||||
/** | /** | ||||
* @return {boolean} - Whether this item is finished. | * @return {boolean} - Whether this item is finished. | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | |||||
{ | { | ||||
delete this.paused; | delete this.paused; | ||||
if (this.entity) | if (this.entity) | ||||
Engine.QueryInterface(this.producer, IID_Trainer).UnpauseBatch(this.entity); | Engine.QueryInterface(this.producer, IID_Trainer).UnpauseBatch(this.entity); | ||||
if (this.technology) | if (this.technology) | ||||
Engine.QueryInterface(this.producer, IID_Researcher).UnpauseTechnology(this.technology); | Engine.QueryInterface(this.producer, IID_Researcher).UnpauseTechnology(this.technology); | ||||
}; | }; | ||||
/** | |||||
* @return {boolean} - Whether the item is currently paused. | |||||
*/ | |||||
ProductionQueue.prototype.Item.prototype.IsPaused = function() | ProductionQueue.prototype.Item.prototype.IsPaused = function() | ||||
{ | { | ||||
return !!this.paused; | return !!this.paused; | ||||
}; | }; | ||||
/** | /** | ||||
* @return {Object} - Some basic information of this item. | * @return {Object} - Some basic information of this item. | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | ProductionQueue.prototype.Item.prototype.Deserialize = function(data) | ||||
this.technology = data.technology; | this.technology = data.technology; | ||||
this.started = data.started; | this.started = data.started; | ||||
this.originalItem = data.originalItem; | this.originalItem = data.originalItem; | ||||
}; | }; | ||||
ProductionQueue.prototype.Init = function() | ProductionQueue.prototype.Init = function() | ||||
{ | { | ||||
this.nextID = 1; | this.nextID = 1; | ||||
this.queue = []; | this.queue = []; | ||||
}; | }; | ||||
ProductionQueue.prototype.Serialize = function() | ProductionQueue.prototype.Serialize = function() | ||||
{ | { | ||||
const queue = []; | const queue = []; | ||||
for (const item of this.queue) | for (const item of this.queue) | ||||
queue.push(item.Serialize()); | queue.push(item.Serialize()); | ||||
▲ Show 20 Lines • Show All 93 Lines • ▼ Show 20 Lines | cmpGUIInterface.PushNotification({ | ||||
"message": markForTranslation("The production queue is full."), | "message": markForTranslation("The production queue is full."), | ||||
"translateMessage": true, | "translateMessage": true, | ||||
}); | }); | ||||
return false; | return false; | ||||
} | } | ||||
const item = new this.Item(); | const item = new this.Item(); | ||||
item.Init(this.entity, metadata); | item.Init(this.entity, metadata); | ||||
if (type == "unit") | if (!item.Queue(type, templateName, count)) | ||||
{ | |||||
if (!item.QueueEntity(templateName, count)) | |||||
return false; | return false; | ||||
} | |||||
else if (type == "technology") | |||||
{ | |||||
if (!item.QueueTechnology(templateName)) | |||||
return false; | |||||
} | |||||
else | |||||
{ | |||||
warn("Tried to add invalid item of type \"" + type + "\" and template \"" + templateName + "\" to a production queue"); | |||||
return false; | |||||
} | |||||
item.SetID(this.nextID++); | item.SetID(this.nextID++); | ||||
if (pushFront) | if (pushFront) | ||||
{ | { | ||||
this.queue[0]?.Pause(); | this.queue[0]?.Pause(); | ||||
this.queue.unshift(item); | this.queue.unshift(item); | ||||
} | } | ||||
else | else | ||||
this.queue.push(item); | this.queue.push(item); | ||||
Engine.PostMessage(this.entity, MT_ProductionQueueChanged, null); | Engine.PostMessage(this.entity, MT_ProductionQueueChanged, null); | ||||
if (!this.timer) | if (!this.timer) | ||||
this.StartTimer(); | this.StartTimer(); | ||||
return true; | return true; | ||||
}; | }; | ||||
/* | /* | ||||
* Removes an item from the queue. | * @param {number} - The ID of the item to remove from the queue. | ||||
*/ | */ | ||||
ProductionQueue.prototype.RemoveItem = function(id) | ProductionQueue.prototype.RemoveItem = function(id) | ||||
{ | { | ||||
let itemIndex = this.queue.findIndex(item => item.id == id); | let itemIndex = this.queue.findIndex(item => item.id == id); | ||||
if (itemIndex == -1) | if (itemIndex == -1) | ||||
return; | return; | ||||
this.queue.splice(itemIndex, 1)[0].Stop(); | this.queue.splice(itemIndex, 1)[0].Stop(); | ||||
▲ Show 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | if (!this.queue.length) | ||||
this.StopTimer(); | this.StopTimer(); | ||||
}; | }; | ||||
ProductionQueue.prototype.PauseProduction = function() | ProductionQueue.prototype.PauseProduction = function() | ||||
{ | { | ||||
this.StopTimer(); | this.StopTimer(); | ||||
this.paused = true; | this.paused = true; | ||||
this.queue[0]?.Pause(); | this.queue[0]?.Pause(); | ||||
this.StopTimer(); | |||||
}; | }; | ||||
ProductionQueue.prototype.UnpauseProduction = function() | ProductionQueue.prototype.UnpauseProduction = function() | ||||
{ | { | ||||
this.queue[0]?.Unpause(); | this.queue[0]?.Unpause(); | ||||
delete this.paused; | delete this.paused; | ||||
this.StartTimer(); | this.StartTimer(); | ||||
}; | }; | ||||
Show All 18 Lines | ProductionQueue.prototype.StopTimer = function() | ||||
if (!this.timer) | if (!this.timer) | ||||
return; | return; | ||||
this.SetAnimation("idle"); | this.SetAnimation("idle"); | ||||
Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer).CancelTimer(this.timer); | Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer).CancelTimer(this.timer); | ||||
delete this.timer; | delete this.timer; | ||||
}; | }; | ||||
/** | |||||
* @return {boolean} - Whether this entity is currently producing. | |||||
*/ | |||||
ProductionQueue.prototype.HasQueuedProduction = function() | ProductionQueue.prototype.HasQueuedProduction = function() | ||||
{ | { | ||||
return this.queue.length > 0; | return this.queue.length > 0; | ||||
}; | }; | ||||
ProductionQueue.prototype.OnOwnershipChanged = function(msg) | ProductionQueue.prototype.OnOwnershipChanged = function(msg) | ||||
{ | { | ||||
// Reset the production queue whenever the owner changes. | // Reset the production queue whenever the owner changes. | ||||
Show All 16 Lines |
Wildfire Games · Phabricator