Difference between revisions of "Regions and Locales"

From SCI Wiki
Jump to navigationJump to search
m (1 revision)
(Sierra Script conversion)
 
Line 11: Line 11:
 
<div class="CodeBlockHeader">Code:</div>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci" class="cs">
 
<syntaxhighlight lang="sci" class="cs">
    /******************* Snow  locale *********************************************/
+
;;;******************* Snow  locale *********************************************/
    (include "sci.sh")
+
(script 777)
    (include "game.sh")
+
;;;******************************************************************************/
    /******************************************************************************/
+
(include sci.sh)
    (script 777)
+
(include game.sh)
    /******************************************************************************/
+
;;;******************************************************************************/
    (use "controls")
+
(use controls)
    (use "game")
+
(use game)
    (use "main")
+
(use main)
    (use "obj")
+
(use obj)
   
 
  
    (instance public snowLocale of Locale
+
(instance snowLocale of Locale
        (properties)
+
(properties)
 
+
(method (handleEvent pEvent)
        (method (handleEvent pEvent)
+
(super handleEvent: pEvent)
            (super:handleEvent(pEvent))
+
(if (Said 'look/snow')
            (if(Said('look/snow'))
+
(Print {Look at all that nice snow.})
                Print("Look at all that nice snow.")
+
)
            )
+
)
        )
+
)
    )
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 
</blockquote>
 
</blockquote>
  
And then, in the init method of each room where you have snow, you can add a call to '''setLocales'''.
+
And then, in the init method of each room where you have snow, you can add a call to '''setLocales'''. For example:
 
 
<blockquote>
 
For example:
 
 
<div class="CodeBlockHeader">Code:</div>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci" class="cs">
 
<syntaxhighlight lang="sci" class="cs">
(instance public rm001 of Rm
+
(instance rm001 of Rm
      (properties
+
(properties
            picture scriptNumber
+
picture scriptNumber
            // Set up the rooms to go to/come from here
+
; Set up the rooms to go to/come from here
            north 0
+
north 0
            east 0
+
east 0
            south 0
+
south 0
            west 0
+
west 0
      )
+
)
 
+
      (method (init)
+
(method (init &tmp button)
            (super:init())
+
(super init:)
            (self:setScript(RoomScript))
+
(self setScript: RoomScript)
         
 
            (switch(gPreviousRoomNumber)
 
  
            // Set up ego's position if it hasn't come from any room
+
(switch gPreviousRoomNumber
                  (default
+
; Set up ego's position if it hasn't come from any room
                        (send gEgo:
+
(else
                              posn(150 130)
+
(gEgo posn: 150 130 loop: 1)
                              loop(1)
+
)
                        )
+
)
                  )
 
            )
 
         
 
            // Set up the ego
 
            SetUpEgo()     
 
            (send gEgo:init())
 
 
            // Set up the snow locale
 
            (self:setLocales(777))
 
  
      )
+
; Set up the ego
 +
(SetUpEgo)
 +
(gEgo init:)
 +
 +
; Set up the snow locale
 +
(self setLocales: 777)
 +
)
 
)
 
)
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   
 
   
You can specify multiple script numbers in the call to setLocales.  The only requirement is that the first public instance in that script must be an instance of the '''Locale''' class.
 
</blockquote>
 
 
 
You can specify multiple script numbers in the call to '''setLocales'''. The only requirement is that the first public instance in that script must be an instance of the '''Locale''' class.
 
You can specify multiple script numbers in the call to '''setLocales'''. The only requirement is that the first public instance in that script must be an instance of the '''Locale''' class.
  
Line 90: Line 77:
 
Region example:  
 
Region example:  
 
<div class="CodeBlockHeader">Code:</div>
 
<div class="CodeBlockHeader">Code:</div>
<syntaxhighlight lang="sci" class="cs">/******************* dog region ****          ********************************/
+
<syntaxhighlight lang="sci" class="cs">
(include "sci.sh")
+
;;;******************* dog region ************************************/
(include "game.sh")
 
/******************************************************************************/
 
 
(script 778)
 
