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

  1. -- tv state machine
  2.  
  3. beginStateMachine()
  4.  
  5.     onEnter(function(msg)
  6.         setState("Off");
  7.     end )
  8.     
  9.     
  10.     onMsg("buildMenu", function(msg)
  11.     
  12.         if (repairMenu()) then return end
  13.     
  14.         local character = getStateObjectFromID(msg.sender);
  15.         
  16.         
  17.  
  18.         -- build the pie menu
  19.         clearPieMenu();
  20.         local button ;
  21.         
  22.         local isWatching = isUser(character, this, TV_ACTIVITIES);
  23.  
  24.         
  25.         if (not isUser(character, this, {"watchRomantic"})) then
  26.             button = addPieMenuButton("pm_watchRomantic", "watch", "Romantic");
  27.             button.addDescription(ACTIVITY, "watchRomantic");
  28.             if (isWatching) then button.addDescription(DONTQUEUE, "true"); end;
  29.         end
  30.         
  31.         if (not isUser(character, this, {"watchNews"})) then
  32.             button = addPieMenuButton("pm_watchNews", "watch", "News");
  33.             button.addDescription(ACTIVITY, "watchNews");
  34.             if (isWatching) then button.addDescription(DONTQUEUE, "true"); end;
  35.         end
  36.         
  37.         if (not isUser(character, this, {"watchAction"})) then
  38.             button = addPieMenuButton("pm_watchAction", "watch", "Action");
  39.             button.addDescription(ACTIVITY, "watchAction");
  40.             if (isWatching) then button.addDescription(DONTQUEUE, "true"); end;
  41.         end
  42.         
  43.         if (not isUser(character, this, {"watchSitcom"})) then
  44.             button = addPieMenuButton("pm_watchSitcom", "watch", "Sitcom");
  45.             button.addDescription(ACTIVITY, "watchSitcom");
  46.             if (isWatching) then button.addDescription(DONTQUEUE, "true"); end;
  47.         end
  48.         
  49.         if (not isUser(character, this, {"watchHorror"})) then
  50.             button = addPieMenuButton("pm_watchHorror", "watch", "Horror");
  51.             button.addDescription(ACTIVITY, "watchHorror");
  52.             if (isWatching) then button.addDescription(DONTQUEUE, "true"); end;
  53.         end
  54.  
  55.         if (this.getState() ~= "Off") then
  56.         
  57.             print("tv.getState() " .. this.getState());
  58.             button = addPieMenuButton("pm_switchOff", "off");
  59.             button.addDescription(DONTQUEUE, "true");
  60.             
  61.             if (not isWatching) then
  62.                 button = addPieMenuButton("pm_watchJoin", "watch", this.getState());
  63.             end
  64.         end
  65.     end )
  66.     
  67.         -- switch on tv
  68.     onMsg("off", function(msg)    
  69.         setState("Off");
  70.     end )
  71.     
  72.     
  73.     -- switch on tv
  74.     onMsg("watch", function(msg)
  75.     
  76.         print(" TV onMsg watch data:" .. msg.data .. "  sender:" .. msg.sender);    
  77.     
  78.         -- get character who initiated this action
  79.         local character = getStateObjectFromID(msg.sender);
  80.         
  81.         -- check if activity if possible
  82.         local possible, result, activity = activityPossible(character, msg.name .. msg.data, this);
  83.         
  84.         -- don't care if timeslot is not fulfilled
  85.         if (possible or enumCompare(result, TIMESLOT)) then
  86.             -- if this is broken: abort characters action
  87.             if abortIfBroken(character) then return end;        
  88.             local wso = character.walkSO;
  89.             
  90.             if (isUser(character, this, TV_ACTIVITIES)) then
  91.                 --character allready watching this TV:  send switch msg
  92.                 print(" TV onMsg watch is User" .. wso.getStateMachine().getName() .. "." .. wso.getState());    
  93.                 sendMsg("switch", wso, msg.data);
  94.             else
  95.                 --character not watching this TV:  find seat to watch
  96.                 print(" TV onMsg watch is not User" .. wso.getStateMachine().getName() .. "." .. getState());    
  97.                 local seat, actionPoint = getNextWatchSit(character, this, 1.0, 1.5);
  98.                 if (not seat) then
  99.                     print("no seat to watch tv found");
  100.                     instantAbort(character, EMOTICON_NOSOFA, "emoThink")
  101.                     return
  102.                 end
  103.                 
  104.                 if (wso.walkToActionPoint(actionPoint)) then
  105.                 
  106.                     if (seat.hasBehavior("sofa") or seat.hasBehavior("armchair")) then
  107.                     
  108.                         -- create state machine contexts
  109.                         local wsoContext = StateMachineContext();
  110.                         --wsoContext.storeStateObject("seat", seat);
  111.                         wsoContext.storeData("actionPointName", actionPoint.getName());
  112.                         wsoContext.storeData("sitState", "watchTV");
  113.                         wsoContext.storeStateObject("tv", this);
  114.                         wsoContext.storeData("channel", msg.data);
  115.                         --wso.queueStateMachine("sofaChar.sitDownToWatchTV", this, wsoContext);                
  116.                         wso.queueStateMachine("sofaChar.sitDown", seat, wsoContext);                
  117.                     else
  118.                         print("unkown seat type to watch tv");
  119.                         instantAbort(character, EMOTICON_CANNOT, "emoThink")
  120.                     end
  121.                 else
  122.                     print("no path found");
  123.                     instantAbort(character, EMOTICON_NOPATH, "emoThink")
  124.                 end
  125.             end
  126.         else
  127.             -- activity not possible
  128.             print(msg.name .. msg.data .. " not possible");
  129.             
  130.             local emoticon;
  131.             if (enumCompare(result, NO_CONDITION)) then
  132.                 emoticon = conditionToEmoticon(activity.getUnfulfilledCondition());
  133.             else
  134.                 emoticon = EMOTICON_CANNOT;
  135.             end
  136.             instantAbort(character, emoticon, "emoThink");        
  137.         end
  138.     end )
  139.     
  140.         
  141.     -- switch on tv when allready sitting in front of it
  142.     onMsg("watchShortcut", function(msg)
  143.     
  144.         print(" TV onMsg watchShortcut data:" .. msg.data .. "  sender:" .. msg.sender);    
  145.         local character = getStateObjectFromID(msg.sender);
  146.         local wso = character.walkSO;
  147.         
  148.         wso.storeStateObject("tv", this);
  149.         wso.storeData("channel", msg.data);
  150.         
  151.     end )
  152.         
  153.         
  154.     -- repair
  155.     onMsg("repair", function(msg)
  156.     
  157.         print("onMsg repair");
  158.         -- get character who initiated this action
  159.         local character = getStateObjectFromID(msg.sender);
  160.         -- walk to the closest action point
  161.         local actionPoint = character.getFreeActionPoint(this, "repair");
  162.         -- get the walk state object
  163.         local wso = character.walkSO;
  164.         if (actionPoint) then
  165.             -- create state machine contexts
  166.             local wsoContext = StateMachineContext();
  167.             -- store the action point
  168.             wsoContext.storeData("actionPointName", actionPoint.getName());
  169.             if (wso.walkToActionPoint(actionPoint)) then
  170.                 wso.queueStateMachine("repairChar.repairStart", this, wsoContext);
  171.             else
  172.                 print("no path found");
  173.                 instantAbort(character, EMOTICON_NOPATH, "emoThink")
  174.             end
  175.         else
  176.             print("no action point found");
  177.             instantAbort(character, EMOTICON_CANNOT, "emoThink")
  178.         end
  179.     end )
  180.     
  181.     
  182.     -- switch
  183.     onMsg("switch", function(msg)
  184.     
  185.         print("TV: onMsg switch" .. msg.data);
  186.         
  187.         setState(msg.data);
  188.         
  189.     end )
  190.         
  191.     -- a chracter stoped watching this tv
  192.     onMsg("stopWatch", function(msg)
  193.         local users = getCharactersWithActivities(this, TV_ACTIVITIES);
  194.         if (getn(users) < 1) then
  195.             setState("Off");
  196.         end
  197.     end )
  198.         
  199.         
  200.     state("Off")
  201.     
  202.         onEnter(function(msg)
  203.             print("Off enter ----------------------------");
  204.             local users = getCharactersWithActivities(this, TV_ACTIVITIES);
  205.             for u = 1, getn(users) do
  206.                 local user = users[u];
  207.                 sendMsg("tvOff", user.walkSO);
  208.             end
  209.             
  210.             replaceTexture("tvOff", "tvOff");
  211.             freePreloadedTextures();
  212.             playSound("tvOff");
  213.         end )
  214.     
  215.         
  216.         onExit(function(msg)
  217.             playSound("tvOn");
  218.         end )        
  219.         
  220.     state("Romantic")
  221.     
  222.         onEnter(function(msg)
  223.             print("Romantic enter ----------------------------");
  224.             sendMsgAllUsers("tvSwitched", TV_ACTIVITIES, "Romantic")
  225.             sendMsgThis("startAnim");
  226.             
  227.             loopSound("tvRomantic");
  228.             preloadTextures(this, {"tvRomantic1", "tvRomantic2", "tvRomantic3", "tvRomantic4"});
  229.         end )
  230.         
  231.         
  232.         onMsg("startAnim", function(msg)
  233.             startAnim({"tvRomantic1", "tvRomantic2", "tvRomantic3", "tvRomantic4", "tvRomantic3", "tvRomantic2"});
  234.         end )
  235.     
  236.         onMsg("showPic", function(msg)
  237.             replaceTexture("tvOff", msg.data);
  238.         end )
  239.         
  240.         onExit(function(msg)
  241.             stopSound("tvRomantic");
  242.         end )        
  243.                     
  244.     state("Sitcom")
  245.     
  246.         onEnter(function(msg)
  247.             print("Sitcom enter ----------------------------");
  248.             sendMsgAllUsers("tvSwitched", TV_ACTIVITIES, "Sitcom")
  249.             sendMsgThis("startAnim");
  250.             
  251.             loopSound("tvSitcom");
  252.             preloadTextures(this, {"tvSitcom1", "tvSitcom2", "tvSitcom3", "tvSitcom4"});
  253.         end )
  254.         
  255.         
  256.         onMsg("startAnim", function(msg)
  257.             startAnim({"tvSitcom1", "tvSitcom2", "tvSitcom3", "tvSitcom4", "tvSitcom3", "tvSitcom2"});
  258.         end )
  259.     
  260.         onMsg("showPic", function(msg)
  261.             replaceTexture("tvOff", msg.data);
  262.         end )
  263.     
  264.         
  265.         onExit(function(msg)
  266.             stopSound("tvSitcom");
  267.         end )        
  268.                     
  269.     state("Action")
  270.     
  271.         onEnter(function(msg)
  272.             print("Action enter ----------------------------");
  273.             sendMsgAllUsers("tvSwitched", TV_ACTIVITIES, "Action")
  274.             sendMsgThis("startAnim");
  275.             
  276.             loopSound("tvAction");
  277.             preloadTextures(this, {"tvAction1", "tvAction2", "tvAction3", "tvAction4"});
  278.         end )
  279.         
  280.         
  281.         onMsg("startAnim", function(msg)
  282.             startAnim({"tvAction1", "tvAction2", "tvAction3", "tvAction4"});
  283.         end )
  284.     
  285.         onMsg("showPic", function(msg)
  286.             replaceTexture("tvOff", msg.data);
  287.         end )
  288.     
  289.         
  290.         onExit(function(msg)
  291.             stopSound("tvAction");
  292.         end )        
  293.         
  294.     state("News")
  295.     
  296.         onEnter(function(msg)
  297.             print("News enter ----------------------------");
  298.             sendMsgAllUsers("tvSwitched", TV_ACTIVITIES, "News")
  299.             sendMsgThis("startAnim");
  300.             
  301.             loopSound("tvNews");
  302.             preloadTextures(this, {"tvNews1", "tvNews2", "tvNews3", "tvNews4"});
  303.         end )
  304.         
  305.         
  306.         onMsg("startAnim", function(msg)
  307.             startAnim({"tvNews1", "tvNews2"});
  308.         end )
  309.     
  310.         onMsg("showPic", function(msg)
  311.             replaceTexture("tvOff", msg.data);
  312.         end )
  313.     
  314.         
  315.         onExit(function(msg)
  316.             stopSound("tvNews");
  317.         end )
  318.         
  319.     state("Horror")
  320.     
  321.         onEnter(function(msg)
  322.             print("Horror enter ----------------------------");
  323.             sendMsgAllUsers("tvSwitched", TV_ACTIVITIES, "Horror")
  324.             sendMsgThis("startAnim");
  325.             
  326.             loopSound("tvHorror");
  327.             preloadTextures(this, {"tvHorror1", "tvHorror2", "tvHorror3", "tvHorror4"});
  328.         end )
  329.         
  330.         
  331.         onMsg("startAnim", function(msg)
  332.             startAnim({"tvHorror1", "tvHorror2", "tvHorror3", "tvHorror4", "tvHorror3", "tvHorror2"});
  333.         end )
  334.     
  335.         onMsg("showPic", function(msg)
  336.             replaceTexture("tvOff", msg.data);
  337.         end )
  338.     
  339.         
  340.         onExit(function(msg)
  341.             stopSound("tvHorror");
  342.         end )        
  343.         
  344.         
  345.                 
  346.             
  347. endStateMachine()
  348.