Skip to content

Commit

Permalink
Merge pull request #21 from AnthonyTornetta/19-lasers-dont-work-when-…
Browse files Browse the repository at this point in the history
…a-structure-is-bigger-than-1x1x1

19 lasers dont work when a structure is bigger than 1x1x1
  • Loading branch information
AnthonyTornetta authored Jan 27, 2023
2 parents d816a27 + 2fd434b commit 94da66c
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 88 deletions.
2 changes: 0 additions & 2 deletions cosmos_client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub mod interactions;
pub mod lang;
pub mod netty;
pub mod plugin;
pub mod projectiles;
pub mod rendering;
pub mod state;
pub mod structure;
Expand Down Expand Up @@ -296,7 +295,6 @@ fn main() {
structure_renderer::register(&mut app);
lang::register(&mut app);
structure::register(&mut app);
projectiles::register(&mut app);

app.run();
}
40 changes: 24 additions & 16 deletions cosmos_client/src/netty/gameplay/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,17 @@ fn client_sync_players(
laser_velocity,
firer_velocity,
strength,
no_hit,
mut no_hit,
} => {
let laser_entity = Laser::spawn_custom_pbr(
if let Some(server_entity) = no_hit {
if let Some(client_entity) = network_mapping.client_from_server(&server_entity)
{
no_hit = Some(*client_entity);
}
}

// let laser_entity =
Laser::spawn_custom_pbr(
position,
laser_velocity,
firer_velocity,
Expand All @@ -393,20 +401,20 @@ fn client_sync_players(
&mut commands,
);

// too laggy ;(
commands.entity(laser_entity).with_children(|parent| {
parent.spawn(PointLightBundle {
transform: Transform::from_xyz(0.0, 0.0, 0.0),
point_light: PointLight {
intensity: 100.0,
range: 10.0,
color,
shadows_enabled: false,
..default()
},
..default()
});
});
// too laggy (and strobey) ;(
// commands.entity(laser_entity).with_children(|parent| {
// parent.spawn(PointLightBundle {
// transform: Transform::from_xyz(0.0, 0.0, 0.0),
// point_light: PointLight {
// intensity: 100.0,
// range: 10.0,
// color,
// shadows_enabled: false,
// ..default()
// },
// ..default()
// });
// });
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions cosmos_client/src/projectiles/mod.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use bevy::ecs::schedule::StateData;
use bevy::prelude::{
App, BuildChildren, Commands, EventReader, Parent, Quat, Query, SystemSet, Transform, Vec3,
With,
};
use bevy::transform::TransformBundle;
use bevy_rapier3d::prelude::{RigidBody, Sensor};

use crate::entities::player::Player;
use crate::events::structure::change_pilot_event::ChangePilotEvent;
use crate::structure::ship::pilot::Pilot;

Expand All @@ -19,6 +21,8 @@ fn event_listener(
if let Ok(prev_pilot) = pilot_query.get(ev.structure_entity) {
let mut transform = *transform_query.get(ev.structure_entity).unwrap();

transform.translation += transform.back() * 1.0 + Vec3::new(0.5, 1.5, 0.5);

commands
.entity(ev.structure_entity)
.remove_children(&[prev_pilot.entity])
Expand Down Expand Up @@ -50,14 +54,28 @@ fn event_listener(
.insert(Sensor)
.insert(RigidBody::Fixed)
.insert(TransformBundle::from_transform(Transform::from_xyz(
0.0, 0.0, 0.0,
0.5, 0.5, 0.5,
)));
} else {
commands.entity(ev.structure_entity).remove::<Pilot>();
}
}
}

fn keep_pilot_in_place(mut query: Query<&mut Transform, (With<Pilot>, With<Player>)>) {
for mut transform in query.iter_mut() {
// This is the block core's location
// This should be moved to the camera system once that's added
transform.translation.x = 0.5;
transform.translation.y = 0.5;
transform.translation.z = 0.5;
}
}

pub fn register<T: StateData + Clone + Copy>(app: &mut App, playing_state: T) {
app.add_system_set(SystemSet::on_update(playing_state).with_system(event_listener));
app.add_system_set(
SystemSet::on_update(playing_state)
.with_system(event_listener)
.with_system(keep_pilot_in_place),
);
}
2 changes: 0 additions & 2 deletions cosmos_core/src/projectiles/laser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,6 @@ fn handle_events(
}
}),
) {
println!("HIT {} @ {}", entity.index(), toi.witness1);

event_writer.send(LaserCollideEvent {
entity_hit: entity,
local_position_hit: toi.witness1 + velocity.linvel.normalize() * 0.01,
Expand Down
8 changes: 1 addition & 7 deletions cosmos_core/src/structure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ impl Structure {
let yy = y as f32 - yoff;
let zz = z as f32 - zoff;

Vec3::new(xx, yy, zz)
Vec3::new(xx + 0.5, yy + 0.5, zz + 0.5)
}

pub fn chunk_world_position(
Expand Down Expand Up @@ -490,13 +490,7 @@ impl Structure {
amount,
);

println!(
"Bang! Block took damage, health is now {}",
self.get_block_health(bx, by, bz, block_hardness)
);

if destroyed {
println!("Block dead!");
if let Some(structure_entity) = self.get_entity() {
if let Some(event_writer) = event_writer {
event_writer.send(BlockDestroyedEvent {
Expand Down
15 changes: 0 additions & 15 deletions cosmos_core/src/structure/systems/laser_cannon_system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,6 @@ impl LaserCannonSystem {

let (bx, by, bz) = (block.x as i32, block.y as i32, block.z as i32);

// println!(
// "Checking ({}, {}, {}) -> ({}, {}, {}) for ({}, {}, {})",
// sx,
// sy,
// sz,
// sx + line.len as i32 * dx,
// sy + line.len as i32 * dy,
// sz + line.len as i32 * dz,
// bx,
// by,
// bz
// );

// Block is before start
if sx - dx == bx && sy - dy == by && sz - dz == bz {
if found_line.is_some() {
Expand Down Expand Up @@ -395,7 +382,5 @@ pub fn register<T: StateData + Clone + Copy>(
block_update_system.run_in_bevy_state(playing_state),
)
.add_system_set(SystemSet::on_update(playing_state).with_system(structure_loaded_event))
// .add_system_set(SystemSet::on_update(playing_state).with_system(update_laser))
.register_type::<LaserCannonSystem>();
}
//
16 changes: 10 additions & 6 deletions cosmos_core/src/structure/systems/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,19 @@ impl Systems {
}

if let Some(active_system) = self.active_system {
commands
.entity(self.systems[active_system as usize])
.remove::<SystemActive>();
if (active_system as usize) < self.systems.len() {
commands
.entity(self.systems[active_system as usize])
.remove::<SystemActive>();
}
}

if let Some(active_system) = active {
commands
.entity(self.systems[active_system as usize])
.insert(SystemActive);
if (active_system as usize) < self.systems.len() {
commands
.entity(self.systems[active_system as usize])
.insert(SystemActive);
}
}

self.active_system = active;
Expand Down
7 changes: 3 additions & 4 deletions cosmos_server/src/init/init_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use cosmos_core::{
use noise::Seedable;

use crate::structure::planet::{
biosphere::test_all_stone_biosphere::TestStoneBiosphere,
server_planet_builder::ServerPlanetBuilder,
biosphere::grass_biosphere::GrassBiosphere, server_planet_builder::ServerPlanetBuilder,
};

pub fn register(app: &mut App) {
Expand All @@ -30,9 +29,9 @@ pub fn register(app: &mut App) {
fn create_world(mut commands: Commands, mut event_writer: EventWriter<StructureCreated>) {
let mut entity_cmd = commands.spawn_empty();

let mut structure = Structure::new(1, 1, 1, entity_cmd.id());
let mut structure = Structure::new(16, 4, 16, entity_cmd.id());

let builder = ServerPlanetBuilder::new(TestStoneBiosphere::default());
let builder = ServerPlanetBuilder::new(GrassBiosphere::default());

builder.insert_planet(
&mut entity_cmd,
Expand Down
49 changes: 22 additions & 27 deletions cosmos_server/src/projectiles/laser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,38 @@ use crate::state::GameState;
*/
fn on_laser_hit_structure(
structure: &mut Structure,
entity_hit: Entity,
local_position_hit: Vec3,
blocks: &Registry<Block>,
block_change_event_writer: &mut EventWriter<BlockChangedEvent>,
block_destroy_event_writer: &mut EventWriter<BlockDestroyedEvent>,
hardness_registry: &Registry<BlockHardness>,
strength: f32,
) {
if let Some(chunk) = structure.chunk_from_entity(&entity_hit) {
let chunk_block_coords = chunk.relative_coords_to_block_coords(&local_position_hit);
if let Ok((bx, by, bz)) = structure.relative_coords_to_local_coords(
local_position_hit.x,
local_position_hit.y,
local_position_hit.z,
) {
let block = structure.block_at(bx, by, bz, blocks);

let (bx, by, bz) = structure.block_coords_for_chunk_block_coords(chunk, chunk_block_coords);

if structure.is_within_blocks(bx, by, bz) {
let block = structure.block_at(bx, by, bz, blocks);

if let Some(hardness) = hardness_registry.from_id(block.unlocalized_name()) {
structure.block_take_damage(
bx,
by,
bz,
hardness,
strength,
Some(block_destroy_event_writer),
);
} else {
println!(
"WARNING: Missing block hardness for {}",
block.unlocalized_name()
);
structure.remove_block_at(bx, by, bz, blocks, Some(block_change_event_writer));
}
if let Some(hardness) = hardness_registry.from_id(block.unlocalized_name()) {
structure.block_take_damage(
bx,
by,
bz,
hardness,
strength,
Some(block_destroy_event_writer),
);
} else {
println!("Bad laser hit spot that isn't actually on structure ;(");
println!("HIT {bx}, {by}, {bz} block coords of structure from {local_position_hit}!");
println!(
"WARNING: Missing block hardness for {}",
block.unlocalized_name()
);
structure.remove_block_at(bx, by, bz, blocks, Some(block_change_event_writer));
}
} else {
println!("Bad laser hit spot that isn't actually on structure ;(");
}
}

Expand All @@ -70,7 +66,6 @@ fn respond_laser_hit_event(

on_laser_hit_structure(
&mut structure,
entity_hit,
local_position_hit,
&blocks,
&mut block_change_event_writer,
Expand Down
6 changes: 4 additions & 2 deletions cosmos_server/src/structure/systems/laser_cannon_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use cosmos_core::{

use crate::state::GameState;

const LASER_BASE_VELOCITY: f32 = 200.0;

fn update_system(
mut query: Query<(&mut LaserCannonSystem, &StructureSystem), With<SystemActive>>,
mut es_query: Query<&mut EnergyStorageSystem>,
Expand All @@ -34,7 +36,7 @@ fn update_system(
cannon_system.last_shot_time = sec;

for line in cannon_system.lines.iter() {
if energy_storage_system.get_capacity() >= line.property.energy_per_shot {
if energy_storage_system.get_energy() >= line.property.energy_per_shot {
energy_storage_system.decrease_energy(line.property.energy_per_shot);

let position = structure.block_world_position(
Expand All @@ -52,7 +54,7 @@ fn update_system(
.affine()
.matrix3
.mul_vec3(-line.direction.direction_vec3())
* 200.0;
* LASER_BASE_VELOCITY;

let strength = (5.0 * line.len as f32).powf(1.2);
let no_hit = Some(system.structure_entity);
Expand Down

0 comments on commit 94da66c

Please sign in to comment.