Ticket #1260: show-carry-anim.2.patch

File show-carry-anim.2.patch, 11.7 KB (added by Deiz, 12 years ago)
  • source/simulation2/components/ICmpVisual.cpp

     
    2424BEGIN_INTERFACE_WRAPPER(Visual)
    2525DEFINE_INTERFACE_METHOD_4("SelectAnimation", void, ICmpVisual, SelectAnimation, std::string, bool, fixed, std::wstring)
    2626DEFINE_INTERFACE_METHOD_1("SelectMovementAnimation", void, ICmpVisual, SelectMovementAnimation, fixed)
     27DEFINE_INTERFACE_METHOD_1("ResetMoveAnimation", void, ICmpVisual, ResetMoveAnimation, std::string)
     28DEFINE_INTERFACE_METHOD_2("ReplaceMoveAnimation", void, ICmpVisual, ReplaceMoveAnimation, std::string, std::string)
    2729DEFINE_INTERFACE_METHOD_1("SetAnimationSyncRepeat", void, ICmpVisual, SetAnimationSyncRepeat, fixed)
    2830DEFINE_INTERFACE_METHOD_1("SetAnimationSyncOffset", void, ICmpVisual, SetAnimationSyncOffset, fixed)
    2931DEFINE_INTERFACE_METHOD_4("SetShadingColour", void, ICmpVisual, SetShadingColour, fixed, fixed, fixed, fixed)
  • source/simulation2/components/ICmpUnitMotion.h

     
    7979    virtual void StopMoving() = 0;
    8080
    8181    /**
     82     * Get the current movement speed.
     83     */
     84    virtual fixed GetCurrentSpeed() = 0;
     85
     86    /**
    8287     * Set the current movement speed.
    8388     */
    8489    virtual void SetSpeed(fixed speed) = 0;
  • source/simulation2/components/CCmpVisualActor.cpp

     
    2323#include "ICmpOwnership.h"
    2424#include "ICmpPosition.h"
    2525#include "ICmpRangeManager.h"
     26#include "ICmpUnitMotion.h"
    2627#include "ICmpVision.h"
    2728#include "simulation2/MessageTypes.h"
    2829#include "simulation2/components/ICmpFootprint.h"
     
    5960
    6061    fixed m_R, m_G, m_B; // shading colour
    6162
     63    std::map<std::string, std::string> m_AnimOverride;
     64
    6265    ICmpRangeManager::ELosVisibility m_Visibility; // only valid between Interpolate and RenderSubmit
    6366
    6467    // Current animation state
     
    372375        }
    373376    }
    374377
     378    virtual void ReplaceMoveAnimation(std::string name, std::string replace)
     379    {
     380        m_AnimOverride[name] = replace;
     381    }
     382
     383    virtual void ResetMoveAnimation(std::string name)
     384    {
     385        std::map<std::string, std::string>::const_iterator it = m_AnimOverride.find(name);
     386        if (it != m_AnimOverride.end())
     387            m_AnimOverride.erase(name);
     388    }
     389
    375390    virtual void SetUnitEntitySelection(const CStr& selection)
    376391    {
    377392        if (m_Unit)
     
    566581    model.SetCustomSelectionShape(shapeDescriptor);
    567582}
    568583
    569 void CCmpVisualActor::Update(fixed turnLength)
     584void CCmpVisualActor::Update(fixed UNUSED(turnLength))
    570585{
    571586    if (m_Unit == NULL)
    572587        return;
     
    580595        if (!cmpPosition || !cmpPosition->IsInWorld())
    581596            return;
    582597
    583         float speed = cmpPosition->GetDistanceTravelled().ToFloat() / turnLength.ToFloat();
     598        CmpPtr<ICmpUnitMotion> cmpUnitMotion(GetSimContext(), GetEntityId());
     599        if (!cmpUnitMotion)
     600            return;
    584601
     602        float speed = cmpUnitMotion->GetCurrentSpeed().ToFloat();
     603
     604        std::string name;
    585605        if (speed == 0.0f)
     606            name = "idle";
     607        else
     608            name = (speed < m_AnimRunThreshold.ToFloat()) ? "walk" : "run";
     609
     610        std::map<std::string, std::string>::const_iterator it = m_AnimOverride.find(name);
     611        if (it != m_AnimOverride.end())
     612            name = it->second;
     613
     614        m_Unit->SetEntitySelection(name);
     615        if (speed == 0.0f)
    586616        {
    587             m_Unit->SetEntitySelection("idle");
     617            m_Unit->SetEntitySelection(name);
    588618            if (m_Unit->GetAnimation())
    589                 m_Unit->GetAnimation()->SetAnimationState("idle", false, 1.f, 0.f, L"");
     619                m_Unit->GetAnimation()->SetAnimationState(name, false, 1.f, 0.f, L"");
    590620        }
    591         else if (speed < m_AnimRunThreshold.ToFloat())
    592         {
    593             m_Unit->SetEntitySelection("walk");
    594             if (m_Unit->GetAnimation())
    595                 m_Unit->GetAnimation()->SetAnimationState("walk", false, speed, 0.f, L"");
    596         }
    597621        else
    598622        {
    599             m_Unit->SetEntitySelection("run");
     623            m_Unit->SetEntitySelection(name);
    600624            if (m_Unit->GetAnimation())
    601                 m_Unit->GetAnimation()->SetAnimationState("run", false, speed, 0.f, L"");
     625                m_Unit->GetAnimation()->SetAnimationState(name, false, speed, 0.f, L"");
    602626        }
    603627    }
    604628}
  • source/simulation2/components/ICmpUnitMotion.cpp

     
    2929DEFINE_INTERFACE_METHOD_3("MoveToFormationOffset", void, ICmpUnitMotion, MoveToFormationOffset, entity_id_t, entity_pos_t, entity_pos_t)
    3030DEFINE_INTERFACE_METHOD_2("FaceTowardsPoint", void, ICmpUnitMotion, FaceTowardsPoint, entity_pos_t, entity_pos_t)
    3131DEFINE_INTERFACE_METHOD_0("StopMoving", void, ICmpUnitMotion, StopMoving)
     32DEFINE_INTERFACE_METHOD_0("GetCurrentSpeed", fixed, ICmpUnitMotion, GetCurrentSpeed)
    3233DEFINE_INTERFACE_METHOD_1("SetSpeed", void, ICmpUnitMotion, SetSpeed, fixed)
    3334DEFINE_INTERFACE_METHOD_0("GetWalkSpeed", fixed, ICmpUnitMotion, GetWalkSpeed)
    3435DEFINE_INTERFACE_METHOD_0("GetRunSpeed", fixed, ICmpUnitMotion, GetRunSpeed)
     
    7172        m_Script.CallVoid("StopMoving");
    7273    }
    7374
     75    virtual fixed GetCurrentSpeed()
     76    {
     77        return m_Script.Call<fixed>("GetCurrentSpeed");
     78    }
     79
    7480    virtual void SetSpeed(fixed speed)
    7581    {
    7682        m_Script.CallVoid("SetSpeed", speed);
  • source/simulation2/components/CCmpUnitMotion.cpp

     
    227227
    228228    fixed m_Speed;
    229229
     230    // Current mean speed (over the last turn).
     231    fixed m_CurSpeed;
     232
    230233    // Currently active paths (storing waypoints in reverse order).
    231234    // The last item in each path is the point we're currently heading towards.
    232235    ICmpPathfinder::Path m_LongPath;
     
    278281
    279282        m_WalkSpeed = paramNode.GetChild("WalkSpeed").ToFixed();
    280283        m_Speed = m_WalkSpeed;
     284        m_CurSpeed = fixed::Zero();
    281285
    282286        if (paramNode.GetChild("Run").IsOk())
    283287        {
     
    405409        return m_PassClass;
    406410    }
    407411
     412    virtual fixed GetCurrentSpeed()
     413    {
     414        return m_CurSpeed;
     415    }
     416
    408417    virtual void SetSpeed(fixed speed)
    409418    {
    410419        m_Speed = speed;
     
    484493        if (cmpObstruction)
    485494            cmpObstruction->SetMovingFlag(false);
    486495
     496        // No longer moving, so speed is 0.
     497        m_CurSpeed = fixed::Zero();
     498
    487499        CMessageMotionChanged msg(false, false);
    488500        GetSimContext().GetComponentManager().PostMessage(GetEntityId(), msg);
    489501    }
     
    858870        if (pos != initialPos)
    859871            cmpPosition->MoveTo(pos.X, pos.Y);
    860872
     873        // Calculate the mean speed over this past turn.
     874        m_CurSpeed = cmpPosition->GetDistanceTravelled() / dt;
     875
    861876        if (wasObstructed)
    862877        {
    863878            // Oops, we hit something (very likely another unit).
  • source/simulation2/components/ICmpVisual.h

     
    9292    virtual void SelectAnimation(std::string name, bool once, fixed speed, std::wstring soundgroup) = 0;
    9393
    9494    /**
     95     * Replaces a specified animation with another. Only affects the special speed-based
     96     * animation determination behaviour.
     97     * @param name Animation to match.
     98     * @param replace Animation that should replace the matched animation.
     99     */
     100    virtual void ReplaceMoveAnimation(std::string name, std::string replace) = 0;
     101
     102    /**
     103     * Ensures that the given animation will be used when it normally would be,
     104     * removing reference to any animation that might replace it.
     105     * @param name Animation name to remove from the replacement map.
     106     */
     107    virtual void ResetMoveAnimation(std::string name) = 0;
     108
     109    /**
    95110     * Sets the specified entity selection on the underlying unit.
    96111     */
    97112    virtual void SetUnitEntitySelection(const CStr& selection) = 0;
  • binaries/data/mods/public/simulation/components/UnitAI.js

     
    818818
    819819            "APPROACHING": {
    820820                "enter": function () {
     821                    // Show weapons rather than carried resources.
     822                    this.SetGathererAnimationOverride(true);
     823
    821824                    this.SelectAnimation("move");
    822825                    this.StartTimer(1000, 1000);
    823826                },
    824827
    825828                "leave": function() {
     829                    // Show carried resources when walking.
     830                    this.SetGathererAnimationOverride();
     831
    826832                    this.StopTimer();
    827833                },
    828834
     
    933939
    934940            "CHASING": {
    935941                "enter": function () {
     942                    // Show weapons rather than carried resources.
     943                    this.SetGathererAnimationOverride(true);
     944
    936945                    this.SelectAnimation("move");
    937946                    this.StartTimer(1000, 1000);
    938947                },
    939948
    940949                "leave": function() {
     950                    // Show carried resources when walking.
     951                    this.SetGathererAnimationOverride();
     952
    941953                    this.StopTimer();
    942954                },
    943955
     
    10861098
    10871099                "leave": function() {
    10881100                    this.StopTimer();
     1101
     1102                    // Show the carried resource, if we've gathered anything.
     1103                    this.SetGathererAnimationOverride();
    10891104                },
    10901105
    10911106                "Timer": function(msg) {
     
    13191334        "RETURNRESOURCE": {
    13201335            "APPROACHING": {
    13211336                "enter": function () {
    1322                     // Work out what we're carrying, in order to select an appropriate animation
    1323                     var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
    1324                     var type = cmpResourceGatherer.GetLastCarriedType();
    1325                     if (type)
    1326                     {
    1327                         var typename = "carry_" + type.generic;
    1328 
    1329                         // Special case for meat
    1330                         if (type.specific == "meat")
    1331                             typename = "carry_" + type.specific;
    1332 
    1333                         this.SelectAnimation(typename, false, this.GetWalkSpeed());
    1334                     }
    1335                     else
    1336                     {
    1337                         // We're returning empty-handed
    1338                         this.SelectAnimation("move");
    1339                     }
     1337                    this.SelectAnimation("move");
    13401338                },
    13411339
    13421340                "MoveCompleted": function() {
     
    13571355                            var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
    13581356                            cmpResourceGatherer.CommitResources(dropsiteTypes);
    13591357
     1358                            // Stop showing the carried resource animation.
     1359                            this.SetGathererAnimationOverride();
     1360
    13601361                            // Our next order should always be a Gather,
    13611362                            // so just switch back to that order
    13621363                            this.FinishOrder();
     
    15461547        "GARRISON": {
    15471548            "APPROACHING": {
    15481549                "enter": function() {
    1549                     this.SelectAnimation("walk", false, this.GetWalkSpeed());
     1550                    this.SelectAnimation("move");
    15501551                },
    15511552
    15521553                "MoveCompleted": function() {
     
    24202421    }
    24212422};
    24222423
     2424UnitAI.prototype.SetGathererAnimationOverride = function(disable)
     2425{
     2426    var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer);
     2427    if (!cmpResourceGatherer)
     2428        return;
     2429
     2430    var cmpVisual = Engine.QueryInterface(this.entity, IID_Visual);
     2431    if (!cmpVisual)
     2432        return;
     2433
     2434    // Remove the animation override, so that weapons are shown again.
     2435    if (disable)
     2436    {
     2437        cmpVisual.ResetMoveAnimation("walk");
     2438        return;
     2439    }
     2440
     2441    // Work out what we're carrying, in order to select an appropriate animation
     2442    var type = cmpResourceGatherer.GetLastCarriedType();
     2443    if (type)
     2444    {
     2445        var typename = "carry_" + type.generic;
     2446
     2447        // Special case for meat
     2448        if (type.specific == "meat")
     2449            typename = "carry_" + type.specific;
     2450
     2451        cmpVisual.ReplaceMoveAnimation("walk", typename);
     2452    }
     2453    else
     2454        cmpVisual.ResetMoveAnimation("walk");
     2455}
     2456
    24232457UnitAI.prototype.SelectAnimation = function(name, once, speed, sound)
    24242458{
    24252459    var cmpVisual = Engine.QueryInterface(this.entity, IID_Visual);