Skip to content

Instantly share code, notes, and snippets.

@yknishidate
Created May 6, 2022 12:28
Show Gist options
  • Save yknishidate/74e97ca3ebdf511aaa57f9c9600dc030 to your computer and use it in GitHub Desktop.
Save yknishidate/74e97ca3ebdf511aaa57f9c9600dc030 to your computer and use it in GitHub Desktop.
#include <vector>
#include <iostream>
#include <functional>
struct Obj
{
float speed = 10.0;
float velocity = 1.0;
std::function<void(void)> Update = [](){ };
};
struct Scene
{
std::vector<Obj> objs; // Array of Entity
Obj& AddObj()
{
objs.push_back(Obj{});
return objs.back();
}
void Update()
{
for (auto&& obj : objs) {
obj.Update();
}
}
};
int main()
{
Scene scene;
Obj& obj1 = scene.AddObj();
obj1.Update = [=](){
std::cout << "speed: " << obj1.speed << std::endl;
};
Obj& obj2 = scene.AddObj();
obj2.Update = [=](){
std::cout << "velocity: " << obj2.velocity << std::endl;
};
scene.Update();
}
@yknishidate
Copy link
Author

  • 継承やコンポーネントを使わずに、インスタンスごとの処理を実装できる
  • 配列の要素とする場合も実体として持てる
  • なにかしらがまずい予感がする(?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment