You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
242 lines
7.9 KiB
242 lines
7.9 KiB
6 years ago
|
use amethyst::{
|
||
|
assets::{AssetStorage, Loader},
|
||
|
core::transform::Transform,
|
||
5 years ago
|
core::math::Vector3,
|
||
6 years ago
|
input::{get_key, is_close_requested, is_key_down, VirtualKeyCode},
|
||
|
prelude::*,
|
||
|
renderer::{Camera, ImageFormat, SpriteRender, SpriteSheet, SpriteSheetFormat, Texture},
|
||
|
window::ScreenDimensions,
|
||
5 years ago
|
ecs::prelude::{Dispatcher, DispatcherBuilder, Component, DenseVecStorage, Entity},
|
||
6 years ago
|
};
|
||
|
|
||
|
use log::info;
|
||
5 years ago
|
use crate::components::*;
|
||
|
use std::collections::HashMap;
|
||
5 years ago
|
use crate::systems::{BirbGravity, ScrollScrollables};
|
||
5 years ago
|
use crate::ready_state::ReadyState;
|
||
6 years ago
|
|
||
5 years ago
|
|
||
5 years ago
|
#[derive(Default)]
|
||
|
pub struct SplashState {
|
||
5 years ago
|
sprites: Vec<Entity>,
|
||
5 years ago
|
persistent_sprites: Vec<Entity>,
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
impl SplashState {
|
||
5 years ago
|
|
||
5 years ago
|
fn load_sprites(world: &mut World) -> HashMap<String, SpriteRender> {
|
||
|
// Load the texture for our sprites. We'll later need to
|
||
|
// add a handle to this texture to our `SpriteRender`s, so
|
||
|
// we need to keep a reference to it.
|
||
|
let texture_handle = {
|
||
|
let loader = world.read_resource::<Loader>();
|
||
|
let texture_storage = world.read_resource::<AssetStorage<Texture>>();
|
||
|
loader.load(
|
||
|
"sprites/flappy.png",
|
||
|
ImageFormat::default(),
|
||
|
(),
|
||
|
&texture_storage,
|
||
|
)
|
||
|
};
|
||
5 years ago
|
|
||
5 years ago
|
// Load the spritesheet definition file, which contains metadata on our
|
||
|
// spritesheet texture.
|
||
|
let sheet_handle = {
|
||
|
let loader = world.read_resource::<Loader>();
|
||
|
let sheet_storage = world.read_resource::<AssetStorage<SpriteSheet>>();
|
||
|
loader.load(
|
||
|
"sprites/flappy.ron",
|
||
|
SpriteSheetFormat(texture_handle),
|
||
|
(),
|
||
|
&sheet_storage,
|
||
|
)
|
||
|
};
|
||
5 years ago
|
|
||
5 years ago
|
let sprite_map = vec![
|
||
|
("day-background".to_string(), 0),
|
||
|
("night-background".to_string(), 1),
|
||
|
("down-pipe".to_string(), 2),
|
||
|
("up-pipe".to_string(), 3),
|
||
|
("ground".to_string(), 4),
|
||
|
("floppy".to_string(), 5),
|
||
|
("tap-tap-dialogue".to_string(), 6),
|
||
|
("play-button".to_string(), 7),
|
||
|
("leaderboard-button".to_string(), 8),
|
||
|
("get-ready-text".to_string(), 9),
|
||
|
("flappy-bird-text".to_string(), 10),
|
||
|
];
|
||
5 years ago
|
|
||
5 years ago
|
sprite_map.iter()
|
||
|
.map(|i| (i.0.clone(), SpriteRender {
|
||
|
sprite_sheet: sheet_handle.clone(),
|
||
|
sprite_number: i.1,
|
||
|
}))
|
||
|
.collect()
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
fn init_camera(world: &mut World) {
|
||
5 years ago
|
|
||
5 years ago
|
let dimensions = (*world.read_resource::<ScreenDimensions>()).clone();
|
||
6 years ago
|
|
||
5 years ago
|
// Center the camera in the middle of the screen, and let it cover
|
||
|
// the entire screen
|
||
|
let mut transform = Transform::default();
|
||
|
transform.set_translation_xyz(dimensions.width() * 0.5, dimensions.height() * 0.5, 1.);
|
||
5 years ago
|
|
||
5 years ago
|
world
|
||
|
.create_entity()
|
||
|
.with(Camera::standard_2d(dimensions.width(), dimensions.height()))
|
||
|
.with(transform)
|
||
|
.build();
|
||
5 years ago
|
}
|
||
6 years ago
|
|
||
5 years ago
|
fn init_sprites(&mut self, world: &mut World) {
|
||
5 years ago
|
|
||
5 years ago
|
let sprites = world.try_fetch_mut::<HashMap<String, SpriteRender>>().unwrap().clone();
|
||
5 years ago
|
|
||
5 years ago
|
let dimensions = (*world.read_resource::<ScreenDimensions>()).clone();
|
||
5 years ago
|
|
||
5 years ago
|
let flappy_bird_text_sprite = sprites
|
||
|
.get("flappy-bird-text").unwrap().clone();
|
||
|
let play_button_sprite = sprites
|
||
|
.get("play-button").unwrap().clone();
|
||
|
let leaderboard_button_sprite = sprites
|
||
|
.get("leaderboard-button").unwrap().clone();
|
||
|
let background_sprite = sprites
|
||
|
.get("day-background").unwrap().clone();
|
||
5 years ago
|
let night_background_sprite = sprites
|
||
|
.get("night-background").unwrap().clone();
|
||
5 years ago
|
let ground_sprite = sprites
|
||
|
.get("ground").unwrap().clone();
|
||
|
|
||
5 years ago
|
|
||
5 years ago
|
let mut transform = Transform::default();
|
||
|
transform.set_scale(Vector3::new(3.0, 3.0, 3.0));
|
||
5 years ago
|
transform.set_translation_xyz(3.0*143.0/2.0, 3.0*256.0/2.0, 0.0);
|
||
5 years ago
|
|
||
5 years ago
|
self.persistent_sprites.push(world
|
||
5 years ago
|
.create_entity()
|
||
|
.with(background_sprite.clone()) // Sprite Render
|
||
5 years ago
|
.with(TiledScroller {
|
||
|
speed: -75.0,
|
||
|
position: 1.0,
|
||
5 years ago
|
width: 143.0 * 3.0,
|
||
5 years ago
|
height: 256.0 * 3.0,
|
||
|
})
|
||
5 years ago
|
.with(transform.clone())
|
||
|
.build());
|
||
5 years ago
|
|
||
5 years ago
|
transform.set_translation_xyz(3.0*143.0/2.0*3.0, 3.0*256.0/2.0, 0.0);
|
||
5 years ago
|
|
||
5 years ago
|
self.persistent_sprites.push(world
|
||
5 years ago
|
.create_entity()
|
||
|
.with(background_sprite.clone()) // Sprite Render
|
||
|
.with(TiledScroller {
|
||
|
speed: -75.0,
|
||
|
position: 2.0,
|
||
5 years ago
|
width: 143.0 * 3.0,
|
||
5 years ago
|
height: 256.0 * 3.0,
|
||
|
})
|
||
|
.with(transform.clone())
|
||
|
.build());
|
||
|
|
||
|
transform.set_translation_xyz(3.0*168.0/2.0, 3.0*56.0/2.0, 0.1);
|
||
|
|
||
|
self.persistent_sprites.push(world
|
||
|
.create_entity()
|
||
|
.with(ground_sprite.clone()) // Sprite Render
|
||
|
.with(TiledScroller {
|
||
|
speed: -100.0,
|
||
|
position: 2.0,
|
||
5 years ago
|
width: 167.0 * 3.0,
|
||
5 years ago
|
height: 56.0 * 3.0,
|
||
5 years ago
|
})
|
||
|
.with(transform.clone())
|
||
|
.build());
|
||
|
|
||
|
transform.set_translation_xyz(3.0*168.0/2.0*3.0, 3.0*56.0/2.0, 0.1);
|
||
|
|
||
5 years ago
|
self.persistent_sprites.push(world
|
||
5 years ago
|
.create_entity()
|
||
|
.with(ground_sprite.clone()) // Sprite Render
|
||
|
.with(TiledScroller {
|
||
|
speed: -100.0,
|
||
|
position: 2.0,
|
||
5 years ago
|
width: 167.0 * 3.0,
|
||
5 years ago
|
height: 56.0 * 3.0,
|
||
5 years ago
|
})
|
||
|
.with(transform.clone())
|
||
|
.build());
|
||
|
|
||
5 years ago
|
transform.set_translation_xyz(dimensions.width()*0.5, dimensions.height()*0.8, 0.2);
|
||
5 years ago
|
|
||
|
self.sprites.push(world
|
||
|
.create_entity()
|
||
|
.with(flappy_bird_text_sprite.clone())
|
||
|
.with(transform.clone())
|
||
|
.build());
|
||
|
|
||
5 years ago
|
transform.set_translation_xyz(dimensions.width()*0.25, dimensions.height()*0.4, 0.2);
|
||
5 years ago
|
|
||
|
self.sprites.push(world
|
||
|
.create_entity()
|
||
|
.with(play_button_sprite.clone())
|
||
|
.with(transform.clone())
|
||
|
.build());
|
||
|
|
||
5 years ago
|
transform.set_translation_xyz(dimensions.width()*0.75, dimensions.height()*0.4, 0.2);
|
||
5 years ago
|
|
||
|
self.sprites.push(world
|
||
|
.create_entity()
|
||
|
.with(leaderboard_button_sprite.clone())
|
||
|
.with(transform.clone())
|
||
|
.build());
|
||
|
}
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
impl SimpleState for SplashState {
|
||
5 years ago
|
|
||
|
fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) {
|
||
|
let world = data.world;
|
||
5 years ago
|
|
||
5 years ago
|
// Load the sprites. Insert them into the world as this is the first function to be called
|
||
|
let sprites = SplashState::load_sprites(world);
|
||
|
world.insert(sprites.clone());
|
||
|
|
||
|
SplashState::load_sprites(world);
|
||
|
SplashState::init_camera(world);
|
||
|
SplashState::init_sprites(self, world);
|
||
|
|
||
|
}
|
||
|
|
||
|
fn handle_event(
|
||
|
&mut self,
|
||
|
mut data: StateData<'_, GameData<'_, '_>>,
|
||
|
event: StateEvent,
|
||
|
) -> SimpleTrans {
|
||
|
|
||
|
if let StateEvent::Window(event) = &event {
|
||
|
// Check if the window should be closed
|
||
|
if is_close_requested(&event) || is_key_down(&event, VirtualKeyCode::Escape) {
|
||
|
return Trans::Quit;
|
||
|
}
|
||
|
|
||
|
// Check if the window should be closed
|
||
|
if is_key_down(&event, VirtualKeyCode::Space) {
|
||
|
let world = data.world;
|
||
|
for i in &self.sprites {
|
||
|
world.delete_entity(*i);
|
||
|
}
|
||
|
self.sprites.clear();
|
||
|
|
||
|
return Trans::Push(Box::new(ReadyState::default()));
|
||
|
}
|
||
|
}
|
||
|
Trans::None
|
||
|
}
|
||
|
|
||
|
fn update(&mut self, data: &mut StateData<'_, GameData<'_, '_>>) -> SimpleTrans {
|
||
|
Trans::None
|
||
|
}
|
||
|
}
|