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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use amethyst::{
    assets::{Loader, ProgressCounter},
    audio::output::init_output,
    core::{ArcThreadPool, math::Vector3, Parent, Transform},
    ecs::{
        Dispatcher,
        DispatcherBuilder,
        Entity,
        world::Builder,
        World,
        WorldExt,
    },
    prelude::*,
    renderer::Camera,
    ui::TtfFormat,
};

use crate::{
    audio::initialise_audio,
    common::{AssetTracker, CommonResources, load_full_texture_sprite_sheet},
    config::GameConfig,
    entities::character::{initialise_player, PlayerEntity},
    events::EventQueue,
    map::{initialise_map, MapCoordinates},
    states::OverworldState,
    systems::AudioSystem,
};

use std::ops::Deref;

pub fn initialise_camera(world: &mut World, player: Entity) {
    let mut transform = Transform::default();
    transform.set_translation_xyz(0., 0., 1.0);
    transform.set_scale(Vector3::new(0.5, 0.5, 0.5));

    world
        .create_entity()
        .with(Camera::standard_2d(800., 600.))
        .with(Parent { entity: player })
        .with(transform)
        .build();
}

pub fn initialise_resources(world: &mut World, progress_counter: &mut ProgressCounter) {
    let font = world.read_resource::<Loader>().load(
        "fonts/arial.ttf",
        TtfFormat,
        &mut *progress_counter,
        &world.read_resource(),
    );

    let text_box = load_full_texture_sprite_sheet(
        world,
        "sprites/text_box.png",
        &(800, 100),
        &mut *progress_counter,
    );

    let black = load_full_texture_sprite_sheet(
        world,
        "sprites/black.png",
        &(32, 32),
        &mut *progress_counter,
    );

    world.insert(CommonResources { font, text_box, black });
}

#[derive(Default)]
pub struct LoadingState<'a, 'b> {
    pub dispatcher: Option<Dispatcher<'a, 'b>>,
    pub cached_num_loaded_assets: usize,
}

impl SimpleState for LoadingState<'_, '_> {
    fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) {
        println!("Loading game...");

        let world = data.world;

        let mut dispatcher = DispatcherBuilder::new()
            .with(AudioSystem::default(), "audio_system", &[])
            .with_pool(world.read_resource::<ArcThreadPool>().deref().clone())
            .build();

        dispatcher.setup(world);
        self.dispatcher = Some(dispatcher);

        world.insert(EventQueue::default());

        init_output(world);
        initialise_audio(world);

        let (starting_map, starting_position) = {
            let game_config = world.read_resource::<GameConfig>();

            (
                game_config.player_starting_map.clone(),
                MapCoordinates::from_tuple(&game_config.player_starting_position),
            )
        };

        let mut progress_counter = ProgressCounter::new();

        initialise_resources(world, &mut progress_counter);
        initialise_map(world, &starting_map, &mut progress_counter);

        let player = initialise_player(
            world,
            &starting_map,
            starting_position,
            &mut progress_counter,
        );
        initialise_camera(world, player);

        world.insert(AssetTracker::new(progress_counter));
        world.insert(PlayerEntity(player));
    }

    fn update(&mut self, data: &mut StateData<'_, GameData<'_, '_>>) -> SimpleTrans {
        if let Some(dispatcher) = &mut self.dispatcher {
            dispatcher.dispatch(data.world);
        }

        let asset_tracker = data.world.read_resource::<AssetTracker>();
        let progress_counter = asset_tracker.get_progress_counter();
        let num_finished = progress_counter.num_finished();

        if num_finished != self.cached_num_loaded_assets {
            self.cached_num_loaded_assets = num_finished;

            let total = progress_counter.num_assets();
            let percentage = 100 * num_finished / total;
            println!("Loading... {}% ({}/{})", percentage, num_finished, total);
        }

        if progress_counter.is_complete() {
            Trans::Switch(Box::new(OverworldState::default()))
        } else {
            Trans::None
        }
    }
}