Skip to content

Commit

Permalink
add tree chopping
Browse files Browse the repository at this point in the history
  • Loading branch information
Defernus committed Feb 24, 2023
1 parent 5e19c17 commit c3bbd77
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/plugins/craft/resources/crafts_registry/crafts/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ impl CraftTrait for SimpleCraft {
/// ```
#[macro_export]
macro_rules! simple_craft {
($id:expr, $result:expr, $(($item:ty, $count:expr)),*) => {
($id:expr, $result:ty, $(($item:ty, $count:expr)),*) => {
CraftEntry::from(SimpleCraft {
id: $id,
items: HashMap::<String, usize>::from([
$(
(<$item>::ID.to_string(), $count),
)*
]),
result: Box::new($result),
result: Box::new(<$result>::default()),
})
};
}
60 changes: 59 additions & 1 deletion src/plugins/objects/components/objects/tree.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
use std::f32::consts::PI;

use crate::plugins::{
inspector::components::InspectorDisabled,
loading::resources::{GameAssets, PhysicsObject},
objects::components::{GameWorldObjectTrait, ObjectDeserializationError},
objects::components::{
items::{log::LogItem, stone_axe::StoneAxeItem},
GameWorldObject, GameWorldObjectTrait, ObjectDeserializationError,
},
};
use bevy::prelude::*;
use bevy_reflect::{FromReflect, Reflect};

use super::stump::StumpObject;

const LOGS_PER_TREE: usize = 3;

#[derive(Debug, Clone, Default, Reflect, FromReflect)]
pub struct TreeObject;

Expand All @@ -24,6 +35,53 @@ impl GameWorldObjectTrait for TreeObject {
Box::new(self.clone())
}

fn on_use(
&mut self,
commands: &mut Commands,
_assets: &GameAssets,
self_entity: Entity,
self_transform: Transform,
hand_item: &mut Option<(Entity, Mut<GameWorldObject>)>,
) -> bool {
if let Some((_, item)) = hand_item {
// TODO add more dynamic vay to check tools
if item.0.id() != StoneAxeItem::ID {
return false;
}
} else {
return false;
};

for i in 0..LOGS_PER_TREE {
let offset = Vec3::Y * (1.0 + i as f32 * 1.5);

let x_rot = (rand::random::<f32>() - 0.5) * PI * 0.25;
let y_rot = (rand::random::<f32>() - 0.5) * PI * 0.25;

let rotation = Quat::from_rotation_x(x_rot) * Quat::from_rotation_y(y_rot);

commands.spawn((
LogItem.to_spawner(
self_transform
.with_translation(self_transform.translation + offset)
.with_rotation(rotation),
),
Name::new("tree_harvest_result"),
InspectorDisabled,
));
}

commands.spawn((
StumpObject.to_spawner(self_transform),
Name::new("tree_harvest_result_stump"),
InspectorDisabled,
));

commands.entity(self_entity).despawn_recursive();

true
}

fn deserialize(
&self,
_data: &[u8],
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/player/components/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl PlayerSave {
let vel = player.velocity;

let head_transform = head_transform.compute_transform();
let player_pos = head_transform.translation - Vec3::Y * HEAD_LEVEL;
let player_pos = head_transform.translation + Vec3::Y * HEAD_LEVEL;

let player_rot: Quat = head_transform.rotation;

Expand Down
4 changes: 2 additions & 2 deletions src/plugins/player/systems/spawn_item.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::plugins::{
inspector::components::InspectorDisabled,
objects::components::{items::log::LogItem, GameWorldObjectTrait},
objects::components::{items::stone_axe::StoneAxeItem, GameWorldObjectTrait},
player::{components::PlayerCameraComponent, events::SpawnItemEvent},
};
use bevy::prelude::*;
Expand All @@ -18,7 +18,7 @@ pub fn spawn_item(
let pos = camera_transform.translation + camera_transform.forward() * far;

commands.spawn((
LogItem.to_spawner(Transform::from_translation(pos)),
StoneAxeItem.to_spawner(Transform::from_translation(pos)),
Name::new("player_spawned_item"),
InspectorDisabled,
));
Expand Down

0 comments on commit c3bbd77

Please sign in to comment.