teecity/src/player.rs

99 lines
2.7 KiB
Rust
Raw Normal View History

2023-05-06 07:55:09 +00:00
use bevy::{math::Vec3Swizzles, prelude::*, window::PrimaryWindow, winit::WinitWindows};
2023-05-05 17:56:20 +00:00
use crate::{
2023-05-06 07:55:09 +00:00
misc::AimTarget,
2023-05-05 17:56:20 +00:00
physics::Velocity,
tee::{TeeBundle, TeeBundleChildren},
MainCamera,
};
2023-05-04 17:41:55 +00:00
#[derive(Component)]
pub struct Player;
2023-05-05 17:56:20 +00:00
pub fn add_player(mut commands: Commands, server: Res<AssetServer>) {
2023-05-06 08:26:16 +00:00
let skin_handle: Handle<Image> = server.load("skins/default.png");
let game_handle: Handle<Image> = server.load("game.png");
2023-05-05 17:56:20 +00:00
2023-05-08 15:00:23 +00:00
let tee_bundle = TeeBundle::new("Player", Vec3::new(32.0, 32.0, 1.0));
2023-05-05 17:56:20 +00:00
2023-05-08 15:00:23 +00:00
let tee_bundle_children = TeeBundleChildren::new(skin_handle, game_handle, 1.0);
2023-05-05 17:56:20 +00:00
commands
2023-05-06 07:55:09 +00:00
.spawn((
Player,
tee_bundle,
Velocity::new(400.0),
AimTarget::default(),
))
2023-05-05 17:56:20 +00:00
.with_children(|parent| {
2023-05-06 08:26:16 +00:00
parent.spawn(tee_bundle_children.weapon);
2023-05-05 17:56:20 +00:00
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>,
2023-05-06 07:55:09 +00:00
mut query_player: Query<&mut AimTarget, With<Player>>,
2023-05-05 17:56:20 +00:00
query_camera: Query<(&Camera, &GlobalTransform), With<MainCamera>>,
) {
let (camera, camera_transform) = query_camera.single();
2023-05-06 07:55:09 +00:00
let mut target = query_player.single_mut();
2023-05-05 17:56:20 +00:00
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 {
2023-05-06 07:55:09 +00:00
target.0 = Some(real_pos);
2023-05-05 17:56:20 +00:00
}
}
2023-05-04 17:41:55 +00:00
}
2023-06-29 08:56:38 +00:00
pub fn player_camera(
mut query_player: Query<(&mut AimTarget, &Transform), (With<Player>, Without<MainCamera>)>,
mut query_camera: Query<&mut Transform, With<MainCamera>>,
) {
let mut camera_transform = query_camera.single_mut();
let (mut aim_target, player_transform) = query_player.single_mut();
let old = camera_transform.translation;
camera_transform.translation = Vec3::new(
player_transform.translation.x,
player_transform.translation.y,
camera_transform.translation.z,
);
// Keep the aim same when camera is moving
let diff = camera_transform.translation - old;
if diff.length() > 0.0 {
if let Some(t) = aim_target.0.as_mut() {
*t += diff.xy();
}
}
}