Ticket #565: Victory_conditions.diff

File Victory_conditions.diff, 10.9 KB (added by fcxSanya, 14 years ago)
  • binaries/data/mods/public/gui/page_summary.xml

     
     1<?xml version="1.0" encoding="utf-8"?>
     2<page>
     3    <include>common/setup.xml</include>
     4    <include>common/styles.xml</include>
     5    <include>common/sprite1.xml</include>
     6    <include>common/init.xml</include>
     7    <include>summary/summary.xml</include>
     8    <include>common/global.xml</include>
     9</page>
  • binaries/data/mods/public/gui/session_new/session.js

     
    8686
    8787function onTick()
    8888{
     89    checkPlayerState();
     90
    8991    while (true)
    9092    {
    9193        var message = Engine.PollNetworkClient();
     
    119121        getGUIObjectByName("resourcePop").textcolor = "0 0 0";
    120122}
    121123
     124function gotoSummary(/*string*/ gameResult)
     125{
     126    stopMusic();
     127    endGame();
     128    Engine.SwitchGuiPage("page_summary.xml", { "gameResult" : gameResult });
     129}
     130
     131function checkPlayerState()
     132{
     133    var simState = Engine.GuiInterfaceCall("GetSimulationState");
     134    var playerState = simState.players[Engine.GetPlayerID()];
     135   
     136    if (playerState.state == simState.player_states.DEFEATED)
     137    {
     138        gotoSummary("You have been defeated...");
     139    }
     140    else if (playerState.state == simState.player_states.WON)
     141    {
     142        gotoSummary("You have won a battle!");
     143    }
     144}
     145
    122146function onSimulationUpdate()
    123147{
    124148    g_Selection.dirty = false;
  • binaries/data/mods/public/gui/summary/summary.js

     
     1function init(data)
     2{
     3    getGUIObjectByName ("summaryTitleBar").caption = "Summary";
     4    getGUIObjectByName ("summaryText").caption = data.gameResult;
     5}
  • binaries/data/mods/public/gui/summary/summary.xml

     
     1<?xml version="1.0" encoding="utf-8"?>
     2
     3<!--
     4==========================================
     5- SUMMARY SCREEN -
     6==========================================
     7-->
     8
     9<objects>
     10    <script file="gui/summary/summary.js"/>
     11    <object name="summary"
     12            type="image"
     13            sprite="bkFillBlack"
     14        >
     15        <!--
     16        ==========================================
     17        - SUMMARY SCREEN - WINDOW
     18        ==========================================
     19        -->
     20
     21        <object name="summaryWindow"
     22            style="wheatWindowGranite"
     23            type="image"
     24            size="25 35 100%-25 100%-25"
     25        >
     26            <object name="summaryTitleBar"
     27                style="wheatWindowTitleBar"
     28                type="button"
     29            />
     30           
     31            <object name="summaryText"
     32                type="text"
     33                size="50 50 100%-50 100%-200"
     34                font="serif-16"
     35                text_align="center"
     36                text_valign="center"
     37            />
     38           
     39            <object type="button" style="wheatButton" size="100%-150 100%-40 100% 100%">
     40                Main menu
     41                <action on="Press"><![CDATA[
     42                    Engine.SwitchGuiPage("page_pregame.xml");
     43                ]]></action>
     44            </object>
     45        </object>
     46    </object>
     47</objects>
  • binaries/data/mods/public/simulation/components/EndGameManager.js

     
     1// Interval, which defines how often game will check end game conditions
     2var g_ProgressInterval = 1000;
     3
     4/*
     5    EndGameManager is the system component, which regularly (by timer) checks
     6    victory/defeat conditions and if it is satisfied ends the game
     7*/
     8function EndGameManager() {}
     9
     10EndGameManager.prototype.Schema =
     11    "<a:component type='system'/><empty/>";
     12
     13// static member - gameTypes enum
     14EndGameManager.GameTypes = { CONQUEST : 0 };
     15   
     16EndGameManager.prototype.Init = function()
     17{
     18    print("EndGameManager init");
     19    // only CONQUEST mode for now
     20    this.gameType = EndGameManager.GameTypes.CONQUEST;
     21   
     22    // start the timer
     23    var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     24    this.timer = cmpTimer.SetTimeout(this.entity, IID_EndGameManager, "ProgressTimeout", g_ProgressInterval, {});   
     25};
     26
     27EndGameManager.prototype.OnDestroy = function()
     28{
     29    if (this.timer)
     30    {
     31        var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     32        cmpTimer.CancelTimer(this.timer);
     33    }
     34};
     35
     36EndGameManager.prototype.ProgressTimeout = function(data)
     37{
     38    this.UpdatePlayerStates();
     39   
     40    // repeat the timer
     41    var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     42    this.timer = cmpTimer.SetTimeout(this.entity, IID_EndGameManager, "ProgressTimeout", g_ProgressInterval, data);
     43}
     44
     45EndGameManager.prototype.UpdatePlayerStates = function()
     46{
     47    var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     48    switch (this.gameType)
     49    {
     50        case EndGameManager.GameTypes.CONQUEST:
     51            for (var i = 0; i < cmpPlayerManager.GetNumPlayers(); i++)
     52            {
     53                var playerEntityId = cmpPlayerManager.GetPlayerByID(i);
     54                var cmpPlayer = Engine.QueryInterface(playerEntityId, IID_Player);
     55                if (cmpPlayer.state == cmpPlayer.GameStates.ACTIVE)
     56                {
     57                    if (cmpPlayer.GetPopulationCount() == 0 && cmpPlayer.structuresCount == 0)
     58                    {
     59                        cmpPlayer.state = cmpPlayer.GameStates.DEFEATED;
     60                    }
     61                }
     62            }
     63            // TODO: update this code for allies
     64            var alivePlayersCount = 0;
     65            var lastAlivePlayerId = -1;
     66            for (var i = 0; i < cmpPlayerManager.GetNumPlayers(); i++)
     67            {
     68                var playerEntityId = cmpPlayerManager.GetPlayerByID(i);
     69                var cmpPlayer = Engine.QueryInterface(playerEntityId, IID_Player);
     70                if (cmpPlayer.state == cmpPlayer.GameStates.ACTIVE)
     71                {
     72                    alivePlayersCount++;
     73                    lastAlivePlayerId = i;
     74                }
     75            }
     76            if (alivePlayersCount == 1)
     77            {
     78                var playerEntityId = cmpPlayerManager.GetPlayerByID(lastAlivePlayerId);
     79                var cmpPlayer = Engine.QueryInterface(playerEntityId, IID_Player);
     80                cmpPlayer.state = cmpPlayer.GameStates.WON;
     81            }
     82            break;
     83        default:
     84            // strange game type ...
     85    }
     86}
     87
     88Engine.RegisterComponentType(IID_EndGameManager, "EndGameManager", EndGameManager);
  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    2020GuiInterface.prototype.GetSimulationState = function(player)
    2121{
    2222    var ret = {
    23         "players": []
     23        "players": [],
     24        "player_states": undefined
    2425    };
    2526
    2627    var cmpPlayerMan = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     
    3637            "popCount": cmpPlayer.GetPopulationCount(),
    3738            "popLimit": cmpPlayer.GetPopulationLimit(),
    3839            "resourceCounts": cmpPlayer.GetResourceCounts(),
    39             "trainingQueueBlocked": cmpPlayer.IsTrainingQueueBlocked()
     40            "trainingQueueBlocked": cmpPlayer.IsTrainingQueueBlocked(),
     41            "state": cmpPlayer.GetState()
    4042        };
    4143        ret.players.push(playerData);
     44        ret.player_states = cmpPlayer.GameStates;
    4245    }
    4346
    4447    return ret;
  • binaries/data/mods/public/simulation/components/Identity.js

     
    6262                        "<value>Mechanical</value>" +
    6363                        "<value>Super</value>" +
    6464                        "<value>Hero</value>" +
     65                        "<value>Structure</value>" +
    6566                        "<value>Civic</value>" +
    6667                        "<value>Economic</value>" +
    6768                        "<value>Defensive</value>" +
  • binaries/data/mods/public/simulation/components/interfaces/EndGameManager.js

     
     1Engine.RegisterInterface("EndGameManager");
  • binaries/data/mods/public/simulation/components/Player.js

     
    1919        "metal": 500,   
    2020        "stone": 1000   
    2121    };
     22    // player game state
     23    this.state = this.GameStates.ACTIVE;
     24    // buildings count
     25    this.structuresCount = 0;
    2226};
    2327
     28// static enum: player game state
     29Player.prototype.GameStates = { ACTIVE : 0, DEFEATED : 1, WON : 2 };
     30
    2431Player.prototype.SetPlayerID = function(id)
    2532{
    2633    this.playerID = id;
     
    146153    return true;
    147154};
    148155
     156Player.prototype.GetState = function()
     157{
     158    return this.state;
     159};
     160
    149161// Keep track of population effects of all entities that
    150162// become owned or unowned by this player
    151163Player.prototype.OnGlobalOwnershipChanged = function(msg)
    152164{
     165    var cmpIdentity = Engine.QueryInterface(msg.entity, IID_Identity);
     166    // Is this entity a structure?
     167    var isStructure = (cmpIdentity && cmpIdentity.GetClassesList().indexOf("Structure") != -1);
     168
    153169    if (msg.from == this.playerID)
    154170    {
     171        if (isStructure)
     172        {
     173            this.structuresCount--;
     174        }
    155175        var cost = Engine.QueryInterface(msg.entity, IID_Cost);
    156176        if (cost)
    157177        {
     
    162182   
    163183    if (msg.to == this.playerID)
    164184    {
     185        if (isStructure)
     186        {
     187            this.structuresCount++;
     188        }
    165189        var cost = Engine.QueryInterface(msg.entity, IID_Cost);
    166190        if (cost)
    167191        {
  • binaries/data/mods/public/simulation/templates/template_structure.xml

     
    22<Entity parent="template_entity_full">
    33  <Identity>
    44    <GenericName>Structure</GenericName>
     5    <Classes datatype="tokens">Structure</Classes>
    56    <IconSheet>snPortraitSheetBuildings</IconSheet>
    67  </Identity>
    78  <BuildRestrictions>
  • source/simulation2/Simulation2.cpp

     
    112112            LOAD_SCRIPTED_COMPONENT("GuiInterface");
    113113            LOAD_SCRIPTED_COMPONENT("PlayerManager");
    114114            LOAD_SCRIPTED_COMPONENT("Timer");
     115            LOAD_SCRIPTED_COMPONENT("EndGameManager");
    115116
    116117#undef LOAD_SCRIPTED_COMPONENT
    117118        }