-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.rs
More file actions
140 lines (116 loc) · 3.85 KB
/
player.rs
File metadata and controls
140 lines (116 loc) · 3.85 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
use std::{f32::consts::PI, time::Duration};
use bevy::{prelude::*, time::common_conditions::on_timer};
use crate::{FromPlayer, Laser, Movable, PLAYER_MAX, PlayerCount, SpriteSize, Velocity, WinSize};
// region: Asset Contants
const PLAYER_SPRITE: &str = "player.png";
const PLAYER_SIZE: (f32, f32) = (150., 150.);
const PLAYER_SCALE: f32 = 0.3;
const PLAYER_LASER_SPRITE: &str = "laser_a.png";
const PLAYER_LASER_SIZE: (f32, f32) = (45., 150.);
const PLAYER_LASER_SCALE: f32 = 0.3;
// endregion: Asset Contants
// region: Resource
#[derive(Resource)]
struct PlayerTextures {
player: Handle<Image>,
player_laser: Handle<Image>,
}
// endregion: Resource
// region: Components
#[derive(Component)]
pub struct Player;
// endregion: Components
pub struct PlayerPlugin;
impl Plugin for PlayerPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup)
.add_systems(Update, player_keyboard_event_system)
.add_systems(Update, player_fire_system)
.add_systems(
Update,
player_spawn_system.run_if(on_timer(Duration::from_secs(2))),
);
}
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// ゲームで使用するテクスチャを読み込み、リソースとして保存
commands.insert_resource(PlayerTextures {
player: asset_server.load(PLAYER_SPRITE),
player_laser: asset_server.load(PLAYER_LASER_SPRITE),
});
}
fn player_spawn_system(
mut commands: Commands,
mut player_count: ResMut<PlayerCount>,
assets: Res<PlayerTextures>,
win_size: Res<WinSize>,
) {
if player_count.0 >= PLAYER_MAX {
return;
}
let bottom = -win_size.h / 2.;
commands
.spawn((
Sprite::from_image(assets.player.clone()),
Transform {
translation: Vec3::new(0., bottom + PLAYER_SIZE.1 / 2. * PLAYER_SCALE + 5., 10.),
scale: Vec3::new(PLAYER_SCALE, PLAYER_SCALE, 1.),
..Default::default()
},
))
.insert(Player)
.insert(SpriteSize::from(PLAYER_SIZE))
.insert(Movable {
auto_despawn: false,
})
.insert(Velocity { x: 0., y: 0. });
player_count.0 += 1;
}
fn player_keyboard_event_system(
kb: Res<ButtonInput<KeyCode>>,
mut query: Query<&mut Velocity, With<Player>>,
) {
if let Ok(mut velocity) = query.single_mut() {
velocity.x = if kb.pressed(KeyCode::ArrowLeft) {
-1.
} else if kb.pressed(KeyCode::ArrowRight) {
1.
} else {
0.
}
}
}
fn player_fire_system(
mut commands: Commands,
kb: Res<ButtonInput<KeyCode>>,
assets: Res<PlayerTextures>,
query: Query<&Transform, With<Player>>,
) {
// Space が押されていない場合は終了
if !kb.just_pressed(KeyCode::Space) {
return;
}
// プレイヤーが単一で存在しない場合は終了
let Ok(player_transform) = query.single() else {
return;
};
let (x, y) = (
player_transform.translation.x,
player_transform.translation.y,
);
const Y_OFFSET: f32 = PLAYER_LASER_SIZE.1 / 2. * PLAYER_LASER_SCALE;
commands
.spawn((
Sprite::from_image(assets.player_laser.clone()),
Transform {
translation: Vec3::new(x, y + Y_OFFSET, 0.),
scale: Vec3::new(PLAYER_LASER_SCALE, PLAYER_LASER_SCALE, 1.),
rotation: Quat::from_rotation_x(PI),
},
))
.insert(Laser)
.insert(SpriteSize::from(PLAYER_LASER_SIZE))
.insert(FromPlayer)
.insert(Movable { auto_despawn: true })
.insert(Velocity { x: 0., y: 1. });
}