Systems には、components に基づいて entities を処理・操作するロジックが含まれます。通常、system は特定の components を持つすべての entities に対して動作します。たとえば “Movement” system は “Position” component の位置を更新するかもしれません。次のコマンドで movement system を作成します。
bolt system system-movement
これにより workspace に新しい system が追加されます。
use bolt_lang::*;use component_position::Position;declare_id!("FSa6qoJXFBR3a7ThQkTAMrC15p6NkchPEjBdd4n6dXxA");#[system]#[program]pub mod system_movement { pub fn execute(ctx: Context<Components>, args_p: Vec<u8>) -> Result<Components> { let position = &mut ctx.accounts.position; position.x += 1; position.y += 1; Ok(ctx.accounts) } // Define the input components #[system_input] pub struct Components { pub position: Position, }}
この簡単な例では、x、y、z 座標を持つ Position component を保持する Player entity を作成しました。movement system を実行することでその状態を変更できます。より高度な移動ロジックのために、Velocity component を定義してみましょう。
use bolt_lang::*;declare_id!("CbHEFbSQdRN4Wnoby9r16umnJ1zWbULBHg4yqzGQonU1");#[component]#[derive(Copy)]pub struct Velocity { pub x: i64}
移動速度を上げる新しい power-up を追加したい場合は、Velocity を使って Position component に作用する新しい system を追加するだけで実現できます。