Skip to content

Commit

Permalink
Merge pull request #200 from AnthonyTornetta/199-build-mode
Browse files Browse the repository at this point in the history
Adding build mode for structures
  • Loading branch information
AnthonyTornetta authored Oct 26, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 30919bb + 487f5a7 commit df5408f
Showing 42 changed files with 1,157 additions and 161 deletions.
34 changes: 14 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -7,14 +7,14 @@ If you're interested, join our new discord server here:

## Screenshots

Cosmos has ships that you can pilot through space. (And yes those are working laser cannons.)
![](./showcase/ship_in_space.png)
Cosmos is a game where you can create your dream spaceship that you can pilot through space. (And add those are working laser cannons.)
![](./showcase/gunship_top.png)

Build your own ship block by block and walk inside while flying through space.
![](./showcase/inside_ship.png)
Build your own ship block by block and walk inside with friends while cruising through space.
![](./showcase/gunner_flying_towards_planet.png)

Once you reach your destination, make a _hopefully safer than this_ landing.
![](./showcase/ship_on_ground.png)
![](./showcase/safely_landed_gunner.png)

## Compilation

@@ -34,10 +34,6 @@ For the server, navigate to the cosmos_server directory and run

For release builds, append the `--release` flag to the build/run commands.

To compile the workspace (not normally necessary), navigate to the root directory of the repo and run

`cargo build`

## Documentation

