home *** CD-ROM | disk | FTP | other *** search
/ Singles (French) / Singles-FrenchVersion-Win95.iso / data1.cab / Statemachine / trashcanChar.lua < prev    next >
Text File  |  2004-03-05  |  6KB  |  182 lines

  1. -- trashcan character state machine
  2. beginStateMachine()
  3.     
  4.     onEnter(function(msg)
  5.         local trashcan = getStateObjectFromID(msg.sender);
  6.         storeStateObject("trashcan", trashcan);
  7.         
  8.         if (trashcan) then
  9.             -- trashcan does exist
  10.             local actionPointName = retrieveData("actionPointName");
  11.             if exitIfWrongPosition(getParent(), trashcan, actionPointName) then return end;
  12.  
  13.             if (getParent().isOneActionPointLocked(trashcan)) then
  14.                 -- action point is locked
  15.                 getParent().setEmoticon(EMOTICON_CANNOT, EMOTICON_DELAY);
  16.                 sendMsg("emoThink", getParent().walkSO);
  17.                 exitStateMachine();
  18.             else
  19.                 getParent().lockActionPoints(trashcan);
  20.             end
  21.         else
  22.             -- trashcan does not exist anymore
  23.             getParent().setEmoticon(EMOTICON_CANNOT, EMOTICON_DELAY);
  24.             sendMsg("emoThink", getParent().walkSO);
  25.             exitStateMachine();
  26.         end
  27.         
  28.         freeHands(getParent());
  29.                 
  30.     end )
  31.     
  32.     onExit(function(msg)
  33. --        local trashcan = retrieveStateObject("trashcan");
  34. --        getParent().unlockActionPoints(trashcan);
  35. --        getParent().stopAllActivities(trashcan);
  36. --        removeStateObject("trashcan");
  37.         
  38.         unlockAll("trashcan");    
  39.         
  40.         getParent().stopAllActivities();
  41.     end )
  42.     
  43.     state("takeTrash")
  44.     
  45.         onEnter(function(msg)
  46.             print("takeTrash");
  47.             
  48.             local trashcan = retrieveStateObject("trashcan");
  49.             local fillLevel = trashcan.retrieveData("fillLevel", 0.0);
  50.             
  51.             if (fillLevel < 0.01) then
  52.                 print("takeTrash traschcan empty");
  53.                 getParent().setEmoticon(EMOTICON_CANNOT, EMOTICON_DELAY);
  54.                 sendMsg("emoThink", getParent().walkSO);
  55.                 exitStateMachine();
  56.                 return
  57.             end
  58.             
  59.             
  60.             
  61.             getParent().startActivity("takeTrash", trashcan);
  62.             
  63.             sendDelayedMsgThis("grabCap", 960);
  64.             sendDelayedMsgThis("grabBag", 1600);
  65.             sendDelayedMsgThis("dropCap", 2600);
  66.                         
  67.             startAnimation("takeTrash");
  68.         end )
  69.     
  70.     
  71.  
  72.         onMsg("grabCap", function(msg)
  73.             getParent().playSound("trashcan"); print("grabCap");
  74.             -- attach cap to right hand of character
  75.             local trashcan = retrieveStateObject("trashcan");
  76.             local cap = trashcan.createGameObject("cap");
  77.             getParent().attachRightObjectHolder(trashcan, "cap");
  78.         end)
  79.  
  80.         onMsg("grabBag", function(msg)
  81.             -- attach cap to left hand of character
  82. --            local trashcan = retrieveStateObject("trashcan");
  83. --            local bag = trashcan.createGameObject("cap");
  84. --            getParent().attachRightObjectHolder(trashcan, "cap");
  85.             
  86.             local trashbag = getParent().loadGameObject("StandardGO","trashbag");
  87.             if (not trashbag) then print("create trashbag failed"); end
  88.             
  89.             local trashcan = retrieveStateObject("trashcan");
  90.             local stink = trashcan.findChildGO("stink");
  91.             local rate = stink.getEmitRate();
  92.             
  93.             if (rate > MIN_STINK_RATE) then
  94.                 print("bagstink rate " .. rate)
  95.                 trashbag.setChildEnable("stink", true);
  96.                 local bagstink = trashbag.findChildGO("stink");
  97.                 bagstink.setEmitRate(rate);
  98.             end;
  99.             
  100.  
  101.             
  102.             -- storeStateObject("trashbag", trashbag);
  103.             getParent().attachLeftObjectHolder(trashbag);            
  104.         end)
  105.  
  106.         onMsg("dropCap", function(msg)
  107.             getParent().playSound("trashcan");
  108.             getParent().detachRightObjectHolder();
  109.         end)
  110.  
  111.  
  112.  
  113.         onMsg("end", function(msg)
  114.             
  115.             local trashcan = retrieveStateObject("trashcan");
  116.             getParent().stopActivity("takeTrash", trashcan);
  117.             
  118.                     
  119.             if (testCancel()) then
  120.                 local trashbag = getParent().getLeftHeldObject()
  121.                 if (trashbag) then trashbag.deleteGameObject(); end
  122.                 exitStateMachine();
  123.             else
  124.  
  125.                 local character = this.getParent();
  126.                 
  127.                 local staircases = character.getObjectsWithBehavior("staircase");
  128.                 local staircase = staircases[1];
  129.                 if (not staircase) then
  130.                     print("no staircase");
  131.                     local trashbag = getParent().getLeftHeldObject()
  132.                     if (trashbag) then trashbag.deleteGameObject(); end
  133.                     character.setEmoticon(EMOTICON_CANNOT, EMOTICON_DELAY);
  134.                     sendMsg("emoThink", character.walkSO);
  135.                     exitStateMachine();
  136.                     return 
  137.                 end
  138.                 
  139.                 
  140.                 -- walk to the closest action point
  141.                 local actionPoint = character.getFreeActionPoint(staircase, "topStep");
  142.                 --local actionPoint = character.getClosestFreeActionPointToClickPoint(staircase, {"topStep"});
  143.                 if (actionPoint) then
  144.                 -- get the walk state object
  145.                     local wso = character.walkSO;
  146.                     -- create state machine contexts
  147.                     local wsoContext = StateMachineContext();
  148.                     -- store the action point
  149.                     --wsoContext.storeData("actionPointName", actionPoint.getName());
  150.                     --wsoContext.storeStateObject("staircase", staircase);
  151.                     if (wso.walkToActionPoint(actionPoint)) then
  152.                         character.handSO.setPose("leftHandHoldHigh");
  153.                         wso.queueStateMachine("staircaseChar.goTrash", staircase, wsoContext);
  154.                         
  155.                         addTrash(trashcan, -1.0);
  156.                                                 
  157.                         exitStateMachine();
  158.                     else
  159.                         print("no path found");
  160.                         local trashbag = getParent().getLeftHeldObject()
  161.                         if (trashbag) then trashbag.deleteGameObject(); end
  162.                         character.setEmoticon(EMOTICON_NOPATH, EMOTICON_DELAY);
  163.                         sendMsg("emoThink", character.walkSO);
  164.                         exitStateMachine();
  165.                     end
  166.                 else
  167.                     print("no action point found");
  168.                     local trashbag = getParent().getLeftHeldObject()
  169.                     if (trashbag) then trashbag.deleteGameObject(); end
  170.                     character.setEmoticon(EMOTICON_CANNOT, EMOTICON_DELAY);
  171.                     sendMsg("emoThink", character.walkSO);
  172.                     exitStateMachine();
  173.                     
  174.                 end
  175.             end
  176.                         
  177.         end )    
  178.         
  179.  
  180.                 
  181. endStateMachine()
  182.