- About this approach -
This approach to AI involves constructing enemy behavior using Flash's timeline. The timeline makes it easy to construct a sequence of actions and behaviors.
One determines what an enemy does by dragging "behavior" movieClips onto the stage. Each frame can have a different set of behaviors in it.

The great thing about the AI system this game uses is that it makes it easy to construct intricate enemy behaviors without having to plan everything out, which helps a person stay creative because their creative momentum doesn't get interrupted.
Another cool thing about this AI system is that it can also be used for cutscenes.
This means that the game can seamlessly transition between battles and cutscenes.



- How the AI works -
Different AI's are stored in separate SWF files.
These basically represent different enemies.
They control an enemy's behavior, appearance, and collision areas.
When you place an enemy sprite in the level editor, you can tell it which SWF file to load.

If a regular SWF file is loaded, it'll simply be displayed and the enemy will just sit there doing nothing. It will still be able to take damage, but it won't respond to having 0 HP. It also won't be able to harm the player.

If a SWF file is loaded containing AI behavior movieClips, it'll control the enemy's actions and reactions.

The game steps through the SWF's timeline and responds to the behavior movieClips on each frame.
These are movieClips that are specifically programmed for AI use.
Their names start with an "AI_" prefix



- Behavior movieClips -
chargePlayer			The enemy walks toward the player's location it that particular moment, and then stops.
walk							The enemy walks in a given direction indefinitely.
walkForward				The enemy walks in the direction it's facing, indefinitely.
jump							The enemy jumps.

disable_attack		
disable_hit				While present, this enemy cannot take damage from player attacks.
gravity						Controls whether or not this enemy is pulled down by gravity.
pause							Turns parts of the game engine on or off. This is mainly used to pause the player before a cutscene, and reactivate them afterwords.
playerState				This changes the player's pose. This isn't merely visual. The programming associated with that pose will also run. Therefore, telling the player to attack will make them damage enemies that are hit.

facePlayer				This makes the enemy turn to face the player.
turnAround				This makes the enemy look in the opposite direction.

letterbox					This makes a letterbox appear on the screen or disappear. This is intended to be a visual cue to tell the player that a cutscene is starting or ending.

timer							This waits for a specified number of seconds. By itself, it doesn't affect anything, but the "wait_for" movieClip can respond to it.
react							This jumps to the specified frame label in the AI's timeline, when it detects a specific condition.
wait_for					This waits for another movieClip on the timeline to finish what it's doing, then it advances the AI timeline to the next frame.

talk							This makes a textbox appear and display a message. This will NOT automatically pause the game. Typically, this will be used during a cutscene after the game has been paused.


You can also display any other movieClips you want to in this timeline. (Such as the speech balloon I threw in there)  Just remember that they'll be relative to this enemy's position.




- Things to be added -
There are still more commands to create, and some changes to be made to existing ones.
For example, I still need to add the ability to affect other sprites besides the one that contains the AI. This will be necessary for cutscenes.
I also plan to add a few more commands to handle music, sound, additional movements, and setting variable flags. (So that dead bosses can stay dead)
And finally, I need to add a teleport command so I can send the player to other maps for story reasons.



- How resources are shared -
How this works is kind of complex.
It involves using "shared libraries" which is kind of a little-known feature in Flash.
This allows SWF files to use resources stored in other SWF files.
This means that the animations are not actually stored in the AI SWF files themselves, but are instead read from other SWF files located in the charset folder.
In the "charset" folder, each enemy has its own SWF file, which contains all of its animations.
The "swf/AI" folder, stores all of the SWF files used for AI, which control enemy behavior.
The AI SWF files access animations stored in the libraries of the charset SWF files.
When editing an AI file, these animations are displayed in its library, but cannot be edited. However they CAN be dragged onto the stage and used normally.
To edit an animation, you need to edit its FLA file stored in the charset folder, and re-export its SWF in the charset folder.
This means that when you change an animation, it'll automatically be updated everywhere else. And game.swf will immediately use the updated animation.

It's basically set up like this:

game/charset/
	enemy1.fla			(used to edit animations, and create enemy1.swf)
	enemy1.swf			(used to store animation movieClips)
game/swf/AI/
	enemy1_ai.swf		(uses animations stored in enemy1.swf)
game.swf					(the game itself. Loads enemy1_ai.swf and reads animations from enemy1.swf)

Each enemy will have a single SWF file containing all of its animations.
But the player will not. The player sprite still uses the original method of loading individual SWF files.



- How to prepare enemy resources -
From your end, you won't need to do anything different to create the animations themselves.
When making the player, you'll construct all the animations in a single FLA file, and then export multiple SWF files for each animation.
When making an enemy, you'll construct all the animations in a single FLA file, and then export a single SWF file, containing all of the animation movieClips.


For enemies, you first create all of an enemy's animations inside of a single FLA file. Each animation should be contained in its own movieClip.
Next, you right-click on each of those movieClips in the library and select "linkage"
Check "Export for runtime sharing"
And type a filepath to the SWF file you'll create like so:  charset/enemy1.swf
This file path needs to be relative to game.swf.
Close the FLA file containing the animations.
Next, open a FLA file in the swf/AI folder. (Or you can start with swf/AI/_template.fla to create a new one)
Select "File>Import>Open External Library..."
Drag the movieClips with linkages to the current AI file's library.
Close the external library.
(do NOT change the linkages. Leave them as charset/enemy1.swf)
Now you can use these movieClips like normal. But you won't be able to edit them from this FLA file.
To edit them, open the original FLA file you used to create them, and then re-export its SWF file.
