2012-07-08 18:25:33 +02:00
|
|
|
function Gate() {}
|
|
|
|
|
|
|
|
Gate.prototype.Schema =
|
2012-07-14 00:34:49 +02:00
|
|
|
"<element name='PassRange'>" +
|
2012-07-08 18:25:33 +02:00
|
|
|
"<ref name='nonNegativeDecimal'/>" +
|
|
|
|
"</element>";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize Gate Component
|
|
|
|
*/
|
|
|
|
Gate.prototype.Init = function()
|
|
|
|
{
|
2012-07-14 00:34:49 +02:00
|
|
|
this.units = [];
|
|
|
|
this.opened = false;
|
|
|
|
this.locked = false;
|
2012-07-08 18:25:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Gate.prototype.OnOwnershipChanged = function(msg)
|
|
|
|
{
|
|
|
|
if (msg.to != -1)
|
|
|
|
{
|
|
|
|
this.SetupRangeQuery(msg.to);
|
2012-07-14 00:34:49 +02:00
|
|
|
if (!this.locked)
|
|
|
|
this.UnlockGate();
|
2012-07-08 18:25:33 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleanup on destroy
|
|
|
|
*/
|
|
|
|
Gate.prototype.OnDestroy = function()
|
|
|
|
{
|
|
|
|
// Clean up range query
|
|
|
|
var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
|
2012-07-14 00:34:49 +02:00
|
|
|
if (this.unitsQuery)
|
|
|
|
cmpRangeManager.DestroyActiveQuery(this.unitsQuery);
|
2012-07-08 18:25:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup the Range Query to detect units coming in & out of range
|
|
|
|
*/
|
|
|
|
Gate.prototype.SetupRangeQuery = function(owner)
|
|
|
|
{
|
|
|
|
var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
|
|
|
|
var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
|
2012-07-14 00:34:49 +02:00
|
|
|
if (this.unitsQuery)
|
|
|
|
cmpRangeManager.DestroyActiveQuery(this.unitsQuery);
|
|
|
|
var players = []
|
2012-07-08 18:25:33 +02:00
|
|
|
|
|
|
|
var cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(owner), IID_Player);
|
|
|
|
var numPlayers = cmpPlayerManager.GetNumPlayers();
|
|
|
|
|
|
|
|
for (var i = 1; i < numPlayers; ++i)
|
2012-07-14 00:34:49 +02:00
|
|
|
{
|
|
|
|
players.push(i);
|
2012-07-08 18:25:33 +02:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:34:49 +02:00
|
|
|
if (this.GetPassRange() > 0)
|
2012-07-08 18:25:33 +02:00
|
|
|
{
|
2012-07-14 00:34:49 +02:00
|
|
|
var range = this.GetPassRange();
|
|
|
|
this.unitsQuery = cmpRangeManager.CreateActiveQuery(this.entity, 0, range, players, 0, cmpRangeManager.GetEntityFlagMask("normal"));
|
|
|
|
cmpRangeManager.EnableActiveQuery(this.unitsQuery);
|
2012-07-08 18:25:33 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when units enter or leave range
|
|
|
|
*/
|
|
|
|
Gate.prototype.OnRangeUpdate = function(msg)
|
|
|
|
{
|
2012-07-14 00:34:49 +02:00
|
|
|
if (msg.tag != this.unitsQuery)
|
2012-07-08 18:25:33 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (msg.added.length > 0)
|
|
|
|
{
|
|
|
|
for each (var entity in msg.added)
|
|
|
|
{
|
|
|
|
var cmpIdentity = Engine.QueryInterface(entity, IID_Identity);
|
|
|
|
if(cmpIdentity)
|
|
|
|
{
|
|
|
|
var classes = cmpIdentity.GetClassesList();
|
|
|
|
if(classes.indexOf("Unit") != -1)
|
2012-07-14 00:34:49 +02:00
|
|
|
this.units.push(entity);
|
2012-07-08 18:25:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (msg.removed.length > 0)
|
|
|
|
{
|
|
|
|
for each (var entity in msg.removed)
|
|
|
|
{
|
2012-07-14 00:34:49 +02:00
|
|
|
this.units.splice(this.units.indexOf(entity), 1);
|
2012-07-08 18:25:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-14 00:34:49 +02:00
|
|
|
this.OperateGate();
|
2012-07-08 18:25:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-07-14 00:34:49 +02:00
|
|
|
* Get the range in which units are detected
|
2012-07-08 18:25:33 +02:00
|
|
|
*/
|
2012-07-14 00:34:49 +02:00
|
|
|
Gate.prototype.GetPassRange = function()
|
2012-07-08 18:25:33 +02:00
|
|
|
{
|
2012-07-14 00:34:49 +02:00
|
|
|
return +this.template.PassRange;
|
2012-07-08 18:25:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open or close the gate
|
|
|
|
*/
|
2012-07-14 00:34:49 +02:00
|
|
|
Gate.prototype.OperateGate = function()
|
2012-07-08 18:25:33 +02:00
|
|
|
{
|
|
|
|
if (this.opened == true )
|
|
|
|
{
|
2012-07-14 00:34:49 +02:00
|
|
|
// If no units are in range, close the gate
|
|
|
|
if (this.units.length == 0)
|
2012-07-08 18:25:33 +02:00
|
|
|
{
|
|
|
|
this.CloseGate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-14 00:34:49 +02:00
|
|
|
// If one units in range is owned by an ally, open the gate
|
|
|
|
for each (var ent in this.units)
|
2012-07-08 18:25:33 +02:00
|
|
|
{
|
2012-07-14 00:34:49 +02:00
|
|
|
if (IsOwnedByAllyOfEntity(this.entity, ent))
|
|
|
|
{
|
|
|
|
this.OpenGate();
|
|
|
|
break;
|
|
|
|
}
|
2012-07-08 18:25:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Gate.prototype.IsLocked = function()
|
|
|
|
{
|
|
|
|
return this.locked;
|
|
|
|
};
|
|
|
|
|
|
|
|
Gate.prototype.LockGate = function()
|
|
|
|
{
|
|
|
|
this.locked = true;
|
2012-07-14 00:34:49 +02:00
|
|
|
// If the door is closed, enable 'block pathfinding'
|
|
|
|
// Else 'block pathfinding' will be enabled the next time the gate close
|
|
|
|
if (!this.opened)
|
|
|
|
{
|
|
|
|
var cmpObstruction = Engine.QueryInterface(this.entity, IID_Obstruction);
|
|
|
|
if (!cmpObstruction)
|
|
|
|
return;
|
|
|
|
cmpObstruction.SetDisableBlockMovementPathfinding(false, false, 0);
|
|
|
|
}
|
2012-07-08 18:25:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Gate.prototype.UnlockGate = function()
|
|
|
|
{
|
|
|
|
var cmpObstruction = Engine.QueryInterface(this.entity, IID_Obstruction);
|
|
|
|
if (!cmpObstruction)
|
|
|
|
return;
|
2012-07-14 00:34:49 +02:00
|
|
|
|
|
|
|
// Disable 'block pathfinding'
|
2012-07-08 18:25:33 +02:00
|
|
|
cmpObstruction.SetDisableBlockMovementPathfinding(false, true, 0);
|
|
|
|
this.locked = false;
|
2012-07-14 00:34:49 +02:00
|
|
|
|
|
|
|
// If the gate is closed, open it if necessary
|
|
|
|
if (!this.opened)
|
|
|
|
this.OperateGate();
|
2012-07-08 18:25:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Gate.prototype.OpenGate = function()
|
|
|
|
{
|
2012-07-14 00:34:49 +02:00
|
|
|
// Do not open the gate if it has been locked
|
2012-07-08 18:25:33 +02:00
|
|
|
if (this.locked)
|
|
|
|
return;
|
2012-07-14 00:34:49 +02:00
|
|
|
|
2012-07-08 18:25:33 +02:00
|
|
|
var cmpObstruction = Engine.QueryInterface(this.entity, IID_Obstruction);
|
|
|
|
if (!cmpObstruction)
|
|
|
|
return;
|
2012-07-14 00:34:49 +02:00
|
|
|
|
|
|
|
// Disable 'block movement'
|
2012-07-08 18:25:33 +02:00
|
|
|
cmpObstruction.SetDisableBlockMovementPathfinding(true, true, 0);
|
|
|
|
this.opened = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
Gate.prototype.CloseGate = function()
|
|
|
|
{
|
|
|
|
var cmpObstruction = Engine.QueryInterface(this.entity, IID_Obstruction);
|
|
|
|
if (!cmpObstruction)
|
|
|
|
return;
|
2012-07-14 00:34:49 +02:00
|
|
|
|
|
|
|
// If we ordered the gate to be locked, enable 'block movement' and 'block pathfinding'
|
|
|
|
if (this.locked)
|
|
|
|
cmpObstruction.SetDisableBlockMovementPathfinding(false, false, 0);
|
|
|
|
// Else just enable 'block movement'
|
|
|
|
else
|
|
|
|
cmpObstruction.SetDisableBlockMovementPathfinding(false, true, 0);
|
2012-07-08 18:25:33 +02:00
|
|
|
this.opened = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
Engine.RegisterComponentType(IID_Gate, "Gate", Gate);
|