Skip to content

Commit 8de0e1f

Browse files
committed
Refactored codes
1 parent 8320b4c commit 8de0e1f

4 files changed

Lines changed: 46 additions & 46 deletions

File tree

Lesson_09/src/main.rs

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1+
mod models;
12
mod repository;
3+
mod systems;
24

5+
use crate::models::*;
6+
use crate::systems::*;
37
use repository::*;
48

59
fn year_sorter(game: &Game) -> u16 {
610
game.year
711
}
812

9-
fn print_games(games: &Vec<Game>) {
10-
for game in games {
11-
println!("{}, {}", game.year, game.title);
12-
}
13-
}
14-
1513
fn main() {
16-
let games = repository::load_games();
17-
1814
let mut games = repository::load_games();
1915

2016
// Yıla göre sıralama klasik fonksiyon ile
21-
// games.sort_by_key(year_sorter);
17+
games.sort_by_key(year_sorter);
2218

2319
// Closure ile artan yıl sıralaması
2420
games.sort_by(|g1, g2| g1.year.cmp(&g2.year));
@@ -53,10 +49,10 @@ fn main() {
5349
],
5450
};
5551

56-
let apply_gravity = |entity: &mut Player| {
57-
entity.position.0 += entity.velocity.0 * 0.9;
58-
entity.position.1 += entity.velocity.1 * 0.9;
59-
};
52+
// let apply_gravity = |entity: &mut Player| {
53+
// entity.position.0 += entity.velocity.0 * 0.9;
54+
// entity.position.1 += entity.velocity.1 * 0.9;
55+
// };
6056

6157
println!("Before Update: {:?}", world.players);
6258
// update_players_system(&mut world, apply_gravity);
@@ -76,34 +72,3 @@ fn main() {
7672
});
7773
println!("Total score after update: {}", total_team_score);
7874
}
79-
80-
#[derive(Debug)]
81-
struct Player {
82-
id: u32,
83-
position: (f32, f32),
84-
velocity: (f32, f32),
85-
score: u32,
86-
}
87-
88-
#[derive(Debug)]
89-
struct GameWorld {
90-
players: Vec<Player>,
91-
}
92-
93-
fn update_players_system<F>(world: &mut GameWorld, mut f: F)
94-
where
95-
F: Fn(&mut Player),
96-
{
97-
for p in &mut world.players {
98-
f(p);
99-
}
100-
}
101-
102-
fn update_score_system<F>(world: &GameWorld, mut f: F)
103-
where
104-
F: FnMut(&Player),
105-
{
106-
for p in &world.players {
107-
f(p);
108-
}
109-
}

Lesson_09/src/models.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#[derive(Debug)]
2+
pub struct Player {
3+
pub id: u32,
4+
pub position: (f32, f32),
5+
pub velocity: (f32, f32),
6+
pub score: u32,
7+
}
8+
9+
#[derive(Debug)]
10+
pub struct GameWorld {
11+
pub players: Vec<Player>,
12+
}

Lesson_09/src/repository.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ pub struct Game {
44
pub year: u16,
55
pub popularity: f32,
66
}
7-
7+
pub fn print_games(games: &Vec<Game>) {
8+
for game in games {
9+
println!("{}, {}", game.year, game.title);
10+
}
11+
}
812
pub fn load_games() -> Vec<Game> {
913
vec![
1014
Game {
@@ -88,4 +92,4 @@ pub fn load_games() -> Vec<Game> {
8892
popularity: 1.98,
8993
},
9094
]
91-
}
95+
}

Lesson_09/src/systems.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use crate::models::*;
2+
3+
pub fn update_players_system<F>(world: &mut GameWorld, mut f: F)
4+
where
5+
F: Fn(&mut Player),
6+
{
7+
for p in &mut world.players {
8+
f(p);
9+
}
10+
}
11+
12+
pub fn update_score_system<F>(world: &GameWorld, mut f: F)
13+
where
14+
F: FnMut(&Player),
15+
{
16+
for p in &world.players {
17+
f(p);
18+
}
19+
}

0 commit comments

Comments
 (0)