Skip to content

Commit 0935da0

Browse files
committed
refactored the client/server architecture
1 parent a116216 commit 0935da0

File tree

11 files changed

+303
-284
lines changed

11 files changed

+303
-284
lines changed
File renamed without changes.
File renamed without changes.

common/src/types.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use std::collections::HashMap;
33
use crate::animation::{AnimationType, CharacterType, Facing};
44
use bincode::{Decode, Encode};
55

6-
pub type UserNameText = String;
7-
86
/// This is what gets sent to the server AND the client.
97
///
108
/// When clients connect to the server, they send a ClientEventNewClient.
@@ -16,14 +14,11 @@ pub type UserNameText = String;
1614
/// The server then sends ClientServerEvent to all clients.
1715
#[derive(Encode, Decode, PartialEq, Debug)]
1816
pub enum ClientEventType {
19-
ClientCharacterUpdate(HashMap<UserNameText, ClientServerEvent>),
17+
ClientCharacterUpdate(HashMap<u64, ServerClient>),
2018
}
2119

2220
#[derive(Encode, Decode, PartialEq, Debug, Clone)]
23-
pub struct ClientServerEvent {
24-
/// We give this to the server
25-
pub username: String,
26-
21+
pub struct ServerClient {
2722
pub x_pos: f32,
2823
pub y_pos: f32,
2924
pub facing: Facing,

game/src/characters/character.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::rc::Rc;
33
use macroquad::{math::Vec2, texture::Texture2D};
44
use macroquad_platformer::Actor;
55

6-
use common::{animation::{AnimationType, CharacterTextures, CharacterType, Facing}, types::ClientServerEvent};
6+
use common::{animation::{AnimationType, CharacterTextures, CharacterType, Facing}, types::ServerClient};
77

88

99
pub trait CharacterTrait {
@@ -19,7 +19,7 @@ pub trait CharacterTrait {
1919
fn get_velocity(&self) -> Vec2;
2020
}
2121

22-
pub async fn into_client_server_event(username: &str, character: &Box<dyn CharacterTrait>) -> ClientServerEvent {
22+
pub async fn into_client_server_event(character: &Box<dyn CharacterTrait>) -> ServerClient {
2323
let pos = character.get_position();
2424
// let size = character.get_size();
2525

@@ -29,8 +29,7 @@ pub async fn into_client_server_event(username: &str, character: &Box<dyn Charac
2929
let character_type = character.get_character_type();
3030
let sprite_frame = character.get_sprite_frame();
3131

32-
ClientServerEvent {
33-
username: username.to_string(),
32+
ServerClient {
3433
x_pos,
3534
y_pos,
3635
facing: character.get_facing(),

game/src/characters/character_1.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct Character1 {
1919
x_v: f32,
2020
y_v: f32,
2121
facing: Facing,
22-
animation_bank: Character1AnimationBank,
22+
animations: Character1Animations,
2323
state: Rc<RefCell<PlayerAnimationState>>,
2424
actor: Actor,
2525
world: Rc<RefCell<World>>,
@@ -76,7 +76,7 @@ impl CharacterTrait for Character1 {
7676

7777
impl Character1 {
7878
pub async fn new(x: f32, y: f32, width: i32, height: i32, world: Rc<RefCell<World>>) -> Self {
79-
let animation_bank = Character1AnimationBank::load().await;
79+
let animation_bank = Character1Animations::load().await;
8080
let state = animation_bank.idle_anim.clone();
8181
let collider = world
8282
.borrow_mut()
@@ -87,7 +87,7 @@ impl Character1 {
8787
y_v: 0.0,
8888
facing: Facing::Right,
8989
state,
90-
animation_bank,
90+
animations: animation_bank,
9191
actor: collider,
9292
world,
9393
}
@@ -250,50 +250,50 @@ impl Character1 {
250250

251251
match next_animation_state {
252252
AnimationType::Idle => {
253-
self.state = Rc::clone(&self.animation_bank.idle_anim);
253+
self.state = Rc::clone(&self.animations.idle_anim);
254254
}
255255
AnimationType::Crouch => {
256-
self.state = Rc::clone(&self.animation_bank.crouch_anim);
256+
self.state = Rc::clone(&self.animations.crouch_anim);
257257
self.state.borrow_mut().actively_playing = true;
258258
}
259259
AnimationType::ForwardRun => {
260-
self.state = Rc::clone(&self.animation_bank.fwd_run_anim);
260+
self.state = Rc::clone(&self.animations.fwd_run_anim);
261261
}
262262
AnimationType::ReverseRun => {
263-
self.state = Rc::clone(&self.animation_bank.rev_run_anim);
263+
self.state = Rc::clone(&self.animations.rev_run_anim);
264264
}
265265
AnimationType::ForwardWalk => {
266-
self.state = Rc::clone(&self.animation_bank.fwd_walk_anim);
266+
self.state = Rc::clone(&self.animations.fwd_walk_anim);
267267
}
268268
AnimationType::ReverseWalk => {
269-
self.state = Rc::clone(&self.animation_bank.rev_walk_anim);
269+
self.state = Rc::clone(&self.animations.rev_walk_anim);
270270
}
271271
AnimationType::Jump => {
272-
self.state = Rc::clone(&self.animation_bank.jump_anim);
272+
self.state = Rc::clone(&self.animations.jump_anim);
273273
self.state.borrow_mut().actively_playing = true;
274274
}
275275
AnimationType::JumpMoving => {
276-
self.state = Rc::clone(&self.animation_bank.jump_anim_moving);
276+
self.state = Rc::clone(&self.animations.jump_anim_moving);
277277
self.state.borrow_mut().actively_playing = true;
278278
}
279279
AnimationType::Landing => {
280-
self.state = Rc::clone(&self.animation_bank.landing_anim);
280+
self.state = Rc::clone(&self.animations.landing_anim);
281281
self.state.borrow_mut().actively_playing = true;
282282
}
283283
AnimationType::Attack1 => {
284-
self.state = Rc::clone(&self.animation_bank.attack_1_anim);
284+
self.state = Rc::clone(&self.animations.attack_1_anim);
285285
self.state.borrow_mut().actively_playing = true;
286286
}
287287
AnimationType::Attack2 => {
288-
self.state = Rc::clone(&self.animation_bank.attack_2_anim);
288+
self.state = Rc::clone(&self.animations.attack_2_anim);
289289
self.state.borrow_mut().actively_playing = true;
290290
}
291291
AnimationType::Attack3 => {
292-
self.state = Rc::clone(&self.animation_bank.attack_3_anim);
292+
self.state = Rc::clone(&self.animations.attack_3_anim);
293293
self.state.borrow_mut().actively_playing = true;
294294
}
295295
AnimationType::SoaringKick => {
296-
self.state = Rc::clone(&self.animation_bank.soaring_kick_anim);
296+
self.state = Rc::clone(&self.animations.soaring_kick_anim);
297297
self.state.borrow_mut().actively_playing = true;
298298
}
299299
}
@@ -334,7 +334,7 @@ impl Character1 {
334334
}
335335
}
336336

337-
pub struct Character1AnimationBank {
337+
pub struct Character1Animations {
338338
pub idle_anim: Rc<RefCell<PlayerAnimationState>>,
339339
pub crouch_anim: Rc<RefCell<PlayerAnimationState>>,
340340
pub fwd_run_anim: Rc<RefCell<PlayerAnimationState>>,
@@ -350,7 +350,7 @@ pub struct Character1AnimationBank {
350350
pub soaring_kick_anim: Rc<RefCell<PlayerAnimationState>>,
351351
}
352352

353-
impl Character1AnimationBank {
353+
impl Character1Animations {
354354
pub async fn load() -> Self {
355355
let idle_anim = Rc::new(RefCell::new(PlayerAnimationState {
356356
anim_type: AnimationType::Idle,

game/src/characters/character_2.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct Character2 {
1818
x_v: f32,
1919
y_v: f32,
2020
facing: Facing,
21-
animation_bank: Character1AnimationBank,
21+
animations: Character2Animations,
2222
state: Rc<RefCell<PlayerAnimationState>>,
2323
actor: Actor,
2424
world: Rc<RefCell<World>>,
@@ -75,7 +75,7 @@ impl CharacterTrait for Character2 {
7575

7676
impl Character2 {
7777
pub async fn new(x: f32, y: f32, width: i32, height: i32, world: Rc<RefCell<World>>) -> Self {
78-
let animation_bank = Character1AnimationBank::load().await;
78+
let animation_bank = Character2Animations::load().await;
7979
let state = animation_bank.idle_anim.clone();
8080
let collider = world
8181
.borrow_mut()
@@ -86,7 +86,7 @@ impl Character2 {
8686
y_v: 0.0,
8787
facing: Facing::Right,
8888
state,
89-
animation_bank,
89+
animations: animation_bank,
9090
actor: collider,
9191
world,
9292
}
@@ -251,50 +251,50 @@ impl Character2 {
251251

252252
match next_animation_state {
253253
AnimationType::Idle => {
254-
self.state = Rc::clone(&self.animation_bank.idle_anim);
254+
self.state = Rc::clone(&self.animations.idle_anim);
255255
}
256256
AnimationType::Crouch => {
257-
self.state = Rc::clone(&self.animation_bank.crouch_anim);
257+
self.state = Rc::clone(&self.animations.crouch_anim);
258258
self.state.borrow_mut().actively_playing = true;
259259
}
260260
AnimationType::ForwardRun => {
261-
self.state = Rc::clone(&self.animation_bank.fwd_run_anim);
261+
self.state = Rc::clone(&self.animations.fwd_run_anim);
262262
}
263263
AnimationType::ReverseRun => {
264-
self.state = Rc::clone(&self.animation_bank.rev_run_anim);
264+
self.state = Rc::clone(&self.animations.rev_run_anim);
265265
}
266266
AnimationType::ForwardWalk => {
267-
self.state = Rc::clone(&self.animation_bank.fwd_walk_anim);
267+
self.state = Rc::clone(&self.animations.fwd_walk_anim);
268268
}
269269
AnimationType::ReverseWalk => {
270-
self.state = Rc::clone(&self.animation_bank.rev_walk_anim);
270+
self.state = Rc::clone(&self.animations.rev_walk_anim);
271271
}
272272
AnimationType::Jump => {
273-
self.state = Rc::clone(&self.animation_bank.jump_anim);
273+
self.state = Rc::clone(&self.animations.jump_anim);
274274
self.state.borrow_mut().actively_playing = true;
275275
}
276276
AnimationType::JumpMoving => {
277-
self.state = Rc::clone(&self.animation_bank.jump_anim_moving);
277+
self.state = Rc::clone(&self.animations.jump_anim_moving);
278278
self.state.borrow_mut().actively_playing = true;
279279
}
280280
AnimationType::Landing => {
281-
self.state = Rc::clone(&self.animation_bank.landing_anim);
281+
self.state = Rc::clone(&self.animations.landing_anim);
282282
self.state.borrow_mut().actively_playing = true;
283283
}
284284
AnimationType::Attack1 => {
285-
self.state = Rc::clone(&self.animation_bank.attack_1_anim);
285+
self.state = Rc::clone(&self.animations.attack_1_anim);
286286
self.state.borrow_mut().actively_playing = true;
287287
}
288288
AnimationType::Attack2 => {
289-
self.state = Rc::clone(&self.animation_bank.attack_2_anim);
289+
self.state = Rc::clone(&self.animations.attack_2_anim);
290290
self.state.borrow_mut().actively_playing = true;
291291
}
292292
AnimationType::Attack3 => {
293-
self.state = Rc::clone(&self.animation_bank.attack_3_anim);
293+
self.state = Rc::clone(&self.animations.attack_3_anim);
294294
self.state.borrow_mut().actively_playing = true;
295295
}
296296
AnimationType::SoaringKick => {
297-
self.state = Rc::clone(&self.animation_bank.soaring_kick_anim);
297+
self.state = Rc::clone(&self.animations.soaring_kick_anim);
298298
self.state.borrow_mut().actively_playing = true;
299299
}
300300
}
@@ -335,7 +335,7 @@ impl Character2 {
335335
}
336336
}
337337

338-
pub struct Character1AnimationBank {
338+
pub struct Character2Animations {
339339
pub idle_anim: Rc<RefCell<PlayerAnimationState>>,
340340
pub crouch_anim: Rc<RefCell<PlayerAnimationState>>,
341341
pub fwd_run_anim: Rc<RefCell<PlayerAnimationState>>,
@@ -351,7 +351,7 @@ pub struct Character1AnimationBank {
351351
pub soaring_kick_anim: Rc<RefCell<PlayerAnimationState>>,
352352
}
353353

354-
impl Character1AnimationBank {
354+
impl Character2Animations {
355355
pub async fn load() -> Self {
356356
let idle_anim = Rc::new(RefCell::new(PlayerAnimationState {
357357
anim_type: AnimationType::Idle,

0 commit comments

Comments
 (0)