Skip to content

Commit

Permalink
Resolve some todos
Browse files Browse the repository at this point in the history
  • Loading branch information
martinstarman committed Nov 20, 2024
1 parent e7d008f commit 2119cce
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 27 deletions.
14 changes: 13 additions & 1 deletion src/bounding_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn bounding_box_draw(query: Query<&BoundingBox>, mut gizmos: Gizmos) {
let half_size = bounding_box.value.half_size();
let rectangle = Rectangle { half_size };

// TODO: do not use gizmos
// TODO: stop using gizmos
gizmos.primitive_2d(
&rectangle,
bounding_box.value.center(),
Expand All @@ -22,3 +22,15 @@ pub fn bounding_box_draw(query: Query<&BoundingBox>, mut gizmos: Gizmos) {
);
}
}

pub fn bounding_box_translation(
mut query: Query<(&mut BoundingBox, &Transform), Changed<Transform>>,
) {
for (mut bounding_box, transform) in &mut query {
let center = bounding_box.value.center();
let translation = transform.translation.xy();
let step = translation - center;

bounding_box.value.translate_by(step);
}
}
10 changes: 8 additions & 2 deletions src/line_of_sight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,20 @@ pub fn line_of_sight_looking_at(
}
}

pub fn line_of_sight_draw(query: Query<(&LineOfSight, &Transform)>, mut gizmos: Gizmos) {
pub fn line_of_sight_looking_at_draw(query: Query<(&LineOfSight, &Transform)>, mut gizmos: Gizmos) {
for (line_of_sight, transform) in &query {
let rect = Rectangle::new(10., 10.);
let position = transform.translation.xy();
let looking_at = position + line_of_sight.looking_at * LINE_OF_SIGHT_DISTANCE as f32;

gizmos.primitive_2d(&rect, position, 0., Color::WHITE);
// TODO: stop using gizmos
gizmos.primitive_2d(&rect, looking_at, 0., Color::WHITE);
}
}

pub fn line_of_sight_draw(query: Query<&LineOfSight>, mut gizmos: Gizmos) {
for line_of_sight in &query {
// TODO: stop using gizmos
gizmos.primitive_2d(&line_of_sight.polygon, Vec2::ZERO, 0., Color::WHITE);
}
}
28 changes: 19 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ use bevy::{
prelude::*,
window::WindowResolution,
};
use bounding_box::bounding_box_draw;
use bounding_box::{bounding_box_draw, bounding_box_translation};
use camera::{camera_pan, camera_setup};
use enemy::{enemy_atlas_layout, enemy_setup, enemy_state};
use gizmo::gizmo;
use line_of_sight::{
line_of_sight_draw, line_of_sight_looking_at, line_of_sight_shift, line_of_sight_update,
line_of_sight_draw, line_of_sight_looking_at, line_of_sight_looking_at_draw, line_of_sight_shift,
line_of_sight_update,
};
use movable::{path_direction, path_draw, path_follow, path_reset};
use navmesh::{navmesh_draw, navmesh_obstacle_draw, navmesh_setup};
Expand Down Expand Up @@ -74,8 +75,6 @@ fn main() -> AppExit {
(
camera_pan,
//
gizmo, // TODO: .run_if(DEBUG)
//
player_animation,
player_path,
player_state,
Expand All @@ -89,17 +88,28 @@ fn main() -> AppExit {
line_of_sight_looking_at,
line_of_sight_draw,
//
bounding_box_draw,
bounding_box_translation,
//
path_draw,
path_reset,
path_follow,
path_direction,
//
navmesh_draw,
navmesh_obstacle_draw,
),
)
.add_systems(
Update,
(
gizmo.run_if(debug),
line_of_sight_looking_at_draw.run_if(debug),
bounding_box_draw.run_if(debug),
path_draw.run_if(debug),
navmesh_draw.run_if(debug),
navmesh_obstacle_draw.run_if(debug),
),
)
.add_systems(PostUpdate, y_sort)
.run()
}

fn debug() -> bool {
true
}
21 changes: 6 additions & 15 deletions src/movable.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use bevy::{math::bounding::BoundingVolume, prelude::*};
use bevy::prelude::*;

use crate::{
bounding_box::BoundingBox,
direction::{Direction, Directions},
};
use crate::direction::{Direction, Directions};

const SPEED_WALK: f32 = 1.;
const SPEED_RUN: f32 = 2.;
Expand All @@ -14,11 +11,10 @@ pub enum Speed {
Fast = 2,
}

// TODO: idle for 5s/fps?
#[derive(Clone)]
pub struct PathItem {
pub position: Vec2,
pub speed: Speed, //
pub speed: Speed,
}

#[derive(Component, Default)]
Expand All @@ -41,7 +37,7 @@ pub fn path_draw(query: Query<(&Transform, &Movable)>, mut gizmos: Gizmos) {
for i in 0..path.len() - 1 {
let (line, center) = Segment2d::from_points(path[i].position, path[i + 1].position);

// TODO: do not use gizmos
// TODO: stop using gizmos
gizmos.primitive_2d(
&line,
center,
Expand Down Expand Up @@ -70,8 +66,8 @@ pub fn path_reset(mut query: Query<&mut Movable, Changed<Movable>>) {
}
}

pub fn path_follow(mut query: Query<(&mut Movable, &mut BoundingBox, &mut Transform)>) {
for (mut movable, mut bounding_box, mut transform) in &mut query {
pub fn path_follow(mut query: Query<(&mut Movable, &mut Transform)>) {
for (mut movable, mut transform) in &mut query {
if movable.path.len() > 0 {
let next = movable.path[0].position.extend(transform.translation.z);
let speed = if movable.path[0].speed == Speed::Slow {
Expand All @@ -83,7 +79,6 @@ pub fn path_follow(mut query: Query<(&mut Movable, &mut BoundingBox, &mut Transf
let step = (next - transform.translation).normalize() * speed;

transform.translation += step;
bounding_box.value.translate_by(step.xy()); // TODO: move to BB

if transform.translation.distance(next) <= speed / 2. {
movable.path.remove(0);
Expand All @@ -96,10 +91,6 @@ pub fn path_direction(mut query: Query<(&Movable, &mut Direction, &Transform), C
for (movable, mut direction, transform) in &mut query {
if movable.path.len() > 0 {
let angle = (movable.path[0].position - transform.translation.xy()).to_angle();
// TODO:
// let v: Vec2 = position - translation
// Dir2::new(v)
// CompassOctant::from(Dir2)
direction.value = Directions::try_from(angle).unwrap();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/navmesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub fn navmesh_obstacle_draw(mut gizmos: Gizmos, query: Query<(&PrimitiveObstacl
for (obstacle, transform) in &query {
match obstacle {
PrimitiveObstacle::Rectangle(primitive) => {
// TODO: stop using gizmos
gizmos.primitive_2d(
primitive,
transform.translation.xy(),
Expand Down

0 comments on commit 2119cce

Please sign in to comment.