This commit is contained in:
griffi-gh 2023-03-13 02:24:37 +01:00
parent 944737c75c
commit a719427eb5
4 changed files with 36 additions and 25 deletions

View file

@ -1,4 +1,4 @@
use shipyard::{UniqueView, NonSendSync, EntitiesViewMut, ViewMut, UniqueViewMut}; use shipyard::{UniqueView, NonSendSync, EntitiesViewMut, ViewMut, UniqueViewMut, AllStoragesView};
use uflow::{server::Event as ServerEvent, SendMode}; use uflow::{server::Event as ServerEvent, SendMode};
use kubi_shared::{ use kubi_shared::{
networking::{ networking::{
@ -10,8 +10,8 @@ use kubi_shared::{
}, },
client::{Client, ClientId}, channels::CHANNEL_AUTH client::{Client, ClientId}, channels::CHANNEL_AUTH
}, },
player::Player, player::{Player, PLAYER_HEALTH},
transform::Transform transform::Transform, entity::{Entity, Health}
}; };
use crate::{ use crate::{
server::{ServerEvents, UdpServer, IsMessageOfType}, server::{ServerEvents, UdpServer, IsMessageOfType},
@ -20,17 +20,14 @@ use crate::{
}; };
pub fn authenticate_players( pub fn authenticate_players(
mut entities: EntitiesViewMut, storages: AllStoragesView,
mut players: ViewMut<Player>,
mut clients: ViewMut<Client>,
mut client_addrs: ViewMut<ClientAddress>,
mut transforms: ViewMut<Transform>,
mut client_entity_map: UniqueViewMut<ClientIdMap>,
mut client_addr_map: UniqueViewMut<ClientAddressMap>,
server: NonSendSync<UniqueView<UdpServer>>,
events: UniqueView<ServerEvents>,
config: UniqueView<ConfigTable>
) { ) {
let mut client_entity_map = storages.borrow::<UniqueViewMut<ClientIdMap>>().unwrap();
let mut client_addr_map = storages.borrow::<UniqueViewMut<ClientAddressMap>>().unwrap();
let server = storages.borrow::<NonSendSync<UniqueView<UdpServer>>>().unwrap();
let events = storages.borrow::<UniqueView<ServerEvents>>().unwrap();
let config = storages.borrow::<UniqueView<ConfigTable>>().unwrap();
for event in &events.0 { for event in &events.0 {
let ServerEvent::Receive(client_addr, data) = event else{ let ServerEvent::Receive(client_addr, data) = event else{
continue continue
@ -93,17 +90,23 @@ pub fn authenticate_players(
}; };
//Spawn the user //Spawn the user
let entity_id = entities.add_entity(( let entity_id = {
&mut players, storages.borrow::<EntitiesViewMut>().unwrap().add_entity((
&mut clients, &mut storages.borrow::<ViewMut<Entity>>().unwrap(),
&mut client_addrs, &mut storages.borrow::<ViewMut<Player>>().unwrap(),
&mut transforms, &mut storages.borrow::<ViewMut<Health>>().unwrap(),
&mut storages.borrow::<ViewMut<Client>>().unwrap(),
&mut storages.borrow::<ViewMut<ClientAddress>>().unwrap(),
&mut storages.borrow::<ViewMut<Transform>>().unwrap(),
), ( ), (
Entity,
Player, Player,
Health::new(PLAYER_HEALTH),
Client(client_id), Client(client_id),
ClientAddress(*client_addr), ClientAddress(*client_addr),
Transform::default(), Transform::default(),
)); ))
};
//Add the user to the ClientIdMap and ClientAddressMap //Add the user to the ClientIdMap and ClientAddressMap
client_entity_map.0.insert(client_id, entity_id); client_entity_map.0.insert(client_id, entity_id);

View file

@ -9,7 +9,7 @@ pub struct Health {
pub max: u8, pub max: u8,
} }
impl Health { impl Health {
fn new(health: u8) -> Self { pub fn new(health: u8) -> Self {
Self { Self {
current: health, current: health,
max: health max: health

View file

@ -1,4 +1,6 @@
use shipyard::Component; use shipyard::Component;
pub const PLAYER_HEALTH: u8 = 20;
#[derive(Component)] #[derive(Component)]
pub struct Player; pub struct Player;

View file

@ -1,4 +1,8 @@
use shipyard::{Component, AllStoragesViewMut}; use shipyard::{Component, AllStoragesViewMut};
use kubi_shared::{
entity::{Entity, Health},
player::PLAYER_HEALTH
};
use crate::{ use crate::{
transform::Transform, transform::Transform,
camera::Camera, camera::Camera,
@ -18,6 +22,8 @@ pub fn spawn_player (
storages.add_entity(( storages.add_entity((
Player, Player,
MainPlayer, MainPlayer,
Entity,
Health::new(PLAYER_HEALTH),
Transform::default(), Transform::default(),
Camera::default(), Camera::default(),
FlyController, FlyController,