This commit is contained in:
@@ -1,20 +1,20 @@
|
||||
use bevy::{
|
||||
ecs::system::EntityCommands, math::{Quat, Vec3}, prelude::*
|
||||
};
|
||||
use bevy::{ecs::system::EntityCommands, prelude::*};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::prefab_defination::AnimationComponent;
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct ComponentDefination {
|
||||
pub struct ComponentDefination
|
||||
{
|
||||
pub path: String,
|
||||
pub animations: Vec<AnimationComponent>,
|
||||
}
|
||||
|
||||
|
||||
impl ComponentDefination {
|
||||
pub fn apply(&self, commands: &mut EntityCommands){
|
||||
impl ComponentDefination
|
||||
{
|
||||
pub fn apply(&self, commands: &mut EntityCommands)
|
||||
{
|
||||
for c in &self.animations {
|
||||
c.apply(commands);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
use bevy::reflect::Reflect;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub mod animation_plugin;
|
||||
pub mod building;
|
||||
pub mod component_defination;
|
||||
pub mod coords;
|
||||
pub mod despawn;
|
||||
pub mod events;
|
||||
pub mod identifiers;
|
||||
pub mod prefab_defination;
|
||||
pub mod resources;
|
||||
pub mod sets;
|
||||
pub mod states;
|
||||
pub mod tags;
|
||||
// pub mod prefab_defination;
|
||||
// pub mod animation_plugin;
|
||||
// pub mod component_defination;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub enum Tier
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
use bevy::{
|
||||
ecs::system::{EntityCommand, EntityCommands},
|
||||
ecs::{relationship::RelatedSpawnerCommands, system::EntityCommands},
|
||||
gltf::{Gltf, GltfMesh},
|
||||
math::{Quat, Vec3},
|
||||
prelude::*,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct PrefabDefination {
|
||||
pub struct PrefabDefination
|
||||
{
|
||||
pub path: String,
|
||||
pub pos: Vec3,
|
||||
pub rot: Vec3,
|
||||
@@ -14,38 +15,54 @@ pub struct PrefabDefination {
|
||||
pub animations: Option<Vec<AnimationComponent>>,
|
||||
}
|
||||
|
||||
impl PrefabDefination {
|
||||
pub fn spawn_recursive(&self, gltf: &Gltf, commands: &mut ChildBuilder, meshes: &Assets<GltfMesh>) {
|
||||
impl PrefabDefination
|
||||
{
|
||||
pub fn spawn_recursive(
|
||||
&self,
|
||||
gltf: &Gltf,
|
||||
commands: &mut RelatedSpawnerCommands<ChildOf>,
|
||||
meshes: &Assets<GltfMesh>,
|
||||
)
|
||||
{
|
||||
let mesh_handle = &gltf.named_meshes[&self.path.clone().into_boxed_str()];
|
||||
if let Some(gltf_mesh) = meshes.get(mesh_handle.id()) {
|
||||
let (m, mat) = gltf_mesh.unpack();
|
||||
let mut entity = commands.spawn((
|
||||
Mesh3d(m),
|
||||
MeshMaterial3d(mat),
|
||||
Transform::from_translation(self.pos).with_rotation(Quat::from_euler(
|
||||
bevy::math::EulerRot::XYZ,
|
||||
self.rot.x,
|
||||
self.rot.y,
|
||||
self.rot.z,
|
||||
)),
|
||||
));
|
||||
if let Some(children) = &self.children {
|
||||
entity.with_children(|b| {
|
||||
for child in children {
|
||||
child.spawn_recursive(gltf, b, meshes);
|
||||
}
|
||||
});
|
||||
if let Some(primitive) = gltf_mesh.primitives.first() {
|
||||
let mesh = primitive.mesh.clone();
|
||||
let mat = primitive
|
||||
.material
|
||||
.clone()
|
||||
.expect(format!("Mesh '{}' does not have a meterial", primitive.name.as_str()).as_str());
|
||||
let mut entity = commands.spawn((
|
||||
Mesh3d(mesh),
|
||||
MeshMaterial3d(mat),
|
||||
Transform::from_translation(self.pos).with_rotation(Quat::from_euler(
|
||||
bevy::math::EulerRot::XYZ,
|
||||
self.rot.x,
|
||||
self.rot.y,
|
||||
self.rot.z,
|
||||
)),
|
||||
));
|
||||
if let Some(children) = &self.children {
|
||||
entity.with_children(|b| {
|
||||
for child in children {
|
||||
child.spawn_recursive(gltf, b, meshes);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait UnpackGltfMesh {
|
||||
pub trait UnpackGltfMesh
|
||||
{
|
||||
fn unpack(&self) -> (Handle<Mesh>, Handle<StandardMaterial>);
|
||||
}
|
||||
|
||||
impl UnpackGltfMesh for GltfMesh {
|
||||
fn unpack(&self) -> (Handle<Mesh>, Handle<StandardMaterial>) {
|
||||
impl UnpackGltfMesh for GltfMesh
|
||||
{
|
||||
fn unpack(&self) -> (Handle<Mesh>, Handle<StandardMaterial>)
|
||||
{
|
||||
let p = &self.primitives[0];
|
||||
let mut mat: Handle<StandardMaterial> = default();
|
||||
if let Some(mesh_material) = &p.material {
|
||||
@@ -56,19 +73,23 @@ impl UnpackGltfMesh for GltfMesh {
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub enum AnimationComponent {
|
||||
pub enum AnimationComponent
|
||||
{
|
||||
Rotation(RotationAnimation),
|
||||
Slider,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Component, Clone, Copy)]
|
||||
pub struct RotationAnimation {
|
||||
pub struct RotationAnimation
|
||||
{
|
||||
pub axis: Vec3,
|
||||
pub speed: f32,
|
||||
}
|
||||
|
||||
impl AnimationComponent {
|
||||
pub fn apply(&self, commands: &mut EntityCommands) {
|
||||
impl AnimationComponent
|
||||
{
|
||||
pub fn apply(&self, commands: &mut EntityCommands)
|
||||
{
|
||||
match self {
|
||||
AnimationComponent::Rotation(comp) => {
|
||||
commands.insert(comp.clone());
|
||||
|
||||
Reference in New Issue
Block a user