player.rs 950 Bytes
Newer Older
1
use crate::colliders;
Rahix's avatar
Rahix committed
2
use crate::components;
Simon Goller's avatar
Simon Goller committed
3
use crate::resources;
Simon Goller's avatar
Simon Goller committed
4
use crate::sprites;
Simon Goller's avatar
Simon Goller committed
5
use crate::svg_loader;
Rahix's avatar
Rahix committed
6

Rahix's avatar
Rahix committed
7
8
use rand::seq::SliceRandom;

Simon Goller's avatar
Simon Goller committed
9
10
11
12
pub fn create_player(
    world: &mut legion::World,
    resources: &legion::Resources,
    level: &svg_loader::SvgLevel,
13
    player: components::Player,
Simon Goller's avatar
Simon Goller committed
14
) -> legion::Entity {
Rahix's avatar
Rahix committed
15
16
17
18
19
    let spawn_locations = level
        .spawnpoints
        .get("player")
        .expect("no player spawns in this map");
    let spawn = spawn_locations.choose(&mut rand::thread_rng()).unwrap();
Simon Goller's avatar
Simon Goller committed
20
21
22
    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;
Simon Goller's avatar
Simon Goller committed
23

24
    world.push((
25
        player,
Rahix's avatar
Rahix committed
26
        components::Position::new(spawn.x, spawn.y),
27
        components::Movable::new(),
28
        colliders::Collider::new_player(50.0),
Simon Goller's avatar
Simon Goller committed
29
        components::Sprite::new(image, center),
30
    ))
Rahix's avatar
Rahix committed
31
}