(script 778)
(use "controls")
+
;;;******************************************************************************/
(use "feature")
+
(include sci.sh)
(use "cycle")
+
(include game.sh)
(use "follow")
+
;;;******************************************************************************/
(use "wander")
+
(use controls)
(use "game")
+
(use feature)
(use "main")
+
(use cycle)
(use "obj")
+
(use follow)
 +
(use wander)
 +
(use game)
 +
(use main)
 +
(use obj)
  
 
(local
 
(local
    goneToBathroom = FALSE
+
goneToBathroom = FALSE
 
)
 
)
  
 
+
(instance dogRegion of Rgn
(instance public dogRegion of Rgn
+
(properties)
    (properties)
+
(method (init)
      (method (init)
+
(super init:)
        (super:init())
+
(self setScript: DogRegionScript)
        (self:setScript(DogRegionScript))
+
 
+
; Make a dog that follows you
 
+
(aDog
        // Make a dog that follows you
+
ignoreControl: ctlWHITE
        (aDog:
+
ignoreActors:
            ignoreControl(ctlWHITE)
+
setMotion: Follow gEgo 15
            ignoreActors()
+
init:
            setMotion(Follow gEgo 15)
+
)
            init()
+
)
        )
 
    )
 
 
)
 
)
  
 
(instance DogRegionScript of Script
 
(instance DogRegionScript of Script
    (properties)
+
(properties)
  
    (method (changeState newState)
+
(method (changeState newState)
        (= state newState)
+
(= state newState)
        (switch (newState)
+
(switch (newState)
            (case 0
+
(0
                // nothing to do here.
+
; Nothing to do here.
            )
+
)
            (case 10
+
(10
                // Make the dog walk to the edge of the screen
+
; Make the dog walk to the edge of the screen...
                (aDog:setMotion(MoveTo 0 150 DogRegionScript))
+
(aDog setMotion: MoveTo 0 150 DogRegionScript)
            )
+
)
            (case 11
+
(11
                // And disappear
+
; ...and disappear
                (aDog:hide())
+
(aDog hide:)
            )
+
)
        )
+
)
    )
+
)
 
    (method (doit)
 
        (super:doit())
 
        (if (not goneToBathroom)
 
            // Assuming that you gave all grass a green control colour...
 
            (if (& (aDog:onControl()) ctlGREEN)
 
                (= goneToBathroom TRUE)
 
                Print("The dog finds a nice patch of grass and goes to the bathroom")
 
            )
 
        )
 
    )
 
  
+
(method (doit)
 +
(super doit:)
 +
(if (not goneToBathroom)
 +
; Assuming that you gave all grass a green control colour...
 +
(if (and (aDog onControl:) ctlGREEN)
 +
(= goneToBathroom TRUE)
 +
(Print {The dog finds a nice patch of grass and goes to the bathroom})
 +
)
 +
)
 +
)
  
    (method (handleEvent pEvent)
+
(method (handleEvent pEvent)
        (super:handleEvent(pEvent))
+
(super handleEvent: pEvent)
   
+
(if (Said 'give/food/dog')
        (if (Said('give/food/dog'))
+
(Print {You give food to the dog and he runs away})
            Print("You give food to the dog and he runs away")
+
(self changeState: 10)
            (self:changeState(10))
+
)
        )
+
)
    )
 
 
)
 
)
 
 
   
 
   
 
(instance aDog of Act
 
(instance aDog of Act
    (properties
+
(properties
        x 50
+
x 50
        y 150
+
y 150
        view 800
+
view 800
    )
+
)
)</syntaxhighlight>
+
)
 +
</syntaxhighlight>
 
</blockquote><br />
 
</blockquote><br />
  
Line 183: Line 167:
 
<div class="CodeBlockHeader">Code:</div>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci" class="cs">
 
<syntaxhighlight lang="sci" class="cs">
            // Set up the dog region
+
; Set up the dog region
            (self:setRegions(778))
+
(self setRegions: 778)
 
</syntaxhighlight>
 
</syntaxhighlight>
 
</blockquote>
 
</blockquote>

Latest revision as of 14:38, 27 February 2016

Chapter 2 - Regions and Locales

If you want to have a common set of behavior (handling Said statements, doit methods, changeStates) that extend across rooms, regions and locales are what you need.

