Friday 10 April 2009

Scripting example - Clouds

Here's a real world example of scripting in XAGE. One of the locations in my game is on top of a mountain and some moving clouds would help bring the room to life.

Photobucket

Let's break this problem down into managable steps:
  • There must be several different clouds
  • They must be obscured by the mountain and all other objects
  • They must start in randomised locations
  • They must slowly scroll to the left & reappear at the right of the screen
Ok, so now we can tackle these steps one at a time.

1) There must be several different clouds: Using the editor we add five new objects to the MountainTop room. So we can identify them within the editor, lets give them IDs cloud to cloud5. We don't need to give them a name as we don't want the user to be able to interact with them in any way. For each cloud we need to create a single AnimFrame. For consistency, lets tag them all as show, and set each cloud's current frame to the show frame.

Photobucket

2) They must be obscured by the mountain and all other objects: In order for the clouds to pass behind the mountain, we create a new object called mountainCover which has an animframe of the mountain itself. We then set the y position and hotspot of mountainCover so that it perfectly overlays the actual room background image. Because of the hotspot, this object will not obscure any other objects (our player character, for instance) but will obscure the clouds, as we'll ensure these always have a lower Y position.

3) They must start in randomised locations: Every time we enter the room, we should set the positions of the five clouds to random locations. We then set each cloud to run its own move script.

Photobucket

4) They must slowly scroll to left: Each cloud's move script checks whether the cloud has scrolled offscreen (i.e. the xPosition is more than 200 pixels beyond the left side of the display). If so, the cloud is repositioned at a random position to the right of the screen so it can scroll back on. At each iteration, the scripts pauses, moves the cloud one pixel left, and then repeats the whole process.

Photobucket

Now if we repeat this for cloud2 to cloud5, we get the following:



Excluding the artwork, the above takes only a few minutes to achieve but really helps to add an organic feel to the location, without a single line of code being written.

There is an obvious improvement in that I could implement script paramaterisation and use the one script for all five clouds, passing each cloud object as a paramater. I'm not sure yet whether I'm going to want to implement this, as you can pretty much achieve the same affect using Global Variables.

No comments: