Here we continue our discussion on interactions. We already covered how to generate the simple ones, were all that is required is a print statement to be generated. Now it is time to get slightly more interactive with our interactions. Now regardless of whether you are interacting with a view, control color, or polygon area the idea and techniques described here can be applied. For each of these, we are adding additional methods in order to achieve the complexity. Let's go ahead and just jump right in.
Approach ObjectThere are a couple of attributes that are available that so far we have not made use of, approachX and approachY. These values let you set a point that the ego will walk to prior to triggering the print message, or whatever other commands you are attempting to trigger. For this example and the others in this chapter, I will be building on our broken mug view that we have previously defined. Let's revisit that instance and add in the approach coordinates as well as include the method that will trigger the approaching depending on which verb is being used.
(instance brokenMug of Prop
(properties view 251
loop 2
cel 0
x 114
y 91
noun N_BROKENMUG
approachX 120
approachY 91
)
(method (init)
(self approachVerbs: V_DO)
(super init:)
)
)
As an alternative, instead of including the init method that specifies the approach verbs, you can instead include them in the views actual init statement. Assuming of course that you have defined the approachX and approachY properties.
(brokenMug approachVerbs: V_DO init:)
So with either method, now when you look at the item, you will just receive the print message but when you click on the the broken mug with the hand cursor then the ego will attempt to move to 120 91 before triggering the do broken mug response.
Scoring Points or Picking up ItemBefore we get too carried away, lets go ahead and trigger a couple of minor occurrences. For this, things start to get a tad deeper but all in all it isn't too bad. The first thing that we will need to do is add in a new method that gives us different options for the different verb combinations. After all, you really don't want the same thing to happen if you look an object that happens when you try to touch it. For this we will need to include a switch inside of our newly added method that will trigger different things based off of the verb. This is probably going to be the most used method you are ever going to come across in these tutorials.
(instance brokenMug of Prop
(properties
view 251
loop 2
cel 0
x 114
y 91
noun N_BROKENMUG
)
(method (doVerb theVerb)
(switch theVerb
(V_DO
(gEgo get: ITEM_BROKENMUG)
(AddToScore POINT_GETBROKENMUG 5)
(brokenMug dispose:)
)
(else
(super doVerb: theVerb &rest)
)
)
)
)
This now looks at the verb, cursor, and determines what to do. I have only added one case statement and a default case. So as of right now if you click on the broken mug with anything other than the hand, the response will default to the message statement that you have set up for the verb and the broken mug noun. However, if you click on the mug with the hand cursor the code inside the V_DO case will be triggered instead of the message statement. Now instead of hearing about how the ego is afraid of all the sharp edges,
the item will be added to the players inventory,
points will be awarded, and the mug will disappear from the screen.
Triggering a Separate ChangeStateSometimes though, you will want to do far more than simply snag an object. You may want to fire off another script that then steps through an animation sequence or elaborate changestate. For this, we will need to create a new script instance to trigger.
(instance brokenMug of Prop
(properties
view 251
loop 2
cel 0
x 114
y 91
noun N_BROKENMUG
)
(method (doVerb theVerb)
(switch theVerb
(V_DO
(gEgo get: ITEM_BROKENMUG)
(AddToScore POINT_GETBROKENMUG 5)
(brokenMug dispose:)
)
(V_GLUE
(brokenMug setScript: glueMugScript)
)
(else
(super doVerb: theVerb &rest)
)
)
)
)
(instance glueMugScript of Script
(properties)
(method (changeState newState)
(= state newState)
(switch state
(0
(gMessager say: N_BROKENMUG V_GLUE 0 1) ; Noun Verb Condition Sequence
(= cycles 30)
)
(1
(gMessager say: N_BROKENMUG V_GLUE 0 2) ; Noun Verb Condition Sequence
(= cycles 20)
)
(3
(gEgo setMotion: MoveTo 282 91 glueMugScript)
)
(4 (gRoom newRoom: 5))
)
)
)