Skip to content

Commit b6a4e40

Browse files
committed
Integrated bevy transform
1 parent b2f4bf8 commit b6a4e40

File tree

13 files changed

+1476
-222
lines changed

13 files changed

+1476
-222
lines changed

Cargo.lock

+1,187-49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-5
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,3 @@ members = [
77
]
88

99
exclude = ["deps"]
10-
11-
[profile.release.package.nes]
12-
# Tell `rustc` to optimize for small code size.
13-
opt-level = "s"
14-
debug = true

games/gomoku/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ crate-type = ["cdylib", "rlib"]
1111
nesbox-utils = { path = "../../packages/nes/utils" }
1212

1313
# fixed ecs macro
14-
bevy_ecs = "0.10"
14+
bevy = { version = "0.10", default-features = false }

games/gomoku/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Development
44

55
```bash
6-
cargo watch -s "wasm-pack build --target web --out-name=index"
6+
cargo watch -s "wasm-pack build --target web --out-name=index --debug"
77
```
88

99
## Serve

games/gomoku/src/game.rs

+76-64
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,137 @@
1-
use nesbox_utils::{bevy_app::*, bevy_ecs::prelude::*, input::*, prelude::*, render::*};
1+
use nesbox_utils::{input::*, prelude::*, render::*};
22

33
pub const RENDER_WIDTH: u32 = 256;
44
pub const RENDER_HEIGHT: u32 = 240;
55
pub const RENDER_PIXELS: usize = (RENDER_WIDTH * RENDER_HEIGHT) as usize;
66
pub const RENDER_SIZE: usize = 4 * RENDER_PIXELS;
77

8-
#[derive(Bundle, Debug)]
9-
struct ObjectBundle {
10-
position: Position,
11-
velocity: Velocity,
12-
size: Size,
13-
color: Color,
14-
}
8+
const OBJ_SIZE: f32 = 64.;
159

16-
#[derive(Bundle, Debug)]
17-
struct BgBundle {
18-
position: Position,
19-
size: Size,
20-
color: Color,
21-
}
10+
#[derive(Component, Debug, Default)]
11+
struct Player;
2212

23-
#[derive(Component)]
13+
#[derive(Component, Debug, Default)]
2414
struct Cursor;
2515

2616
fn setup(mut commands: Commands) {
27-
let bg = BgBundle {
28-
position: Position { x: 0, y: 0 },
29-
size: Size {
30-
width: RENDER_WIDTH,
31-
height: RENDER_HEIGHT,
32-
},
33-
color: Color(0x48, 0xb2, 0xe8, 0xff),
34-
};
35-
commands.spawn(bg);
36-
let box_object = ObjectBundle {
37-
position: Position { x: 24, y: 16 },
38-
velocity: Velocity { x: 1, y: 1 },
39-
size: Size {
40-
width: 64,
41-
height: 64,
42-
},
43-
color: Color(0x5e, 0x48, 0xe8, 0xff),
44-
};
45-
commands.spawn(box_object);
17+
commands
18+
.spawn((
19+
Player,
20+
Velocity { x: 1., y: 1. },
21+
SpatialBundle {
22+
transform: Transform::from_xyz(1., 1., 0.),
23+
..default()
24+
},
25+
))
26+
.with_children(|parent| {
27+
for i in 0..2 {
28+
for j in 0..2 {
29+
parent.spawn((
30+
SpatialBundle {
31+
transform: Transform::from_xyz(
32+
i as f32 * OBJ_SIZE / 2.,
33+
j as f32 * OBJ_SIZE / 2.,
34+
0.,
35+
),
36+
..default()
37+
},
38+
Size {
39+
width: OBJ_SIZE / 2.,
40+
height: OBJ_SIZE / 2.,
41+
},
42+
Color(random(), random(), random(), 0xff),
43+
));
44+
}
45+
}
46+
});
4647
commands.spawn((
48+
SpatialBundle {
49+
transform: Transform::from_xyz(0., 0., 0.),
50+
..default()
51+
},
4752
Cursor,
48-
Position { x: -2, y: -2 },
4953
Size {
50-
width: 4,
51-
height: 4,
54+
width: 4.,
55+
height: 4.,
5256
},
53-
Color(0x00, 0x00, 0x00, 0xff),
57+
Color(0xff, 0x00, 0x00, 0x66),
5458
));
5559
}
5660

