Skip to content

Feature/3d models #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/target

Cargo.lock

*.blend1
8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ proc_macros = { path = "proc_macros" }
bevy = { workspace = true }
server = { workspace = true, optional = true, default-features = false }
spectator_client = { workspace = true, optional = true, default-features = false }
rand = "0.9.0"

[features]
default = ["debug"]
debug = ["bevy/bevy_dev_tools", "bevy/file_watcher"]
default = []
server = ["server/server"]
server_debug = ["debug", "server", "server/debug"]
server_debug = ["server", "server/debug"]
spectator_client = ["spectator_client/spectator_client"]
spectator_client_debug = ["debug", "spectator_client", "spectator_client/debug"]
spectator_client_debug = ["spectator_client", "spectator_client/debug"]

# Enable a small amount of optimization in debug mode
[profile.dev]
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ The project is split into multiple members.
- Smoke: low damage, high splash, low visibility (great for cover)
- Generally bad vision
- Depends much on the vision of other units

# Credit
- Tank Models made by Alexander Siegmann
3 changes: 3 additions & 0 deletions assets/config/config.tanks.ron
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
RIGHT: 0.2,
},
respawnTimer: 25,
model: "light_tank",
),
HEAVY_TANK: (
moveSpeed: 0.25,
Expand Down Expand Up @@ -65,6 +66,7 @@
RIGHT: 0.2,
},
respawnTimer: 25,
model: "heavy_tank",
),
SELF_PROPELLED_ARTILLERY: (
moveSpeed: 0.1,
Expand Down Expand Up @@ -98,6 +100,7 @@
RIGHT: 0.2,
},
respawnTimer: 25,
model: "light_tank",
),
},
)
Binary file added assets/models/tanks/blend/heavy_tank.blend
Binary file not shown.
Binary file added assets/models/tanks/blend/light_tank.blend
Binary file not shown.
Binary file added assets/models/tanks/exported/heavy_tank.glb
Binary file not shown.
Binary file added assets/models/tanks/exported/light_tank.glb
Binary file not shown.
10 changes: 9 additions & 1 deletion server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,13 @@ server = [
"bevy/bevy_color",
"bevy/serialize",
"bevy/multi_threaded",
"bevy/file_watcher",
"shared/server",
]
debug = [
"bevy/default",
"bevy-inspector-egui",
"shared/debug",
"bevy_flycam",
"bevy/bevy_dev_tools",
]
debug = ["bevy/default", "bevy-inspector-egui", "shared/debug", "bevy_flycam"]
4 changes: 2 additions & 2 deletions server/src/gameplay/handle_players/dummy_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn simulate_movement(
.get_tank_type_config(tank_type)
.expect("Failed to get tank config");

// Simulate movement (always forward)
/* // Simulate movement (always forward)
commands.trigger_targets(
MoveTankCommandTrigger {
sender: None,
Expand Down Expand Up @@ -86,7 +86,7 @@ pub fn simulate_movement(
},
},
*player,
);
); */

// Simulate movement (randomly)
/* if rand::random::<bool>() {
Expand Down
11 changes: 11 additions & 0 deletions server/src/gameplay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct MyGameplayPlugin;
impl Plugin for MyGameplayPlugin {
fn build(&self, app: &mut App) {
app.register_type::<CleanupNextTick>()
.register_type::<LobbyWaitTicksUntilStart>()
.configure_sets(
Update,
(
Expand Down Expand Up @@ -69,3 +70,13 @@ fn add_observers_to_lobby(trigger: Trigger<OnAdd, MyLobby>, mut commands: Comman
.observe(process_messages::process_lobby_messages)
.observe(lobby_cleanup::cleanup_entities);
}

#[derive(Debug, Component, Reflect, Deref, DerefMut)]
#[reflect(Component)]
pub struct LobbyWaitTicksUntilStart(pub u32);

impl Default for LobbyWaitTicksUntilStart {
fn default() -> Self {
Self(1)
}
}
9 changes: 7 additions & 2 deletions server/src/gameplay/start_lobby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ use shared::{

use crate::networking::handle_clients::lib::MyNetworkClient;

use super::handle_players::{
dummy_handling::DummyClientMarker, handle_spawning::RespawnPlayerTrigger,
use super::{
handle_players::{dummy_handling::DummyClientMarker, handle_spawning::RespawnPlayerTrigger},
LobbyWaitTicksUntilStart,
};

#[derive(Debug, Event)]
Expand Down Expand Up @@ -244,6 +245,10 @@ pub fn start_lobby(
tank_configs: tank_configs.tanks.clone(),
}),
));

commands
.entity(lobby_entity)
.insert(LobbyWaitTicksUntilStart::default());
}

lobby_management
Expand Down
18 changes: 14 additions & 4 deletions server/src/gameplay/tick_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use shared::{

use crate::gameplay::triggers::CollectAndTriggerMessagesTrigger;

use super::{system_sets::MyGameplaySet, triggers::StartNextTickProcessingTrigger};
use super::{
system_sets::MyGameplaySet, triggers::StartNextTickProcessingTrigger, LobbyWaitTicksUntilStart,
};

pub struct TickSystemsPlugin;

Expand All @@ -22,13 +24,21 @@ impl Plugin for TickSystemsPlugin {

fn process_tick_timer(
mut commands: Commands,
mut lobbies: Query<(Entity, &mut MyLobby)>,
mut lobbies: Query<(Entity, &mut MyLobby, Option<&mut LobbyWaitTicksUntilStart>)>,
time: Res<Time>,
) {
for (entity, mut lobby) in lobbies.iter_mut() {
for (entity, mut lobby, lobby_wait_tick_until_start) in lobbies.iter_mut() {
if LobbyState::InProgress == lobby.state {
if lobby.tick_timer.tick(time.delta()).just_finished() {
commands.trigger_targets(StartNextTickProcessingTrigger, entity);
if let Some(mut lobby_wait_tick_until_start) = lobby_wait_tick_until_start {
if lobby_wait_tick_until_start.0 > 0 {
lobby_wait_tick_until_start.0 -= 1;
} else {
commands.entity(entity).remove::<LobbyWaitTicksUntilStart>();
}
} else {
commands.trigger_targets(StartNextTickProcessingTrigger, entity);
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ serde_json = { workspace = true }
default = ["shared", "debug"]
shared = ["bevy/serialize"]
debug = []
server = []
spectator_client = []
19 changes: 19 additions & 0 deletions shared/src/asset_handling/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use bevy_asset_loader::{
config::{ConfigureLoadingState, LoadingStateConfig},
LoadingStateAppExt,
},
mapped::AssetFileStem,
};
use bevy_common_assets::ron::RonAssetPlugin;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -43,6 +44,10 @@ struct MyConfigAsset {
client: Handle<ClientConfig>,
#[asset(path = "config/config.tanks.ron")]
tank: Handle<TankConfigs>,
#[cfg(feature = "spectator_client")]
#[reflect(ignore)]
#[asset(path = "models/tanks/exported", collection(mapped, typed))]
tank_models: HashMap<AssetFileStem, Handle<Gltf>>,
}

#[derive(Debug, Default, Reflect, Clone, Asset, Deserialize)]
Expand Down Expand Up @@ -102,6 +107,10 @@ pub struct TankConfig {
pub armor: HashMap<Side, f32>,
/// The time in ticks it takes for the tank to respawn after dying
pub respawn_timer: u32,
#[cfg(feature = "spectator_client")]
/// The model of the tank
#[serde(default, skip_serializing_if = "String::is_empty")]
pub model: String,
}

#[derive(SystemParam)]
Expand Down Expand Up @@ -154,4 +163,14 @@ impl<'w> TankConfigSystemParam<'w> {
.get(self.config_asset.tank.id())
.expect("Tank configs not loaded")
}

#[cfg(feature = "spectator_client")]
pub fn get_tank_model(&self, model: &str) -> Handle<Gltf> {
self.config_asset
.tank_models
.iter()
.find(|(stem, _)| stem.as_ref() == model)
.map(|(_, handle)| handle.clone())
.expect("Failed to get tank model")
}
}
10 changes: 8 additions & 2 deletions spectator_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@ bevy_mod_billboard = { git = "https://github.com/voximity/bevy_mod_billboard.git

[features]
default = ["spectator_client", "debug"]
spectator_client = ["bevy/default", "bevy/serialize"]
debug = ["bevy-inspector-egui", "shared/debug"]
spectator_client = [
"bevy/default",
"bevy/serialize",
"shared/spectator_client",
"bevy/file_watcher",
"shared/spectator_client",
]
debug = ["bevy-inspector-egui", "shared/debug", "bevy/bevy_dev_tools"]
2 changes: 1 addition & 1 deletion spectator_client/src/game_handling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn add_observers(trigger: Trigger<OnAdd, MyNetworkStream>, mut commands: Command
commands
.entity(trigger.entity())
.observe(game_starts::game_starts)
.observe(player_handling::update_player_state_on_game_state_update)
//.observe(player_handling::update_player_state_on_game_state_update)
.observe(projectile_handling::handle_projectile_on_game_state_update)
.observe(despawn_delayed::despawn_delayed_entites);
}
Expand Down
14 changes: 10 additions & 4 deletions spectator_client/src/game_handling/player_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use shared::{
networking::messages::message_container::GameStateTrigger,
};

use crate::VisualOffset;

use super::entity_mapping::MyEntityMapping;

pub fn update_player_state_on_game_state_update(
Expand All @@ -20,6 +22,7 @@ pub fn update_player_state_on_game_state_update(
&mut ShootCooldown,
&mut PlayerState,
&TankBodyMarker,
&VisualOffset,
),
Without<TankTurretMarker>,
>,
Expand All @@ -37,6 +40,7 @@ pub fn update_player_state_on_game_state_update(
mut shoot_cooldown,
mut player_state,
tank_body,
body_visual_offset,
)) = tank_body.get_mut(client_side_entity)
{
health.health = server_side_client_state
Expand All @@ -56,12 +60,13 @@ pub fn update_player_state_on_game_state_update(
.clone();

// TRANSFORM UPDATES
let new_body_transform = server_side_client_state
let mut new_body_transform = server_side_client_state
.as_ref()
.expect("Client state is missing")
.transform_body
.clone()
.expect("Position is missing");
new_body_transform.translation -= body_visual_offset.0;

// Setting the actual current body transform to the next target body transform (the one that was previously set)
current_body_transform.translation = next_target_body_transform.translation;
Expand All @@ -71,15 +76,16 @@ pub fn update_player_state_on_game_state_update(
next_target_body_transform.translation = new_body_transform.translation;
next_target_body_transform.rotation = new_body_transform.rotation;

let (mut current_turret_transform, mut next_target_turret_transform) = tank_turret
.get_mut(tank_body.turret.expect("Failed to get turret entity"))
.expect("Failed to get turret");

let new_turret_transform = server_side_client_state
.as_ref()
.expect("Client state is missing")
.transform_turret
.clone()
.expect("Position is missing");
let (mut current_turret_transform, mut next_target_turret_transform) = tank_turret
.get_mut(tank_body.turret.expect("Failed to get turret entity"))
.expect("Failed to get turret");

current_turret_transform.translation = next_target_turret_transform.translation;
current_turret_transform.rotation = next_target_turret_transform.rotation;
Expand Down
7 changes: 6 additions & 1 deletion spectator_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ impl Plugin for MySpectatorClientPlugin {
.add_sub_state::<MyGameState>()
.enable_state_scoped_entities::<MyGameState>()
.register_type::<MyEntityMapping>()
.init_resource::<MyEntityMapping>();
.init_resource::<MyEntityMapping>()
.register_type::<VisualOffset>();

#[cfg(feature = "debug")]
{
Expand Down Expand Up @@ -71,3 +72,7 @@ fn change_camera_transform(
let center_of_map = game_config.map_definition.get_center_of_map();
camera_transform.look_at(center_of_map, Vec3::Y);
}

#[derive(Debug, Component, Reflect, Default, Deref, DerefMut)]
#[reflect(Component)]
pub struct VisualOffset(pub Vec3);
5 changes: 4 additions & 1 deletion spectator_client/src/map_visualization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use shared::{
};
use visualize_colliders::{visualize_colliders, MyColliderGizmos};
use visualize_markers::{draw_markers, MyMarkerGizmos};
use visualize_players::{setup_tank, AwaitsSetup, TeamColorMaterialsResource};
use visualize_positions::{visualize_cells, MyPositionGizmos};
use visulize_turret_ranges::{draw_turret_ranges, MyTurretRangeGizmos};

Expand All @@ -24,6 +25,8 @@ pub struct MyMapVisualizationPlugin;
impl Plugin for MyMapVisualizationPlugin {
fn build(&self, app: &mut App) {
app.register_type::<MapMeshMarker>()
.register_type::<AwaitsSetup>()
.register_type::<TeamColorMaterialsResource>()
.init_gizmo_group::<MyMarkerGizmos>()
.init_gizmo_group::<MyPositionGizmos>()
.init_gizmo_group::<MyTurretRangeGizmos>()
Expand All @@ -34,7 +37,7 @@ impl Plugin for MyMapVisualizationPlugin {
(listen_for_map_changes,).run_if(any_with_component::<MapMeshMarker>),
(draw_turret_ranges,).run_if(any_with_component::<TankTurretMarker>),
(visualize_colliders,).run_if(any_with_component::<Collider>),
(draw_markers, visualize_cells),
(draw_markers, visualize_cells, setup_tank),
)
.run_if(resource_exists::<GameStarts>),)
.run_if(in_state(MyMainState::Ready)),
Expand Down
13 changes: 10 additions & 3 deletions spectator_client/src/map_visualization/visualize_colliders.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
use bevy::{color::palettes::css::WHITE, prelude::*};
use shared::game::collision_handling::{components::Collider, structs::Obb3d};

use crate::VisualOffset;

#[derive(Default, Reflect, GizmoConfigGroup)]
pub struct MyColliderGizmos {}

pub fn visualize_colliders(
mut my_gizmos: Gizmos<MyColliderGizmos>,
query: Query<(&Transform, &Collider)>,
query: Query<(&Transform, &Collider, Option<&VisualOffset>)>,
) {
for (transform, collider) in query.iter() {
let obb = Obb3d::from_transform(transform, collider);
for (transform, collider, visual_offset) in query.iter() {
let mut transform = *transform;
if let Some(offset) = visual_offset {
transform.translation += offset.0;
}

let obb = Obb3d::from_transform(&transform, collider);

my_gizmos.primitive_3d(
&Cuboid {
Expand Down
Loading