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

  1. -- fridge character state machine
  2. beginStateMachine()
  3.     
  4.     onEnter(function(msg)
  5.         local fridge = getStateObjectFromID(msg.sender);
  6.         storeStateObject("fridge", fridge);
  7.     end )
  8.     
  9.     onExit(function(msg)
  10. --        local fridge = retrieveStateObject("fridge");
  11. --        getParent().unlockActionPoints(fridge);
  12. --        removeStateObject("fridge");
  13.     end )
  14.     
  15. --    onReturn(function(msg)
  16. --        -- back from subwalk
  17. --        print("onReturn global");
  18. --        setState(retrieveData("snackCommand"));
  19. --    end )
  20.  
  21.     -- open the fridge
  22.     state("open")
  23.     
  24.         onEnter(function(msg)
  25.             local fridge = retrieveStateObject("fridge");
  26.                 
  27.             if (fridge) then
  28.                 -- fridge does exist
  29.                 
  30.                 if exitIfWrongPosition(getParent(), fridge, retrieveData("actionPointName")) then return end;    
  31.                 
  32.                 
  33.                 if (getParent().isOneActionPointLocked(fridge)) then
  34.                     -- action point is locked
  35.                     getParent().setEmoticon(EMOTICON_CANNOT, EMOTICON_DELAY);
  36.                     sendMsg("emoThink", getParent().walkSO);
  37.                     exitStateMachine();
  38.                 else
  39.                     getParent().lockActionPoints(fridge);
  40.                 end
  41.             else
  42.                 -- fridge does not exist anymore
  43.                 getParent().setEmoticon(EMOTICON_CANNOT, EMOTICON_DELAY);
  44.                 sendMsg("emoThink", getParent().walkSO);
  45.                 exitStateMachine();
  46.             end
  47.             
  48.             freeHands(getParent());
  49.                 
  50.             -- create the cracker box
  51.             -- local crackerBox = fridge.createGameObject("CrackerBox");
  52.             -- start to open the fridge
  53.             startAnimation("openFridge");
  54.             fridge.startAnimation("fridge");
  55.             -- show the inside of the fridge
  56.             fridge.setChildEnable("fridgeIn", true);
  57.             -- show the cracker box
  58.             fridge.setChildEnable("CrackerBox", true);
  59.             -- send a delayed message for open sound
  60.             sendDelayedMsgThis("openSound", 800);
  61.             -- send a delayed message for attach
  62.             sendDelayedMsgThis("grabCrackerBox", 5000);
  63.             -- send a delayed message for close sound
  64.             sendDelayedMsgThis("closeSound", 6000);
  65.         end )
  66.     
  67.         onMsg("openSound", function(msg)
  68.             local fridge = retrieveStateObject("fridge");
  69.             fridge.playSound("fridgeOpen");
  70.             fridge.stopSound("fridgeLoop");
  71.         end)
  72.  
  73.         onMsg("grabCrackerBox", function(msg)
  74.             -- attach cracker box to left hand of character
  75.             local crackerBox = getParent().loadGameObject("StandardGO", "crackerBox");
  76.             storeStateObject("crackerBox", crackerBox);
  77.             getParent().attachLeftObjectHolder(crackerBox);
  78.             local fridge = retrieveStateObject("fridge");
  79.             fridge.playSound("fridgeTake");
  80.             fridge.setChildEnable("CrackerBox", false);
  81.         end)
  82.  
  83.         onMsg("closeSound", function(msg)
  84.             local fridge = retrieveStateObject("fridge");
  85.             fridge.playSound("fridgeClose");
  86.             fridge.stopSound("fridgeLoop");
  87.         end)
  88.  
  89.         onMsg("canceled", function(msg)
  90.             local crackerBox = getParent().getLeftHeldObject();
  91.             if (crackerBox) then
  92.                 getParent().detachLeftObjectHolder();
  93.                 crackerBox.deleteGameObject();
  94.                 -- hide the cracker box
  95.                 --fridge.setChildEnable("CrackerBox", false);
  96.             end
  97.             exitStateMachine();
  98.         end)
  99.  
  100.         onMsg("end", function(msg)
  101.             -- hide the inside of the fridge
  102.             local fridge = retrieveStateObject("fridge");
  103.             fridge.setChildEnable("fridgeIn", false);
  104.             
  105.             if testCancel() then
  106.                 sendMsgThis("canceled");
  107. --                -- detach cracker box if attached to left hand
  108. --                local crackerBox = getParent().getLeftHeldObject();
  109. --                if (crackerBox) then
  110. --                    getParent().detachLeftObjectHolder();
  111. --                    crackerBox.deleteGameObject();
  112. --                    -- hide the cracker box
  113. --                    --fridge.setChildEnable("CrackerBox", false);
  114. --                end
  115. --                exitStateMachine();
  116.             else
  117.                 --setState(retrieveData("snackCommand"));
  118.                 local actionPoint = getParent().findPointNear(1.0, 5.0);
  119.                 if (actionPoint) then
  120.                 
  121.                     if (walkToPointImmediate(actionPoint)) then
  122.                         local wsoContext = StateMachineContext();
  123.                         --wsoContext.storeStateObject("fridge", fridge);
  124.                         --queueStateMachine("fridgeSnackChar." .. retrieveData("snackCommand"), fridge, wsoContext);
  125.                         enterStateMachine("subwalk.walk");
  126.                     else
  127.                         print("no path found");
  128.                         sendMsgThis("canceled");
  129.                     end
  130.                                 
  131. --                    if (walkToActionPoint(actionPoint)) then
  132. --                        local wsoContext = StateMachineContext();
  133. --                        --wsoContext.storeStateObject("fridge", fridge);
  134. --                        queueStateMachine("fridgeSnackChar." .. retrieveData("snackCommand"), fridge, wsoContext);
  135. --                        exitStateMachine();
  136. --                    else
  137. --                        print("no path found");
  138. --                        sendMsgThis("canceled");
  139. --                    end
  140.  
  141.                 else
  142.                     print("no near free space found");
  143.                     setState(retrieveData("snackCommand"));
  144.                 end
  145.             end
  146.         end )
  147.         
  148.         onReturn(function(msg)
  149.             -- back from subwalk
  150.             print("onReturn open");
  151.             setState(retrieveData("snackCommand"));
  152.         end )
  153.             
  154.         
  155.         onExit(function(msg)
  156.             print("open exit");
  157.             local fridge = retrieveStateObject("fridge");
  158.             getParent().unlockActionPoints(fridge);
  159.             removeStateObject("fridge");
  160.         end )
  161.         
  162.     
  163.     -- eat a snack    
  164.     state("eatSnack")
  165.     
  166.         onEnter(function(msg)
  167.             
  168.             if testCancel() or (not this.getParent().getCurrentActivityGain()) then
  169.                 exitStateMachine();
  170.             else
  171.                 local crackerBox = getParent().getLeftHeldObject();
  172.                 local eatSnack = getParent().startActivity("eatSnack", crackerBox);
  173.                 local length = getActivityLength(eatSnack);
  174.                 
  175.                 --flipPoseDirection(); -- animation ist wrong oriented
  176.                 
  177.                 sendDelayedMsgThis("complete", length);
  178.                 sendDelayedMsgThis("testCancel", CANCEL_POLLING_INTERVAL);
  179.                 sendMsgThis("startLoop");
  180.                 
  181.                 crackerBox.buy(); -- pay money for food
  182.             end
  183.             
  184.         end )
  185.         
  186.         onExit(function(msg)
  187.             local crackerBox = getParent().getLeftHeldObject();
  188.             if (crackerBox) then
  189.                 print("deleting crackerBox")
  190.                 getParent().stopAllActivities(crackerBox);
  191.                 getParent().detachLeftObjectHolder();
  192.                 crackerBox.deleteGameObject();
  193.             end
  194.             
  195.             createTrash(0.1);
  196.             
  197.             --flipPoseDirection(); -- animation ist wrong oriented
  198.         end )
  199.         
  200.         
  201.         onMsg("startLoop", function(msg)
  202.             startAnimation("eatSnackLoop");
  203.             sendDelayedMsgThis("eatSound", 2200);
  204.             sendDelayedMsgThis("eatSound", 4500);
  205.         end )
  206.         
  207.         onMsg("eatSound", function(msg)
  208.             getParent().playSound("eat1");
  209.         end )
  210.                     
  211.         onMsg("testCancel", function(msg)
  212.             if testCancel() or (not this.getParent().getCurrentActivityGain()) then
  213.                 exitStateMachine();
  214.             else
  215.                 sendDelayedMsgThis("testCancel", CANCEL_POLLING_INTERVAL);
  216.             end
  217.         end )
  218.  
  219.         onMsg("complete", function(msg)
  220.             exitStateMachine()
  221.         end )
  222.         
  223.         onMsg("end", function(msg)
  224.             if (testCancel() or (getParent().getCondition(NEED_HUNGER) > 0.99)) then
  225.                 exitStateMachine();
  226.             else
  227.                 sendMsgThis("startLoop");
  228.             end
  229.         end )    
  230.         
  231.         
  232.     state("eatVegSnack")
  233.     
  234.         onEnter(function(msg)
  235.             
  236.             local crackerBox = getParent().getLeftHeldObject();
  237.             local eatSnack = getParent().startActivity("eatSnack", crackerBox);
  238.             local length = getActivityLength(eatSnack);
  239.             
  240.             sendDelayedMsgThis("complete", length);
  241.             sendDelayedMsgThis("testCancel", CANCEL_POLLING_INTERVAL);
  242.             sendMsgThis("startLoop");
  243.             
  244.             --this.actionComplete();
  245.         end )
  246.         
  247.         onExit(function(msg)
  248.             local crackerBox = getParent().getLeftHeldObject();
  249.             getParent().stopAllActivities(crackerBox);
  250.             if (crackerBox) then
  251.                 getParent().detachLeftObjectHolder();
  252.                 crackerBox.deleteGameObject();
  253.             end
  254.         end )
  255.         
  256.         
  257.         onMsg("startLoop", function(msg)
  258.             startAnimation("eatSnackLoop");
  259.             sendDelayedMsgThis("eatSound", 2200);
  260.             sendDelayedMsgThis("eatSound", 4500);
  261.         end )
  262.         
  263.         onMsg("eatSound", function(msg)
  264.             getParent().playSound("eat1");
  265.         end )
  266.                     
  267.         onMsg("testCancel", function(msg)
  268.             if testCancel() or (not this.getParent().getCurrentActivityGain()) then
  269.                 exitStateMachine();
  270.             else
  271.                 sendDelayedMsgThis("testCancel", CANCEL_POLLING_INTERVAL);
  272.             end
  273.         end )
  274.  
  275.         onMsg("end", function(msg)
  276.             if (testCancel() or (getParent().getCondition(NEED_HUNGER) > 0.99)) then
  277.                 exitStateMachine();
  278.             else
  279.                 sendMsgThis("startLoop");
  280.             end
  281.         end )    
  282.         
  283.         
  284. --    -- eat a vegetarian snack    
  285. --    state("eatVegSnack")
  286. --    
  287. --        onEnter(function(msg)
  288. --            startAnimation("eatSnackLoop");
  289. --            local crackerBox = getParent().getLeftHeldObject();
  290. --            getParent().startActivity("eat", crackerBox);
  291. --            
  292. --            sendDelayedMsgThis("boxEmpty", 30000);
  293. --            this.actionComplete();
  294. --        end )
  295. --        
  296. --        onExit(function(msg)
  297. --            local crackerBox = getParent().getLeftHeldObject();
  298. --            getParent().stopAllActivities(crackerBox);
  299. --            if (crackerBox) then
  300. --                getParent().detachLeftObjectHolder();
  301. --                crackerBox.deleteGameObject();
  302. --            end
  303. --        end )
  304. --        
  305. --        
  306. --        onMsg("boxEmpty", function(msg)
  307. --            -- remove the cracker box
  308. --            print("boxEmpty");
  309. --            exitStateMachine();
  310. --        end )
  311. --            
  312. --        onMsg("end", function(msg)
  313. --            if (testCancel() or (getParent().getCondition(NEED_HUNGER) > 0.99)) then
  314. --                exitStateMachine();
  315. --            else
  316. --                startAnimation("eatSnackLoop");
  317. --                doSomething();
  318. --            end
  319. --        end )    
  320. --        
  321.         
  322. --    -- eat a snack    
  323. --    state("eatVegSnack")
  324. --    
  325. --        onEnter(function(msg)
  326. --            startAnimation("eatSnackLoop");
  327. --            local fridge = retrieveStateObject("fridge");
  328. --            getParent().startActivity("eatVegSnack", fridge)
  329. --            sendDelayedMsgThis("boxEmpty", 30000);
  330. --        end )
  331. --        
  332. --        onExit(function(msg)
  333. --            local fridge = retrieveStateObject("fridge");
  334. --            if (fridge) then getParent().stopAllActivities(fridge) end;
  335. --        end )
  336. --        
  337. --        
  338. --        onMsg("boxEmpty", function(msg)
  339. --            -- remove the cracker box
  340. --            print("boxEmpty");
  341. --            local crackerBox = getParent().getLeftHeldObject();
  342. --            if (crackerBox) then
  343. --                getParent().detachLeftObjectHolder();
  344. --                crackerBox.deleteGameObject();
  345. --                -- hide the cracker box
  346. --                --local fridge = retrieveStateObject("fridge");
  347. --                --fridge.setChildEnable("CrackerBox", false);
  348. --            end
  349. --            exitStateMachine();
  350. --        end )
  351. --        
  352. --    
  353. --        onMsg("end", function(msg)
  354. --            if (testCancel() or (getParent().getCondition(NEED_HUNGER) > 0.99)) then
  355. --                -- remove the cracker box
  356. --                local crackerBox = getParent().getLeftHeldObject();
  357. --                if (crackerBox) then
  358. --                    getParent().detachLeftObjectHolder();
  359. --                    crackerBox.deleteGameObject();
  360. --                    -- hide the cracker box
  361. --                    --local fridge = retrieveStateObject("fridge");
  362. --                    --fridge.setChildEnable("CrackerBox", false);
  363. --                end
  364. --                exitStateMachine();
  365. --            else
  366. --                startAnimation("eatSnackLoop");
  367. --                doSomething();
  368. --            end
  369. --        end )            
  370. --        
  371.         
  372.         
  373. endStateMachine()
  374.