-
Notifications
You must be signed in to change notification settings - Fork 16
/
breakout.rs
220 lines (189 loc) · 6.44 KB
/
breakout.rs
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//! A simplified implementation of the classic game "Breakout".
//! => Original example by Bevy, modified for Bevy Quinnet to add a 2 players versus mode.
use std::net::{IpAddr, Ipv4Addr};
use bevy::prelude::*;
use bevy_quinnet::{
client::QuinnetClientPlugin,
server::{QuinnetServer, QuinnetServerPlugin},
};
use client::BACKGROUND_COLOR;
mod client;
mod protocol;
mod server;
const SERVER_HOST: &str = "127.0.0.1";
const LOCAL_BIND_IP: IpAddr = IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0));
const SERVER_PORT: u16 = 6000;
// Defines the amount of time that should elapse between each physics step.
const TIME_STEP: f32 = 1.0 / 60.0;
// These constants are defined in `Transform` units.
// Using the default 2D camera they correspond 1:1 with screen pixels.
const PADDLE_SIZE: Vec3 = Vec3::new(120.0, 20.0, 0.0);
const GAP_BETWEEN_PADDLE_AND_FLOOR: f32 = 60.0;
const PADDLE_SPEED: f32 = 500.0;
// How close can the paddle get to the wall
const PADDLE_PADDING: f32 = 10.0;
const BALL_DIAMETER: f32 = 30.;
const BALL_SIZE: Vec3 = Vec3::new(BALL_DIAMETER, BALL_DIAMETER, 0.0);
const BALL_SPEED: f32 = 400.0;
const WALL_THICKNESS: f32 = 10.0;
// x coordinates
const LEFT_WALL: f32 = -450.;
const RIGHT_WALL: f32 = 450.;
// y coordinates
const BOTTOM_WALL: f32 = -300.;
const TOP_WALL: f32 = 300.;
const BRICK_SIZE: Vec2 = Vec2::new(100., 30.);
// These values are exact
const GAP_BETWEEN_PADDLE_AND_BRICKS: f32 = 140.0;
const GAP_BETWEEN_BRICKS: f32 = 5.0;
// These values are lower bounds, as the number of bricks is computed
const GAP_BETWEEN_BRICKS_AND_SIDES: f32 = 20.0;
#[derive(Default, Clone, Eq, PartialEq, Debug, Hash, States)]
enum GameState {
#[default]
MainMenu,
HostingLobby,
JoiningLobby,
Running,
}
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
pub enum GameSystems {
HostSystems,
ClientSystems,
}
#[derive(Component, Deref, DerefMut)]
struct Velocity(Vec2);
#[derive(Default, Event)]
struct CollisionEvent;
#[derive(Component)]
struct Score;
#[derive(Resource)]
struct CollisionSound(Handle<AudioSource>);
pub type BrickId = u64;
/// Which side of the arena is this wall located on?
enum WallLocation {
Left,
Right,
Bottom,
Top,
}
impl WallLocation {
fn position(&self) -> Vec2 {
match self {
WallLocation::Left => Vec2::new(LEFT_WALL, 0.),
WallLocation::Right => Vec2::new(RIGHT_WALL, 0.),
WallLocation::Bottom => Vec2::new(0., BOTTOM_WALL),
WallLocation::Top => Vec2::new(0., TOP_WALL),
}
}
fn size(&self) -> Vec2 {
let arena_height = TOP_WALL - BOTTOM_WALL;
let arena_width = RIGHT_WALL - LEFT_WALL;
// Make sure we haven't messed up our constants
assert!(arena_height > 0.0);
assert!(arena_width > 0.0);
match self {
WallLocation::Left | WallLocation::Right => {
Vec2::new(WALL_THICKNESS, arena_height + WALL_THICKNESS)
}
WallLocation::Bottom | WallLocation::Top => {
Vec2::new(arena_width + WALL_THICKNESS, WALL_THICKNESS)
}
}
}
}
fn server_is_listening(server: Res<QuinnetServer>) -> bool {
server.is_listening()
}
fn main() {
let mut app = App::new();
app.add_plugins((
DefaultPlugins,
QuinnetServerPlugin::default(),
QuinnetClientPlugin::default(),
));
app.add_event::<CollisionEvent>();
app.init_state::<GameState>();
app.insert_resource(ClearColor(BACKGROUND_COLOR))
.insert_resource(server::Players::default())
.insert_resource(client::Scoreboard { score: 0 })
.insert_resource(client::ClientData::default())
.insert_resource(client::NetworkMapping::default())
.insert_resource(client::BricksMapping::default());
// ------ Main menu
app.add_systems(Update, close_on_esc)
.add_systems(OnEnter(GameState::MainMenu), client::setup_main_menu)
.add_systems(
Update,
client::handle_menu_buttons.run_if(in_state(GameState::MainMenu)),
)
.add_systems(OnExit(GameState::MainMenu), client::teardown_main_menu);
// ------ Hosting a server on a client
app.add_systems(
OnEnter(GameState::HostingLobby),
(server::start_listening, client::start_connection),
)
.add_systems(
Update,
(
server::handle_client_messages,
server::handle_server_events,
client::handle_server_messages,
)
.run_if(in_state(GameState::HostingLobby)),
);
// ------ or just Joining as a client
app.add_systems(OnEnter(GameState::JoiningLobby), client::start_connection)
.add_systems(
Update,
client::handle_server_messages.run_if(in_state(GameState::JoiningLobby)),
);
// ------ Running the game.
// ------ Every app is a client
app.add_systems(OnEnter(GameState::Running), client::setup_breakout);
app.edit_schedule(FixedUpdate, |schedule| {
schedule.configure_sets(GameSystems::ClientSystems.run_if(in_state(GameState::Running)));
schedule.add_systems(
(
client::handle_server_messages.before(client::apply_velocity),
client::apply_velocity,
client::move_paddle,
client::update_scoreboard,
client::play_collision_sound,
)
.in_set(GameSystems::ClientSystems),
);
});
// ------ But hosting apps are also a server
app.edit_schedule(FixedUpdate, |schedule| {
schedule.configure_sets(
GameSystems::HostSystems
.run_if(in_state(GameState::Running))
.run_if(server_is_listening),
);
schedule.add_systems(
(
server::handle_client_messages.before(server::update_paddles),
server::update_paddles.before(server::check_for_collisions),
server::apply_velocity.before(server::check_for_collisions),
server::check_for_collisions,
)
.in_set(GameSystems::HostSystems),
);
});
app.run();
}
pub fn close_on_esc(
mut commands: Commands,
focused_windows: Query<(Entity, &Window)>,
input: Res<ButtonInput<KeyCode>>,
) {
for (window, focus) in focused_windows.iter() {
if !focus.focused {
continue;
}
if input.just_pressed(KeyCode::Escape) {
commands.entity(window).despawn();
}
}
}