diff --git a/src/player.rs b/src/player.rs index a69714e..bce30f3 100644 --- a/src/player.rs +++ b/src/player.rs @@ -11,11 +11,12 @@ use crate::{ pub struct Player; pub fn add_player(mut commands: Commands, server: Res) { - let handle: Handle = server.load("skins/default.png"); + let skin_handle: Handle = server.load("skins/default.png"); + let game_handle: Handle = server.load("game.png"); let tee_bundle = TeeBundle::new("Player", Vec3::new(32.0, 32.0, 0.0)); - let tee_bundle_children = TeeBundleChildren::new(handle); + let tee_bundle_children = TeeBundleChildren::new(skin_handle, game_handle); commands .spawn(( @@ -25,6 +26,7 @@ pub fn add_player(mut commands: Commands, server: Res) { AimTarget::default(), )) .with_children(|parent| { + parent.spawn(tee_bundle_children.weapon); parent.spawn(tee_bundle_children.body); parent.spawn(tee_bundle_children.left_foot); parent.spawn(tee_bundle_children.right_foot); diff --git a/src/tee.rs b/src/tee.rs index 09be3d4..e940028 100644 --- a/src/tee.rs +++ b/src/tee.rs @@ -1,4 +1,4 @@ -use std::borrow::Cow; +use std::{borrow::Cow, default}; use bevy::prelude::*; @@ -14,6 +14,16 @@ pub struct LeftFoot; #[derive(Component)] pub struct RightFoot; +#[derive(Component, Default)] +pub enum Weapon { + Gun, + #[default] + Grenade, + Katana, + Laser, + Shotgun, +} + #[derive(Bundle)] pub struct TeeBundle { pub tee: Tee, @@ -29,6 +39,7 @@ pub struct TeePartBundle { } pub struct TeeBundleChildren { + pub weapon: TeePartBundle, pub body: TeePartBundle, pub left_foot: TeePartBundle, pub right_foot: TeePartBundle, @@ -48,11 +59,24 @@ impl TeeBundle { } impl TeeBundleChildren { - pub fn new(handle: Handle) -> Self { + pub fn new(skin_handle: Handle, game_handle: Handle) -> Self { Self { + weapon: TeePartBundle { + sprite: SpriteBundle { + texture: game_handle, + sprite: Sprite { + rect: Some(Weapon::default().get_texture_rect()), + ..Default::default() + }, + transform: Weapon::default().get_transform(), + ..default() + }, + marker: Weapon::default(), + name: Name::new("Weapon"), + }, body: TeePartBundle { sprite: SpriteBundle { - texture: handle.clone(), + texture: skin_handle.clone(), sprite: Sprite { rect: Some(Rect::from_corners(Vec2::ZERO, Vec2::new(96.0, 96.0))), ..Default::default() @@ -65,7 +89,7 @@ impl TeeBundleChildren { }, left_foot: TeePartBundle { sprite: SpriteBundle { - texture: handle.clone(), + texture: skin_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() @@ -79,7 +103,7 @@ impl TeeBundleChildren { }, right_foot: TeePartBundle { sprite: SpriteBundle { - texture: handle, + texture: skin_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() @@ -96,6 +120,50 @@ impl TeeBundleChildren { } } +impl Weapon { + pub fn get_texture_rect(&self) -> Rect { + match self { + Weapon::Gun => Rect::new(64.0, 32.0 + 96.0, 64.0 + 128.0, 32.0 + 96.0 + 64.0), + Weapon::Shotgun => Rect::new( + 64.0, + 32.0 + 96.0 + 64.0, + 64.0 + 256.0, + 32.0 + 96.0 + 64.0 + 64.0, + ), + Weapon::Grenade => Rect::new( + 64.0, + 32.0 + 96.0 + 64.0 * 2.0, + 64.0 + 256.0, + 32.0 + 96.0 + 64.0 * 2.0 + 64.0, + ), + Weapon::Katana => Rect::new( + 64.0 + 32.0, + 32.0 + 96.0 + 64.0 * 3.0, + 64.0 + 256.0, + 32.0 + 96.0 + 64.0 * 3.0 + 64.0, + ), + Weapon::Laser => Rect::new( + 64.0, + 32.0 + 96.0 + 64.0 * 4.0, + 64.0 + 224.0, + 32.0 + 96.0 + 64.0 * 4.0 + 96.0, + ), + } + } + + pub fn get_transform(&self) -> Transform { + match self { + Weapon::Katana => Transform::from_xyz(11.5, 50.0, 0.25) + .with_scale(Vec3::splat(0.5)) + .with_rotation(Quat::from_rotation_z(1.8)), + Weapon::Gun => Transform::from_xyz(2.0, 38.0, 0.25), + _ => Transform::from_xyz(2.0, 60.0, 0.25) + .with_scale(Vec3::splat(0.5)) + .with_rotation(Quat::from_rotation_z(std::f32::consts::FRAC_PI_2)), + } + } +} + /* pub fn add_tee(commands: Commands, handle: Handle, translation: Vec3, name: &str) { TeeBundle {