News Box
WELCOME TO MLNO!!! ... ENJOY YOUR TIME HERE! ... WHILE YOU ARE HERE, CHECK OUT OUR RPGS!
My LEGO Nexus Organization
January 31, 2026, 12:31:12 pm
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Welcome to My Lego Nexus Organization! A LEGO Universe, LEGO and My LEGO Network Fan Site! The LEGO Company does not sponsor, authorize, or endorse this site.-- New? Check out the welcome forum and get started! Got questions? PM Blade, or one of the other active members of the staff.
 
  Home   Forum   Help Arcade Gallery Rules Staff List Login Register Chat  

BrashSmashes Behaviors Tutorial #4

Pages: [1]   Go Down
  Print  
Author Topic: BrashSmashes Behaviors Tutorial #4  (Read 261 times)
0 Members and 56 Guests are viewing this topic.
MsRowdyRedhead
Sage Mentor......... (renowned for profound wisdom)
A - MLNO Featured Admin of '08
Exciting Poster
*

MLNO Reputation 100
Offline Offline

Gender: Female
Awards:
Team: No Team
Purpose: Harmony
Posts: 15328


The forum can't be the same without our friends.


WWW
Badges: (View All)
Tenth Year Anniversary Ninth Year Anniversary Eighth Year Anniversary
« on: May 12, 2011, 09:32:55 pm »

Previous tutorials focused on the basics of behaviors: #1 on custom behaviors, #2 on communication and #3 on states. This tutorial puts that all together to create a simple game. I encourage you to go through the earlier tutorials, especially #2 and #3, since we will build on them for this one.

The game we'll create is a challenge to smash a number of items within an alotted time. The scenario: the player encounters a Maelstrom communications array that must be destroyed. Before starting the game, only the central controller object is active, and interacting with it starts the game. The player must smash all eight antennae before the time runs out. If he does, the central object smashes too and he wins, otherwise the antennae rebuild and he loses.

I've made a video showing two play-throughs of this game, the first one too slow and the second one within the time limit.

This game requires some problems be solved:

    * the antennae are only smashable during the game
    * we need to count how many antennae are smashed to determine that the player won
    * we need to run a timer to determine that the player lost


These problems are solved with three sets of objects:

    * the central controller that manages the game
    * a set of eight antenna models, all with the same behavior
    * a set of models making a counter of how many antennae have been smashed



The Controller

The central controller object is the only one interactable in the Home (initial) state. This means that none of the models can be interacted with or smashed until the game starts. All together, the controller uses four states:

    * Home: before the game starts, the only behavior is Interact to start the game
    * Circle: the game is running (the counter and timer are active)
    * Square: the player won and the game is over
    * Star: the player lost and the game is over




The Home state (top left above) has only the behavior chain that starts the game:

    * On Interact
    * Silent Chat "reset" (restores the state of all models)
    * Chat "so you want to challenge the maelstrom?"
    * Pause 3
    * Chat "then prepare to smash all the antennas"
    * Pause 3
    * Chat "go!"
    * Change State Circle


The Circle state (top right above) has two parallel behavior chains. The first one is the timer to determine if the player loses (runs out of time).

    * On Start (when we transition to this state)
    * Chat "go!"
    * Silent Chat "game on" (set up the antennae)
    * Pause 15
    * Change Start Star (player lost)


The second Circle chain simply listens for the counter to complete. (The counter message is "for carry out" for reasons that will be described later.)

    * On Chat "for carry out"
    * Change State Square (player won)


Thus we have two parallel behavior chains in the Circle state that are used to determine win/loss. If the counter chat is seen before the timer runs out, switch to the Square state (which ignores the timer). Otherwise, the timer will switch to the Star state (which ignores the counter).

The Square state (bottom left above), representing a win, has two behavior chains. The first is the win player feedback:

    * On Start (when we transition to this state)
    * Chat "ooh! you got me!"
    * Sound BrickSmash
    * Smash


The second Square state is simply our generic reset handler which restores the object and resets to the Home state.

The Star state (bottom right above), representing a loss, contains a single behavior chain:

    * On Start (when we transition to this state)
    * Chat "not fast enough!"
    * Sound Incorrect
    * Silent Chat "reset" (to rebuild any antennae that were smashed)
    * Change State Home (to allow player to retry)


It may help to draw out a flowchart for yourself to see how these behaviors, and the states they transition through, create the game logic.

The Counter

There is no built-in behavior to do something like "when all models are smashed", so we implement that by counting how many antennae are smashed. Each antenna does a silent chat when smashed (see below) and the counter adds them up and sends another message when it counts to eight.

How do we build a counter? We will use three (active) models to implement a 3-bit ripple carry adder! Using virtual LEGO bricks to implement a simple electronics circuit has an appeal of its own, but is also necessary since this sort of thing isn't built in the LU behaviors.

My counter is like a binary abacus with a frame and three bead strings. Since it's binary, there is only one bead per string, which can be at the bottom (Home, indicating 0) or the top (Diamond, indicating 1). The rightmost string indicates the "ones place" and moving left we have the "twos place" and "fours place" strings. (This may be easier if you think of base-10 numbers having ones, tens and hundreds places.)

