state switching better, but need to transfer sprites

master
mitchellhansen 5 years ago
parent fc0cb579b3
commit 58f0c2a8d8

@ -37,6 +37,36 @@
y: 490, y: 490,
width: 17, width: 17,
height: 13, height: 13,
) ),
( // Tap Tap Dialogue
x: 292,
y: 91,
width: 56,
height: 48,
),
( // Play Button
x: 354,
y: 118,
width: 50,
height: 28,
),
( // Leaderboard button
x: 414,
y: 118,
width: 42,
height: 28,
),
( // Get Ready
x: 295,
y: 59,
width: 91,
height: 24,
),
( // Flappy Bird Text
x: 351,
y: 91,
width: 88,
height: 25,
),
] ]
) )

@ -51,8 +51,9 @@ fn main() -> amethyst::Result<()> {
.with_plugin(RenderFlat2D::default()), .with_plugin(RenderFlat2D::default()),
)?; )?;
// 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::PlayState::default(), game_data)?; let mut game = Application::new(resources, state::SplashState::default(), game_data)?;
game.run(); game.run();
Ok(()) Ok(())

@ -16,16 +16,90 @@ use crate::systems::{BirbGravity, ScrollScrollables};
#[derive(Default)] #[derive(Default)]
pub struct PlayState<'a, 'b> { pub struct PlayState<'a, 'b> {
/// The `State` specific `Dispatcher`, containing `System`s only relevant for this `State`.
// Custom dispatch systems for this state
dispatcher: Option<Dispatcher<'a, 'b>>, 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 background_sprite = sprites.get("day-background").unwrap().clone();
let background_object = TiledScroller {
speed: -75.0,
position: 1.0,
width: 144.0 * 3.0,
height: 256.0 * 3.0,
};
let mut transform = Transform::default();
transform.set_scale(Vector3::new(3.0, 3.0, 3.0));
transform.set_translation_xyz(background_object.width/2.0, background_object.height/2.0, 0.0);
self.sprites.push(world
.create_entity()
.with(background_sprite.clone()) // Sprite Render
.with(background_object.clone())
.with(transform.clone())
.build());
transform.set_translation_xyz(3.0*144.0/2.0*3.0, 3.0*256.0/2.0, 0.0);
self.sprites.push(world
.create_entity()
.with(background_sprite.clone()) // Sprite Render
.with(TiledScroller {
speed: -75.0,
position: 2.0,
width: 0.0,
height: 0.0
})
.with(transform.clone())
.build());
let ground_sprite = sprites.get("ground").unwrap();
transform.set_translation_xyz(3.0*168.0/2.0*3.0, 3.0*56.0/2.0, 0.1);
self.sprites.push(world
.create_entity()
.with(ground_sprite.clone()) // Sprite Render
.with(TiledScroller {
speed: -100.0,
position: 2.0,
width: 0.0,
height: 0.0,
})
.with(transform.clone())
.build());
let birb_sprite = sprites.get("floppy").unwrap();
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> { impl<'a, 'b> SimpleState for PlayState<'a, 'b> {
// On start will run when this state is initialized. For more
// state lifecycle hooks, see:
// https://book.amethyst.rs/stable/concepts/state.html#life-cycle
fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) { fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) {
let world = data.world; let world = data.world;
// Get the screen dimensions so we can initialize the camera and // Get the screen dimensions so we can initialize the camera and
@ -36,7 +110,7 @@ impl<'a, 'b> SimpleState for PlayState<'a, 'b> {
// Place the camera // Place the camera
/// function sets size of camera window /// function sets size of camera window
init_camera(world, &dimensions);
// Create the `DispatcherBuilder` and register some `System`s that should only run for this `State`. // Create the `DispatcherBuilder` and register some `System`s that should only run for this `State`.
let mut dispatcher_builder = DispatcherBuilder::new(); let mut dispatcher_builder = DispatcherBuilder::new();
@ -49,10 +123,7 @@ impl<'a, 'b> SimpleState for PlayState<'a, 'b> {
self.dispatcher = Some(dispatcher); self.dispatcher = Some(dispatcher);
// Load our sprites and display them PlayState::init_sprites(self, world);
let sprites = load_sprites(world);
world.insert(sprites.clone());
init_sprites(world, &sprites, &dimensions);
} }
@ -69,13 +140,9 @@ impl<'a, 'b> SimpleState for PlayState<'a, 'b> {
} }
if is_key_down(&event, VirtualKeyCode::P) { if is_key_down(&event, VirtualKeyCode::P) {
// So I need to set the scrolling and gravity systems to pause return Trans::Pop;
return Trans::Push(Box::new(PausedState{ sprite: None }));
} }
} }
// Keep going
Trans::None Trans::None
} }
@ -84,12 +151,21 @@ impl<'a, 'b> SimpleState for PlayState<'a, 'b> {
if let Some(dispatcher) = self.dispatcher.as_mut() { if let Some(dispatcher) = self.dispatcher.as_mut() {
dispatcher.dispatch(&data.world); dispatcher.dispatch(&data.world);
} }
Trans::None Trans::None
} }
} }
fn init_camera(world: &mut World, dimensions: &ScreenDimensions) { #[derive(Default)]
pub struct SplashState {
sprites: Vec<Entity>,
}
impl SplashState {
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 // Center the camera in the middle of the screen, and let it cover
// the entire screen // the entire screen
let mut transform = Transform::default(); let mut transform = Transform::default();
@ -100,9 +176,96 @@ fn init_camera(world: &mut World, dimensions: &ScreenDimensions) {
.with(Camera::standard_2d(dimensions.width(), dimensions.height())) .with(Camera::standard_2d(dimensions.width(), dimensions.height()))
.with(transform) .with(transform)
.build(); .build();
} }
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 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();
let ground_sprite = sprites
.get("ground").unwrap().clone();
let background_object = TiledScroller {
speed: -75.0,
position: 1.0,
width: 144.0 * 3.0,
height: 256.0 * 3.0,
};
let mut transform = Transform::default();
transform.set_scale(Vector3::new(3.0, 3.0, 3.0));
transform.set_translation_xyz(dimensions.width()/2.0 - 100.0, dimensions.height() - 100.0, 0.1);
self.sprites.push(world
.create_entity()
.with(background_sprite.clone()) // Sprite Render
.with(background_object.clone())
.with(transform.clone())
.build());
transform.set_translation_xyz(3.0*144.0/2.0*3.0, 3.0*256.0/2.0, 0.0);
self.sprites.push(world
.create_entity()
.with(background_sprite.clone()) // Sprite Render
.with(TiledScroller {
speed: -75.0,
position: 2.0,
width: 0.0,
height: 0.0
})
.with(transform.clone())
.build());
transform.set_translation_xyz(3.0*168.0/2.0*3.0, 3.0*56.0/2.0, 0.1);
fn load_sprites(world: &mut World) -> HashMap<String, SpriteRender> { self.sprites.push(world
.create_entity()
.with(ground_sprite.clone()) // Sprite Render
.with(TiledScroller {
speed: -100.0,
position: 2.0,
width: 0.0,
height: 0.0,
})
.with(transform.clone())
.build());
transform.set_translation_xyz(background_object.width/2.0, background_object.height/2.0, 0.0);
self.sprites.push(world
.create_entity()
.with(flappy_bird_text_sprite.clone())
.with(transform.clone())
.build());
transform.set_translation_xyz(100.0/2.0, 100.0/2.0, 0.1);
self.sprites.push(world
.create_entity()
.with(play_button_sprite.clone())
.with(transform.clone())
.build());
transform.set_translation_xyz(500.0/2.0, 100.0/2.0, 0.1);
self.sprites.push(world
.create_entity()
.with(leaderboard_button_sprite.clone())
.with(transform.clone())
.build());
}
fn load_sprites(world: &mut World) -> HashMap<String, SpriteRender> {
// Load the texture for our sprites. We'll later need to // Load the texture for our sprites. We'll later need to
// add a handle to this texture to our `SpriteRender`s, so // add a handle to this texture to our `SpriteRender`s, so
// we need to keep a reference to it. // we need to keep a reference to it.
@ -137,6 +300,11 @@ fn load_sprites(world: &mut World) -> HashMap<String, SpriteRender> {
("up-pipe".to_string(), 3), ("up-pipe".to_string(), 3),
("ground".to_string(), 4), ("ground".to_string(), 4),
("floppy".to_string(), 5), ("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() sprite_map.iter()
@ -145,102 +313,101 @@ fn load_sprites(world: &mut World) -> HashMap<String, SpriteRender> {
sprite_number: i.1, sprite_number: i.1,
})) }))
.collect() .collect()
}
} }
fn init_sprites(world: &mut World, sprites: &HashMap<String, SpriteRender>, dimensions: &ScreenDimensions) { impl SimpleState for SplashState {
let background_sprite = sprites.get("day-background").unwrap();
let background_object = TiledScroller {
speed: -75.0,
position: 1.0,
width: 144.0 * 3.0,
height: 256.0 * 3.0,
};
let mut transform = Transform::default(); fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) {
transform.set_scale(Vector3::new(3.0, 3.0, 3.0)); let world = data.world;
transform.set_translation_xyz(background_object.width/2.0, background_object.height/2.0, 0.0);
// 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());
world SplashState::load_sprites(world);
.create_entity() SplashState::init_camera(world);
.with(background_sprite.clone()) // Sprite Render SplashState::init_sprites(self, world);
.with(background_object.clone())
.with(transform.clone())
.build();
transform.set_translation_xyz(3.0*144.0/2.0*3.0, 3.0*256.0/2.0, 0.0); }
world fn handle_event(
.create_entity() &mut self,
.with(background_sprite.clone()) // Sprite Render mut data: StateData<'_, GameData<'_, '_>>,
.with(TiledScroller { event: StateEvent,
speed: -75.0, ) -> SimpleTrans {
position: 2.0,
width: 0.0,
height: 0.0
})
.with(transform.clone())
.build();
let ground_sprite = sprites.get("ground").unwrap(); if let StateEvent::Window(event) = &event {
transform.set_translation_xyz(3.0*168.0/2.0*3.0, 3.0*56.0/2.0, 0.1); // Check if the window should be closed
if is_close_requested(&event) || is_key_down(&event, VirtualKeyCode::Escape) {
return Trans::Quit;
}
world // Check if the window should be closed
.create_entity() if is_key_down(&event, VirtualKeyCode::Space) {
.with(ground_sprite.clone()) // Sprite Render let world = data.world;
.with(TiledScroller { for i in &self.sprites {
speed: -100.0, world.delete_entity(*i);
position: 2.0, }
width: 0.0, self.sprites.clear();
height: 0.0,
})
.with(transform.clone())
.build();
let birb_sprite = sprites.get("floppy").unwrap(); return Trans::Push(Box::new(ReadyState::default()));
transform.set_translation_xyz(dimensions.width()/2.0, dimensions.height()/2.0, 0.2); }
}
world // Keep going
.create_entity() Trans::None
.with(birb_sprite.clone()) // Sprite Render }
.with(Birb {
vertical_speed: 0.0,
position: 0.0,
starting_height: 0.0
})
.with(transform)
.build();
fn update(&mut self, data: &mut StateData<'_, GameData<'_, '_>>) -> SimpleTrans {
Trans::None
}
} }
#[derive(Default)] #[derive(Default)]
pub struct PausedState { pub struct ReadyState {
sprite: Option<Entity>, sprites: Vec<Entity>,
} }
impl SimpleState for PausedState { impl ReadyState {
fn init_sprites(&mut self, world: &mut World, dimensions: &ScreenDimensions) {
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();
let sprites = world.try_fetch_mut::<HashMap<String, SpriteRender>>().unwrap().clone();
let sprite = world.try_fetch::<HashMap<String, SpriteRender>>().unwrap().get("up-pipe").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 sprite = resource.get("up-pipe").unwrap().clone();
let mut transform = Transform::default(); let mut transform = Transform::default();
transform.set_scale(Vector3::new(3.0, 3.0, 3.0)); transform.set_scale(Vector3::new(3.0, 3.0, 3.0));
transform.set_translation_xyz(dimensions.width()/2.0 - 100.0, dimensions.height() - 100.0, 0.1);
self.sprites.push(world
.create_entity()
.with(get_ready_text_sprite.clone()) // Sprite Render
.with(transform.clone())
.build());
transform.set_translation_xyz(500.0/2.0, 500.0/2.0, 0.1); transform.set_translation_xyz(500.0/2.0, 500.0/2.0, 0.1);
self.sprite = Some(world self.sprites.push(world
.create_entity() .create_entity()
.with(sprite.clone()) // Sprite Render .with(tap_tap_dialogue_sprite.clone()) // Sprite Render
.with(transform.clone()) .with(transform.clone())
.build()); .build());
} }
}
impl SimpleState for ReadyState {
fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) {
let world = data.world;
}
fn handle_event( fn handle_event(
&mut self, &mut self,
@ -250,12 +417,19 @@ impl SimpleState for PausedState {
if let StateEvent::Window(event) = &event { if let StateEvent::Window(event) = &event {
// Check if the window should be closed // Check if the window should be closed
if is_key_down(&event, VirtualKeyCode::Space) { 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; let world = data.world;
world.delete_entity(self.sprite.unwrap()); for i in &self.sprites {
self.sprite = None; world.delete_entity(*i);
return Trans::Pop; }
self.sprites.clear();
return Trans::Push(Box::new(PlayState::default()));
} }
} }

Loading…
Cancel
Save