mitchellhansen 5 years ago
parent a736bfdbfc
commit 04295af04a

@ -10,7 +10,7 @@ amethyst = "0.15.0"
log = { version = "0.4.8", features = ["serde"] } log = { version = "0.4.8", features = ["serde"] }
[features] [features]
#default = ["vulkan"] default = ["vulkan"]
#empty = ["amethyst/empty"] #empty = ["amethyst/empty"]
#metal = ["amethyst/metal"] #metal = ["amethyst/metal"]
vulkan = ["amethyst/vulkan"] vulkan = ["amethyst/vulkan"]

@ -1,6 +1,9 @@
mod components; mod components;
mod systems; mod systems;
mod state; mod ready_state;
mod play_state;
mod splash_state;
use amethyst::{ use amethyst::{
input::{InputBundle, StringBindings}, input::{InputBundle, StringBindings},
core::transform::TransformBundle, core::transform::TransformBundle,
@ -22,6 +25,7 @@ use crate::components::*;
use crate::systems::*; use crate::systems::*;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use crate::splash_state::SplashState;
fn main() -> amethyst::Result<()> { fn main() -> amethyst::Result<()> {
@ -54,7 +58,7 @@ fn main() -> amethyst::Result<()> {
// Creates the app with the startup state and bound game data // Creates the app with the startup state and bound game data
let mut game = Application::new(resources, state::SplashState::default(), game_data)?; let mut game = Application::new(resources, SplashState::default(), game_data)?;
game.run(); game.run();
Ok(()) Ok(())

@ -0,0 +1,104 @@
use amethyst::{
assets::{AssetStorage, Loader},
core::transform::Transform,
core::math::Vector3,
input::{get_key, is_close_requested, is_key_down, VirtualKeyCode},
prelude::*,
renderer::{Camera, ImageFormat, SpriteRender, SpriteSheet, SpriteSheetFormat, Texture},
window::ScreenDimensions,
ecs::prelude::{Dispatcher, DispatcherBuilder, Component, DenseVecStorage, Entity},
};
use log::info;
use crate::components::*;
use std::collections::HashMap;
use crate::systems::{BirbGravity, ScrollScrollables};
#[derive(Default)]
pub struct PlayState<'a, 'b> {
// Custom dispatch systems for this state
dispatcher: Option<Dispatcher<'a, 'b>>,
sprites: Vec<Entity>,
}
impl<'a, 'b> PlayState<'a, 'b> {
fn init_sprites(&mut self, world: &mut World) {
let sprites = world.try_fetch_mut::<HashMap<String, SpriteRender>>().unwrap().clone();
let dimensions = (*world.read_resource::<ScreenDimensions>()).clone();
let birb_sprite = sprites
.get("floppy").unwrap();
let mut transform = Transform::default();
transform.set_scale(Vector3::new(3.0, 3.0, 3.0));
transform.set_translation_xyz(dimensions.width()/2.0, dimensions.height()/2.0, 0.2);
self.sprites.push(world
.create_entity()
.with(birb_sprite.clone()) // Sprite Render
.with(Birb {
vertical_speed: 0.0,
position: 0.0,
starting_height: 0.0
})
.with(transform)
.build());
}
}
impl<'a, 'b> SimpleState for PlayState<'a, 'b> {
fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) {
let world = data.world;
let dimensions = (*world.read_resource::<ScreenDimensions>()).clone();
// Create the `DispatcherBuilder` and register some `System`s that should only run for this `State`.
let mut dispatcher_builder = DispatcherBuilder::new();
dispatcher_builder.add(BirbGravity { fired: false }, "gravity", &[]);
let mut dispatcher = dispatcher_builder.build();
dispatcher.setup(world);
self.dispatcher = Some(dispatcher);
PlayState::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;
}
if is_key_down(&event, VirtualKeyCode::P) {
let world = data.world;
for i in &self.sprites {
world.delete_entity(*i);
}
self.sprites.clear();
return Trans::Pop;
}
}
Trans::None
}
fn update(&mut self, data: &mut StateData<'_, GameData<'_, '_>>) -> SimpleTrans {
if let Some(dispatcher) = self.dispatcher.as_mut() {
dispatcher.dispatch(&data.world);
}
Trans::None
}
}

@ -0,0 +1,104 @@
use amethyst::{
assets::{AssetStorage, Loader},
core::transform::Transform,
core::math::Vector3,
input::{get_mouse_button, get_key, is_close_requested, is_key_down, VirtualKeyCode},
prelude::*,
renderer::{Camera, ImageFormat, SpriteRender, SpriteSheet, SpriteSheetFormat, Texture},
window::ScreenDimensions,
ecs::prelude::{Dispatcher, DispatcherBuilder, Component, DenseVecStorage, Entity},
};
use log::info;
use crate::components::*;
use std::collections::HashMap;
use crate::systems::{BirbGravity, ScrollScrollables};
use crate::play_state::PlayState;
#[derive(Default)]
pub struct ReadyState {
sprites: Vec<Entity>,
}
impl ReadyState {
fn init_sprites(&mut self, world: &mut World) {
let dimensions = (*world.read_resource::<ScreenDimensions>()).clone();
let sprites = world.try_fetch_mut::<HashMap<String, SpriteRender>>().unwrap().clone();
let get_ready_text_sprite = sprites
.get("get-ready-text").unwrap().clone();
let tap_tap_dialogue_sprite = sprites
.get("tap-tap-dialogue").unwrap().clone();
let mut transform = Transform::default();
transform.set_scale(Vector3::new(3.0, 3.0, 3.0));
transform.set_translation_xyz(dimensions.width()*0.5, dimensions.height()*0.8, 0.2);
self.sprites.push(world
.create_entity()
.with(get_ready_text_sprite.clone()) // Sprite Render
.with(transform.clone())
.build());
transform.set_translation_xyz(dimensions.width()*0.5, dimensions.height()*0.5, 0.2);
self.sprites.push(world
.create_entity()
.with(tap_tap_dialogue_sprite.clone()) // Sprite Render
.with(transform.clone())
.build());
}
}
impl SimpleState for ReadyState {
fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) {
let world = data.world;
ReadyState::init_sprites(self, world);
}
fn on_resume(&mut self, data: StateData<'_, GameData<'_, '_>>) {
}
fn handle_event(
&mut self,
mut data: StateData<'_, GameData<'_, '_>>,
event: StateEvent,
) -> SimpleTrans {
if let StateEvent::Ui(event) = &event {
if event.event_type == UiEventType::Click {
}
}
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(PlayState::default()));
}
}
Trans::None
}
fn update(&mut self, data: &mut StateData<'_, GameData<'_, '_>>) -> SimpleTrans {
Trans::None
}
}

