-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
318 lines (296 loc) · 10.9 KB
/
mod.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
use self::{player::*, stage::*};
use crate::{
character::{frame_data::*, state::*},
geo::*,
player::Player,
AppState,
};
use bevy::prelude::State as BevyState;
use bevy::{
math::*,
prelude::*,
render::camera::{Camera, PerspectiveProjection},
tasks::IoTaskPool,
window::Windows,
};
use bevy_backroll::*;
use serde::{Deserialize, Serialize};
use std::ops::Deref;
pub mod backroll;
pub mod events;
pub mod hitbox;
pub mod input;
pub mod physics;
pub mod player;
pub mod rule;
pub mod stage;
pub const MAX_PLAYERS_PER_MATCH: usize = 4;
#[derive(Clone, Default, Deserialize, Serialize)]
pub struct MatchConfig {
pub rule: rule::MatchRule,
/// Optional max time for the match. If not none, the
/// match will prematurely end if time reaches zero.
pub time: Option<u32>,
pub players: [Option<PlayerConfig>; MAX_PLAYERS_PER_MATCH],
}
impl MatchConfig {
pub fn active_player_count(&self) -> usize {
self.players.iter().flatten().count()
}
pub fn validate(&self) -> Result<(), MatchConfigValidationError> {
if self.active_player_count() < 2 {
Err(MatchConfigValidationError::NotEnoughPlayers)
} else {
Ok(())
}
}
}
pub enum MatchConfigValidationError {
NotEnoughPlayers,
}
#[derive(Clone, Debug, Default)]
pub struct MatchState {
/// The number of frames remaining before the end of the match.
/// If None, the match has no set time limit.
pub time_remaining: Option<u32>,
pub players: [Option<Entity>; MAX_PLAYERS_PER_MATCH],
}
#[derive(Debug, Default)]
pub struct MatchResult {
pub winner: rule::MatchWinner,
pub players: [Option<PlayerResult>; MAX_PLAYERS_PER_MATCH],
}
impl MatchResult {
pub fn from_config(config: &MatchConfig) -> Self {
let mut players: [Option<PlayerResult>; MAX_PLAYERS_PER_MATCH] = Default::default();
for (idx, player) in config.players.iter().enumerate() {
if player.is_some() {
players[idx] = Some(PlayerResult::default());
}
}
Self {
winner: rule::MatchWinner::Undecided,
players,
}
}
}
#[derive(Debug, Default)]
pub struct PlayerResult {}
fn init_match(
config: Res<MatchConfig>,
spawn_points: Query<&SpawnPoint>,
task_pool: Res<IoTaskPool>,
mut result: ResMut<MatchResult>,
mut commands: Commands,
) {
assert!(config.validate().is_ok());
// Clear any prior match results
*result = MatchResult::from_config(&config);
let mut state = MatchState {
time_remaining: config.time,
..Default::default()
};
// TODO(jamessliu): This will not work for a new match from a menu.
// Systems need to be properly ordered to ensure that spawn points are added before players
// are spawned.
let mut spawn_points = spawn_points.iter();
let mut builder = backroll::P2PSession::build();
for (id, player_config) in config.players.iter().enumerate() {
state.players[id] = player_config.as_ref().map(|cfg| {
info!("Spawning player {}", id);
let spawn_point = spawn_points
.next()
.expect("Stage does not have enough spawn points");
let transform = Transform::from_translation(Vec3::from((spawn_point.position, 0.0)));
let bundle = player::PlayerBundle {
player: Player { id: id as u8 },
handle: builder.add_player(cfg.player.clone()),
damage: config.rule.create_damage(&cfg),
body: physics::Body {
ecb: physics::EnvironmentCollisionBox(Bounds2D {
center: Vec2::new(0.0, 0.5),
extents: Vec2::new(0.25, 0.5),
}),
location: physics::Location::Airborne(transform.translation.xy()),
gravity: 1.0,
..Default::default()
},
movement: PlayerMovement {
jump_power: vec![2.5, 1.5],
short_jump_power: 0.9,
max_fall_speed: 2.0,
fast_fall_speed: 5.0,
..Default::default()
},
transform,
input_source: cfg.input.clone(),
// pbr: PbrBundle {
// mesh: mesh.clone(),
// material: materials.add(player::get_player_color(id as PlayerId).into()),
// transform,
// ..Default::default()
// },
..Default::default()
};
player::spawn_player(&mut commands, bundle)
});
}
let pool = task_pool.deref().deref().clone();
commands.insert_resource(state);
commands.start_backroll_session(builder.start(pool).unwrap());
}
fn cleanup_match(state: Res<MatchState>, mut commands: Commands) {
// Despawn players
for player in state.players.iter().flatten() {
commands.entity(*player).despawn_recursive();
}
commands.remove_resource::<MatchState>();
}
fn sample_frames(mut players: Query<(&mut CharacterFrame, &mut PlayerState, &StateMachine)>) {
players.for_each_mut(|(mut frame, mut state, state_machine)| {
state.tick();
if let Some(sampled) = state_machine.sample_frame(&state) {
*frame = sampled.clone();
}
});
}
fn update_camera(
mut cameras: Query<(&mut Transform, &Camera, &PerspectiveProjection)>,
players: Query<(&GlobalTransform, &physics::Body), With<Player>>,
windows: Res<Windows>,
) {
let mut total_bounds: Option<Bounds2D> = None;
for (transform, body) in players.iter() {
let mut bounds = Bounds2D::from(body.ecb.clone());
bounds.center += transform.translation.xy();
if let Some(ref mut total) = total_bounds {
total.merge_with(bounds);
} else {
total_bounds = Some(bounds);
}
}
if total_bounds.is_none() {
return;
}
let total_bounds = total_bounds.unwrap();
let (width, height) = total_bounds.size().into();
let center = total_bounds.center;
if height == 0.0 {
return;
}
let rect_aspect = width / height;
cameras.for_each_mut(|(mut transform, camera, projection)| {
if let Some(window) = windows.get(camera.window) {
let (w_width, w_height) = (window.physical_width(), window.physical_height());
if w_height == 0 {
return;
}
let aspect_ratio = (w_width as f32) / (w_height as f32);
// camera distance
let radius = (height - center.y).max(width - center.x);
let camera_distance = &mut transform.translation.z;
*camera_distance = radius / (projection.fov / 2.0).tan();
if rect_aspect > aspect_ratio {
if aspect_ratio > 1.0 {
*camera_distance /= aspect_ratio;
} else {
*camera_distance *= aspect_ratio;
}
} else if width > height {
*camera_distance /= rect_aspect;
}
} else {
warn!("Cannot find associated window: {}", camera.window);
}
transform.translation.x = total_bounds.center.x;
transform.translation.y = total_bounds.center.y;
});
}
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
struct MatchUpdateStage;
pub struct FcMatchPlugin;
impl Plugin for FcMatchPlugin {
fn build(&self, builder: &mut AppBuilder) {
builder
.insert_resource(MatchConfig::default())
.insert_resource(MatchResult::default())
.add_plugin(backroll::FcBackrollPlugin)
.add_system_set(SystemSet::on_enter(AppState::MATCH).with_system(init_match.system()))
.add_system_set(SystemSet::on_exit(AppState::MATCH).with_system(cleanup_match.system()))
.add_system_set(
SystemSet::on_update(AppState::MATCH).with_system(update_camera.system()),
)
.with_rollback_system_set::<backroll::BackrollConfig>(
SystemSet::new()
// Update inputs
.with_system(input::inject_input.system().label("SAMPLE_INPUT"))
// Run physics updates
.with_system(
physics::move_players
.system()
.label("MOVE_PLAYERS")
.after("SAMPLE_INPUT"),
)
.with_system(
physics::update_bodies
.system()
.label("UPDATE_BODIES")
.after("MOVE_PLAYERS"),
)
// Update animations
.with_system(
sample_frames
.system()
.label("SAMPLE_FRAMES")
.after("UPDATE_BODIES"),
)
// Updated hitboxes and players
.with_system(
hitbox::update_hitboxes
.system()
.label("UPDATE_HITBOXES")
.after("SAMPLE_FRAMES"),
)
.with_system(
hitbox::collide_hitboxes
.system()
.label("COLLIDE_HITBOXES")
.after("UPDATE_HITBOXES"),
)
.with_system(
hitbox::hit_players
.system()
.label("HIT_PLAYERS")
.after("COLLIDE_HITBOXES"),
)
.with_system(
stage::kill_players
.system()
.label("KILL_PLAYERS")
.after("HIT_PLAYERS"),
)
// Evaluate the match state
.with_system(
rule::update_match_state
.system()
.label("UPDATE_MATCH_STATE")
.after("KILL_PLAYERS"),
)
.with_system(
rule::on_player_died
.system()
.label("ON_PLAYER_DIED")
.after("UPDATE_MATCH_STATE"),
)
.with_system(
rule::finish_match
.system()
.label("FINISH_MATCH")
.after("ON_PLAYER_DIED"),
),
);
stage::build(builder);
hitbox::build(builder);
events::build(builder);
}
}