|
1 |
| -use nesbox_utils::{bevy_app::*, bevy_ecs::prelude::*, input::*, prelude::*, render::*}; |
| 1 | +use nesbox_utils::{input::*, prelude::*, render::*}; |
2 | 2 |
|
3 | 3 | pub const RENDER_WIDTH: u32 = 256;
|
4 | 4 | pub const RENDER_HEIGHT: u32 = 240;
|
5 | 5 | pub const RENDER_PIXELS: usize = (RENDER_WIDTH * RENDER_HEIGHT) as usize;
|
6 | 6 | pub const RENDER_SIZE: usize = 4 * RENDER_PIXELS;
|
7 | 7 |
|
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.; |
15 | 9 |
|
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; |
22 | 12 |
|
23 |
| -#[derive(Component)] |
| 13 | +#[derive(Component, Debug, Default)] |
24 | 14 | struct Cursor;
|
25 | 15 |
|
26 | 16 | 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 | + }); |
46 | 47 | commands.spawn((
|
| 48 | + SpatialBundle { |
| 49 | + transform: Transform::from_xyz(0., 0., 0.), |
| 50 | + ..default() |
| 51 | + }, |
47 | 52 | Cursor,
|
48 |
| - Position { x: -2, y: -2 }, |
49 | 53 | Size {
|
50 |
| - width: 4, |
51 |
| - height: 4, |
| 54 | + width: 4., |
| 55 | + height: 4., |
52 | 56 | },
|
53 |
| - Color(0x00, 0x00, 0x00, 0xff), |
| 57 | + Color(0xff, 0x00, 0x00, 0x66), |
54 | 58 | ));
|
55 | 59 | }
|
56 | 60 |
|
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(); |
69 | 81 | color.0 = random();
|
70 | 82 | color.1 = random();
|
71 | 83 | color.2 = random();
|
72 | 84 | }
|
73 | 85 | }
|
74 | 86 | }
|
75 | 87 |
|
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>>) { |
77 | 89 | let mut velocity = query.single_mut();
|
78 | 90 |
|
79 | 91 | if input.just_pressed(Button::Joypad1Left) {
|
80 |
| - velocity.x = velocity.x.abs() * -1; |
| 92 | + velocity.x = velocity.x.abs() * -1.; |
81 | 93 | }
|
82 | 94 |
|
83 | 95 | if input.just_pressed(Button::Joypad1Right) {
|
84 | 96 | velocity.x = velocity.x.abs();
|
85 | 97 | }
|
86 | 98 |
|
87 | 99 | if input.just_pressed(Button::Joypad1Up) {
|
88 |
| - velocity.y = velocity.y.abs() * -1; |
| 100 | + velocity.y = velocity.y.abs() * -1.; |
89 | 101 | }
|
90 | 102 |
|
91 | 103 | if input.just_pressed(Button::Joypad1Down) {
|
92 | 104 | velocity.y = velocity.y.abs();
|
93 | 105 | }
|
94 | 106 | }
|
95 | 107 |
|
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; |
100 | 112 | }
|
101 | 113 | }
|
102 | 114 |
|
103 | 115 | fn mouse_motion(
|
104 | 116 | mut mouse_evt: EventReader<MouseEvent>,
|
105 |
| - mut query: Query<(&Cursor, &mut Position)>, |
| 117 | + mut query: Query<(&Cursor, &mut Transform)>, |
106 | 118 | ) {
|
107 | 119 | 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.; |
111 | 123 | }
|
112 | 124 | }
|
113 | 125 | }
|
114 | 126 |
|
115 | 127 | 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)); |
117 | 129 |
|
118 | 130 | app.add_startup_system(setup)
|
119 | 131 | .add_system(bounce)
|
| 132 | + .add_system(movement.after(bounce)) |
120 | 133 | .add_system(mouse_motion)
|
121 |
| - .add_system(handle) |
122 |
| - .add_system(movement.after(bounce)); |
| 134 | + .add_system(handle); |
123 | 135 |
|
124 | 136 | app
|
125 | 137 | }
|
0 commit comments