1
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を書いてみる 3

Posted at

前回はこのようなコードを書きました

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

    // 配列をやめて、ベクタで宣言する
    let mut pos_ary: Vec<(i32, i32)> = vec![];

    // 効果を確かめるために、10回 create_entity を呼び出す
    for i in 0..10 {
        create_entity(&mut id, &mut pos_ary);
    }

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

// idを増やして、配列に要素を追加する
fn create_entity(id: &mut usize, ary: &mut Vec<(i32, i32)>){
    *id += 1;
    ary.push((0, 0));
}

このコードには問題点があります。
pos_aryの要素が必ず(0, 0)になること。

この解決策は2つあります。
1つめは、create_entity関数の引数を増やす
2つめは、エンティティを作った直後にコンポーネントの値を変更する

どちらが有利か今のところわからないので、ふたつとも実装してみます

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

    // 配列をやめて、ベクタで宣言する
    let mut pos_ary: Vec<(i32, i32)> = vec![];

    // 効果を確かめるために、10回 create_entity を呼び出す
    for i in 0..10 {
        create_entity(&mut id, &mut pos_ary);
    }



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

// idを増やして、配列に要素を追加する
fn create_entity(id: &mut usize, ary: &mut Vec<(i32, i32)>){
    *id += 1;
    ary.push((0, 0));
}

// idを増やして、配列に指定された値の要素を追加する
fn create_entity_pos(id: &mut usize, ary: &mut Vec<(i32, i32)>, data: (i32, i32)) {
    *id += 1;
    ary.push(data);
}

// 配列の指定されたidの要素に、指定された値を代入する
fn set_data (id: &usize, ary: &mut Vec<(i32, i32)>, data: (i32, i32)) {
    ary[*id] = data;
}

新たに関数を書いて、使ってみます。
代入する値はランダムにしてみたいと思います。

ターミナルでcargo add randをしてrandクレートを追加します。

まずは、create_entity_pos関数を使ってみます。

main.rs
use rand;

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

    // 配列をやめて、ベクタで宣言する
    let mut pos_ary: Vec<(i32, i32)> = vec![];

    // 効果を確かめるために、10回 create_entity を呼び出す
    for i in 0..10 {
        // ランダムな値を取得する
        let (x, y) = rand::random();
        create_entity_pos(&mut id, &mut pos_ary, (x, y));
    }



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

// idを増やして、配列に要素を追加する
fn create_entity(id: &mut usize, ary: &mut Vec<(i32, i32)>){
    *id += 1;
    ary.push((0, 0));
}

// idを増やして、配列に指定された値の要素を追加する
fn create_entity_pos(id: &mut usize, ary: &mut Vec<(i32, i32)>, data: (i32, i32)) {
    *id += 1;
    ary.push(data);
}

// 配列の指定されたidの要素に、指定された値を代入する
fn set_data (id: &usize, ary: &mut Vec<(i32, i32)>, data: (i32, i32)) {
    ary[*id] = data;
}

結果は、このようになりました。
問題なく値がランダムになっています

0: (631301613, -1321658491)
1: (1064433629, 714273517)
2: (651317540, 1527130298)
3: (637752836, -1758116208)
4: (1064518213, 286156136)
5: (1338577346, -1317936462)
6: (-488325186, -51452308)
7: (-636916261, 429026785)
8: (-1750250952, -1231776558)
9: (1426910556, -1350481101)

次に、set_data関数を使ってみます。

main.rs
use rand;

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

    // 配列をやめて、ベクタで宣言する
    let mut pos_ary: Vec<(i32, i32)> = vec![];

    // 効果を確かめるために、10回 create_entity を呼び出す
    for i in 0..10 {
        // ランダムな値を取得する
        let (x, y) = rand::random();
        create_entity(&mut id, &mut pos_ary,);
        set_data(&id, &mut pos_ary, (x, y));
    }



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

// idを増やして、配列に要素を追加する
fn create_entity(id: &mut usize, ary: &mut Vec<(i32, i32)>){
    *id += 1;
    ary.push((0, 0));
}

// idを増やして、配列に指定された値の要素を追加する
fn create_entity_pos(id: &mut usize, ary: &mut Vec<(i32, i32)>, data: (i32, i32)) {
    *id += 1;
    ary.push(data);
}

// 配列の指定されたidの要素に、指定された値を代入する
fn set_data (id: &usize, ary: &mut Vec<(i32, i32)>, data: (i32, i32)) {
    ary[*id] = data;
}

結果は、エラー

thread 'main' panicked at 'index out of bounds: the len is 1 but the index is 1', src\main.rs:40:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\EntitiyCompornentSystem-Rust.exe` (exit code: 101)

set_data関数でidを-1していませんでした。
気を取り直して修正します。

set_data関数
fn set_data (id: &usize, ary: &mut Vec<(i32, i32)>, data: (i32, i32)) {
    ary[*id -1] = data;
}

修正したので結果を見てみます

0: (-1624067781, 1784680369)
1: (-678787888, -217647087)
2: (-281588722, -77193557)
3: (811644284, 26805558)
4: (1165551360, -253299836)
5: (-1638065505, -465493239)
6: (-1235961454, 2018485865)
7: (654307265, 2093340278)
8: (-966811836, 1103924710)
9: (1149485780, 1684219541)

ちゃんとランダムになっていますね
今回はここまでにしようと思います。

1
0
4

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
1
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?