|
1 | | -# Cruise v0.3.0 documentation: Game logics |
| 1 | +# Cruise v0.3.0 documentation: Game Logics |
2 | 2 |
|
3 | | -A game logic is a chunk of code that execute a specific task in your code. |
4 | | -In regular game engines they are often called **scripts**. These scripts are then tied to an object. when the object is active, his script execute, when it's not, the script isn't executed. |
| 3 | +A **game logic** is a self-contained block of code that performs a specific task in your game. |
| 4 | +In most engines these are called *scripts*, and they’re attached to game objects: if the object is active, its script runs; if not, it doesn’t. |
5 | 5 |
|
6 | | -With Cruise, it's a little bit different. Scripts are called **game logics** and are independent of objects. They can be activated/desactivated at will. Their purpose is to accomplish a specific task in your game. |
| 6 | +Cruise doesn’t follow that model. |
| 7 | +Here, a game logic is **not tied to any object**. |
| 8 | +It can be enabled or disabled at any time, connected to other logics, and integrated directly into the main execution graph. |
| 9 | +Its only responsibility is to perform a clearly defined operation inside your game. |
7 | 10 |
|
8 | | -One can create a new game logic like this |
| 11 | +--- |
| 12 | + |
| 13 | +## Declaring a Game Logic |
9 | 14 |
|
10 | 15 | ```julia |
11 | | -app = CruiseApp() # Make sure to have called this at least once |
| 16 | +app = CruiseApp() # Must be called at least once |
12 | 17 |
|
13 | 18 | logic_id = @gamelogic logic_name begin |
14 | | - # My code |
| 19 | + # Logic code |
15 | 20 | end |
16 | 21 | ``` |
17 | 22 |
|
18 | | -So here we made a new game logic that we named `logic_name`. The `@gamelogic` macro will return the id of our new logic. |
19 | | -Why an id ? |
20 | | -Because a game logic is in fact a system in the main plugin graph. This means that all your logic benefits from all the features a regular plugin node have (capabilities, dependencies, enabling/disabling, etc). Your logic is automatically added to the main plugin, but you can optionnaly pas the keyword argument `plugin=myplugin` so the logic is added to your custom plugin instead. |
| 23 | +`@gamelogic` creates the logic, registers it inside the plugin graph, and returns its **ID**. |
| 24 | +Why an ID? |
| 25 | +Because a game logic is essentially a **system node in the main plugin graph**, with all the same features as any plugin node: |
| 26 | + |
| 27 | +* capabilities |
| 28 | +* dependencies |
| 29 | +* enable/disable |
| 30 | +* ordered execution |
21 | 31 |
|
22 | | -Each game logic is an intace of the object `GameCode{Name}` where `Name` is the name of your logic as a `Symbol`. |
23 | | -So when getting it from adependency for example, you will just do |
| 32 | +By default, the logic is added to the main plugin, but you can place it elsewhere: |
24 | 33 |
|
25 | 34 | ```julia |
26 | | -pluginnode.deps[GameCode{:name}] |
| 35 | +@gamelogic logic_name plugin=myplugin begin |
| 36 | + ... |
| 37 | +end |
| 38 | +``` |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +## Identity and Access |
| 43 | + |
| 44 | +Each logic is an instance of: |
| 45 | + |
| 46 | +``` |
| 47 | +GameCode{Name} |
27 | 48 | ``` |
28 | 49 |
|
29 | | -In your logics, the internal representation of your logic (the node of the graph) is called `self`. for example |
| 50 | +where `Name` is your logic’s name as a `Symbol`. |
| 51 | + |
| 52 | +Example: accessing it from a dependency list: |
| 53 | + |
| 54 | +```julia |
| 55 | +pluginnode.deps[GameCode{:logic_name}] |
| 56 | +``` |
| 57 | + |
| 58 | +--- |
| 59 | + |
| 60 | +## The `self` Variable |
| 61 | + |
| 62 | +Inside every game logic, Cruise injects a variable called `self`. |
| 63 | +This is the actual node in the plugin graph that represents your system. |
30 | 64 |
|
31 | 65 | ```julia |
32 | 66 | @gamelogic logic begin |
33 | | - println(self) # self are is a variable specific to the logic that is the node containing the logic |
34 | | - # You can use it as with any other node |
| 67 | + println(self) # The node itself |
| 68 | + # You can use it exactly like any other graph node |
35 | 69 | end |
36 | 70 | ``` |
| 71 | + |
| 72 | +`self` gives you: |
| 73 | + |
| 74 | +* access to your capability |
| 75 | +* access to your dependencies |
| 76 | +* node state information |
| 77 | +* full interaction with the graph |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +## Keyword Arguments |
| 82 | + |
| 83 | +Game logics accept several optional keywords: |
| 84 | + |
| 85 | +### `mainthread=false` |
| 86 | + |
| 87 | +Forces the system to always run on the main thread. |
| 88 | + |
| 89 | +### `plugin=<plugin>` |
| 90 | + |
| 91 | +Places the logic inside a specific plugin instead of the main one. |
| 92 | + |
| 93 | +### `capability=<obj>` |
| 94 | + |
| 95 | +Associates a capability to the logic. |
| 96 | +Other systems depending on this logic can query that capability. |
| 97 | + |
| 98 | +Example: |
| 99 | + |
| 100 | +```julia |
| 101 | +@gamelogic movement capability=MovementCap() begin |
| 102 | + # ... |
| 103 | +end |
| 104 | +``` |
| 105 | + |
0 commit comments