Skip to content

Commit 6ce9260

Browse files
committed
Applied trait usages
1 parent 56a52b4 commit 6ce9260

File tree

2 files changed

+68
-19
lines changed

2 files changed

+68
-19
lines changed

game-engine/src/framework.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#[allow(dead_code)]
2+
pub trait Actor {
3+
fn draw(&self);
4+
fn update(&mut self);
5+
}
6+
7+
#[derive(Default)]
8+
pub struct Game {
9+
// width: f32,
10+
// height: f32,
11+
size: Size,
12+
actors: Vec<Box<dyn Actor>>,
13+
}
14+
15+
impl Game {
16+
// pub fn new() -> Self {
17+
// Game {
18+
// actors: Vec::new(),
19+
// size:Size::default()
20+
// }
21+
// }
22+
pub fn add_actor(&mut self, actor: Box<dyn Actor>) {
23+
self.actors.push(actor);
24+
}
25+
pub fn draw(&self) {
26+
for actor in &self.actors {
27+
actor.draw();
28+
}
29+
}
30+
pub fn update(&mut self) {
31+
for actor in &mut self.actors{
32+
actor.update();
33+
}
34+
}
35+
}
36+
37+
// #[derive(Default)]
38+
#[allow(dead_code)]
39+
pub struct Size {
40+
width: f32,
41+
height: f32,
42+
}
43+
44+
impl Default for Size {
45+
fn default() -> Self {
46+
Size {
47+
width: 1280_f32,
48+
height: 960_f32,
49+
}
50+
}
51+
}

game-engine/src/main.rs

+17-19
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1+
use std::{thread::sleep, time::Duration};
2+
3+
use framework::*;
4+
5+
mod framework;
6+
17
fn main() {
2-
let mut game = Game::new();
8+
// let mut game = Game::new();
9+
let mut game = Game::default();
310
let super_mario = Player::new(1, "Super Mario".to_string());
411
let mushroom = Mushroom::new(2, 10);
512
game.add_actor(Box::new(super_mario));
613
game.add_actor(Box::new(mushroom));
14+
15+
loop {
16+
sleep(Duration::from_secs(2));
17+
game.draw();
18+
game.update();
19+
// state check
20+
}
721
}
822

23+
#[allow(dead_code)]
924
struct Player {
1025
id: u32,
1126
name: String,
@@ -25,6 +40,7 @@ impl Actor for Player {
2540
}
2641
}
2742

43+
#[allow(dead_code)]
2844
struct Mushroom {
2945
id: u32,
3046
strength: u8,
@@ -44,21 +60,3 @@ impl Actor for Mushroom {
4460
println!("Calculation strength");
4561
}
4662
}
47-
48-
trait Actor {
49-
fn draw(&self);
50-
fn update(&mut self);
51-
}
52-
53-
struct Game {
54-
actors: Vec<Box<dyn Actor>>,
55-
}
56-
57-
impl Game {
58-
pub fn new() -> Self {
59-
Game { actors: Vec::new() }
60-
}
61-
pub fn add_actor(&mut self, actor: Box<dyn Actor>) {
62-
self.actors.push(actor);
63-
}
64-
}

0 commit comments

Comments
 (0)