sprites aligned

master
mitchellhansen 5 years ago
parent 58f0c2a8d8
commit 3d34224fe6

@ -47,14 +47,14 @@
( // Play Button ( // Play Button
x: 354, x: 354,
y: 118, y: 118,
width: 50, width: 52,
height: 28, height: 29,
), ),
( // Leaderboard button ( // Leaderboard button
x: 414, x: 414,
y: 118, y: 118,
width: 42, width: 52,
height: 28, height: 29,
), ),
( // Get Ready ( // Get Ready
x: 295, x: 295,
@ -65,8 +65,8 @@
( // Flappy Bird Text ( // Flappy Bird Text
x: 351, x: 351,
y: 91, y: 91,
width: 88, width: 90,
height: 25, height: 25,
), )
] ]
) )

@ -42,6 +42,7 @@ fn main() -> amethyst::Result<()> {
.with_bundle(TransformBundle::new())? .with_bundle(TransformBundle::new())?
.with_bundle(input_bundle)? .with_bundle(input_bundle)?
// .with(System, "system", &["required_things"]) // .with(System, "system", &["required_things"])
.with(ScrollScrollables, "scroll", &[])
.with_bundle( .with_bundle(
RenderingBundle::<DefaultBackend>::new() RenderingBundle::<DefaultBackend>::new()
.with_plugin( .with_plugin(

@ -30,57 +30,11 @@ impl<'a, 'b> PlayState<'a, 'b> {
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();
let birb_sprite = sprites
let background_sprite = sprites.get("day-background").unwrap().clone(); .get("floppy").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(); 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(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); transform.set_translation_xyz(dimensions.width()/2.0, dimensions.height()/2.0, 0.2);
self.sprites.push(world self.sprites.push(world
@ -101,23 +55,12 @@ impl<'a, 'b> SimpleState for PlayState<'a, 'b> {
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
// place our sprites correctly later. We'll clone this since we'll
// pass the world mutably to the following functions.
let dimensions = (*world.read_resource::<ScreenDimensions>()).clone(); let dimensions = (*world.read_resource::<ScreenDimensions>()).clone();
// Place the camera
/// function sets size of camera window
// 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();
dispatcher_builder.add(ScrollScrollables, "scroll", &[]);
dispatcher_builder.add(BirbGravity { fired: false }, "gravity", &[]); dispatcher_builder.add(BirbGravity { fired: false }, "gravity", &[]);
// Build and setup the `Dispatcher`.
let mut dispatcher = dispatcher_builder.build(); let mut dispatcher = dispatcher_builder.build();
dispatcher.setup(world); dispatcher.setup(world);
@ -129,7 +72,7 @@ impl<'a, 'b> SimpleState for PlayState<'a, 'b> {
fn handle_event( fn handle_event(
&mut self, &mut self,
mut _data: StateData<'_, GameData<'_, '_>>, mut data: StateData<'_, GameData<'_, '_>>,
event: StateEvent, event: StateEvent,
) -> SimpleTrans { ) -> SimpleTrans {
@ -140,6 +83,11 @@ impl<'a, 'b> SimpleState for PlayState<'a, 'b> {
} }
if is_key_down(&event, VirtualKeyCode::P) { 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; return Trans::Pop;
} }
} }
@ -158,26 +106,11 @@ impl<'a, 'b> SimpleState for PlayState<'a, 'b> {
#[derive(Default)] #[derive(Default)]
pub struct SplashState { pub struct SplashState {
sprites: Vec<Entity>, sprites: Vec<Entity>,
persistent_sprites: Vec<Entity>,
} }
impl SplashState { 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
// 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 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();
@ -191,56 +124,71 @@ impl SplashState {
.get("leaderboard-button").unwrap().clone(); .get("leaderboard-button").unwrap().clone();
let background_sprite = sprites let background_sprite = sprites
.get("day-background").unwrap().clone(); .get("day-background").unwrap().clone();
let night_background_sprite = sprites
.get("night-background").unwrap().clone();
let ground_sprite = sprites let ground_sprite = sprites
.get("ground").unwrap().clone(); .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(); 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); transform.set_translation_xyz(3.0*144.0/2.0, 3.0*256.0/2.0, 0.0);
self.sprites.push(world self.persistent_sprites.push(world
.create_entity() .create_entity()
.with(background_sprite.clone()) // Sprite Render .with(background_sprite.clone()) // Sprite Render
.with(background_object.clone()) .with(TiledScroller {
speed: -75.0,
position: 1.0,
width: 144.0 * 3.0,
height: 256.0 * 3.0,
})
.with(transform.clone()) .with(transform.clone())
.build()); .build());
transform.set_translation_xyz(3.0*144.0/2.0*3.0, 3.0*256.0/2.0, 0.0); transform.set_translation_xyz(3.0*144.0/2.0*3.0, 3.0*256.0/2.0, 0.0);
self.sprites.push(world self.persistent_sprites.push(world
.create_entity() .create_entity()
.with(background_sprite.clone()) // Sprite Render .with(background_sprite.clone()) // Sprite Render
.with(TiledScroller { .with(TiledScroller {
speed: -75.0, speed: -75.0,
position: 2.0, position: 2.0,
width: 0.0, width: 144.0 * 3.0,
height: 0.0 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,
width: 168.0 * 3.0,
height: 56.0 * 3.0,
}) })
.with(transform.clone()) .with(transform.clone())
.build()); .build());
transform.set_translation_xyz(3.0*168.0/2.0*3.0, 3.0*56.0/2.0, 0.1); transform.set_translation_xyz(3.0*168.0/2.0*3.0, 3.0*56.0/2.0, 0.1);
self.sprites.push(world self.persistent_sprites.push(world
.create_entity() .create_entity()
.with(ground_sprite.clone()) // Sprite Render .with(ground_sprite.clone()) // Sprite Render
.with(TiledScroller { .with(TiledScroller {
speed: -100.0, speed: -100.0,
position: 2.0, position: 2.0,
width: 0.0, width: 168.0 * 3.0,
height: 0.0, height: 56.0 * 3.0,
}) })
.with(transform.clone()) .with(transform.clone())
.build()); .build());
transform.set_translation_xyz(background_object.width/2.0, background_object.height/2.0, 0.0); transform.set_translation_xyz(dimensions.width()*0.5, dimensions.height()*0.8, 0.2);
self.sprites.push(world self.sprites.push(world
.create_entity() .create_entity()
@ -248,7 +196,7 @@ impl SplashState {
.with(transform.clone()) .with(transform.clone())
.build()); .build());
transform.set_translation_xyz(100.0/2.0, 100.0/2.0, 0.1); transform.set_translation_xyz(dimensions.width()*0.25, dimensions.height()*0.4, 0.2);
self.sprites.push(world self.sprites.push(world
.create_entity() .create_entity()
@ -256,7 +204,7 @@ impl SplashState {
.with(transform.clone()) .with(transform.clone())
.build()); .build());
transform.set_translation_xyz(500.0/2.0, 100.0/2.0, 0.1); transform.set_translation_xyz(dimensions.width()*0.75, dimensions.height()*0.4, 0.2);
self.sprites.push(world self.sprites.push(world
.create_entity() .create_entity()
@ -265,6 +213,22 @@ impl SplashState {
.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> { 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
@ -354,8 +318,6 @@ impl SimpleState for SplashState {
return Trans::Push(Box::new(ReadyState::default())); return Trans::Push(Box::new(ReadyState::default()));
} }
} }
// Keep going
Trans::None Trans::None
} }
@ -370,7 +332,8 @@ pub struct ReadyState {
} }
impl ReadyState { impl ReadyState {
fn init_sprites(&mut self, world: &mut World, dimensions: &ScreenDimensions) {
fn init_sprites(&mut self, world: &mut 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 sprites = world.try_fetch_mut::<HashMap<String, SpriteRender>>().unwrap().clone();
@ -383,7 +346,8 @@ impl ReadyState {
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);
transform.set_translation_xyz(dimensions.width()*0.5, dimensions.height()*0.8, 0.2);
self.sprites.push(world self.sprites.push(world
.create_entity() .create_entity()
@ -391,7 +355,7 @@ impl ReadyState {
.with(transform.clone()) .with(transform.clone())
.build()); .build());
transform.set_translation_xyz(500.0/2.0, 500.0/2.0, 0.1); transform.set_translation_xyz(dimensions.width()*0.5, dimensions.height()*0.5, 0.2);
self.sprites.push(world self.sprites.push(world
.create_entity() .create_entity()
@ -402,11 +366,9 @@ impl ReadyState {
} }
impl SimpleState for ReadyState { impl SimpleState for ReadyState {
fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) { fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) {
let world = data.world; let world = data.world;
ReadyState::init_sprites(self, world);
} }
fn handle_event( fn handle_event(
@ -432,8 +394,6 @@ impl SimpleState for ReadyState {
return Trans::Push(Box::new(PlayState::default())); return Trans::Push(Box::new(PlayState::default()));
} }
} }
// Keep going
Trans::None Trans::None
} }

@ -35,15 +35,17 @@ impl<'a> System<'a> for ScrollScrollables {
fn run(&mut self, (mut transforms, mut scrolling, time): Self::SystemData) { fn run(&mut self, (mut transforms, mut scrolling, time): Self::SystemData) {
for (mut transform, mut object) in (&mut transforms, &mut scrolling).join() { for (mut transform, mut object) in (&mut transforms, &mut scrolling).join() {
// I need to tile it by it's width // I need to tile it by it's width
transform.prepend_translation_x(object.speed * time.delta_seconds()); transform.prepend_translation_x(object.speed * time.delta_seconds());
if transform.translation().x+144.0*1.5 < 0.0 { if transform.translation().x+object.width/2.0 < 0.0 {
transform.set_translation_x(144.0*3.0/2.0*3.0); transform.set_translation_x(object.width/2.0*3.0);
} }
} }
} }
} }
#[derive(Default)]
pub struct BirbGravity { pub struct BirbGravity {
pub fired: bool, pub fired: bool,
} }
@ -61,7 +63,6 @@ impl<'a> System<'a> for BirbGravity {
for (mut transform, mut object) in (&mut transforms, &mut scrolling).join() { for (mut transform, mut object) in (&mut transforms, &mut scrolling).join() {
//match game.current_state //match game.current_state
if input.action_is_down("flap").expect("No action") { if input.action_is_down("flap").expect("No action") {
object.vertical_speed = 600.0; object.vertical_speed = 600.0;
} }

Loading…
Cancel
Save