Skip to content

Commit

Permalink
Merge pull request #237 from AnthonyTornetta/233-make-asteroids-good
Browse files Browse the repository at this point in the history
233 make asteroids good
  • Loading branch information
AnthonyTornetta authored Dec 29, 2023
2 parents ba940db + 32a0183 commit c95ae94
Show file tree
Hide file tree
Showing 32 changed files with 792 additions and 214 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion cosmos_client/assets/cosmos/lang/blocks/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,7 @@ cosmos:reactor_controller=Reactor Controller
cosmos:reactor_casing=Reactor Casing
cosmos:reactor_window=Reactor Window
cosmos:reactor_cell=Reactor Power Cell
cosmos:fan=Fan
cosmos:fan=Fan
cosmos:storage=Storage
cosmos:station_core=Station Core
cosmos:test_ore=Test Ore
1 change: 1 addition & 0 deletions cosmos_client/assets/cosmos/lang/items/en_us.lang
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cosmos:test_crystal=Test Crystal
18 changes: 14 additions & 4 deletions cosmos_client/src/netty/gameplay/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,22 @@ pub(crate) fn client_sync_players(

entity_cmds.insert((structure, BiosphereMarker::new(biosphere)));
}
ServerReliableMessages::NumberOfChunks {
entity: server_entity,
chunks_needed,
} => {
let Some(entity) = network_mapping.client_from_server(&server_entity) else {
continue;
};

if let Some(mut ecmds) = commands.get_entity(entity) {
ecmds.insert(chunks_needed);
}
}
ServerReliableMessages::Ship {
entity: server_entity,
body,
dimensions,
chunks_needed,
} => {
let Some(entity) = network_mapping.client_from_server(&server_entity) else {
continue;
Expand All @@ -451,7 +462,7 @@ pub(crate) fn client_sync_players(
let builder = ClientShipBuilder::default();
builder.insert_ship(&mut entity_cmds, location, body.create_velocity(), &mut structure);

entity_cmds.insert((structure, chunks_needed));
entity_cmds.insert((structure /*chunks_needed*/,));

client.send_message(
NettyChannelClient::Reliable,
Expand All @@ -464,7 +475,6 @@ pub(crate) fn client_sync_players(
entity: server_entity,
body,
dimensions,
chunks_needed,
} => {
let Some(entity) = network_mapping.client_from_server(&server_entity) else {
continue;
Expand All @@ -489,7 +499,7 @@ pub(crate) fn client_sync_players(
let builder = ClientStationBuilder::default();
builder.insert_station(&mut entity_cmds, location, &mut structure);

entity_cmds.insert((structure, chunks_needed));
entity_cmds.insert((structure /*chunks_needed*/,));
}
ServerReliableMessages::ChunkData {
structure_entity: server_structure_entity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ impl Default for ClientAsteroidBuilder {
}

impl TAsteroidBuilder for ClientAsteroidBuilder {
fn insert_asteroid(&self, entity: &mut EntityCommands, location: Location, structure: &mut Structure) {
self.builder.insert_asteroid(entity, location, structure);
fn insert_asteroid(&self, entity: &mut EntityCommands, location: Location, structure: &mut Structure, temperature: f32) {
self.builder.insert_asteroid(entity, location, structure, temperature);
}
}

Expand Down
3 changes: 2 additions & 1 deletion cosmos_client/src/structure/asteroid/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn receive_asteroids(
entity: server_entity,
body,
dimensions,
temperature,
} => {
let Some(entity) = network_mapping.client_from_server(&server_entity) else {
continue;
Expand All @@ -54,7 +55,7 @@ fn receive_asteroids(

let builder = ClientAsteroidBuilder::default();

builder.insert_asteroid(&mut entity_cmds, location, &mut structure);
builder.insert_asteroid(&mut entity_cmds, location, &mut structure, temperature);

entity_cmds.insert(structure);
}
Expand Down
9 changes: 9 additions & 0 deletions cosmos_client/src/ui/ship_flight/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use cosmos_core::{
entities::player::Player,
physics::location::Location,
structure::{
asteroid::Asteroid,
planet::Planet,
ship::{pilot::Pilot, Ship},
station::Station,
Expand Down Expand Up @@ -233,6 +234,7 @@ fn add_indicators(
fn added(
ship_query: Query<Entity, Added<Ship>>,
station_query: Query<Entity, Added<Station>>,
asteroid_query: Query<Entity, Added<Asteroid>>,
planet_query: Query<Entity, Added<Planet>>,
player_query: Query<Entity, (Added<Player>, Without<LocalPlayer>)>,
mut commands: Commands,
Expand All @@ -258,6 +260,13 @@ fn added(
offset: Vec3::ZERO,
});
});
asteroid_query.for_each(|ent| {
commands.entity(ent).insert(IndicatorSettings {
color: Color::hex("6159427F").unwrap(),
max_distance: 20_000.0,
offset: Vec3::ZERO,
});
});
player_query.for_each(|ent| {
commands.entity(ent).insert(IndicatorSettings {
color: Color::hex("FFFFFF7F").unwrap(),
Expand Down
6 changes: 6 additions & 0 deletions cosmos_core/src/block/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,12 @@ fn add_cosmos_blocks(
.create(),
);

blocks.register(
BlockBuilder::new("cosmos:test_ore", 10.0, 50.0)
.add_property(BlockProperty::Full)
.create(),
);

loading.finish_loading(id, &mut end_writer);
}

Expand Down
33 changes: 18 additions & 15 deletions cosmos_core/src/item/items.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
//! Loads all the items for cosmos & adds the item registry.
// use crate::loader::{AddLoadingEvent, DoneLoadingEvent, LoadingManager};
use crate::registry;
use bevy::prelude::App;
use crate::loader::{AddLoadingEvent, DoneLoadingEvent, LoadingManager};
use crate::registry::{self, Registry};
use bevy::prelude::*;

use super::Item;
use super::{Item, DEFAULT_MAX_STACK_SIZE};

// pub fn add_cosmos_items(
// mut items: ResMut<Registry<Item>>,
// mut loading: ResMut<LoadingManager>,
// mut end_writer: EventWriter<DoneLoadingEvent>,
// mut start_writer: EventWriter<AddLoadingEvent>,
// ) {
// let id = loading.register_loader(&mut start_writer);
// loading.finish_loading(id, &mut end_writer);
// }
fn add_cosmos_items(
mut items: ResMut<Registry<Item>>,
mut loading: ResMut<LoadingManager>,
mut end_writer: EventWriter<DoneLoadingEvent>,
mut start_writer: EventWriter<AddLoadingEvent>,
) {
let id = loading.register_loader(&mut start_writer);

pub(super) fn register(app: &mut App) {
items.register(Item::new("cosmos:test_crystal", DEFAULT_MAX_STACK_SIZE));

loading.finish_loading(id, &mut end_writer);
}

pub(super) fn register<T: States>(app: &mut App, loading_state: T) {
registry::create_registry::<Item>(app);

// app.add_system_set(SystemSet::on_enter(loading_state).with_system(add_cosmos_items));
app.add_systems(OnEnter(loading_state), add_cosmos_items);
}
10 changes: 5 additions & 5 deletions cosmos_core/src/item/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pub mod items;

use bevy::prelude::App;
use bevy::{ecs::schedule::States, prelude::App};

use crate::registry::identifiable::Identifiable;

Expand Down Expand Up @@ -35,9 +35,9 @@ pub const DEFAULT_MAX_STACK_SIZE: u16 = 999;

impl Item {
/// Creates an item
pub fn new(unlocalized_name: String, max_stack_size: u16) -> Self {
pub fn new(unlocalized_name: impl Into<String>, max_stack_size: u16) -> Self {
Self {
unlocalized_name,
unlocalized_name: unlocalized_name.into(),
numeric_id: 0, // this will get set when this item is registered
max_stack_size,
}
Expand All @@ -49,6 +49,6 @@ impl Item {
}
}

pub(super) fn register(app: &mut App) {
items::register(app);
pub(super) fn register<T: States>(app: &mut App, loading_state: T) {
items::register(app, loading_state);
}
11 changes: 7 additions & 4 deletions cosmos_core/src/netty/server_reliable_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ pub enum ServerReliableMessages {
/// Planet's location
location: Location,
},
/// This is sent whenever `SendAllChunks` is requested - it is used to specify how much chunks you should expect before marking the structure as loaded
NumberOfChunks {
/// The fixed structure's server entity.
entity: Entity,
/// The number of chunks that need to be loaded from the server.
chunks_needed: ChunksNeedLoaded,
},
/// A ship should be created on the client-side.
/// This does NOT mean the ship was just created by the sever, just that one should be created on the client.
Ship {
Expand All @@ -125,8 +132,6 @@ pub enum ServerReliableMessages {
body: NettyRigidBody,
/// The width to be passed into the structure's constructor.
dimensions: ChunkCoordinate,
/// The number of chunks that need to be loaded from the server.
chunks_needed: ChunksNeedLoaded,
},
/// A station should be created on the client-side.
/// This does NOT mean the station was just created by the sever, just that one should be created on the client.
Expand All @@ -137,8 +142,6 @@ pub enum ServerReliableMessages {
body: NettyRigidBody,
/// The width to be passed into the structure's constructor.
dimensions: ChunkCoordinate,
/// The number of chunks that need to be loaded from the server.
chunks_needed: ChunksNeedLoaded,
},
/// Represents the server's message of the day.
MOTD {
Expand Down
2 changes: 1 addition & 1 deletion cosmos_core/src/plugin/cosmos_core_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl<T: States + Clone + Copy> Plugin for CosmosCorePlugin<T> {
self.post_loading_state,
self.playing_state,
);
item::register(app);
item::register(app, self.loading_state);
blockitems::register(app, self.post_loading_state);
physics::register(app, self.post_loading_state);
events::register(app, self.playing_state);
Expand Down
6 changes: 3 additions & 3 deletions cosmos_core/src/structure/asteroid/asteroid_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use super::*;
/// Implement this to add a custom way to build asteroids
pub trait TAsteroidBuilder {
/// Adds everything to the entity needed to have an asteroid
fn insert_asteroid(&self, entity: &mut EntityCommands, location: Location, structure: &mut Structure);
fn insert_asteroid(&self, entity: &mut EntityCommands, location: Location, structure: &mut Structure, temperature: f32);
}

/// Default way to build an asteroid
Expand All @@ -30,12 +30,12 @@ impl<T: TStructureBuilder> AsteroidBuilder<T> {
}

impl<T: TStructureBuilder> TAsteroidBuilder for AsteroidBuilder<T> {
fn insert_asteroid(&self, entity: &mut EntityCommands, location: Location, structure: &mut Structure) {
fn insert_asteroid(&self, entity: &mut EntityCommands, location: Location, structure: &mut Structure, temperature: f32) {
self.structure_builder
.insert_structure(entity, location, Velocity::default(), structure);

entity.insert((
Asteroid,
Asteroid::new(temperature),
Name::new("Asteroid"),
RigidBody::Fixed,
LoadingDistance::new(ASTEROID_LOAD_RADIUS, ASTEROID_UNLOAD_RADIUS),
Expand Down
2 changes: 2 additions & 0 deletions cosmos_core/src/structure/asteroid/asteroid_netty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ pub enum AsteroidServerMessages {
body: NettyRigidBody,
/// The width to be passed into the structure's constructor
dimensions: ChunkCoordinate,
/// The asteroid's temperature
temperature: f32,
},
}
22 changes: 18 additions & 4 deletions cosmos_core/src/structure/asteroid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,24 @@ pub mod asteroid_netty;
pub mod loading;

/// How far away an asteroid should be loaded
pub const ASTEROID_LOAD_RADIUS: u32 = 5;
pub const ASTEROID_LOAD_RADIUS: u32 = 2;
/// How far away an asteroid should be unloaded
pub const ASTEROID_UNLOAD_RADIUS: u32 = 6;
pub const ASTEROID_UNLOAD_RADIUS: u32 = 3;

#[derive(Debug, Component, Default, Reflect)]
#[derive(Debug, Component, Reflect)]
/// A small structure that is non-controllable
pub struct Asteroid;
pub struct Asteroid {
temperature: f32,
}

impl Asteroid {
/// Creates a new asteroid with this temperature
pub fn new(temperature: f32) -> Self {
Self { temperature }
}

/// Gets the asteroid's temperature
pub fn temperature(&self) -> f32 {
self.temperature
}
}
5 changes: 5 additions & 0 deletions cosmos_core/src/structure/full_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ impl FullStructure {
self.loaded = true;
}

/// Returns true if the `set_loaded` member function has been called.
pub fn is_loaded(&self) -> bool {
self.loaded
}

/// Returns the chunk's state
pub fn get_chunk_state(&self, coords: ChunkCoordinate) -> ChunkState {
if !self.is_within_chunks(coords) {
Expand Down
2 changes: 0 additions & 2 deletions cosmos_core/src/structure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::ops::DerefMut;
use std::sync::{Arc, Mutex};

use bevy::app::Update;
use bevy::log::info;
use bevy::prelude::{App, Event, IntoSystemConfigs, Name, PreUpdate, VisibilityBundle};
use bevy::reflect::Reflect;
use bevy::transform::TransformBundle;
Expand Down Expand Up @@ -650,7 +649,6 @@ fn add_chunks_system(
};

if !chunk.is_empty() && structure.chunk_entity(chunk_coordinate).is_none() {
info!("Spawning chunk entity!");
spawn_chunk_entity(
&mut commands,
&mut structure,
Expand Down
Loading

0 comments on commit c95ae94

Please sign in to comment.