Logic Graph idea

This builds further on the ideas proposed here and here.

image

The game consists of two players and a game world. Each player has a variable that represents how many other players he has killed, and a state machine with two states: "alive" and "dead".

The black dot at the top of the orange "alive" node of "player a" contains instructions that are only called when the node is first called (and every time the node is called again after not being called before -for instance when respawning).

The remainder of the node contains logic that is called continuously while in this state.

The small orange dots in the black slots contain logic for the "dead" state, which is currently not being called (since the ninja's are alive!)

image

If we drag out the small black dot, we can see it contains logic to set the position of the character. This logic is only called once: the first time that the node that contains the black dot is called, so at the moment of (re)spawing.

image

We won't need "player 2" for the moment, so we've tucked his logic back into the "game" node. We've also put the black "once" dot back in its place and have dragged out other properties of the node: a tag that defines the game object as "ninja" and a spawner for his deadly weapon: the ninja star.

The black dot above the grey "star spawner" sticks out a bit more that the back dot above the orange "alive" node. That's because it has a different function. The black dot above the the spawner is a boolean gate. When it is TRUE, the "star spawner" node will be called. Otherwise it will not.

image

If we drag out the boolean dot above the "star spawner" node, we can see that it is set to TRUE by a key press.

When a key is pressed, the spawner will instantiate a copy of the "star" prefab.

Note that the "star" node also has "top dot" that is only called once, on creation. It contains logic to copy the rotation of the ninja character to the operator that defines the movement of the star.

Every frame the position of the star is incremented with a speed variable multiplied by the rotation of the player at spawn time. The "*" operator has an option in its Inspector to make the speed independent of framerate (multiply with Time.deltaTime).

Note that the increment operator (the grey node with the plus sign) also has a boolean dot. We'll need that one later. For now, assume that the boolean is set to TRUE, which allows the star to move.

image

Cleaning up. We tuck back the transform of the "alive" node, the boolean of "star spawner" and the "once" dot of the "star" prefab. And we fold up the star's "move" operator into a single node.

If we drag out the middle grey child of the yellow "star" node, we can see it's called "self destruct". It has a similar black mini-node above it like the boolean of the "move" node next to it. Only this one has a count-down: it will turn to true after 10 seconds, at which point the star will be destroyed.

Boolean dots and count-down dots are actually the same operator, just differently configured in its Inspector.

image

Tucking the "self destruct" node safely back into the "star" node, we drag out the third visible child.
The teal node represents the collisions that happen with the star. It has a state machine with two states: "colliding" and "not colliding".

image

When a collision happens, the state changes to "colliding". We can drag out this node to add logic to call during the collision.
The teal "collisions" node also has a "hit" child that contains information about the collision.

image

The orange "colliding" state node has a brown dot in its top that, as you remember from before, is only called once. The first time the node is called, in other words, only at the moment of collision.

On collision entry, a "stop" operator node is called which sets the top boolean of the "move" node to FALSE, ending the movement of the star.

The dark red node next to the "stop" node is only called when the small black boolean dot above it is TRUE.

image

The black boolean dot above the dark red node is set to TRUE when the tag of the object the star collided with is "ninja". As you may recall, the "alive" state of our player node was tagged "ninja".

Since we now know the star has hit a ninja, we increment our "killsa" score with 1.

image

Simultaneously, we set the "alive/dead" state machine of "player b" to its "dead" state. The top dot of the "dead" state could start the "dying" animation of the character when the transition happens and start a time-out for the state machine to switch back to its "alive" state, when it can respawn.

Challenge:
- Ninja throws stars when a key is pressed
- Can throw many stars
- Each star is a game object instantiated from a prefab
- Stars fly in the direction player was facing when they were thrown, move at certain speed
- Each of those stars have their lifetime (say 10 seconds) after which they are destroyed
- They stop flying if they hit some collider that is not a player
- If they hit a player, player dies and respawns, "kill" counter is incremented for the ninja that had thrown the star