In this exercise, you have to create a character that paces around while he is unaware of you. But as soon as you come within a certain range, he stops and looks at you. While the character is watching you, you can click on him to start a conversation with him. If you have entered a conversation, you can click on something else to stop talking. If at any point you leave the character, he goes back to his pacing around. These states and the events that trigger transitions are shown in this figure.
You will need the following modules imported into your program:
import direct.directbase.DirectStart from direct.showbase.DirectObject import DirectObject from pandac.PandaModules import * from direct.actor.Actor import Actor from direct.fsm import FSM from direct.interval.IntervalGlobal import * from direct.task.Task import Task from direct.gui.OnscreenText import OnscreenText from picker import Picker
FSM.FSM.__init__(self,"Character")
Then you should load an Actor from the model “Models/eve” with the animation 'Walk':'Models/eve_walk'“. Place the actor at (0,-20,0) and scale it down to 80%. Start your actor's Walk animation by calling loop(“Walk”) on your instance of it. Now create a new Sequence of Lerp Intervals that first changes your actor's heading to -90 (in about 0.8 seconds) and then moves him to position (-10,-20,0) in about 9 seconds. Rotate back and move back to the original position. Now loop this interval and watch your actor pace around!
<colnodepath> = <modelnodepath>.attachNewNode(CollisionNode(<name>)) <colnodepath>.node().addSolid(CollisionSphere(0,0,0,<radius>))
Use a radius of 10 for the avatar sphere and 35 for the sensor sphere. Now that you have the two collision spheres set up, you need to create a collision handler that sends out the types of messages we want. Inside your World constructor, create a new CollisionHandlerEvent instance and then define these two event patterns:
<collhandler>.addInPattern('%fn-into-%in') <collhandler>.addOutPattern('%fn-out-%in')
Then set up the default collision traverser (base.cTrav) to use your avatar collision node (that you created above) as the from object and associate events its collision to the handler. Here's how that could be done:
base.cTrav = CollisionTraverser('world traverser') base.cTrav.addCollider(self.avatar,<collhandler>)
Inside you Character class, you can now accept the messages 'avatar-into-sensor' and 'avatar-out-sensor' that represent when the user enters the sensor sphere around the character and when he/she leaves the sphere, respectively. Create handlers for these messages (as methods of Character), which you can call approach_handler and leave_handler. In the former, you should have the character enter the Noticing state and in the latter the Pacing state. Make sure this works (remember you can make collision solids visible by calling the show() method on their nodes).
<mouse_picker>=Picker(<youractornodepath>) <mouse_picker>.make_pickable(<youractornodepath>)
Now, whenever you click on your actor, an event with the name 'clicked_render/Eve' occurs, which you can of course accept in your Character class. See if you can make your character respond to your clicking.
defaultFilter
) to create a new Conversation class that handles the branching dialog shown in this diagram: self.request(<input>)
method for advancing to the next state.