Skip to content

Commit

Permalink
Completed data ui in bevy_ui. Minor clippy fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
pellico committed Oct 8, 2023
1 parent 57ce20c commit c835623
Show file tree
Hide file tree
Showing 11 changed files with 497 additions and 195 deletions.
Binary file added assets/arial.ttf
Binary file not shown.
Binary file modified assets/bullet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/bin/ui_client/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::{Parser};
use clap::Parser;
use ktanks_server::remote_ch::*;
use ktanks_server::{enable_human_panic};
use ktanks_server::enable_human_panic;

const VERSION: &str = env!("CARGO_PKG_VERSION");

Expand Down
2 changes: 1 addition & 1 deletion src/bin/ui_client_bevy/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{Parser};
use clap::Parser;
use ktanks_server::remote_ch::*;
use ktanks_server::enable_human_panic;

Expand Down
6 changes: 3 additions & 3 deletions src/physics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
///! Implements the physical simulation

mod networking;
mod report;
mod tank;
mod ui_interface;
mod util;
pub use self::tank::{Bullet, Tank,EntityId};
pub use self::tank::{Bullet, Tank,ObjUID};
pub use self::ui_interface::*;
use self::util::*;
use crate::conf::*;
Expand Down Expand Up @@ -539,7 +539,7 @@ impl PhysicsEngine {
* Vector2::<Real>::x();
//angle from radar vector to target_tank vector
let angle = Rotation2::rotation_between(&radar_vector, &relative_vector).angle();
if angle.abs() < tank.radar_width / 2.0 {
if angle.abs() < tank.radar_width() / 2.0 {
result.push((target_tank, distance));
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/physics/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use super::tank::{Tank};
use std::fs::{File};
use super::tank::Tank;
use std::fs::File;
use csv;
pub(super) fn save_tank_report(path:&str,tanks:&Vec<Tank>) -> std::io::Result<()> {
let file = File::create(path)?;
let mut wtr = csv::Writer::from_writer(file);
wtr.write_record(&["Name", "Damage", "Energy"])?;
wtr.write_record(["Name", "Damage", "Energy"])?;
for tank in tanks {
let damage = &format!("{}",tank.damage);
let energy = &format!("{}",tank.energy);
let name :&str = &tank.name;
wtr.write_record(&[name, damage,energy])?;
wtr.write_record([name, damage,energy])?;

}
wtr.flush()?;
Expand Down
32 changes: 24 additions & 8 deletions src/physics/tank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use rapier2d::prelude::*;
use serde::{Deserialize, Serialize};

#[repr(transparent)]
#[derive(Hash, Eq, PartialEq, Clone, Copy)]
pub struct EntityId(u64);
#[derive(Hash, Eq, PartialEq, Clone, Copy,Default)]
pub struct ObjUID(u64);

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Turret {
Expand Down Expand Up @@ -40,7 +40,7 @@ pub struct Tank {
pub(super) cannon_joint_handle: ImpulseJointHandle,
pub name: String,
pub(super) turret: Turret,
pub damage: f32,
pub(super) damage: f32,
pub(super) energy: f32,
pub(super) engine_power: f32,
pub(super) max_engine_power: f32,
Expand All @@ -51,7 +51,7 @@ pub struct Tank {
pub(super) linvel: Vector<Real>,
pub(super) angular_velocity: Real, //Present angular velocity
pub(super) radar_position: f32, // Angle relative to the tank
pub(super) radar_width: f32,
radar_width: f32,
detected_tank: Vec<Tank>,
tank_energy_max: f32,
damage_max: f32,
Expand Down Expand Up @@ -181,9 +181,9 @@ impl Tank {
#[inline]
/// Get unique id of Tank
/// It is derived from RigidBodyHandle
pub fn get_id(&self) -> EntityId {
pub fn get_id(&self) -> ObjUID {
let (a, b) = self.phy_body_handle.into_raw_parts();
EntityId((a as u64) << 32 | b as u64)
ObjUID((a as u64) << 32 | b as u64)
}

#[inline]
Expand Down Expand Up @@ -245,10 +245,16 @@ impl Tank {
}

#[inline]
/// Angle relative to the tank
pub fn radar_position(&self) -> f32 {
self.radar_position
}

#[inline]
pub fn radar_width(&self) -> f32 {
self.radar_width
}

#[inline]
pub fn shape_polyline(&self) -> &Vec<Point2<Real>> {
&self.shape_polyline
Expand Down Expand Up @@ -292,6 +298,16 @@ impl Tank {
self.turning_power = self.turning_power_max * power_fraction_wrapped;
}

#[inline]
pub fn damage_max(&self) -> f32 {
self.damage_max
}

#[inline]
pub fn damage(&self) -> f32 {
self.damage
}

/**
* Set cannon position
*/
Expand Down Expand Up @@ -467,8 +483,8 @@ impl Bullet {
#[inline]
/// Get unique id of Bullet
/// It is derived from RigidBodyHandle so it is unique globally
pub fn get_id(&self) -> EntityId {
pub fn get_id(&self) -> ObjUID {
let (a, b) = self.phy_body_handle.into_raw_parts();
EntityId((a as u64) << 32 | b as u64)
ObjUID((a as u64) << 32 | b as u64)
}
}
2 changes: 1 addition & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ impl GameUI {
widgets::Window::new(hash!(), vec2(0., 0.), vec2(300., 400.))
.label("Tanks")
.titlebar(true)
.ui(&mut *root_ui(), |ui| {
.ui(&mut root_ui(), |ui| {
for (index, p_tank) in p_tanks.iter().enumerate() {
let uppercase_label;
let label = if index == self.selected_tank {
Expand Down
68 changes: 68 additions & 0 deletions src/ui_bevy/gizmos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use crate::physics::{Point2, Real, };
use bevy::prelude::*;
use super::{*};

fn draw_polyline(gizmos: &mut Gizmos, polyline: &[Point2<Real>], scaling_factor: f32) {
let polyline_size = polyline.len();
let poly_vec: Vec<Vec2> = polyline
.iter()
.map(|&x| <Point2<Real> as Into<Vec2>>::into(x) * scaling_factor)
.collect();
gizmos.linestrip_2d(poly_vec, Color::RED);
//Close shape
gizmos.line_2d(
<Point2<Real> as Into<Vec2>>::into(polyline[polyline_size - 1]) * scaling_factor,
<Point2<Real> as Into<Vec2>>::into(polyline[0]) * scaling_factor,
Color::RED,
);
}

pub(super) fn gizmos(mut gizmos: Gizmos, physics_state: Res<PhysicsState>) {
let physical_scaling_factor = 1.0;
// Draw tank and turret
for tank in physics_state.tanks.values() {
draw_polyline(&mut gizmos, tank.shape_polyline(), physical_scaling_factor);
draw_polyline(
&mut gizmos,
tank.turret().shape_polyline(),
physical_scaling_factor,
);
// Draw radar range

let scaled_radar_range = tank.radar_range() * physical_scaling_factor;
gizmos.arc_2d(
tank.position().translation.into(),
-tank.position().rotation.angle() - tank.radar_position() + PI / 2.0,
tank.radar_width(),
scaled_radar_range,
Color::YELLOW,
).segments(10);

// Draw side of radar detection area
let v1 = tank.position().translation.vector * physical_scaling_factor;
let (min_angle, max_angle) = tank.min_max_radar_angle();

let v2 = (nalgebra::Isometry2::rotation(min_angle)
* nalgebra::vector![scaled_radar_range, 0.0])
+ v1;
let v3 = (nalgebra::Isometry2::rotation(max_angle)
* nalgebra::vector![scaled_radar_range, 0.0])
+ v1;

gizmos.line_2d(v1.into(), v2.into(), Color::YELLOW);
gizmos.line_2d(v1.into(), v3.into(), Color::YELLOW);
}

// Draw bullets
for tank in physics_state.bullets.values() {
let a = tank.shape_polyline();
draw_polyline(&mut gizmos, a, physical_scaling_factor);
}

// Draw zero power limit
gizmos.circle_2d(
Vec2::ZERO,
physics_state.zero_power_limit * physical_scaling_factor,
Color::GREEN,
);
}
Loading

0 comments on commit c835623

Please sign in to comment.