Locales

Locales are the simpler of the two. Basically, they allow you to specify a handleEvent method that can be used across multiple rooms. To create a locale, you need to create a new (empty) script, and put a public instance of the class Locale at the top of the script.

For example, suppose you have many rooms with snow. You want to only code up the responses to someone looking at snow once, you could write the following locale:

Code:
;;;******************* Snow  locale *********************************************/
(script 777)
;;;******************************************************************************/
(include sci.sh)
(include game.sh)
;;;******************************************************************************/
(use controls)
(use game)
(use main)
(use obj)

(instance snowLocale of Locale
	(properties)
	(method (handleEvent pEvent)
		(super handleEvent: pEvent)
		(if (Said 'look/snow')
			(Print {Look at all that nice snow.})
		)
	)
)

And then, in the init method of each room where you have snow, you can add a call to setLocales. For example:

Code:
(instance rm001 of Rm
	(properties
		picture scriptNumber
		; Set up the rooms to go to/come from here
		north 0
		east 0
		south 0
		west 0
	)
	
	(method (init &tmp button)
		(super init:)
		(self setScript: RoomScript)

		(switch gPreviousRoomNumber
			; Set up ego's position if it hasn't come from any room
			(else 
				(gEgo posn: 150 130 loop: 1)
			)
		)

		; Set up the ego
		(SetUpEgo)
		(gEgo init:)
		
		; Set up the snow locale
		(self setLocales: 777)
	)
)

You can specify multiple script numbers in the call to setLocales. The only requirement is that the first public instance in that script must be an instance of the Locale class.

Regions

Regions are very similar to Locales, except that they provide more functionality. Basically anything you can do in a room, you can do with a region too: apply a custom script that has not only handleEvent, but also doit, and changeState methods. Again, make a new script for your region, and ensure the first public instance in that script, is of the class Rgn.

Region example:

Code:
;;;******************* dog region ************************************/
(script 778)
;;;******************************************************************************/
(include sci.sh)
(include game.sh)
;;;******************************************************************************/
(use controls)
(use feature)
(use cycle)
(use follow)
(use wander)
(use game)
(use main)
(use obj)

(local
	goneToBathroom = FALSE
)

(instance dogRegion of Rgn
	(properties)
	(method (init)
		(super init:)
		(self setScript: DogRegionScript)
		
		; Make a dog that follows you
		(aDog
			ignoreControl: ctlWHITE
			ignoreActors:
			setMotion: Follow gEgo 15
			init:
		)
	)
)

(instance DogRegionScript of Script
	(properties)

	(method (changeState newState)
		(= state newState)
		(switch (newState)
			(0
				; Nothing to do here.
			)
			(10
				; Make the dog walk to the edge of the screen...
				(aDog setMotion: MoveTo 0 150 DogRegionScript)
			)
			(11
				; ...and disappear
				(aDog hide:)
			)
		)
	)

	(method (doit)
		(super doit:)
		(if (not goneToBathroom)
			; Assuming that you gave all grass a green control colour...
			(if (and (aDog onControl:) ctlGREEN)
				(= goneToBathroom TRUE)
				(Print {The dog finds a nice patch of grass and goes to the bathroom})
			)
		)
	)

	(method (handleEvent pEvent)
		(super handleEvent: pEvent)
		(if (Said 'give/food/dog')
			(Print {You give food to the dog and he runs away})
			(self changeState: 10)
		)
	)
)
 
(instance aDog of Act
	(properties
		x 50
		y 150
		view 800
	)
)


To apply this to a room, in its init method, do:

Code:
; Set up the dog region
(self setRegions: 778)

When this region is applied to a room (via the setRegions method, which works just like setLocales), the dog will follow you around. If you give it food, it will run away. If it encounters a patch of grass, it will go to the bathroom. So pretty much any logic you put into a room, you can also put into a region that can be applied to any room.

Note that the room's methods are called before the regions', and the regions 'before the locales'. So if in a region you have a Said clause that is identical to one in a room, the room's Said clause will win.

 

< Previous: Chapter 1 - Advanced Said() Strings - Part 1 Next: Chapter 3 - Scripting Props and Acts >