home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Singles (French)
/
Singles-FrenchVersion-Win95.iso
/
data1.cab
/
Statemachine
/
tv.lua
< prev
next >
Wrap
Text File
|
2004-03-05
|
10KB
|
348 lines
-- tv state machine
beginStateMachine()
onEnter(function(msg)
setState("Off");
end )
onMsg("buildMenu", function(msg)
if (repairMenu()) then return end
local character = getStateObjectFromID(msg.sender);
-- build the pie menu
clearPieMenu();
local button ;
local isWatching = isUser(character, this, TV_ACTIVITIES);
if (not isUser(character, this, {"watchRomantic"})) then
button = addPieMenuButton("pm_watchRomantic", "watch", "Romantic");
button.addDescription(ACTIVITY, "watchRomantic");
if (isWatching) then button.addDescription(DONTQUEUE, "true"); end;
end
if (not isUser(character, this, {"watchNews"})) then
button = addPieMenuButton("pm_watchNews", "watch", "News");
button.addDescription(ACTIVITY, "watchNews");
if (isWatching) then button.addDescription(DONTQUEUE, "true"); end;
end
if (not isUser(character, this, {"watchAction"})) then
button = addPieMenuButton("pm_watchAction", "watch", "Action");
button.addDescription(ACTIVITY, "watchAction");
if (isWatching) then button.addDescription(DONTQUEUE, "true"); end;
end
if (not isUser(character, this, {"watchSitcom"})) then
button = addPieMenuButton("pm_watchSitcom", "watch", "Sitcom");
button.addDescription(ACTIVITY, "watchSitcom");
if (isWatching) then button.addDescription(DONTQUEUE, "true"); end;
end
if (not isUser(character, this, {"watchHorror"})) then
button = addPieMenuButton("pm_watchHorror", "watch", "Horror");
button.addDescription(ACTIVITY, "watchHorror");
if (isWatching) then button.addDescription(DONTQUEUE, "true"); end;
end
if (this.getState() ~= "Off") then
print("tv.getState() " .. this.getState());
button = addPieMenuButton("pm_switchOff", "off");
button.addDescription(DONTQUEUE, "true");
if (not isWatching) then
button = addPieMenuButton("pm_watchJoin", "watch", this.getState());
end
end
end )
-- switch on tv
onMsg("off", function(msg)
setState("Off");
end )
-- switch on tv
onMsg("watch", function(msg)
print(" TV onMsg watch data:" .. msg.data .. " sender:" .. msg.sender);
-- get character who initiated this action
local character = getStateObjectFromID(msg.sender);
-- check if activity if possible
local possible, result, activity = activityPossible(character, msg.name .. msg.data, this);
-- don't care if timeslot is not fulfilled
if (possible or enumCompare(result, TIMESLOT)) then
-- if this is broken: abort characters action
if abortIfBroken(character) then return end;
local wso = character.walkSO;
if (isUser(character, this, TV_ACTIVITIES)) then
--character allready watching this TV: send switch msg
print(" TV onMsg watch is User" .. wso.getStateMachine().getName() .. "." .. wso.getState());
sendMsg("switch", wso, msg.data);
else
--character not watching this TV: find seat to watch
print(" TV onMsg watch is not User" .. wso.getStateMachine().getName() .. "." .. getState());
local seat, actionPoint = getNextWatchSit(character, this, 1.0, 1.5);
if (not seat) then
print("no seat to watch tv found");
instantAbort(character, EMOTICON_NOSOFA, "emoThink")
return
end
if (wso.walkToActionPoint(actionPoint)) then
if (seat.hasBehavior("sofa") or seat.hasBehavior("armchair")) then
-- create state machine contexts
local wsoContext = StateMachineContext();
--wsoContext.storeStateObject("seat", seat);
wsoContext.storeData("actionPointName", actionPoint.getName());
wsoContext.storeData("sitState", "watchTV");
wsoContext.storeStateObject("tv", this);
wsoContext.storeData("channel", msg.data);
--wso.queueStateMachine("sofaChar.sitDownToWatchTV", this, wsoContext);
wso.queueStateMachine("sofaChar.sitDown", seat, wsoContext);
else
print("unkown seat type to watch tv");
instantAbort(character, EMOTICON_CANNOT, "emoThink")
end
else
print("no path found");
instantAbort(character, EMOTICON_NOPATH, "emoThink")
end
end
else
-- activity not possible
print(msg.name .. msg.data .. " not possible");
local emoticon;
if (enumCompare(result, NO_CONDITION)) then
emoticon = conditionToEmoticon(activity.getUnfulfilledCondition());
else
emoticon = EMOTICON_CANNOT;
end
instantAbort(character, emoticon, "emoThink");
end
end )
-- switch on tv when allready sitting in front of it
onMsg("watchShortcut", function(msg)
print(" TV onMsg watchShortcut data:" .. msg.data .. " sender:" .. msg.sender);
local character = getStateObjectFromID(msg.sender);
local wso = character.walkSO;
wso.storeStateObject("tv", this);
wso.storeData("channel", msg.data);
end )
-- repair
onMsg("repair", function(msg)
print("onMsg repair");
-- get character who initiated this action
local character = getStateObjectFromID(msg.sender);
-- walk to the closest action point
local actionPoint = character.getFreeActionPoint(this, "repair");
-- get the walk state object
local wso = character.walkSO;
if (actionPoint) then
-- create state machine contexts
local wsoContext = StateMachineContext();
-- store the action point
wsoContext.storeData("actionPointName", actionPoint.getName());
if (wso.walkToActionPoint(actionPoint)) then
wso.queueStateMachine("repairChar.repairStart", this, wsoContext);
else
print("no path found");
instantAbort(character, EMOTICON_NOPATH, "emoThink")
end
else
print("no action point found");
instantAbort(character, EMOTICON_CANNOT, "emoThink")
end
end )
-- switch
onMsg("switch", function(msg)
print("TV: onMsg switch" .. msg.data);
setState(msg.data);
end )
-- a chracter stoped watching this tv
onMsg("stopWatch", function(msg)
local users = getCharactersWithActivities(this, TV_ACTIVITIES);
if (getn(users) < 1) then
setState("Off");
end
end )
state("Off")
onEnter(function(msg)
print("Off enter ----------------------------");
local users = getCharactersWithActivities(this, TV_ACTIVITIES);
for u = 1, getn(users) do
local user = users[u];
sendMsg("tvOff", user.walkSO);
end
replaceTexture("tvOff", "tvOff");
freePreloadedTextures();
playSound("tvOff");
end )
onExit(function(msg)
playSound("tvOn");
end )
state("Romantic")
onEnter(function(msg)
print("Romantic enter ----------------------------");
sendMsgAllUsers("tvSwitched", TV_ACTIVITIES, "Romantic")
sendMsgThis("startAnim");
loopSound("tvRomantic");
preloadTextures(this, {"tvRomantic1", "tvRomantic2", "tvRomantic3", "tvRomantic4"});
end )
onMsg("startAnim", function(msg)
startAnim({"tvRomantic1", "tvRomantic2", "tvRomantic3", "tvRomantic4", "tvRomantic3", "tvRomantic2"});
end )
onMsg("showPic", function(msg)
replaceTexture("tvOff", msg.data);
end )
onExit(function(msg)
stopSound("tvRomantic");
end )
state("Sitcom")
onEnter(function(msg)
print("Sitcom enter ----------------------------");
sendMsgAllUsers("tvSwitched", TV_ACTIVITIES, "Sitcom")
sendMsgThis("startAnim");
loopSound("tvSitcom");
preloadTextures(this, {"tvSitcom1", "tvSitcom2", "tvSitcom3", "tvSitcom4"});
end )
onMsg("startAnim", function(msg)
startAnim({"tvSitcom1", "tvSitcom2", "tvSitcom3", "tvSitcom4", "tvSitcom3", "tvSitcom2"});
end )
onMsg("showPic", function(msg)
replaceTexture("tvOff", msg.data);
end )
onExit(function(msg)
stopSound("tvSitcom");
end )
state("Action")
onEnter(function(msg)
print("Action enter ----------------------------");
sendMsgAllUsers("tvSwitched", TV_ACTIVITIES, "Action")
sendMsgThis("startAnim");
loopSound("tvAction");
preloadTextures(this, {"tvAction1", "tvAction2", "tvAction3", "tvAction4"});
end )
onMsg("startAnim", function(msg)
startAnim({"tvAction1", "tvAction2", "tvAction3", "tvAction4"});
end )
onMsg("showPic", function(msg)
replaceTexture("tvOff", msg.data);
end )
onExit(function(msg)
stopSound("tvAction");
end )
state("News")
onEnter(function(msg)
print("News enter ----------------------------");
sendMsgAllUsers("tvSwitched", TV_ACTIVITIES, "News")
sendMsgThis("startAnim");
loopSound("tvNews");
preloadTextures(this, {"tvNews1", "tvNews2", "tvNews3", "tvNews4"});
end )
onMsg("startAnim", function(msg)
startAnim({"tvNews1", "tvNews2"});
end )
onMsg("showPic", function(msg)
replaceTexture("tvOff", msg.data);
end )
onExit(function(msg)
stopSound("tvNews");
end )
state("Horror")
onEnter(function(msg)
print("Horror enter ----------------------------");
sendMsgAllUsers("tvSwitched", TV_ACTIVITIES, "Horror")
sendMsgThis("startAnim");
loopSound("tvHorror");
preloadTextures(this, {"tvHorror1", "tvHorror2", "tvHorror3", "tvHorror4"});
end )
onMsg("startAnim", function(msg)
startAnim({"tvHorror1", "tvHorror2", "tvHorror3", "tvHorror4", "tvHorror3", "tvHorror2"});
end )
onMsg("showPic", function(msg)
replaceTexture("tvOff", msg.data);
end )
onExit(function(msg)
stopSound("tvHorror");
end )
endStateMachine()