1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//! Hides the contents of the screen with an animation.
//! Affected by [`GameConfig::fade_duration`](../config/struct.GameConfig.html#structfield.fade_duration).

use amethyst::{
    core::Time,
    ecs::{
        Component,
        Entity,
        HashMapStorage,
        Read,
        ReadExpect,
        SystemData,
        world::Builder,
        World,
        WorldExt,
        WriteStorage,
    },
    renderer::SpriteRender,
    ui::{Anchor, UiImage, UiTransform},
};

use crate::{
    common::CommonResources,
    config::GameConfig,
};

use super::{BoxedGameEvent, ExecutionConditions, GameEvent};

/// A marker component to allow easy retrieval of the fade-related entities.
pub struct Fade {
    pub id: u8,
}

impl Component for Fade {
    type Storage = HashMapStorage<Self>;
}

#[derive(Clone, Default)]
pub struct FadeOutEvent {
    top_fade: Option<Entity>,
    bottom_fade: Option<Entity>,
    elapsed_time: f32,
    completed: bool,
}

impl GameEvent for FadeOutEvent {
    fn boxed_clone(&self) -> BoxedGameEvent {
        Box::new(self.clone())
    }

    fn get_execution_conditions(&self) -> ExecutionConditions {
        ExecutionConditions {
            requires_disabled_input: true,
        }
    }

    fn start(&mut self, world: &mut World) {
        self.top_fade = Some(
            initialise_fade_entity(world, Anchor::TopMiddle, Anchor::TopMiddle, 0)
        );

        self.bottom_fade = Some(
            initialise_fade_entity(world, Anchor::BottomMiddle, Anchor::BottomMiddle, 1)
        );
    }

    fn tick(&mut self, world: &mut World, _disabled_inputs: bool) {
        let (mut ui_transforms, game_config, time) = <(
            WriteStorage<UiTransform>,
            ReadExpect<GameConfig>,
            Read<Time>,
        )>::fetch(world);

        let fade_duration = game_config.fade_duration;

        self.elapsed_time += time.delta_seconds();

        ui_transforms
            .get_mut(*self.top_fade.as_mut().unwrap())
            .expect("Failed to retrieve UiTransform")
            .height = 300. * (self.elapsed_time / fade_duration);

        ui_transforms
            .get_mut(*self.bottom_fade.as_mut().unwrap())
            .expect("Failed to retrieve UiTransform")
            .height = 300. * (self.elapsed_time / fade_duration);

        self.completed = self.elapsed_time >= fade_duration;
    }

    fn is_complete(&self, _world: &mut World) -> bool {
        self.completed
    }
}

fn initialise_fade_entity(world: &mut World, anchor: Anchor, pivot: Anchor, id: u8) -> Entity {
    let sprite_render = SpriteRender {
        sprite_sheet: world.read_resource::<CommonResources>().black.clone(),
        sprite_number: 0,
    };

    let ui_transform = UiTransform::new(
        "Fade".to_string(), anchor, pivot,
        0., 0., 2., 800., 0.
    );

    world.register::<Fade>();

    world
        .create_entity()
        .with(Fade { id })
        .with(UiImage::Sprite(sprite_render))
        .with(ui_transform)
        .build()
}