-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
hello_world.rs
121 lines (105 loc) · 3.48 KB
/
hello_world.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use bevy::{core::FixedTimestep, prelude::*};
use bevy_retrograde::prelude::*;
// Create a stage label that will be used for our game logic stage
#[derive(StageLabel, Debug, Eq, Hash, PartialEq, Clone)]
struct GameStage;
fn main() {
App::build()
.insert_resource(WindowDescriptor {
title: "Bevy Retrograde Hello World".into(),
..Default::default()
})
.add_plugins(RetroPlugins)
.add_startup_system(setup.system())
.add_stage(
GameStage,
SystemStage::parallel()
.with_run_criteria(FixedTimestep::step(0.015))
.with_system(move_player.system()),
)
.run();
}
struct Player;
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut scene_graph: ResMut<SceneGraph>,
) {
// Load our sprites
let red_radish_image = asset_server.load("redRadish.png");
let yellow_radish_image = asset_server.load("yellowRadish.png");
let blue_radish_image = asset_server.load("blueRadish.png");
// Spawn the camera
commands.spawn().insert_bundle(CameraBundle {
camera: Camera {
// Set our camera to have a fixed height and an auto-resized width
size: CameraSize::FixedHeight(100),
background_color: Color::new(0.2, 0.2, 0.2, 1.0),
..Default::default()
},
..Default::default()
});
// Spawn a red radish
let red_radish = commands
.spawn()
.insert_bundle(SpriteBundle {
image: red_radish_image,
position: Position::new(0, 0, 0),
..Default::default()
})
// Add our player marker component so we can move it
.insert(Player)
.id();
// Spawn a yellow radish
let yellow_radish = commands
.spawn()
.insert_bundle(SpriteBundle {
image: yellow_radish_image,
position: Position::new(-20, 0, 0),
sprite: Sprite {
// Make him upside down 🙃
flip_y: true,
..Default::default()
},
..Default::default()
})
.id();
// Make the yellow radish a child of the red radish
scene_graph
.add_child(red_radish, yellow_radish)
// This could fail if the child is an ancestor of the parent
.unwrap();
// Spawn a blue radish
commands.spawn().insert_bundle(SpriteBundle {
image: blue_radish_image,
// Set the blue radish back a layer so that he shows up under the other two
position: Position::new(-20, -20, -1),
sprite: Sprite {
flip_x: true,
flip_y: false,
..Default::default()
},
..Default::default()
});
}
fn move_player(keyboard_input: Res<Input<KeyCode>>, mut query: Query<&mut Position, With<Player>>) {
for mut pos in query.iter_mut() {
const SPEED: i32 = 1;
let mut direction = IVec3::new(0, 0, 0);
if keyboard_input.pressed(KeyCode::Left) {
direction += IVec3::new(-SPEED, 0, 0);
}
if keyboard_input.pressed(KeyCode::Right) {
direction += IVec3::new(SPEED, 0, 0);
}
if keyboard_input.pressed(KeyCode::Up) {
direction += IVec3::new(0, -SPEED, 0);
}
if keyboard_input.pressed(KeyCode::Down) {
direction += IVec3::new(0, SPEED, 0);
}
if direction != IVec3::new(0, 0, 0) {
**pos += direction;
}
}
}