The counter models actually move up and down as their state changes, which makes what is happening visible in the video if you watch the counter at the edge of the property space.


The behavior chains for the ones bit implement the initial counting of the antenna smashes and pass the count up to the twos bit. Remember that a binary digit (bit) can only contain 0 or 1, so when we count, we think: 0, 1, 0 with carry out, ...

    * Home: On Chat "smash" > Move Up 1 > Change State Diamond
    * Diamond: On Chat "smash" > Silent Chat "one carry out" > Move down 1 > Restart
    * Diamond: On Chat "reset" > Restart


(The second behavior chain is used when the controller sends the reset message.)

The behavior chains for the twos bit is very much the same, except that its input is the overflow of the ones bit:

    * Home: On Chat "one carry out" > Move Up 1 > Change State Diamond
    * Diamond: On Chat "one carry out" > Silent Chat "too carry out" > Move down 1 > Restart
    * Diamond: On Chat "reset" > Restart


And of course the fours bit is again the same, except that its input is the overflow of the twos bit:

    * Home: On Chat "too carry out" > Move Up 1 > Change State Diamond
    * Diamond: On Chat "too carry out" > Silent Chat "for carry out" > Move down 1 > Restart
    * Diamond: On Chat "reset" > Restart


And that's why the chat "for carry out" is used by the controller to determine when all models have been smashed. The fours bit counted two overflows of the twos bit, which counted two overflows of the ones bit, and 2 × 2 × 2 = 8. Voila!

The Antennae

After all that, the antenna models are nice and simple. Also, all eight are the same models with the same behavior chains. We use three states for these models:

    * Home for when the game is not started
    * Square for when the game is running
    * Star for when this antenna is smashed



The Home state has the behavior used when the game starts:

    * On Chat "game on" > Change State Square
    * On Chat "reset" > Restart (for safety, shouldn't do anything normally)


Using the home state this way means that the user cannot smash or interact with the antenna until the game starts (and the antenna switches to the Square state).

The Square state is activated when the game starts and enables the model to be smashed:

    * On Attack > Silent Chat "smash" > Sound BrickSmash > Smash > Change State Star
    * On Chat "reset" > Restart


The Star state is activated after the antenna is smashed and simply holds that state until the game is reset (using the by-now familar) "OnChat reset" chain.

Putting it all together

OK, that was a lot of stuff! Let's do a quick review of what happens.

The controller starts the game when the user interacts with it. Starting the game allows the antennae to be smashed. Each time an antenna is smashed, the counter adds one and when it counts up to eight, it sends a message that the controller is listening for. This triggers the "win" behavior.

In parallel, the controller is waiting for the timeout and if the user runs out of time, this triggers the "lose" behavior.

I suggest taking another look at the video showing these two scenarios in action and then drawing out a flow chart or whatever diagram makes sense to you to see how you might implement the game. Then read through this tutorial again to see how I did it. Happy building!
« Last Edit: May 13, 2011, 05:41:48 pm by MsRowdyRedhead » Report Spam   Logged



The dream is free.. the work to achieve it is sold separately.
OOC: An OOC expression of horror would confirm your humanity nicely. Wink
MLNO.  The only forum I know where expressions like the one above are commonplace. Tongue
Everyone is frustrating in their own special way.
Quote
"Right is right, even if everyone is against it. Wrong is wrong, even if everyone is for it." ♥
"If you're not havin a good day, change your mind!!"
Magical Girl Mimi
Lightning Girl
A - Azure Striker
Crimson Apple Masterpiece Built!
*

MLNO Reputation 100
Offline Offline

Gender: Female
Awards:
Team: Network Strikers
Purpose: To stay DETERMINED
Posts: 36585


Bird... GO!



Badges: (View All)
Tenth Year Anniversary Ninth Year Anniversary Eighth Year Anniversary
« Reply #1 on: May 13, 2011, 11:24:46 am »

And I thought the on-chat opening stuff was advanced. This takes the cake. =O

One slight note; the youtube link in the top of the post is slightly broken. Wink
Report Spam   Logged

I'm a girl - I'm a lesbian.

-==Blade's Story Library==-
-==Blade's RPG Library==-
A fresh start for the signature, aside for the libraries.

Blade is laughing ominously RUN FOR YOUR LIVES!!!
66226
The forum is a girl? 0_o
I imagine new people coming to the forum expecting to see discussion about LEGO, only to see RPGs full of deranged characters and insane people talking about cheese and brains.
I'm beet. I don't think I'll do homework tonight.
So, you're a vegetable? Tongue
Pages: [1]   Go Up
  Print  
 
Jump to:  

LEGO® is a trademark of the LEGO Group, which does not sponsor, authorize or endorse this site.
Bookmark this site! | Upgrade This Forum
SMF For Free - Create your own Forum

Powered by SMF | SMF © 2016, Simple Machines
Privacy Policy
Page created in 0.068 seconds with 13 queries.