Skip to content

Commit

Permalink
Fix sprite camera follow
Browse files Browse the repository at this point in the history
  • Loading branch information
valkum committed May 16, 2019
1 parent 369cd92 commit 3d99de7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
59 changes: 44 additions & 15 deletions examples/sprite_camera_follow/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::sync::Arc;
use amethyst::{
assets::{AssetStorage, Loader, Processor},
core::{Parent, Transform, TransformBundle},
core::{Parent, Transform, TransformBundle, Named},
ecs::{Component, Entity, Join, NullStorage, Read, ReadStorage, System, WriteStorage, ReadExpect, SystemData, Resources},
input::{InputBundle, InputHandler, StringBindings},
input::{InputBundle, InputHandler, StringBindings, is_key_down, is_close_requested},
prelude::*,
renderer::{
pass::DrawFlat2DTransparentDesc,
Expand All @@ -26,6 +26,7 @@ use amethyst::{
},
window::{WindowBundle, Window, ScreenDimensions},
utils::application_root_dir,
winit,
};

#[derive(Default)]
Expand All @@ -52,13 +53,7 @@ impl<'s> System<'s> for MovementSystem {
for (_, transform) in (&players, &mut transforms).join() {
transform.prepend_translation_x(x_move as f32 * 5.0);
transform.prepend_translation_y(y_move as f32 * 5.0);
println!("Player = {:?}", transform);
}

for (_, transform) in (&cameras, &mut transforms).join() {
transform.prepend_translation_x(x_move as f32 * 5.0);
transform.prepend_translation_y(y_move as f32 * 5.0);
println!("Camera = {:?}", transform);
// println!("Player = {:?}", transform);
}
}
}
Expand Down Expand Up @@ -92,7 +87,7 @@ fn init_background_sprite(world: &mut World, sprite_sheet: &SpriteSheetHandle) -
sprite_sheet: sprite_sheet.clone(),
sprite_number: 0,
};
world.create_entity().with(transform).with(sprite).build()
world.create_entity().with(transform).with(sprite).named("background").with(Transparent).build()
}

// Initialize a sprite as a reference point at a fixed location
Expand All @@ -108,13 +103,14 @@ fn init_reference_sprite(world: &mut World, sprite_sheet: &SpriteSheetHandle) ->
.with(transform)
.with(sprite)
.with(Transparent)
.named("reference")
.build()
}

// Initialize a sprite as a reference point
fn init_screen_reference_sprite(world: &mut World, sprite_sheet: &SpriteSheetHandle) -> Entity {
let mut transform = Transform::default();
transform.set_translation_xyz(10.0, 10.0, 0.0);
transform.set_translation_xyz(-250.0, -245.0, -11.0);
let sprite = SpriteRender {
sprite_sheet: sprite_sheet.clone(),
sprite_number: 0,
Expand All @@ -124,12 +120,13 @@ fn init_screen_reference_sprite(world: &mut World, sprite_sheet: &SpriteSheetHan
.with(transform)
.with(sprite)
.with(Transparent)
.named("screen_reference")
.build()
}

fn init_player(world: &mut World, sprite_sheet: &SpriteSheetHandle) -> Entity {
let mut transform = Transform::default();
transform.set_translation_xyz(0.0, 0.0, 0.0);
transform.set_translation_xyz(0.0, 0.0, -3.0);
let sprite = SpriteRender {
sprite_sheet: sprite_sheet.clone(),
sprite_number: 1,
Expand All @@ -140,6 +137,7 @@ fn init_player(world: &mut World, sprite_sheet: &SpriteSheetHandle) -> Entity {
.with(Player)
.with(sprite)
.with(Transparent)
.named("player")
.build()
}

Expand All @@ -151,13 +149,14 @@ fn initialise_camera(world: &mut World, parent: Entity) -> Entity {
//println!("Init camera with dimensions: {}x{}", width, height);

let mut camera_transform = Transform::default();
camera_transform.set_translation_z(1.0);
camera_transform.set_translation_z(5.0);

world
.create_entity()
.with(camera_transform)
.with(Parent{entity: parent})
.with(Camera::standard_2d(width, height))
.named("camera")
.build()
}

Expand All @@ -166,21 +165,51 @@ struct Example;
impl SimpleState for Example {
fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) {
let world = data.world;
world.register::<Named>();

let circle_sprite_sheet_handle =
load_sprite_sheet(world, "Circle_Spritesheet.png", "Circle_Spritesheet.ron");
let background_sprite_sheet_handle =
load_sprite_sheet(world, "Background.png", "Background.ron");

//let _background = init_background_sprite(world, &background_sprite_sheet_handle);
let _background = init_background_sprite(world, &background_sprite_sheet_handle);
let _reference = init_reference_sprite(world, &circle_sprite_sheet_handle);
let player = init_player(world, &circle_sprite_sheet_handle);
let _camera = initialise_camera(world, player);
let _reference_screen = init_screen_reference_sprite(world, &circle_sprite_sheet_handle);
}

fn handle_event(
&mut self,
data: StateData<'_, GameData<'_, '_>>,
event: StateEvent,
) -> SimpleTrans {
let StateData { world, .. } = data;
if let StateEvent::Window(event) = &event {
if is_close_requested(&event) || is_key_down(&event, winit::VirtualKeyCode::Escape) {
Trans::Quit
} else if is_key_down(&event, winit::VirtualKeyCode::Space) {
world.exec(
|(named, transforms): (ReadStorage<Named>, ReadStorage<Transform>)| {
for (name, transform) in (&named, &transforms).join() {
println!("{} => {:?}", name.name, transform.translation());
}
},
);
Trans::None
} else {
Trans::None
}
} else {
Trans::None
}
}
}

fn main() -> amethyst::Result<()> {
amethyst::start_logger(Default::default());
amethyst::Logger::from_config(Default::default())
.level_for("amethyst_assets", log::LevelFilter::Debug)
.start();

let app_root = application_root_dir()?;
let assets_directory = app_root.join("examples/sprite_camera_follow/resources");
Expand Down
4 changes: 2 additions & 2 deletions examples/sprite_camera_follow/resources/input.ron
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(
axes: {
"entity_x": Emulated(
pos: Key(A),
neg: Key(D),
pos: Key(D),
neg: Key(A),
),
"entity_y": Emulated(
pos: Key(W),
Expand Down

0 comments on commit 3d99de7

Please sign in to comment.