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
|
use bevy::window::{PresentMode, WindowResolution};
use bevy::{prelude::*, winit::WinitSettings};
#[derive(Component)]
enum Direction {
Up,
Down,
Left,
Right,
None,
}
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "I am a window!".into(),
resolution: WindowResolution::new(500., 300.).with_scale_factor_override(1.0),
present_mode: PresentMode::AutoVsync,
// Tells wasm to resize the window according to the available canvas
fit_canvas_to_parent: true,
// Tells wasm not to override default event handling, like F5, Ctrl+R etc.
prevent_default_event_handling: false,
..default()
}),
..default()
}))
.add_plugins(EnemyPlugin)
.insert_resource(ClearColor(Color::srgb(0.5, 0.5, 0.9)))
.insert_resource(WinitSettings::game())
.add_systems(Startup, setup_player)
.add_systems(Update, player_movements)
.run();
}
fn offset_to_velocity(abs_v: f32, tr: Vec3, tr_target: Vec3) -> Vec2 {
let dx = tr_target.x - tr.x;
let dy = tr_target.y - tr.y;
let v = Vec2 { x: dx, y: dy }.normalize_or_zero();
Vec2 {
x: v.x * abs_v,
y: v.y * abs_v,
}
}
fn map_key_to_movement(key: &KeyCode) -> Direction {
match key {
KeyCode::KeyW => Direction::Up,
KeyCode::KeyS => Direction::Down,
KeyCode::KeyA => Direction::Left,
KeyCode::KeyD => Direction::Right,
_ => Direction::None,
}
}
pub struct EnemyPlugin;
impl Plugin for EnemyPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup_enemy)
.add_systems(Update, enemy_follow);
}
fn name(&self) -> &str {
"Enemy plugin"
}
}
#[derive(Debug)]
enum EntityType {
Player,
Enemy,
}
#[derive(Component)]
pub struct Player {
name: String,
}
#[derive(Component)]
pub struct Enemy {
name: String,
}
fn enemy_follow(
time: Res<Time>,
mut enemy: Query<(&Enemy, &Sprite, &mut Transform), (Without<Player>, With<Enemy>)>,
p: Query<(&Player, &Sprite, &Transform), (With<Player>, Without<Enemy>)>,
) {
let ev = 400. * time.delta_secs();
let (pl, ps, pt) = match p.get_single() {
Ok(p) => p,
Err(e) => return,
};
for (e, s, mut t) in &mut enemy {
let dv = offset_to_velocity(ev, t.translation, pt.translation);
let (ex, ey) = (t.translation.x, t.translation.y);
t.translation.x = safe_move(ex, dv.x, -500., 500.);
t.translation.y = safe_move(ey, dv.y, -300., 300.);
println!(
"Moving {} with v: {:?}, to translation: {:?}",
e.name, dv, t.translation
);
}
}
fn setup_enemy(mut commands: Commands) {
let enemy = Sprite::from_color(Color::srgb(0., 0.5, 0.), Vec2 { x: 10., y: 50. });
commands.spawn((
Enemy {
name: "Evil guy".to_string(),
},
enemy,
Transform::from_xyz(0., 0., 0.),
));
}
fn setup_player(mut commands: Commands) {
let s = Sprite::from_color(Color::srgb(1., 1., 1.), Vec2 { x: 10., y: 50. });
commands.spawn(Camera2d);
commands.spawn((
Player {
name: "Person".to_string(),
},
s,
Transform::from_xyz(0., 0., 0.),
));
}
fn safe_move(x: f32, v: f32, bl: f32, br: f32) -> f32 {
if x + v > 0. && x + v > br {
return x;
}
if x + v < 0. && x + v < bl {
return x;
}
x + v
}
/// The sprite is animated by changing its translation depending on the time that has passed since
/// the last frame.
fn player_movements(
time: Res<Time>,
input: Res<ButtonInput<KeyCode>>,
mut ps: Query<(&Player, &Sprite, &mut Transform), (With<Player>, Without<Enemy>)>,
) {
let pv = 750. * time.delta_secs();
let ev = 500. * time.delta_secs();
let movements: Vec<Direction> = input.get_pressed().map(map_key_to_movement).collect();
for (p, s, mut transform) in &mut ps {
let mut pvel_vec = Vec2 { x: 0., y: 0. };
movements.iter().for_each(|m| match m {
Direction::Up => pvel_vec.y = pv,
Direction::Down => pvel_vec.y = -pv,
Direction::Left => pvel_vec.x = -pv,
Direction::Right => pvel_vec.x = pv,
Direction::None => pvel_vec = Vec2 { x: 0., y: 0. },
});
let pvv = pvel_vec.normalize_or_zero();
let nx = safe_move(transform.translation.x, pv * pvv.x, -500., 500.);
let ny = safe_move(transform.translation.y, pv * pvv.y, -300., 300.);
transform.translation.x = nx;
transform.translation.y = ny;
println!(
"Moving {} with v: {:?}, to translation: {:?}",
p.name, pvv, transform.translation
);
}
}
|