The first time you view the cosmos documentation, run the following commands
@@ -98,6 +94,15 @@ See [the issues page](https://github.com/AnthonyTornetta/Cosmos/issues) for the
- [x] Update to bevy 0.11
- [x] Update physics
- [x] Update bevy
- [x] GUI to interact with inventory
- [x] Easier way of adding 3d blocks to GUI
- [x] Move items around in inventory via mouse
- [x] Structure build mode
- [x] Interact with build block to enter build mode
- [x] Build mode
- [x] Camera becomes a noclip free cam and goes outside of player's body.
- [x] Player no longer piloting ship, & is able to create + destroy blocks on the ship but ONLY the ship
- [x] Symmetry modes are added that will mirror blocks on user-defined axis

## Release 0.0.4a

@@ -251,14 +256,6 @@ See [the issues page](https://github.com/AnthonyTornetta/Cosmos/issues) for the

## Everything that will still have to be done after 0.0.5a

- [ ] GUI to interact with inventory
- [ ] Easier way of adding 3d blocks to GUI
- [ ] Move items around in inventory via mouse
- [ ] Structure build mode
- [ ] Press B while piloting ship to enter build mode
- [ ] Build mode
- [ ] Camera becomes a noclip free cam and goes outside of player's body.
- [ ] Player no longer piloting ship, & is able to create + destroy blocks on the ship but ONLY the ship
- [ ] Mining beam system
- [ ] Mining beam block
- [ ] Can be placed in line to create more powerful miners
@@ -279,9 +276,6 @@ See [the issues page](https://github.com/AnthonyTornetta/Cosmos/issues) for the
- [ ] Camera block
- [ ] Use left/right to switch between ship cameras
- [ ] Changes where your view is
- [ ] Planet Generation
- [ ] New planet types
- [ ] A bunch of new blocks
- [ ] Shops
- [ ] Sell blocks/items
- [ ] Buy blocks/items
70 changes: 70 additions & 0 deletions cosmos_client/assets/cosmos/shaders/repeated.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Stolen and heavily modified from: https://github.com/janhohenheim/foxtrot/blob/main/assets/shaders/repeated.wgsl - TY!

#import bevy_pbr::mesh_vertex_output MeshVertexOutput
#import bevy_pbr::mesh_vertex_output as OutputTypes
#import bevy_pbr::pbr_functions as PbrCore
#import bevy_pbr::pbr_bindings as MaterialBindings
#import bevy_pbr::pbr_types as PbrTypes
#import bevy_pbr::mesh_view_bindings as ViewBindings

struct Repeats {
horizontal: u32,
vertical: u32,
_wasm_padding1: u32,
_wasm_padding2: u32,
}

@group(1) @binding(0)
var texture: texture_2d<f32>;
@group(1) @binding(1)
var texture_sampler: sampler;
@group(1) @binding(2)
var<uniform> repeats: Repeats;
@group(1) @binding(3)
var<uniform> color: vec4<f32>;

fn get_texture_sample(coords: vec2<f32>) -> vec4<f32> {
let repeated_coords = vec2<f32>(
(coords.x % (1. / f32(repeats.horizontal))) * f32(repeats.horizontal),
(coords.y % (1. / f32(repeats.vertical))) * f32(repeats.vertical)
);
return textureSample(texture, texture_sampler, repeated_coords);
}

/// Adapted from <https://github.com/bevyengine/bevy/blob/main/crates/bevy_pbr/src/render/pbr.wgsl#L30>
fn get_pbr_output(in: MeshVertexOutput) -> vec4<f32> {
var material = PbrTypes::standard_material_new();
material.perceptual_roughness = 1.0;

var output_color: vec4<f32> = color;

output_color = PbrCore::alpha_discard(material, output_color);

#ifdef TONEMAP_IN_SHADER
output_color = tone_mapping(output_color);
#endif
#ifdef DEBAND_DITHER
var output_rgb = output_color.rgb;
output_rgb = powsafe(output_rgb, 1.0 / 2.2);
output_rgb = output_rgb + screen_space_dither(in.frag_coord.xy);
// This conversion back to linear space is required because our output texture format is
// SRGB; the GPU will assume our output is linear and will apply an SRGB conversion.
output_rgb = powsafe(output_rgb, 2.2);
output_color = vec4(output_rgb, output_color.a);
#endif
#ifdef PREMULTIPLY_ALPHA
output_color = premultiply_alpha(material.flags, output_color);
#endif
return output_color;
}

@fragment
fn fragment(mesh: MeshVertexOutput) -> @location(0) vec4<f32> {
let texture = get_texture_sample(mesh.uv);
if (texture[3] < 0.5) {
discard;
}
let pbr_output = get_pbr_output(mesh);

return texture * pbr_output;
}
Binary file added cosmos_client/assets/images/blocks/build_block.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cosmos_client/assets/images/misc/symmetry.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions cosmos_client/assets/lang/blocks/en_us.lang
Original file line number Diff line number Diff line change
@@ -18,3 +18,4 @@ cosmos:molten_stone=Molten Stone
cosmos:cheese=Cheese (Lava)
cosmos:ice=Ice
cosmos:water=Water
cosmos:build_block=Build Block
2 changes: 2 additions & 0 deletions cosmos_client/src/asset/mod.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,9 @@ use bevy::prelude::App;

pub mod asset_loader;
pub mod asset_loading;
pub mod repeating_material;

pub(super) fn register(app: &mut App) {
asset_loading::register(app);
repeating_material::register(app);
}
42 changes: 42 additions & 0 deletions cosmos_client/src/asset/repeating_material.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! A garbage repeated material. Don't use this.
use bevy::{
prelude::{AlphaMode, App, Color, Handle, Image, Material, MaterialPlugin},
reflect::{Reflect, TypeUuid},
render::render_resource::{AsBindGroup, ShaderRef, ShaderType},
};

#[repr(C, align(16))] // All WebGPU uniforms must be aligned to 16 bytes
#[derive(Clone, Copy, ShaderType, Debug, Hash, Eq, PartialEq, Default, Reflect)]
pub(crate) struct Repeats {
pub(crate) horizontal: u32,
pub(crate) vertical: u32,
pub(crate) _wasm_padding1: u32,
pub(crate) _wasm_padding2: u32,
}

#[derive(AsBindGroup, Debug, Clone, TypeUuid, Reflect)]
#[uuid = "82d336c5-fd6c-41a3-bdd4-267cd4c9be22"]
pub(crate) struct UnlitRepeatedMaterial {
#[texture(0)]
#[sampler(1)]
pub(crate) texture: Handle<Image>,
#[uniform(2)]
pub(crate) repeats: Repeats,
#[uniform(3)]
pub(crate) color: Color,
}

impl Material for UnlitRepeatedMaterial {
fn fragment_shader() -> ShaderRef {
"cosmos/shaders/repeated.wgsl".into()
}

fn alpha_mode(&self) -> AlphaMode {
AlphaMode::Mask(0.5)
}
}

pub(super) fn register(app: &mut App) {
app.add_plugins(MaterialPlugin::<UnlitRepeatedMaterial>::default());
}
7 changes: 5 additions & 2 deletions cosmos_client/src/entities/player/player_movement.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
use bevy::prelude::*;
use bevy_rapier3d::prelude::Velocity;
use cosmos_core::structure::ship::pilot::Pilot;
use cosmos_core::structure::ship::{build_mode::BuildMode, pilot::Pilot};

use crate::{
input::inputs::{CosmosInputs, InputChecker, InputHandler},
@@ -15,7 +15,10 @@ use crate::{
fn process_player_movement(
time: Res<Time>,
input_handler: InputChecker,
mut query: Query<(Entity, &mut Velocity, &Transform, Option<&PlayerAlignment>), (With<LocalPlayer>, Without<Pilot>)>,
mut query: Query<
(Entity, &mut Velocity, &Transform, Option<&PlayerAlignment>),
(With<LocalPlayer>, Without<Pilot>, Without<BuildMode>),
>,
cam_query: Query<&Transform, With<MainCamera>>,
parent_query: Query<&Parent>,
global_transform_query: Query<&GlobalTransform>,
19 changes: 16 additions & 3 deletions cosmos_client/src/events/ship/create_ship.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
//! Event & its processing for when a player wants to create a ship
use bevy::prelude::{in_state, App, Event, EventReader, EventWriter, IntoSystemConfigs, ResMut, Update};
use bevy::prelude::{in_state, App, Event, EventReader, EventWriter, IntoSystemConfigs, Query, ResMut, Update, With};
use bevy_renet::renet::RenetClient;
use cosmos_core::netty::{client_reliable_messages::ClientReliableMessages, cosmos_encoder, NettyChannelClient};
use cosmos_core::{
netty::{client_reliable_messages::ClientReliableMessages, cosmos_encoder, NettyChannelClient},
structure::ship::build_mode::BuildMode,
};

use crate::{
input::inputs::{CosmosInputs, InputChecker, InputHandler},
netty::flags::LocalPlayer,
state::game_state::GameState,
};

@@ -15,7 +19,16 @@ pub struct CreateShipEvent {
name: String,
}

fn listener(input_handler: InputChecker, mut event_writer: EventWriter<CreateShipEvent>) {
fn listener(
in_build_mode: Query<(), (With<LocalPlayer>, With<BuildMode>)>,
input_handler: InputChecker,
mut event_writer: EventWriter<CreateShipEvent>,
) {
if in_build_mode.get_single().is_ok() {
// Don't create ships while in build mode
return;
}

if input_handler.check_just_pressed(CosmosInputs::CreateShip) {
event_writer.send(CreateShipEvent { name: "Cool name".into() });
}
17 changes: 17 additions & 0 deletions cosmos_client/src/input/inputs.rs
Original file line number Diff line number Diff line change
@@ -100,6 +100,17 @@ pub enum CosmosInputs {
ToggleInventory,
/// "Shift-Clicking" an item in minecraft
AutoMoveItem,

/// Toggles the player between being in build mode and not on a ship
ToggleBuildMode,
/// A toggle to clear the symmetry - when combined with a symmetry key the symmetry will be cleared
ClearSymmetry,
/// Creates an X symmetry
SymmetryX,
/// Creates a Y symmetry
SymmetryY,
/// Creates a Z symmetry
SymmetryZ,
}

fn init_input(mut input_handler: ResMut<CosmosInputHandler>) {
@@ -154,6 +165,12 @@ fn init_input(mut input_handler: ResMut<CosmosInputHandler>) {

input_handler.set_keycode(CosmosInputs::ToggleInventory, KeyCode::T);
input_handler.set_keycode(CosmosInputs::AutoMoveItem, KeyCode::ShiftLeft);

input_handler.set_keycode(CosmosInputs::ToggleBuildMode, KeyCode::B);
input_handler.set_keycode(CosmosInputs::ClearSymmetry, KeyCode::ShiftLeft);
input_handler.set_keycode(CosmosInputs::SymmetryX, KeyCode::X);
input_handler.set_keycode(CosmosInputs::SymmetryY, KeyCode::Y);
input_handler.set_keycode(CosmosInputs::SymmetryZ, KeyCode::Z);
}

#[derive(Resource, Default, Debug)]
Loading

0 comments on commit df5408f

Please sign in to comment.