@ -13,105 +13,85 @@ use log::info;
use crate::components::*; use crate::components::*;
use std::collections::HashMap; use std::collections::HashMap;
use crate::systems::{BirbGravity, ScrollScrollables}; use crate::systems::{BirbGravity, ScrollScrollables};
use crate::ready_state::ReadyState;
#[derive(Default)]
pub struct PlayState<'a, 'b> {
// Custom dispatch systems for this state
dispatcher: Option<Dispatcher<'a, 'b>>,
#[derive(Default)]
pub struct SplashState {
sprites: Vec<Entity>, sprites: Vec<Entity>,
persistent_sprites: Vec<Entity>,
} }
impl<'a, 'b> PlayState<'a, 'b> { impl SplashState {
fn init_sprites(&mut self, world: &mut World) {
let sprites = world.try_fetch_mut::<HashMap<String, SpriteRender>>().unwrap().clone(); fn load_sprites(world: &mut World) -> HashMap<String, SpriteRender> {
let dimensions = (*world.read_resource::<ScreenDimensions>()).clone(); // 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,
)
};
let birb_sprite = sprites // Load the spritesheet definition file, which contains metadata on our
.get("floppy").unwrap(); // 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,
)
};
let mut transform = Transform::default(); let sprite_map = vec![
transform.set_scale(Vector3::new(3.0, 3.0, 3.0)); ("day-background".to_string(), 0),
transform.set_translation_xyz(dimensions.width()/2.0, dimensions.height()/2.0, 0.2); ("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),
];
self.sprites.push(world sprite_map.iter()
.create_entity() .map(|i| (i.0.clone(), SpriteRender {
.with(birb_sprite.clone()) // Sprite Render sprite_sheet: sheet_handle.clone(),
.with(Birb { sprite_number: i.1,
vertical_speed: 0.0, }))
position: 0.0, .collect()
starting_height: 0.0
})
.with(transform)
.build());
}
} }
impl<'a, 'b> SimpleState for PlayState<'a, 'b> { fn init_camera(world: &mut World) {
fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) {
let world = data.world;
let dimensions = (*world.read_resource::<ScreenDimensions>()).clone(); let dimensions = (*world.read_resource::<ScreenDimensions>()).clone();
// Create the `DispatcherBuilder` and register some `System`s that should only run for this `State`. // Center the camera in the middle of the screen, and let it cover
let mut dispatcher_builder = DispatcherBuilder::new(); // the entire screen
dispatcher_builder.add(BirbGravity { fired: false }, "gravity", &[]); let mut transform = Transform::default();
transform.set_translation_xyz(dimensions.width() * 0.5, dimensions.height() * 0.5, 1.);
let mut dispatcher = dispatcher_builder.build();
dispatcher.setup(world);
self.dispatcher = Some(dispatcher);
PlayState::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;
}
if is_key_down(&event, VirtualKeyCode::P) {
let world = data.world;
for i in &self.sprites {
world.delete_entity(*i);
}
self.sprites.clear();
return Trans::Pop;
}
}
Trans::None
}
fn update(&mut self, data: &mut StateData<'_, GameData<'_, '_>>) -> SimpleTrans {
if let Some(dispatcher) = self.dispatcher.as_mut() {
dispatcher.dispatch(&data.world);
}
Trans::None
}
}
#[derive(Default)] world
pub struct SplashState { .create_entity()
sprites: Vec<Entity>, .with(Camera::standard_2d(dimensions.width(), dimensions.height()))
persistent_sprites: Vec<Entity>, .with(transform)
.build();
} }
impl SplashState {
fn init_sprites(&mut self, world: &mut World) { fn init_sprites(&mut self, world: &mut World) {
let sprites = world.try_fetch_mut::<HashMap<String, SpriteRender>>().unwrap().clone(); let sprites = world.try_fetch_mut::<HashMap<String, SpriteRender>>().unwrap().clone();
let dimensions = (*world.read_resource::<ScreenDimensions>()).clone(); let dimensions = (*world.read_resource::<ScreenDimensions>()).clone();
@ -212,72 +192,6 @@ impl SplashState {
.with(transform.clone()) .with(transform.clone())
.build()); .build());
} }
fn init_camera(world: &mut World) {
let dimensions = (*world.read_resource::<ScreenDimensions>()).clone();
// 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.);
world
.create_entity()
.with(Camera::standard_2d(dimensions.width(), dimensions.height()))
.with(transform)
.build();
}
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,
)
};
// 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,
)
};
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),
];
sprite_map.iter()
.map(|i| (i.0.clone(), SpriteRender {
sprite_sheet: sheet_handle.clone(),
sprite_number: i.1,
}))
.collect()
}
} }
impl SimpleState for SplashState { impl SimpleState for SplashState {
@ -325,83 +239,3 @@ impl SimpleState for SplashState {
Trans::None Trans::None
} }
} }
#[derive(Default)]
pub struct ReadyState {
sprites: Vec<Entity>,
}
impl ReadyState {
fn init_sprites(&mut self, world: &mut World) {
let dimensions = (*world.read_resource::<ScreenDimensions>()).clone();
let sprites = world.try_fetch_mut::<HashMap<String, SpriteRender>>().unwrap().clone();
let get_ready_text_sprite = sprites
.get("get-ready-text").unwrap().clone();
let tap_tap_dialogue_sprite = sprites
.get("tap-tap-dialogue").unwrap().clone();
let mut transform = Transform::default();
transform.set_scale(Vector3::new(3.0, 3.0, 3.0));
transform.set_translation_xyz(dimensions.width()*0.5, dimensions.height()*0.8, 0.2);
self.sprites.push(world
.create_entity()
.with(get_ready_text_sprite.clone()) // Sprite Render
.with(transform.clone())
.build());
transform.set_translation_xyz(dimensions.width()*0.5, dimensions.height()*0.5, 0.2);
self.sprites.push(world
.create_entity()
.with(tap_tap_dialogue_sprite.clone()) // Sprite Render
.with(transform.clone())
.build());
}
}
impl SimpleState for ReadyState {
fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) {
let world = data.world;
ReadyState::init_sprites(self, world);
}
fn on_resume(&mut self, data: StateData<'_, GameData<'_, '_>>) {
}
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(PlayState::default()));
}
}
Trans::None
}
fn update(&mut self, data: &mut StateData<'_, GameData<'_, '_>>) -> SimpleTrans {
Trans::None
}
}

@ -19,8 +19,6 @@ pub struct ScrollScrollables;
/* /*
Pausable systems Pausable systems
https://book.amethyst.rs/stable/controlling_system_execution/pausable_systems.html https://book.amethyst.rs/stable/controlling_system_execution/pausable_systems.html
*/ */
// This system iterates all the objects with transform (and falling object) component // This system iterates all the objects with transform (and falling object) component

Loading…
Cancel
Save