SHARE
TWEET

PlayerPrefab

a guest Jan 19th, 2020 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use crate::{components::character::Player, data::animation::AnimationId};
  2. use amethyst::{
  3.     animation::AnimationSetPrefab,
  4.     assets::{PrefabData, ProgressCounter},
  5.     ecs::{Entity, WriteStorage},
  6.     renderer::sprite::{prefab::SpriteScenePrefab, SpriteRender},
  7.     Error,
  8. };
  9. use log::info;
  10. use serde::Deserialize;
  11.  
  12. #[derive(Debug, Clone, Deserialize)]
  13. #[serde(rename = "Player")]
  14. pub struct PlayerPrefab {
  15.     sprite_scene: SpriteScenePrefab,
  16.     animation_set: AnimationSetPrefab<AnimationId, SpriteRender>,
  17. }
  18.  
  19. impl<'a> PrefabData<'a> for PlayerPrefab {
  20.     type SystemData = (
  21.         <SpriteScenePrefab as PrefabData<'a>>::SystemData,
  22.        <AnimationSetPrefab<AnimationId, SpriteRender> as PrefabData<'a>>::SystemData,
  23.         WriteStorage<'a, Player>,
  24.    );
  25.    type Result = ();
  26.  
  27.    fn add_to_entity(
  28.        &self,
  29.        entity: Entity,
  30.        (sprite_scene_data, animation_set_data, players): &mut Self::SystemData,
  31.        entities: &[Entity],
  32.        children: &[Entity],
  33.    ) -> Result<Self::Result, Error> {
  34.        info!("Player adding to entity {}", entity.id());
  35.  
  36.        self.sprite_scene
  37.            .add_to_entity(entity, sprite_scene_data, entities, children)?;
  38.        self.animation_set
  39.            .add_to_entity(entity, animation_set_data, entities, children)?;
  40.  
  41.        players.insert(entity, Player)?;
  42.  
  43.        info!("Player successfully added to entity {}", entity.id());
  44.  
  45.        Ok(())
  46.    }
  47.  
  48.    fn load_sub_assets(
  49.        &mut self,
  50.        progress: &mut ProgressCounter,
  51.        (sprite_scene_data, animation_set_data, _): &mut Self::SystemData,
  52.    ) -> Result<bool, Error> {
  53.        info!("Loading sub assets for player prefab!");
  54.  
  55.        self.sprite_scene
  56.            .load_sub_assets(progress, sprite_scene_data)
  57.            .and_then(|scene_res| {
  58.                self.animation_set
  59.                    .load_sub_assets(progress, animation_set_data)
  60.                    .map(|anim_res| scene_res && anim_res)
  61.            })
  62.    }
  63. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top