2010-01-22 21:03:14 +01:00
|
|
|
function Player() {}
|
|
|
|
|
2010-04-23 18:09:03 +02:00
|
|
|
Player.prototype.Schema =
|
|
|
|
"<a:component type='system'/><empty/>";
|
|
|
|
|
2010-01-22 21:03:14 +01:00
|
|
|
Player.prototype.Init = function()
|
|
|
|
{
|
|
|
|
this.playerID = undefined;
|
2010-06-30 23:41:04 +02:00
|
|
|
this.name = "Unknown";
|
|
|
|
this.civ = "gaia";
|
|
|
|
this.colour = { "r": 0.0, "g": 0.0, "b": 0.0, "a": 1.0 };
|
2010-08-21 22:43:55 +02:00
|
|
|
this.popUsed = 0; // population of units owned by this player
|
|
|
|
this.popReserved = 0; // population of units currently being trained
|
|
|
|
this.popLimit = 50; // maximum population
|
2010-02-12 23:46:53 +01:00
|
|
|
this.resourceCount = {
|
2010-03-12 22:41:40 +01:00
|
|
|
"food": 2000,
|
|
|
|
"wood": 1500,
|
|
|
|
"metal": 500,
|
|
|
|
"stone": 1000
|
2010-02-12 23:46:53 +01:00
|
|
|
};
|
2010-01-22 21:03:14 +01:00
|
|
|
};
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
Player.prototype.SetPlayerID = function(id)
|
2010-04-23 20:36:48 +02:00
|
|
|
{
|
2010-06-30 23:41:04 +02:00
|
|
|
this.playerID = id;
|
2010-04-23 20:36:48 +02:00
|
|
|
};
|
|
|
|
|
2010-08-21 22:43:55 +02:00
|
|
|
Player.prototype.GetPlayerID = function()
|
|
|
|
{
|
|
|
|
return this.playerID;
|
|
|
|
};
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
Player.prototype.SetName = function(name)
|
2010-01-22 21:03:14 +01:00
|
|
|
{
|
2010-06-30 23:41:04 +02:00
|
|
|
this.name = name;
|
|
|
|
};
|
|
|
|
|
2010-07-02 04:44:25 +02:00
|
|
|
Player.prototype.GetName = function()
|
|
|
|
{
|
|
|
|
return this.name;
|
|
|
|
};
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
Player.prototype.SetCiv = function(civcode)
|
|
|
|
{
|
|
|
|
this.civ = civcode;
|
2010-01-22 21:03:14 +01:00
|
|
|
};
|
|
|
|
|
2010-07-02 04:44:25 +02:00
|
|
|
Player.prototype.GetCiv = function()
|
|
|
|
{
|
|
|
|
return this.civ;
|
|
|
|
};
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
Player.prototype.SetColour = function(r, g, b)
|
|
|
|
{
|
|
|
|
this.colour = { "r": r/255.0, "g": g/255.0, "b": b/255.0, "a": 1.0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
Player.prototype.GetColour = function()
|
|
|
|
{
|
|
|
|
return this.colour;
|
|
|
|
};
|
|
|
|
|
2010-08-21 22:43:55 +02:00
|
|
|
Player.prototype.TryReservePopulationSlots = function(num)
|
|
|
|
{
|
|
|
|
if (num > this.GetPopulationLimit() - this.GetPopulationCount())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
this.popReserved += num;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
Player.prototype.UnReservePopulationSlots = function(num)
|
|
|
|
{
|
|
|
|
this.popReserved -= num;
|
|
|
|
};
|
|
|
|
|
2010-01-22 21:03:14 +01:00
|
|
|
Player.prototype.GetPopulationCount = function()
|
|
|
|
{
|
2010-08-21 22:43:55 +02:00
|
|
|
return this.popUsed + this.popReserved;
|
2010-01-22 21:03:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Player.prototype.GetPopulationLimit = function()
|
|
|
|
{
|
|
|
|
return this.popLimit;
|
|
|
|
};
|
|
|
|
|
2010-02-12 23:46:53 +01:00
|
|
|
Player.prototype.GetResourceCounts = function()
|
|
|
|
{
|
|
|
|
return this.resourceCount;
|
|
|
|
};
|
|
|
|
|
2010-03-12 22:41:40 +01:00
|
|
|
Player.prototype.AddResource = function(type, amount)
|
2010-02-12 23:46:53 +01:00
|
|
|
{
|
|
|
|
this.resourceCount[type] += (+amount);
|
|
|
|
};
|
|
|
|
|
2010-04-19 21:47:23 +02:00
|
|
|
Player.prototype.AddResources = function(amounts)
|
|
|
|
{
|
|
|
|
for (var type in amounts)
|
|
|
|
this.resourceCount[type] += (+amounts[type]);
|
|
|
|
};
|
|
|
|
|
2010-03-12 22:41:40 +01:00
|
|
|
Player.prototype.TrySubtractResources = function(amounts)
|
|
|
|
{
|
2010-08-14 00:02:27 +02:00
|
|
|
// Check if we can afford it all
|
|
|
|
var amountsNeeded = {};
|
2010-03-12 22:41:40 +01:00
|
|
|
for (var type in amounts)
|
|
|
|
if (amounts[type] > this.resourceCount[type])
|
2010-08-14 00:02:27 +02:00
|
|
|
amountsNeeded[type] = amounts[type] - this.resourceCount[type];
|
|
|
|
|
|
|
|
var formattedAmountsNeeded = [];
|
|
|
|
for (var type in amountsNeeded)
|
|
|
|
formattedAmountsNeeded.push(type + ": " + amountsNeeded[type]);
|
|
|
|
|
|
|
|
// If we don't have enough resources, send a notification to the player
|
|
|
|
if (formattedAmountsNeeded.length)
|
|
|
|
{
|
2010-08-14 17:34:36 +02:00
|
|
|
var notification = {"player": this.playerID, "message": "Insufficient resources - " + formattedAmountsNeeded.join(", ")};
|
2010-08-14 00:02:27 +02:00
|
|
|
var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
|
2010-08-14 06:34:49 +02:00
|
|
|
cmpGUIInterface.PushNotification(notification);
|
2010-08-14 00:02:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Subtract the resources
|
|
|
|
for (var type in amounts)
|
2010-08-14 17:34:36 +02:00
|
|
|
this.resourceCount[type] -= amounts[type];
|
2010-08-14 00:02:27 +02:00
|
|
|
}
|
2010-03-12 22:41:40 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2010-08-21 22:43:55 +02:00
|
|
|
// Keep track of population effects of all entities that
|
|
|
|
// become owned or unowned by this player
|
2010-01-22 21:03:14 +01:00
|
|
|
Player.prototype.OnGlobalOwnershipChanged = function(msg)
|
|
|
|
{
|
|
|
|
if (msg.from == this.playerID)
|
|
|
|
{
|
|
|
|
var cost = Engine.QueryInterface(msg.entity, IID_Cost);
|
|
|
|
if (cost)
|
2010-01-24 18:24:35 +01:00
|
|
|
{
|
2010-08-21 22:43:55 +02:00
|
|
|
this.popUsed -= cost.GetPopCost();
|
2010-04-04 22:52:21 +02:00
|
|
|
this.popLimit -= cost.GetPopBonus();
|
2010-01-24 18:24:35 +01:00
|
|
|
}
|
2010-01-22 21:03:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.to == this.playerID)
|
|
|
|
{
|
|
|
|
var cost = Engine.QueryInterface(msg.entity, IID_Cost);
|
|
|
|
if (cost)
|
2010-01-24 18:24:35 +01:00
|
|
|
{
|
2010-08-21 22:43:55 +02:00
|
|
|
this.popUsed += cost.GetPopCost();
|
2010-04-04 22:52:21 +02:00
|
|
|
this.popLimit += cost.GetPopBonus();
|
2010-01-24 18:24:35 +01:00
|
|
|
}
|
2010-01-22 21:03:14 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Engine.RegisterComponentType(IID_Player, "Player", Player);
|