steam-train/src/main.rs

138 lines
3.8 KiB
Rust
Raw Permalink Normal View History

2022-01-02 19:18:22 -06:00
use bevy::{
prelude::*,
render::{camera::Camera, render_graph::base::camera::CAMERA_2D},
};
2022-01-02 18:14:47 -06:00
2022-01-02 18:51:09 -06:00
#[derive(Default, Debug)]
2022-01-02 18:14:47 -06:00
struct Player {
entity: Option<Entity>,
2022-01-02 18:51:09 -06:00
x: isize,
y: isize,
2022-01-02 18:14:47 -06:00
}
#[derive(Default)]
struct GameState {
player: Player,
score: u32,
2022-01-02 19:18:22 -06:00
camera_focus: Vec3,
2022-01-02 18:14:47 -06:00
}
2022-01-02 17:34:58 -06:00
fn main() {
2022-01-02 18:14:47 -06:00
App::build()
.init_resource::<GameState>()
.add_plugins(DefaultPlugins)
.add_startup_system(setup.system())
2022-01-02 18:51:09 -06:00
.add_system(movement.system())
2022-01-02 19:08:55 -06:00
.add_system(focus_camera.system())
2022-01-02 18:14:47 -06:00
.run();
2022-01-02 17:34:58 -06:00
}
2022-01-02 18:14:47 -06:00
2022-01-02 18:51:09 -06:00
fn setup(
mut commands: Commands,
mut state: ResMut<GameState>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
2022-01-02 18:14:47 -06:00
state.score = 0;
// assuming that bounds are (900, 600)
2022-01-02 18:51:09 -06:00
state.player.x = 0;
state.player.y = -50;
2022-01-02 18:14:47 -06:00
// cameras
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands.spawn_bundle(UiCameraBundle::default());
2022-01-02 19:14:43 -06:00
// create the player
2022-01-02 18:51:09 -06:00
state.player.entity = Some(
commands
.spawn_bundle(SpriteBundle {
material: materials.add(Color::GOLD.into()),
transform: Transform::from_xyz(0.0, -50.0, 1.0),
sprite: Sprite::new(Vec2::new(30.0, 30.0)),
..Default::default()
})
.id(),
);
2022-01-02 18:14:47 -06:00
// Add walls!
let wall_thickness = 10.0;
let bounds = Vec2::new(900.0, 600.0);
// left
2022-01-02 18:51:09 -06:00
commands.spawn_bundle(SpriteBundle {
transform: Transform::from_xyz(-bounds.x / 2.0, 0.0, 0.0),
sprite: Sprite::new(Vec2::new(wall_thickness, bounds.y + wall_thickness)),
..Default::default()
});
2022-01-02 18:14:47 -06:00
// right
2022-01-02 18:51:09 -06:00
commands.spawn_bundle(SpriteBundle {
transform: Transform::from_xyz(bounds.x / 2.0, 0.0, 0.0),
sprite: Sprite::new(Vec2::new(wall_thickness, bounds.y + wall_thickness)),
..Default::default()
});
2022-01-02 18:14:47 -06:00
// bottom
2022-01-02 18:51:09 -06:00
commands.spawn_bundle(SpriteBundle {
transform: Transform::from_xyz(0.0, -bounds.y / 2.0, 0.0),
sprite: Sprite::new(Vec2::new(bounds.x + wall_thickness, wall_thickness)),
..Default::default()
});
commands.spawn_bundle(SpriteBundle {
transform: Transform::from_xyz(0.0, bounds.y / 2.0, 0.0),
sprite: Sprite::new(Vec2::new(bounds.x + wall_thickness, wall_thickness)),
..Default::default()
});
}
fn movement(
keyboard_input: Res<Input<KeyCode>>,
mut query: Query<&mut Transform>,
mut game: ResMut<GameState>,
) {
let mut moved = false;
if keyboard_input.pressed(KeyCode::Left) {
game.player.x -= 5;
moved = true;
}
if keyboard_input.pressed(KeyCode::Right) {
game.player.x += 5;
moved = true;
}
if keyboard_input.pressed(KeyCode::Up) {
game.player.y += 5;
moved = true;
}
if keyboard_input.pressed(KeyCode::Down) {
game.player.y -= 5;
moved = true;
}
2022-01-02 19:14:43 -06:00
// limit movement of player
2022-01-02 18:58:16 -06:00
game.player.x = game.player.x.min(435).max(-435);
game.player.y = game.player.y.min(285).max(-285);
2022-01-02 18:51:09 -06:00
if moved {
if let Some(entity) = game.player.entity {
*query.get_mut(entity).unwrap() = Transform {
2022-01-02 19:18:22 -06:00
translation: Vec3::new(game.player.x as f32, game.player.y as f32, 0.0),
2022-01-02 18:51:09 -06:00
..Default::default()
};
}
}
}
2022-01-02 19:08:55 -06:00
2022-01-02 19:18:22 -06:00
fn focus_camera(
mut game: ResMut<GameState>,
mut transforms: QuerySet<(Query<(&mut Transform, &Camera)>, Query<&Transform>)>,
) {
2022-01-02 19:08:55 -06:00
if let Some(player_entity) = game.player.entity {
if let Ok(player_transform) = transforms.q1().get(player_entity) {
game.camera_focus = player_transform.translation;
}
}
for (mut transform, camera) in transforms.q0_mut().iter_mut() {
if camera.name == Some(CAMERA_2D.to_string()) {
*transform.translation = *game.camera_focus;
}
}
2022-01-02 19:18:22 -06:00
}