This commit is contained in:
Edgar 2023-05-05 19:56:20 +02:00
parent 3a582c8ec4
commit c72946963a
No known key found for this signature in database
GPG key ID: 70ADAE8F35904387
13 changed files with 249 additions and 9 deletions

33
.cargo/config.toml Normal file
View file

@ -0,0 +1,33 @@
# Add the contents of this file to `config.toml` to enable "fast build" configuration. Please read the notes below.
# NOTE: For maximum performance, build using a nightly compiler
# If you are using rust stable, remove the "-Zshare-generics=y" below.
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-Clink-arg=-fuse-ld=mold", "-Zshare-generics=y"]
# NOTE: you must install [Mach-O LLD Port](https://lld.llvm.org/MachO/index.html) on mac. you can easily do this by installing llvm which includes lld with the "brew" package manager:
# `brew install llvm`
[target.x86_64-apple-darwin]
rustflags = [
"-C",
"link-arg=-fuse-ld=/usr/local/opt/llvm/bin/ld64.lld",
"-Zshare-generics=y",
]
[target.aarch64-apple-darwin]
rustflags = [
"-C",
"link-arg=-fuse-ld=/opt/homebrew/opt/llvm/bin/ld64.lld",
"-Zshare-generics=y",
]
[target.x86_64-pc-windows-msvc]
linker = "rust-lld.exe"
rustflags = ["-Zshare-generics=n"]
# Optional: Uncommenting the following improves compile times, but reduces the amount of debug info to 'line number tables only'
# In most cases the gains are negligible, but if you are on macos and have slow compile times you should see significant gains.
#[profile.dev]
#debug = 1

BIN
assets/emoticons.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

BIN
assets/game.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 144 KiB

BIN
assets/generic_clear.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 KiB

BIN
assets/particles.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

BIN
assets/skins/default.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

View file

@ -1 +0,0 @@
use bevy::prelude::*;

View file

@ -1,19 +1,27 @@
use bevy::prelude::*; use bevy::prelude::*;
use bevy_inspector_egui::quick::WorldInspectorPlugin; use bevy_inspector_egui::quick::WorldInspectorPlugin;
use physics::Velocity;
pub mod entity; pub mod physics;
pub mod player; pub mod player;
pub mod tee;
fn main() { fn main() {
App::new() App::new()
.register_type::<Velocity>()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_plugin(WorldInspectorPlugin::new()) .add_plugin(WorldInspectorPlugin::new())
.add_startup_system(general_setup)
.add_startup_system(player::add_player) .add_startup_system(player::add_player)
.add_system(player::player_input.before(physics::move_system))
.add_system(player::player_mouse.before(physics::move_system))
.add_system(physics::move_system)
.run(); .run();
} }
fn print_position_system(query: Query<&Transform>) { #[derive(Component)]
for transform in &query { pub struct MainCamera;
println!("position: {:?}", transform.translation);
} fn general_setup(mut commands: Commands, server: Res<AssetServer>) {
commands.spawn((Camera2dBundle::default(), MainCamera));
} }

27
src/physics.rs Normal file
View file

@ -0,0 +1,27 @@
use bevy::prelude::*;
#[derive(Debug, Reflect, Component, Default, Clone)]
pub struct Velocity {
pub vel: Vec2,
pub speed: f32,
}
impl Velocity {
pub fn new(speed: f32) -> Self {
Self {
speed,
..Default::default()
}
}
}
#[derive(Component)]
pub struct BoundingBox {
pub rect: Rect,
}
pub fn move_system(mut query: Query<(&Velocity, &mut Transform)>, time: Res<Time>) {
for (vel, mut transform) in query.iter_mut() {
transform.translation += vel.vel.extend(0.0) * time.delta_seconds();
}
}

View file

