Skip to content

Commit 58e498c

Browse files
committed
Used a simple thread
1 parent 6ce9260 commit 58e498c

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ Rust dilinde **isimlendirme standartları _(Naming Conventions)_** da kod okunur
6868
- sysco; Basit bir terminal aracı. Lesson_01 dersinde kullanılan örneğin farklı bir sürümü.
6969
- sysco2; sysco programının daha iyileştirilmiş bir sürümü.
7070
- collector; sysinfo küfesini kullanarak cpu, memory metrikleri toplayan uygulama.
71+
- mocking; Birim testlerde mock nesne kullanımının ele alındığı örnek.
7172
- drone-lab; Konu tekrarı, birim testler, lifetimes kullanımları.
7273
- fops; Temel I/O işlemleri.
7374
- router; trait kullanımını örneklemek için eklenmiş olan uygulama.
75+
- game-engine; trait, thread ve channel kullanımlarının ele alındığı örnektir.

game-engine/src/framework.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1+
pub trait Bot {
2+
fn apply(&self) {
3+
println!("Default AI movements");
4+
}
5+
}
6+
17
#[allow(dead_code)]
28
pub trait Actor {
39
fn draw(&self);
410
fn update(&mut self);
511
}
612

713
#[derive(Default)]
14+
#[allow(dead_code)]
815
pub struct Game {
916
// width: f32,
1017
// height: f32,
1118
size: Size,
1219
actors: Vec<Box<dyn Actor>>,
20+
bots: Vec<Box<dyn Bot>>,
1321
}
1422

1523
impl Game {
@@ -27,10 +35,16 @@ impl Game {
2735
actor.draw();
2836
}
2937
}
38+
pub fn add_bot(&mut self, bot: Box<dyn Bot>) {
39+
self.bots.push(bot);
40+
}
3041
pub fn update(&mut self) {
31-
for actor in &mut self.actors{
42+
for actor in &mut self.actors {
3243
actor.update();
3344
}
45+
for bot in &self.bots {
46+
bot.apply();
47+
}
3448
}
3549
}
3650

game-engine/src/main.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::{thread::sleep, time::Duration};
1+
use std::{
2+
thread::{self, sleep},
3+
time::Duration,
4+
};
25

36
use framework::*;
47

@@ -11,6 +14,8 @@ fn main() {
1114
let mushroom = Mushroom::new(2, 10);
1215
game.add_actor(Box::new(super_mario));
1316
game.add_actor(Box::new(mushroom));
17+
let mega_mind = MindController::default();
18+
game.add_bot(Box::new(mega_mind));
1419

1520
loop {
1621
sleep(Duration::from_secs(2));
@@ -20,6 +25,21 @@ fn main() {
2025
}
2126
}
2227

28+
#[derive(Default)]
29+
pub struct MindController {}
30+
31+
impl Bot for MindController {
32+
fn apply(&self) {
33+
//todo@buraksenyurt Buradaki thread'in bir kere açılmasını garanti et
34+
thread::spawn(|| {
35+
loop {
36+
println!("Applying simulation...");
37+
sleep(Duration::from_secs(5));
38+
}
39+
});
40+
}
41+
}
42+
2343
#[allow(dead_code)]
2444
struct Player {
2545
id: u32,

0 commit comments

Comments
 (0)