Commit 20688a8e authored by Rahix's avatar Rahix 🦀
Browse files

Add super basic player entity

Add plumbing for the most basic player:

- A `Player` component to mark the entity which is the player.
- A `Player` resource which holds the entity ID of the player.
- A `draw_tmp_player_system` to draw the player as a circle (for now).
- A `create_player` function to create the player entity.
- Update the `moving_system` to put the camera at the player position.
parent 6282dd5a
mod edge;
mod movable;
mod node;
mod player;
mod position;
mod thesun;
pub use edge::Edge;
pub use movable::{Gravity, Movable};
pub use node::Node;
pub use player::Player;
pub use position::Position;
pub use thesun::TheSun;
......
pub struct Player;
mod player;
pub use player::create_player;
use crate::components;
pub fn create_player(world: &mut legion::World) -> legion::Entity {
world.push((components::Player, components::Position::new(600.0, 600.0)))
}
......@@ -3,6 +3,7 @@ use wasm_bindgen::prelude::*;
pub mod colliders;
pub mod colors;
pub mod components;
pub mod entities;
pub mod gamestate;
pub mod resources;
pub mod states;
......
mod camera;
mod clock;
mod player;
mod rendering;
pub use camera::Camera;
pub use clock::Clock;
pub use player::Player;
pub use rendering::Color;
pub use rendering::ImageHandle;
pub use rendering::Rendering;
/// Stores the entity ID of the player
pub struct Player(pub legion::Entity);
use crate::colliders;
use crate::colors;
use crate::entities;
use crate::gamestate;
use crate::resources;
use crate::svg_loader;
......@@ -20,22 +21,28 @@ impl InGameState {
let background = rendering.register_image(level.background_image.clone());
let mut resources = legion::Resources::default();
let mut world = legion::World::default();
resources.insert(resources::Clock::new());
resources.insert(rendering);
resources.insert(resources::Camera::new(1920.0, 1080.0));
resources.insert(colliders::CollisionWorld::new());
let mut world = legion::World::default();
let player = entities::create_player(&mut world);
resources.insert(resources::Player(player));
level.apply_colliders(&mut world);
let schedule = legion::Schedule::builder()
.add_system(colliders::synchronize_collisision_world_system())
.flush()
.add_system(systems::moving_system())
.add_thread_local(systems::camera_system())
.add_thread_local(systems::moving_system())
.add_system(colliders::update_collision_world_system())
.flush()
.add_thread_local(systems::camera_system())
.flush()
.add_thread_local(systems::draw_level_layer_system(background))
.add_thread_local(systems::draw_tmp_player_system())
.add_thread_local(systems::draw_level_layer_system(foreground))
.build();
......
mod camera;
mod level;
mod moving;
mod tmp_player;
pub use camera::camera_system;
pub use level::draw_level_layer_system;
pub use moving::moving_system;
pub use tmp_player::draw_tmp_player_system;
use crate::components;
use crate::resources;
use legion::IntoQuery;
#[legion::system]
pub fn moving(#[resource] camera: &mut resources::Camera) {
camera.position.x += 0.1;
#[read_component(components::Position)]
pub fn moving(
world: &legion::world::SubWorld,
#[resource] camera: &mut resources::Camera,
#[resource] player: &resources::Player,
) {
let mut positions = <&components::Position>::query();
let pos = positions.get(world, player.0).unwrap().0;
camera.position = nalgebra::Point2::new(pos.x as f64, pos.y as f64);
}
use crate::colors;
use crate::components;
use crate::resources;
#[legion::system(for_each)]
pub fn draw_tmp_player(
_player: &components::Player,
pos: &components::Position,
#[resource] rendering: &mut resources::Rendering,
) {
rendering.begin_path();
rendering.set_fill_style(&colors::PRIMARY3_SHADE1);
rendering.arc(
pos.0.x as f64,
pos.0.y as f64,
50.0,
0.0,
std::f64::consts::TAU,
);
rendering.fill();
}
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