0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ゼロからEntityComponentSystemを書いてみる

Last updated at Posted at 2023-04-29

Rust言語で書きます

EntityComponentSystemって何かというと、ふわっとしたイメージで、配列に入っているデータをループで書き換える感じ。
配列はメモリ上で連続して配置されるので効率的にアクセスできるというメリットがある。

EntityはIDだとか、Componentはデータだとか、Systemは処理だとか説明されますが実際のソースコードではどうなるか確かめてみましょう。

まずは、ループを書きます。

main.rs
fn main() {

    let id = 2;

    for i in 0..id  {
        println!("{}", i)
    }
}

つぎに、配列の各要素を出力してみましょう

main.rs
fn main() {
    
    // idは配列の要素数を表す
    let id = 2;

    let pos_ary = [(0, 0), (0, 0)];

    for i in 0..id  {
        println!("{}: {:?}", i, pos_ary[i])
    }
}

はい。これがEntityComponentSystemの基礎です。

Entityは、配列の要素数を表す変数でした。
Componetは、配列の各要素です。
Systemは、forループでした。

ただ、このままでは配列の要素数は固定だし、idが3とかだったら実行時エラーになってしまうなど問題ありです。

次回はこれを改善しようとおもいます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?