00001 -- Author: Li,Xizhi 00002 -- Date: 2006-5 00003 -- Desc: This sample code demostrate character simulation in ParaEngine. two NPCs and GirlPC1 character is created at position near (191, 101) 00004 -- user can click on NPC0 and NPC1. NPCs will wave at and face to the nearest player when it perceives it. 00005 -- NPL.load("(gl)script/samples/AI_script.lua"); 00006 00007 local PlayerX = 191 00008 local PlayerY = 101 00009 00010 --create asset or resources used in this scene 00011 ParaAsset.LoadParaX("HeroAsset", "Character/Human/Female/HumanFemale.x"); 00012 00013 local player; 00014 local playerChar; 00015 00016 --create two NPCs. 00017 local i=0; 00018 for i=0, 1 do 00019 00020 player = ParaScene.CreateCharacter ("NPC"..i, "HeroAsset", "", true, 0.35, 3.14159, 1.0); 00021 player:SetPosition(PlayerX, 0, PlayerY+i); 00022 player:SnapToTerrainSurface(0); 00023 00024 player:SetSentientField(0, true); -- make this sentient to group ID 0 00025 player:SetGroupID(1); -- set group 1 00026 player:SetPerceptiveRadius(5); 00027 player.onperceived = ";NPC_onperceived();"; -- called when object perceived other objects in its perceptive radius.. 00028 player.onclick = ";NPC_onclick();"; -- called when user clicked 00029 player.onframemove = ";NPC_onframemove();"; -- called when object is sentient 00030 00031 ParaScene.Attach(player); 00032 playerChar = player:ToCharacter(); 00033 playerChar:LoadStoredModel(213); 00034 playerChar:AssignAIController("face", "true"); 00035 00036 -- a sequence can be read from file or database; here I just hard-coded them. 00037 local s = player:ToCharacter():GetSeqController(); 00038 s:BeginAddKeys(); 00039 s:Lable("start"); 00040 s:PlayAnim("EmotePoint"); 00041 s:Wait(3); 00042 s:Turn(0); 00043 s:WalkTo(10,0,0); 00044 s:Wait(3); 00045 s:Turn(-1.57); 00046 s:RunTo(0,0,10); 00047 s:Turn(3.14); 00048 --s:Pause(); 00049 s:WalkTo(-5,0,0);s:Jump();s:RunTo(-5,0,0); 00050 s:Turn(1.57); 00051 s:MoveTo(0,0,-10); 00052 s:Goto("start"); 00053 s:Exec(";NPC_SEQ_EXEC_Test()"); 00054 s:EndAddKeys(); 00055 end 00056 00057 -- create the GirlPC 00058 player = ParaScene.CreateCharacter ("girlPC1", "HeroAsset", "", true, 0.35, 3.9, 1.0); 00059 player:SetPosition(PlayerX-3, 0, PlayerY+3); 00060 player:SnapToTerrainSurface(0); 00061 player:SetGroupID(0); -- set group 0 00062 player:SetPerceptiveRadius(30); 00063 ParaScene.Attach(player); 00064 playerChar = player:ToCharacter(); 00065 playerChar:LoadStoredModel(213); 00066 playerChar:SetFocus(); 00067 00068 function NPC_SEQ_EXEC_Test() 00069 local player = ParaScene.GetObject(sensor_name); 00070 if(player:IsValid() == true) then 00071 local s = player:ToCharacter():GetSeqController(); 00072 s:Suspend(); 00073 end 00074 end 00075 00076 function NPC_onperceived() 00077 -- do your AI code here. 00078 local player = ParaScene.GetObject(sensor_name); 00079 local bResume=true; 00080 if(player:IsValid() == true) then 00081 00082 local nCount = player:GetNumOfPerceivedObject(); 00083 for i=0,nCount-1 do 00084 local gameobj = player:GetPerceivedObject(i); 00085 if(gameobj:DistanceTo(player) < 5) then 00086 local s = player:ToCharacter():GetSeqController(); 00087 s:Suspend(); 00088 player:ToCharacter():PlayAnimation("EmoteWave"); 00089 s:Turn(gameobj:GetFacing()+3.14); 00090 bResume = false; 00091 break; 00092 end 00093 end 00094 end 00095 if(bResume == true) then 00096 local s = player:ToCharacter():GetSeqController(); 00097 s:Resume(); 00098 end 00099 end 00100 00101 function NPC_onframemove() 00102 -- do your AI code here. 00103 end 00104 00105 function NPC_onclick() 00106 if(mouse_button=="left") then 00107 _guihelper.MessageBox("Hello: I am "..sensor_name); 00108 end 00109 end