Add sprites

master
Hilmar Wiegand 6 years ago
parent 09b7c0228c
commit da1cbadfea

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

@ -0,0 +1,24 @@
(
texture_width: 690,
texture_height: 230,
sprites: [
(
x: 0,
y: 0,
width: 230,
height: 230,
),
(
x: 230,
y: 0,
width: 230,
height: 230,
),
(
x: 460,
y: 0,
width: 230,
height: 230,
)
]
)

@ -1,5 +1,5 @@
use amethyst::{ use amethyst::{
assets::{AssetStorage, Handle, Loader, Processor}, assets::{AssetStorage, Loader, Processor},
core::transform::{Transform, TransformBundle}, core::transform::{Transform, TransformBundle},
ecs::prelude::{ReadExpect, Resources, SystemData}, ecs::prelude::{ReadExpect, Resources, SystemData},
prelude::*, prelude::*,
@ -14,13 +14,16 @@ use amethyst::{
hal::{format::Format, image}, hal::{format::Format, image},
}, },
types::DefaultBackend, types::DefaultBackend,
Camera, GraphCreator, ImageFormat, RenderingSystem, SpriteSheet, SpriteSheetFormat, Camera, GraphCreator, ImageFormat, RenderingSystem, SpriteRender, SpriteSheet,
Texture, SpriteSheetFormat, Texture,
}, },
utils::application_root_dir, utils::application_root_dir,
window::{ScreenDimensions, Window, WindowBundle}, window::{ScreenDimensions, Window, WindowBundle},
}; };
static WIDTH: u32 = 800;
static HEIGHT: u32 = 600;
struct MyState; struct MyState;
impl SimpleState for MyState { impl SimpleState for MyState {
@ -29,8 +32,8 @@ impl SimpleState for MyState {
init_camera(world); init_camera(world);
// TODO(happens): Add entity for this let sprites = load_sprites(world);
let _spritesheet = load_sprites(world); init_sprites(world, &sprites);
} }
} }
@ -45,7 +48,6 @@ fn main() -> amethyst::Result<()> {
let render_graph = RenderGraph::default(); let render_graph = RenderGraph::default();
let render_system = RenderingSystem::<DefaultBackend, _>::new(render_graph); let render_system = RenderingSystem::<DefaultBackend, _>::new(render_graph);
// TODO(happens): Add UI example
let game_data = GameDataBuilder::default() let game_data = GameDataBuilder::default()
.with_bundle(WindowBundle::from_config_path(display_config))? .with_bundle(WindowBundle::from_config_path(display_config))?
.with_bundle(TransformBundle::new())? .with_bundle(TransformBundle::new())?
@ -64,22 +66,21 @@ fn main() -> amethyst::Result<()> {
fn init_camera(world: &mut World) { fn init_camera(world: &mut World) {
let mut transform = Transform::default(); let mut transform = Transform::default();
transform.set_translation_xyz(100., 100., 1.); transform.set_translation_xyz(WIDTH as f32 * 0.5, HEIGHT as f32 * 0.5, 1.);
world world
.create_entity() .create_entity()
.with(Camera::standard_2d(200., 200.)) .with(Camera::standard_2d(WIDTH as f32, HEIGHT as f32))
.with(transform) .with(transform)
.build(); .build();
} }
fn load_sprites(world: &mut World) -> Handle<SpriteSheet> { fn load_sprites(world: &mut World) -> Vec<SpriteRender> {
// TODO(happens): Add sprite assets
let texture_handle = { let texture_handle = {
let loader = world.read_resource::<Loader>(); let loader = world.read_resource::<Loader>();
let texture_storage = world.read_resource::<AssetStorage<Texture>>(); let texture_storage = world.read_resource::<AssetStorage<Texture>>();
loader.load( loader.load(
"sprites/ayaya.png", "sprites/logo.png",
ImageFormat::default(), ImageFormat::default(),
(), (),
&texture_storage, &texture_storage,
@ -90,14 +91,34 @@ fn load_sprites(world: &mut World) -> Handle<SpriteSheet> {
let loader = world.read_resource::<Loader>(); let loader = world.read_resource::<Loader>();
let sheet_storage = world.read_resource::<AssetStorage<SpriteSheet>>(); let sheet_storage = world.read_resource::<AssetStorage<SpriteSheet>>();
loader.load( loader.load(
"sprites/ayaya.ron", "sprites/logo.ron",
SpriteSheetFormat(texture_handle), SpriteSheetFormat(texture_handle),
(), (),
&sheet_storage, &sheet_storage,
) )
}; };
sheet_handle (0..3)
.map(|i| SpriteRender {
sprite_sheet: sheet_handle.clone(),
sprite_number: i,
})
.collect()
}
fn init_sprites(world: &mut World, sprites: &[SpriteRender]) {
for (i, sprite) in sprites.iter().enumerate() {
let x = (i as f32 - 1.) * 100. + WIDTH as f32 * 0.5;
let y = (i as f32 - 1.) * 100. + HEIGHT as f32 * 0.5;
let mut transform = Transform::default();
transform.set_translation_xyz(x, y, 0.);
world
.create_entity()
.with(sprite.clone())
.with(transform)
.build();
}
} }
// TODO(happens): Can we provide this with a few parameters, // TODO(happens): Can we provide this with a few parameters,

Loading…
Cancel
Save