-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy.rs
More file actions
161 lines (135 loc) · 4.53 KB
/
enemy.rs
File metadata and controls
161 lines (135 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use std::time::Duration;
use bevy::prelude::*;
use bevy::time::common_conditions::on_timer;
use rand::Rng;
use crate::{ENEMY_MAX, EnemyCount, FromEnemy, Laser, Movable, SpriteSize, Velocity, WinSize};
// region: Asset Contants
const ENEMY_SPRITE: &str = "enemy.png";
const ENEMY_SIZE: (f32, f32) = (150., 150.);
const ENEMY_SCALE: f32 = 0.3;
const LASER_SPRITE: &str = "laser_b.png";
const LASER_SIZE: (f32, f32) = (45., 150.);
const LASER_SCALE: f32 = 0.3;
// endregion: Asset Contants
// region: Resource
#[derive(Resource)]
struct EnemyTextures {
enemy: Handle<Image>,
enemy_laser: Handle<Image>,
}
// endregion: Resource
// region: Components
#[derive(Component)]
pub struct Enemy;
// 敵の円運動用の軌道情報
#[derive(Component)]
struct Orbit {
center: Vec2,
radius: f32,
omega: f32, // 1秒あたりの角度変化量。値が大きくなると回転速度が速くなり、負の値にすると反時計回りに回転する。 [rad/s]
phase: f32, // 初期角度。円のどこから回転を開始するかのオフセット。 [rad]
}
impl Default for Orbit {
fn default() -> Self {
Self {
center: Vec2::ZERO,
radius: 40.0,
omega: 1.5,
phase: 0.0,
}
}
}
// endregion: Components
pub struct EnemyPlugin;
impl Plugin for EnemyPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup)
.add_systems(
Update,
enemy_spawn_system.run_if(on_timer(Duration::from_secs(2))),
)
.add_systems(
Update,
enemy_fire_system.run_if(on_timer(Duration::from_secs(3))),
)
.add_systems(Update, enemy_movement_system);
}
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// ゲームで使用するテクスチャを読み込み、リソースとして保存
commands.insert_resource(EnemyTextures {
enemy: asset_server.load(ENEMY_SPRITE),
enemy_laser: asset_server.load(LASER_SPRITE),
});
}
fn enemy_spawn_system(
mut commands: Commands,
mut enemy_count: ResMut<EnemyCount>,
assets: Res<EnemyTextures>,
win_size: Res<WinSize>,
) {
if enemy_count.0 >= ENEMY_MAX {
return;
}
let w_span = win_size.w / 2. - 100.;
let h_span = win_size.h / 2. - 100.;
let mut rng = rand::thread_rng();
let x = rng.gen_range(-w_span..w_span);
let y = rng.gen_range(-h_span..h_span);
commands
.spawn((
Sprite::from_image(assets.enemy.clone()),
Transform {
translation: Vec3::new(x, y, 10.),
scale: Vec3::new(ENEMY_SCALE, ENEMY_SCALE, 1.),
..Default::default()
},
))
.insert(Enemy)
.insert(SpriteSize::from(ENEMY_SIZE))
.insert(Orbit {
center: Vec2::new(x, y),
..Default::default()
});
enemy_count.0 += 1;
}
fn enemy_fire_system(
mut commands: Commands,
assets: Res<EnemyTextures>,
enemy_query: Query<&Transform, With<Enemy>>,
) {
for &enemy_transform in enemy_query.iter() {
let enemy_pos = enemy_transform.translation;
commands
.spawn((
Sprite::from_image(assets.enemy_laser.clone()),
Transform {
translation: Vec3::new(
enemy_pos.x,
enemy_pos.y - ENEMY_SIZE.1 / 2. * ENEMY_SCALE,
10.,
),
scale: Vec3::new(LASER_SCALE, LASER_SCALE, 1.),
..Default::default()
},
))
.insert(Laser)
.insert(FromEnemy)
.insert(SpriteSize::from(LASER_SIZE))
.insert(Movable { auto_despawn: true })
.insert(Velocity { x: 0., y: -1. });
}
}
// スポーンした敵が円を描くように移動するシステム
fn enemy_movement_system(
time: Res<Time>,
mut query: Query<(&mut Transform, &mut Orbit), With<Enemy>>,
) {
for (mut transform, mut orbit) in query.iter_mut() {
orbit.phase += orbit.omega * time.delta_secs();
let cx = orbit.center.x;
let cy = orbit.center.y;
transform.translation.x = cx + orbit.radius * orbit.phase.cos();
transform.translation.y = cy + orbit.radius * orbit.phase.sin();
}
}