57-
fn bounce(mut query: Query<(&Position, &mut Velocity, &Size, &mut Color)>) {
58-
for (position, mut velocity, size, mut color) in query.iter_mut() {
59-
let mut bounce = false;
60-
if position.x == 0 || position.x + size.width as i32 > RENDER_WIDTH as i32 {
61-
velocity.x *= -1;
62-
bounce = true;
63-
}
64-
if position.y == 0 || position.y + size.height as i32 > RENDER_HEIGHT as i32 {
65-
velocity.y *= -1;
66-
bounce = true;
67-
}
68-
if bounce {
61+
fn bounce(
62+
mut query: Query<(&Transform, &mut Velocity, &Children), With<Player>>,
63+
mut q_child: Query<&mut Color>,
64+
) {
65+
let mut bounce = false;
66+
let (transform, mut velocity, children) = query.single_mut();
67+
let Vec3 { x, y, .. } = transform.translation;
68+
69+
if x < 0. || x + OBJ_SIZE > RENDER_WIDTH as f32 {
70+
velocity.x *= -1.;
71+
bounce = true;
72+
}
73+
if y < 0. || y + OBJ_SIZE > RENDER_HEIGHT as f32 {
74+
velocity.y *= -1.;
75+
bounce = true;
76+
}
77+
78+
if bounce {
79+
for &child in children.iter() {
80+
let mut color = q_child.get_mut(child).unwrap();
6981
color.0 = random();
7082
color.1 = random();
7183
color.2 = random();
7284
}
7385
}
7486
}
7587

76-
fn handle(input: Res<Input<Button>>, mut query: Query<&mut Velocity>) {
88+
fn handle(input: Res<Input<Button>>, mut query: Query<&mut Velocity, With<Player>>) {
7789
let mut velocity = query.single_mut();
7890

7991
if input.just_pressed(Button::Joypad1Left) {
80-
velocity.x = velocity.x.abs() * -1;
92+
velocity.x = velocity.x.abs() * -1.;
8193
}
8294

8395
if input.just_pressed(Button::Joypad1Right) {
8496
velocity.x = velocity.x.abs();
8597
}
8698

8799
if input.just_pressed(Button::Joypad1Up) {
88-
velocity.y = velocity.y.abs() * -1;
100+
velocity.y = velocity.y.abs() * -1.;
89101
}
90102

91103
if input.just_pressed(Button::Joypad1Down) {
92104
velocity.y = velocity.y.abs();
93105
}
94106
}
95107

96-
fn movement(mut query: Query<(&mut Position, &Velocity)>) {
97-
for (mut position, velocity) in query.iter_mut() {
98-
position.x = position.x + velocity.x;
99-
position.y = position.y + velocity.y;
108+
fn movement(mut query: Query<(&mut Transform, &Velocity), With<Player>>) {
109+
for (mut transform, velocity) in query.iter_mut() {
110+
transform.translation.x += velocity.x;
111+
transform.translation.y += velocity.y;
100112
}
101113
}
102114

103115
fn mouse_motion(
104116
mut mouse_evt: EventReader<MouseEvent>,
105-
mut query: Query<(&Cursor, &mut Position)>,
117+
mut query: Query<(&Cursor, &mut Transform)>,
106118
) {
107119
if let Some(event) = mouse_evt.iter().last() {
108-
if let Some((_, mut position)) = query.iter_mut().last() {
109-
position.x = event.delta.x as i32 - 2;
110-
position.y = event.delta.y as i32 - 2;
120+
if let Some((_, mut transform)) = query.iter_mut().last() {
121+
transform.translation.x = event.delta.x - 2.;
122+
transform.translation.y = event.delta.y - 2.;
111123
}
112124
}
113125
}
114126

115127
pub fn create_app() -> App {
116-
let mut app = create_bevy_app(RENDER_WIDTH, RENDER_HEIGHT);
128+
let mut app = create_bevy_app(RENDER_WIDTH, RENDER_HEIGHT, Color(0x48, 0xb2, 0xe8, 0xff));
117129

118130
app.add_startup_system(setup)
119131
.add_system(bounce)
132+
.add_system(movement.after(bounce))
120133
.add_system(mouse_motion)
121-
.add_system(handle)
122-
.add_system(movement.after(bounce));
134+
.add_system(handle);
123135

124136
app
125137
}

games/gomoku/src/lib.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use game::*;
2-
use nesbox_utils::{
3-
bevy_app::App, bevy_ecs::prelude::*, bevy_ecs::system::*, bevy_math::Vec2, input::*,
4-
prelude::*, render::*,
5-
};
2+
use nesbox_utils::{bevy::ecs::system::*, input::*, prelude::*};
63

74
mod game;
85

packages/nes/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ git submodule update
1212

1313
```bash
1414
# development
15-
cargo watch -s "wasm-pack build --out-dir ../nes-pkg --target web --scope mantou"
15+
cargo watch -s "wasm-pack build --out-dir ../nes-pkg --target web --scope mantou --debug"
1616
# build
1717
wasm-pack build --out-dir ../nes-pkg --target web --scope mantou
1818
```

packages/nes/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ impl Nes {
231231
}
232232
}
233233

234-
/// 2k(0u16..=0x07FF) ram + 8K(0x6000u16..=0x7FFF) sram
235234
pub fn ram(&mut self) -> Vec<u8> {
235+
// 2k(0u16..=0x07FF) ram + 8K(0x6000u16..=0x7FFF) sram
236236
let mut ram = Vec::new();
237237
for addr in 0u16..=0x07FF {
238238
ram.push(self.control_deck.cpu().bus.peek(addr));

packages/nes/utils/Cargo.toml

+1-13
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,9 @@ edition = "2021"
77
[lib]
88
crate-type = ["cdylib", "rlib"]
99

10-
[features]
11-
default = ["console_error_panic_hook"]
12-
1310
[dependencies]
1411
qoi = "0.4.0"
15-
bevy_ecs = "0.10"
16-
bevy_app = "0.10"
17-
bevy_input = "0.10"
18-
bevy_math = "0.10"
12+
bevy = { version = "0.10", default-features = false }
1913
bincode = "1.3.3"
2014
rand = "0.8"
2115
wasm-bindgen = "0.2.63"
22-
23-
# The `console_error_panic_hook` crate provides better debugging of panics by
24-
# logging them with `console.error`. This is great for development, but requires
25-
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
26-
# code size when deploying.
27-
console_error_panic_hook = { version = "0.1.6", optional = true }

packages/nes/utils/src/input.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
pub use bevy_input::*;
1+
pub use bevy::input::*;
22

3-
use bevy_math::Vec2;
3+
use bevy::prelude::*;
44
use wasm_bindgen::prelude::*;
55

66
#[wasm_bindgen]

packages/nes/utils/src/lib.rs

+24-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
pub use bevy_app;
2-
pub use bevy_ecs;
3-
pub use bevy_math;
4-
51
pub mod prelude {
62
pub use crate::codes::*;
73
pub use crate::create_bevy_app;
84
pub use crate::log;
5+
pub use crate::pixels::*;
6+
pub use bevy::prelude::*;
97
pub use rand::prelude::random;
108
pub use wasm_bindgen;
119
pub use wasm_bindgen::prelude::*;
1210
}
1311

14-
use bevy_app::App;
12+
use bevy::{prelude::*, time::TimePlugin};
1513
use input::{Button, Input, MouseEvent};
14+
use pixels::Color;
1615
use render::RenderPlugin;
1716

17+
pub use bevy;
1818
pub use wasm_bindgen;
1919
pub use wasm_bindgen::prelude::*;
2020
pub mod codes;
2121
pub mod input;
22+
pub mod pixels;
2223
pub mod render;
2324

2425
#[wasm_bindgen]
@@ -34,23 +35,33 @@ macro_rules! log {
3435
}};
3536
}
3637

37-
pub fn create_bevy_app(width: u32, height: u32) -> App {
38+
pub fn create_bevy_app(width: u32, height: u32, clear_color: Color) -> App {
3839
let mut app = App::new();
3940

41+
#[cfg(debug_assertions)]
42+
app.add_plugin(bevy::log::LogPlugin::default());
43+
4044
// TODO: audio
41-
app.add_plugin(RenderPlugin { width, height })
42-
.add_event::<MouseEvent>()
43-
.init_resource::<Input<Button>>();
45+
app.add_plugin(RenderPlugin {
46+
width,
47+
height,
48+
clear_color,
49+
})
50+
.add_plugin(TaskPoolPlugin::default())
51+
.add_plugin(TypeRegistrationPlugin::default())
52+
.add_plugin(FrameCountPlugin::default())
53+
.add_plugin(TimePlugin::default())
54+
.add_plugin(TransformPlugin::default())
55+
.add_plugin(HierarchyPlugin::default())
56+
.add_event::<MouseEvent>()
57+
.init_resource::<Input<Button>>();
4458

4559
app
4660
}
4761

4862
#[wasm_bindgen(start)]
4963
pub fn run() {
50-
#[cfg(debug_assertions)]
51-
console_error_panic_hook::set_once();
52-
log!("WASM import console#log");
53-
let mut app = create_bevy_app(1, 1);
64+
let mut app = create_bevy_app(1, 1, Color::default());
5465
app.update();
5566
log!("WASM import bevy deps");
5667
}

0 commit comments

Comments
 (0)