@ -1,8 +1,70 @@
use bevy::prelude::*; use bevy::{math::Vec3Swizzles, prelude::*};
use crate::{
physics::Velocity,
tee::{TeeBundle, TeeBundleChildren},
MainCamera,
};
#[derive(Component)] #[derive(Component)]
pub struct Player; pub struct Player;
pub fn add_player(mut commands: Commands) { pub fn add_player(mut commands: Commands, server: Res<AssetServer>) {
commands.spawn((Player, Name::new("Ryo"), Transform::default())); let handle: Handle<Image> = server.load("skins/default.png");
let tee_bundle = TeeBundle::new("Player", Vec3::new(32.0, 32.0, 0.0));
let tee_bundle_children = TeeBundleChildren::new(handle);
commands
.spawn((Player, tee_bundle, Velocity::new(100.0)))
.with_children(|parent| {
parent.spawn(tee_bundle_children.body);
parent.spawn(tee_bundle_children.left_foot);
parent.spawn(tee_bundle_children.right_foot);
});
}
pub fn player_input(
keys: Res<Input<KeyCode>>,
mut query_player: Query<&mut Velocity, With<Player>>,
) {
let mut dir = Vec2::ZERO;
if keys.pressed(KeyCode::W) {
dir.y += 1.0;
}
if keys.pressed(KeyCode::S) {
dir.y -= 1.0;
}
if keys.pressed(KeyCode::D) {
dir.x += 1.0;
}
if keys.pressed(KeyCode::A) {
dir.x -= 1.0;
}
let mut v = query_player.single_mut();
v.vel = dir.normalize_or_zero() * v.speed;
}
pub fn player_mouse(
mut cursor_evr: EventReader<CursorMoved>,
mut query_player: Query<&mut Transform, With<Player>>,
query_camera: Query<(&Camera, &GlobalTransform), With<MainCamera>>,
) {
let (camera, camera_transform) = query_camera.single();
for ev in cursor_evr.iter() {
let real_pos = camera.viewport_to_world_2d(camera_transform, ev.position);
if let Some(real_pos) = real_pos {
let mut transform = query_player.single_mut();
let translation = transform.translation.xy();
let vector = (real_pos - translation).normalize();
let rotate_to_mouse = Quat::from_rotation_arc(Vec3::Y, vector.extend(0.0));
transform.rotation = rotate_to_mouse;
}
}
} }

111
src/tee.rs Normal file
View file

@ -0,0 +1,111 @@
use std::borrow::Cow;
use bevy::prelude::*;
#[derive(Component)]
pub struct Tee;
#[derive(Component)]
pub struct Body;
#[derive(Component)]
pub struct LeftFoot;
#[derive(Component)]
pub struct RightFoot;
#[derive(Bundle)]
pub struct TeeBundle {
pub tee: Tee,
pub name: Name,
pub spatial_bundle: SpatialBundle,
}
#[derive(Bundle)]
pub struct TeePartBundle<T: Component> {
pub sprite: SpriteBundle,
pub marker: T,
pub name: Name,
}
pub struct TeeBundleChildren {
pub body: TeePartBundle<Body>,
pub left_foot: TeePartBundle<LeftFoot>,
pub right_foot: TeePartBundle<RightFoot>,
}
impl TeeBundle {
pub fn new(name: impl Into<Cow<'static, str>>, translation: Vec3) -> Self {
Self {
tee: Tee,
name: Name::new(name),
spatial_bundle: SpatialBundle {
transform: Transform::from_translation(translation),
..Default::default()
},
}
}
}
impl TeeBundleChildren {
pub fn new(handle: Handle<Image>) -> Self {
Self {
body: TeePartBundle {
sprite: SpriteBundle {
texture: handle.clone(),
sprite: Sprite {
rect: Some(Rect::from_corners(Vec2::ZERO, Vec2::new(96.0, 96.0))),
..Default::default()
},
transform: Transform::from_xyz(0.0, 0.0, 0.5),
..default()
},
marker: Body,
name: Name::new("Body"),
},
left_foot: TeePartBundle {
sprite: SpriteBundle {
texture: handle.clone(),
sprite: Sprite {
rect: Some(Rect::new(96.0 * 2.0, 32.0, 96.0 * 2.0 + 64.0, 32.0 + 32.0)),
..Default::default()
},
transform: Transform::from_xyz(-28.0, -5.0, 0.0)
.with_rotation(Quat::from_rotation_z(1.7)),
..default()
},
marker: LeftFoot,
name: Name::new("LeftFoot"),
},
right_foot: TeePartBundle {
sprite: SpriteBundle {
texture: handle,
sprite: Sprite {
rect: Some(Rect::new(96.0 * 2.0, 32.0, 96.0 * 2.0 + 64.0, 32.0 + 32.0)),
..Default::default()
},
transform: Transform::from_xyz(28.0, -5.0, 0.0)
.with_scale(Vec3::new(-1.0, 1.0, 1.0))
.with_rotation(Quat::from_rotation_z(-1.7)),
..default()
},
marker: RightFoot,
name: Name::new("RightFoot"),
},
}
}
}
/*
pub fn add_tee(commands: Commands, handle: Handle<Image>, translation: Vec3, name: &str) {
TeeBundle {
tee: Tee,
name: Name::new(name.to_string()),
spatial_bundle: SpatialBundle {
transform: Transform::from_translation(translation),
..Default::default()
},
}
.spawn(commands, handle);
}
*/