Commit ff31586d authored by Rahix's avatar Rahix 🦀
Browse files

sprites: Don't hold center coordinates in component

Query the size of the sprite on the fly during rendering.  This makes
component instanciation much easier because it no longer requires access
to the `Rendering` API.

As a later optimization, we could move this ofsetting into the
draw_image() function via a new parameter.
parent d0488472
use crate::resources;
use nalgebra;
pub struct Sprite {
pub image_handle: resources::ImageHandle,
pub center: nalgebra::Vector2<f32>,
}
impl Sprite {
pub fn new(image_handle: resources::ImageHandle, center: nalgebra::Vector2<f32>) -> Self {
Sprite {
image_handle,
center,
}
pub fn new(image_handle: resources::ImageHandle) -> Self {
Sprite { image_handle }
}
}
use crate::colliders;
use crate::components;
use crate::resources;
use crate::sprites;
use crate::svg_loader;
......@@ -8,7 +7,6 @@ use rand::seq::SliceRandom;
pub fn create_player(
world: &mut legion::World,
resources: &legion::Resources,
level: &svg_loader::SvgLevel,
player: components::Player,
) -> legion::Entity {
......@@ -17,15 +15,12 @@ pub fn create_player(
.get("player")
.expect("no player spawns in this map");
let spawn = spawn_locations.choose(&mut rand::thread_rng()).unwrap();
let image: resources::ImageHandle = sprites::Sprite::Player.into();
let rendering = resources.get::<resources::Rendering>().unwrap();
let center = rendering.get_image_size(&image).unwrap() / 2.0;
world.push((
player,
components::Position::new(spawn.x, spawn.y),
components::Movable::new(),
colliders::Collider::new_player(50.0),
components::Sprite::new(image, center),
components::Sprite::new(sprites::Sprite::Player.into()),
))
}
......@@ -38,7 +38,7 @@ impl InGameState {
resources.insert(resources::Camera::new(1920.0, 1080.0));
resources.insert(colliders::CollisionWorld::new());
let player = entities::create_player(&mut world, &resources, &level, player);
let player = entities::create_player(&mut world, &level, player);
let obstacles = entities::create_stationary_obstacles(&mut world, &level);
resources.insert(resources::Player(player));
......
......@@ -9,7 +9,8 @@ pub fn draw_sprites(
position: &components::Position,
sprite: &components::Sprite,
) {
let upper_left = position.0 - sprite.center;
let spritesize = rendering.get_image_size(&sprite.image_handle).unwrap();
let upper_left = position.0 - spritesize / 2.0;
rendering
.draw_image(
&sprite.image_handle,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment