今日は6時15分に目が覚めて、10分くらい散歩した。
炊飯器のタイマーが6:50分になってたから 朝飯前に作業を始めてる。
今回の目標、カードをエンティティとして生成してみる。
まず、カードEntityにコンポーネントとして以下の4つの情報を与えたいです。
name
energy
power
location
#[derive(Component)]
struct Health {
hp: f32,
extra: f32,
}
#[derive(Component)]
enum AiMode {
Passive,
ChasingPlayer,
}
bevy-cheatbook.github.io
use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(MinimalPlugins)
.add_systems(Startup, setup)
.add_systems(Update, show_information)
.run();
}
#[derive(Component)]
struct Card {
power: u32,
name: String,
energy: u32,
}
#[derive(Component, Default)]
InHand,
SZone,
XZone,
FZone,
#[default]
InLibrary,
}
fn setup(mut commands: Commands) {
// create a new entity
let my_entity = commands
.id();
println!("{:?} is spawned", my_entity)
}
fn show_information(query: Query<(&Card, &Location)>){
for (card, location) in query.iter(){
let location_str = match location {
Location::InHand => "InHand",
Location::SZone => "SZone",
Location::XZone => "XZone",
Location::FZone => "FZone",
Location::InLibrary => "InLibrary",
};
println!("Card Name: {}, Power: {}, Energy: {}, Location:{}", card.name, card.power, card.energy, location_str);
}
}
取り敢えず、カード1枚を追加してそのidを表示し、カードの情報を取得するシステム作り終わった。
let my_entity = commands.spawn*2
.id();
println!("{:?} is spawned", my_entity);
let my_entity = commands
.id();
println!("{:?} is spawned", my_entity);
let my_entity = commands
.id();
println!("{:?} is spawned", my_entity);
let my_entity = commands
.id();
println!("{:?} is spawned", my_entity);
let my_entity = commands
.id();
println!("{:?} is spawned", my_entity);
}
fn show_information(query: Query<(&Card, &Location)>){
for (card, location) in query.iter(){
let location_str = match location {
Location::InHand => "InHand",
Location::SZone => "SZone",
Location::XZone => "XZone",
Location::FZone => "FZone",
Location::InLibrary => "InLibrary",
};
println!("Card Name: {}, Power: {}, Energy: {}, Location:{}", card.name, card.power, card.energy, location_str);
}
}
取り敢えず、カードを5枚追加してみました。
先ほどは、Update関数にしてたので、PostSetupスケジュールを使って、連続で動かないようにした。

とりあえず、5つ生成することには生成した。
そのあと、F zoneにあるカードの合計エナジーを計算する。
そして、山札にあるカードを表示する。
fn sum_energy_in_Szone(query: Query<(&Card, &Location)>){
let usable_energy = 0;
for (card, location) in query.iter(){
if Location::SZone {
usable_energy+=card.energy
};
}
}
このようなコードを書いたがAIにダメだしされて、
mutを使えと、言われた。
fn sum_energy_in_szone(query: Query<(&Card, &Location)>) {
let mut usable_energy = 0;
for (card, location) in query.iter() {
if let Location::SZone = location {
usable_energy += card.energy; // energyを加算
}
}
println!("Total usable energy in SZone: {}", usable_energy); // 結果を表示
}
これにした。 同様に、
fn cards_in_deck(query: Query<(&Card, &Location)>){
print!("cards in your deck are ");
for(card, location) in query.iter(){
if let Location::InLibrary = location{
println!("{}, ", card.name);
}
}
}
できた。
最後に必要な関数は入力を受け取って、選択してエンティティの位置を移動する関数を作る。
まず、手札にあるカードをいくつか追加する。
let my_entity = commands
.id();
println!("{:?} is spawned", my_entity);
let my_entity = commands
.id();
println!("{:?} is spawned", my_entity);
let my_entity = commands
.id();
println!("{:?} is spawned", my_entity);
これでOK
fn select_a_card_in_hand(query: Query<(Entity, &Card, &Location)>) {
let mut cards_in_hand = Vec::new();
println!("Cards in your hand are:");
for (index, (entity, card, location)) in query.iter().enumerate() {
if let Location::InHand = location {
println!("{}: {}", cards_in_hand.len()+1, card.name); // 番号を振って表示
cards_in_hand.push(entity); // エンティティIDを保存
}
}
if cards_in_hand.is_empty() {
println!("No cards in hand.");
return;
}
println!("Select a card by number (1-{}):", cards_in_hand.len());
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");
if let Ok(selected_index) = input.trim().parse::<usize>() {
if selected_index > 0 && selected_index <= cards_in_hand.len() {
let selected_entity = cards_in_hand[selected_index - 1]; // 選択したエンティティIDを取得
println!("You selected card with Entity ID: {:?}", selected_entity);
} else {
println!("Invalid selection.");
}
} else {
println!("Please enter a valid number.");
}
}
ChatGptに作ってもらった
手札のカード1枚を選択して、それのEntity IDを返す関数が以上になります。
これを改造して、指定したIDのエンティティの
コンポーネントを変更します。
commands.entity(selected_entity).insert(Location::FZone);
これにより、指定したエンティティにをFzoneに移動します。

少なくとも入力に合わせて、カードの名前を取得したり、Locationを変更したりすることが出来た。
これで操作の7割くらいは終わったと言える。
次は、カードのスピンリスピンを実装したい。
#[derive(Component, Default)]
Spin,
#[default]
ReSpin,
}
これで、状態を宣言し、
数字を選択し、Sゾーンにあるリスピン状態のカードを数字以上の
エナジーになるように
スピンさせる関数を作る。
// Initialize all your components and bundles here
Card {
power: 100,
name: String::from("リトルナイト"),
energy: 1,
},
Location::InLibrary,
次の目標
特定の要素だけを持つものについての演算を行うシステムを作る。
use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(MinimalPlugins)
.add_systems(Startup, setup)
.add_systems(PostStartup, show_information)
.run();
}
#[derive(Component)]
struct Card {
power: u32,
name: String,
energy: u32,
}
#[derive(Component, Default)]
InHand,
SZone,
XZone,
FZone,
#[default]
InLibrary,
}
fn setup(mut commands: Commands) {
let my_entity = commands
.spawn((
Card {
power: 1000,
name: String::from("リトルナイト"),
energy: 1,
},
Location::InLibrary,
Card {
power: 500,
name: String::from("なまけ騎士 ハームゥ"),
energy: 0,
},
Location::FZone,
Card {
power: 2000,
name: String::from("アサシン・タイクーン"),
energy: 2,
},
Location::InLibrary,
Card {
power: 2000,
name: String::from("騎士ニャタタ・ビィ"),
energy: 3,
},
Location::InLibrary,
Card {
power: 1000,
name: String::from("密林の騎士リンキー"),
energy: 2,
},
Location::InLibrary,
Card {
power: 1000,
name: String::from("ワンダフル・ウィザード ドギィ"),
energy: 4,
},
Location::InHand,
Card {
power: 1000,
name: String::from("ワンダフルウィザード・ワッチチ"),
energy: 3,
},
Location::InHand,
Card {
power: 500,
name: String::from("ワンダフル・ウィザード ゴン"),
energy: 3,
},
Location::InHand,