put the unit commands in a JS object

This was SVN commit r15340.
This commit is contained in:
sanderd17 2014-06-13 11:35:16 +00:00
parent 2b408e766b
commit 2b8758745f
3 changed files with 255 additions and 198 deletions

View File

@ -1505,82 +1505,16 @@ function changePrimarySelectionGroup(templateName, deselectGroup)
// Performs the specified command (delete, town bell, repair, etc.)
function performCommand(entity, commandName)
{
if (entity)
{
var entState = GetExtendedEntityState(entity);
var playerID = Engine.GetPlayerID();
if (!entity)
return;
var entState = GetExtendedEntityState(entity);
var playerID = Engine.GetPlayerID();
if (entState.player == playerID || g_DevSettings.controlAll)
{
switch (commandName)
{
case "delete":
var selection = g_Selection.toList();
if (selection.length > 0)
if (!entState.resourceSupply || !entState.resourceSupply.killBeforeGather)
openDeleteDialog(selection);
break;
case "stop":
var selection = g_Selection.toList();
if (selection.length > 0)
stopUnits(selection);
break;
case "garrison":
inputState = INPUT_PRESELECTEDACTION;
preSelectedAction = ACTION_GARRISON;
break;
case "repair":
inputState = INPUT_PRESELECTEDACTION;
preSelectedAction = ACTION_REPAIR;
break;
case "add-guard":
inputState = INPUT_PRESELECTEDACTION;
preSelectedAction = ACTION_GUARD;
break;
case "remove-guard":
removeGuard();
break;
case "unload":
unloadSelection();
break;
case "unload-all":
unloadAll();
break;
case "focus-rally":
// if the selected building has a rally point set, move the camera to it; otherwise, move to the building itself
// (since that's where units will spawn without a rally point)
var focusTarget = null;
if (entState.rallyPoint && entState.rallyPoint.position)
{
focusTarget = entState.rallyPoint.position;
}
else
{
if (entState.position)
focusTarget = entState.position;
}
if (focusTarget !== null)
Engine.CameraMoveTo(focusTarget.x, focusTarget.z);
break;
case "back-to-work":
backToWork();
break;
case "increase-alert-level":
increaseAlertLevel();
break;
case "alert-end":
endOfAlert();
break;
case "select-trading-goods":
toggleTrade();
break;
default:
break;
}
}
}
if (!entState.player == playerID && !g_DevSettings.controlAll)
return;
if (entityCommands[commandName])
entityCommands[commandName].execute(entState);
}
// Performs the specified formation

View File

@ -621,6 +621,245 @@ var unitActions =
},
};
/**
* Info and actions for the entity commands
* Currently displayed in the bottom of the central pane
*/
var entityCommands =
{
// Unload
"unload-all": {
"getInfo": function(entState)
{
if (!entState.garrisonHolder)
return false;
return {
"tooltip": translate("Unload All"),
"icon": "garrison-out.png"
};
},
"execute": function(entState)
{
unloadAdd();
},
},
// Delete
"delete": {
"getInfo": function(entState)
{
return {
"tooltip": translate("Delete"),
"icon": "kill_small.png"
};
},
"execute": function(entState)
{
var selection = g_Selection.toList();
if (selection.length < 1)
return;
if (!entState.resourceSupply || !entState.resourceSupply.killBeforeGather)
openDeleteDialog(selection);
},
},
// Stop
"stop": {
"getInfo": function(entState)
{
if (!entState.unitAI)
return false;
return {
"tooltip": translate("Stop"),
"icon": "stop.png"
};
},
"execute": function(entState)
{
var selection = g_Selection.toList();
if (selection.length > 0)
stopUnits(selection);
},
},
// Garrison
"garrison": {
"getInfo": function(entState)
{
if (!entState.unitAI || entState.turretParent)
return false;
return {
"tooltip": translate("Garrison"),
"icon": "garrison.png"
};
},
"execute": function(entState)
{
inputState = INPUT_PRESELECTEDACTION;
preSelectedAction = ACTION_GARRISON;
},
},
// Ungarrison
"unload": {
"getInfo": function(entState)
{
if (!entState.unitAI || !entState.turretParent)
return false;
var p = GetEntityState(entState.turretParent);
if (p.garrisonHolder || p.garrisonHolder.entities.indexOf(entState.id) == -1)
return false;
return {
"tooltip": translate("Unload"),
"icon": "garrison-out.png"
};
},
"execute": function(entState)
{
unloadSelection();
},
},
// Repair
"repair": {
"getInfo": function(entState)
{
if (!entState.buildEntities)
return false;
return {
"tooltip": translate("Repair"),
"icon": "repair.png"
};
},
"execute": function(entState)
{
inputState = INPUT_PRESELECTEDACTION;
preSelectedAction = ACTION_REPAIR;
},
},
// Focus on rally point
"focus-rally": {
"getInfo": function(entState)
{
if (!entState.rallyPoint)
return false;
return {
"tooltip": translate("Focus on Rally Point"),
"icon": "focus-rally.png"
};
},
"execute": function(entState)
{
var focusTarget = null;
if (entState.rallyPoint && entState.rallyPoint.position)
focusTarget = entState.rallyPoint.position;
else if (entState.position)
focusTarget = entState.position;
if (focusTarget)
Engine.CameraMoveTo(focusTarget.x, focusTarget.z);
},
},
// Back to work
"back-to-work": {
"getInfo": function(entState)
{
if (!entState.unitAI || !entState.unitAI.hasWorkOrders)
return false;
return {
"tooltip": translate("Back to Work"),
"icon": "production.png"
};
},
"execute": function(entState)
{
backToWork();
},
},
// Guard
"add-guard": {
"getInfo": function(entState)
{
if (!entState.unitAI || !entState.unitAI.canGuard || entState.unitAI.isGuarding)
return false;
return {
"tooltip": translate("Guard"),
"icon": "add-guard.png"
};
},
"execute": function(entState)
{
inputState = INPUT_PRESELECTEDACTION;
preSelectedAction = ACTION_GUARD;
},
},
// Remove guard
"remove-guard": {
"getInfo": function(entState)
{
if (!entState.unitAI || !entState.unitAI.isGuarding)
return false;
return {
"tooltip": translate("Remove guard"),
"icon": "remove-guard.png"
};
},
"execute": function(entState)
{
removeGuard();
},
},
// Trading
"select-trading-goods": {
"getInfo": function(entState)
{
if (!hasClass(entState, "Market"))
return false;
return {
"tooltip": translate("Select trading goods"),
"icon": "economics.png"
};
},
"execute": function(entState)
{
toggleTrade();
},
},
// Raise alert
"increase-alert-level": {
"getInfo": function(entState)
{
if(!entState.alertRaiser || !entState.alertRaiser.canIncreaseLevel)
return false;
if(entState.alertRaiser.hasRaisedAlert)
var tooltip = translate("Increase the alert level to protect more units");
else
var tooltip = translate("Raise an alert!");
return {
"tooltip": tooltip,
"icon": "bell_level1.png"
};
},
"execute": function(entState)
{
increaseAlertLevel();
},
},
// End alert
"alert-end": {
"getInfo": function(entState)
{
if(!entState.alertRaiser || !entState.alertRaiser.hasRaisedAlert)
return false
return {
"tooltip": translate("End of alert."),
"icon": "bell_level0.png"
};
},
"execute": function(entState)
{
endOfAlert();
},
},
};
function playerCheck(entState, targetState, validPlayers)
{
var playerState = GetSimState().players[entState.player];
@ -634,4 +873,4 @@ function playerCheck(entState, targetState, validPlayers)
return true;
}
return false;
}
};

View File

@ -282,130 +282,14 @@ function armorTypesToText(dmg)
function getEntityCommandsList(entState)
{
var commands = [];
if (entState.garrisonHolder)
for (var c in entityCommands)
{
commands.push({
"name": "unload-all",
"tooltip": translate("Unload All"),
"icon": "garrison-out.png"
});
var info = entityCommands[c].getInfo(entState);
if (!info)
continue;
info.name = c;
commands.push(info);
}
commands.push({
"name": "delete",
"tooltip": translate("Delete"),
"icon": "kill_small.png"
});
if (hasClass(entState, "Unit"))
{
commands.push({
"name": "stop",
"tooltip": translate("Stop"),
"icon": "stop.png"
});
}
if (entState.unitAI)
{
if (entState.turretParent)
{
var parent = GetEntityState(entState.turretParent);
if (
parent.garrisonHolder &&
parent.garrisonHolder.entities.indexOf(entState.id) != -1
)
commands.push({
"name": "unload",
"tooltip": translate("Unload"),
"icon": "garrison-out.png"
});
}
else
commands.push({
"name": "garrison",
"tooltip": translate("Garrison"),
"icon": "garrison.png"
});
}
if (entState.buildEntities)
{
commands.push({
"name": "repair",
"tooltip": translate("Repair"),
"icon": "repair.png"
});
}
if (entState.rallyPoint)
{
commands.push({
"name": "focus-rally",
"tooltip": translate("Focus on Rally Point"),
"icon": "focus-rally.png"
});
}
if (entState.unitAI && entState.unitAI.hasWorkOrders)
{
commands.push({
"name": "back-to-work",
"tooltip": translate("Back to Work"),
"icon": "production.png"
});
}
if (entState.unitAI && entState.unitAI.canGuard && !entState.unitAI.isGuarding)
{
commands.push({
"name": "add-guard",
"tooltip": translate("Guard"),
"icon": "add-guard.png"
});
}
if (entState.unitAI && entState.unitAI.isGuarding)
{
commands.push({
"name": "remove-guard",
"tooltip": translate("Remove guard"),
"icon": "remove-guard.png"
});
}
if (hasClass(entState, "Market"))
{
commands.push({
"name": "select-trading-goods",
"tooltip": translate("Select trading goods"),
"icon": "economics.png"
});
}
if(entState.alertRaiser)
{
if(entState.alertRaiser.canIncreaseLevel)
{
if(entState.alertRaiser.hasRaisedAlert)
var tooltip = translate("Increase the alert level to protect more units");
else
var tooltip = translate("Raise an alert!");
commands.push({
"name": "increase-alert-level",
"tooltip": tooltip,
"icon": "bell_level1.png"
});
}
if(entState.alertRaiser.hasRaisedAlert)
commands.push({
"name": "alert-end",
"tooltip": translate("End of alert."),
"icon": "bell_level0.png"
});
}
return commands;
}