Skip to content

Commit

Permalink
Merge pull request #96 from amethyst/hitbox_debug_lines
Browse files Browse the repository at this point in the history
Hitbox Debug Lines
  • Loading branch information
cdsupina authored Dec 7, 2020
2 parents d2b78e2 + c2f4af8 commit c6214eb
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 4,251 deletions.
4,195 changes: 0 additions & 4,195 deletions Cargo.lock

This file was deleted.

38 changes: 5 additions & 33 deletions assets/data/enemies.ron
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
hitbox_component: (
width: 14.0,
height: 14.0,
offset_x: 0.0,
offset_y: 0.0,
offset_rotation: 0.0,
),
motion2d_component: (
velocity: [0.0, -40.0],
Expand Down Expand Up @@ -94,9 +91,6 @@
hitbox_component: (
width: 14.0,
height: 14.0,
offset_x: 0.0,
offset_y: 0.0,
offset_rotation: 0.0,
),
motion2d_component: (
velocity: [0.0, -30.0],
Expand Down Expand Up @@ -135,9 +129,6 @@
hitbox_component: (
width: 14.0,
height: 28.0,
offset_x: 0.0,
offset_y: 0.0,
offset_rotation: 0.0,
),
motion2d_component: (
velocity: [0.0, -20.0],
Expand Down Expand Up @@ -200,9 +191,6 @@
hitbox_component: (
width: 22.0,
height: 10.0,
offset_x: 0.0,
offset_y: 0.0,
offset_rotation: 0.0,
),
motion2d_component: (
velocity: [25.0, -25.0],
Expand Down Expand Up @@ -247,9 +235,6 @@
hitbox_component: (
width: 15.0,
height: 20.0,
offset_x: 0.0,
offset_y: 0.0,
offset_rotation: 0.0,
),
motion2d_component: (
velocity: [0.0, -5.0],
Expand Down Expand Up @@ -288,11 +273,8 @@
armor: 0,
),
hitbox_component: (
width: 15.0,
height: 20.0,
offset_x: 0.0,
offset_y: 0.0,
offset_rotation: 0.0,
width: 10.0,
height: 15.0,
),
motion2d_component: (
velocity: [0.0, -40.0],
Expand Down Expand Up @@ -336,9 +318,6 @@
hitbox_component: (
width: 48.0,
height: 26.0,
offset_x: 0.0,
offset_y: 0.0,
offset_rotation: 0.0,
),
motion2d_component: (
velocity: [0.0, -30.0],
Expand Down Expand Up @@ -382,9 +361,6 @@
hitbox_component: (
width: 30.0,
height: 28.0,
offset_x: 0.0,
offset_y: 0.0,
offset_rotation: 0.0,
),
motion2d_component: (
velocity: [0.0, -30.0],
Expand Down Expand Up @@ -428,8 +404,7 @@
hitbox_component: (
width: 28.0,
height: 6.0,
offset_x: 15,
offset_y: -10.0,
offset: [15.0, -10.0],
offset_rotation: 0.7,
),
motion2d_component: (
Expand Down Expand Up @@ -474,8 +449,7 @@
hitbox_component: (
width: 28.0,
height: 6.0,
offset_x: -15.0,
offset_y: -10.0,
offset: [-15.0, -10.0],
offset_rotation: -0.7,
),
motion2d_component: (
Expand Down Expand Up @@ -521,9 +495,7 @@
hitbox_component: (
width: 20.0,
height: 5.0,
offset_x: 0.0,
offset_y: -60.0,
offset_rotation: 0.0,
offset: [0.0, -60.0],
),
motion2d_component: (
velocity: [0.0, -30.0],
Expand Down
4 changes: 4 additions & 0 deletions config/debug_lines.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DebugLinesConfig (
line_width: 1.0,
hitbox_color: {red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0},
)
30 changes: 19 additions & 11 deletions src/components/hitbox.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use amethyst::{
core::transform::Transform,
core::{math::Vector2, transform::Transform},
ecs::prelude::{Component, DenseVecStorage},
};

Expand All @@ -9,10 +9,18 @@ use serde::{Deserialize, Serialize};
pub struct Hitbox2DComponent {
pub width: f32,
pub height: f32,
pub offset_x: f32,
pub offset_y: f32,
#[serde(default = "des_offset")]
pub offset: Vector2<f32>,
#[serde(default = "des_offset_rotation")]
pub offset_rotation: f32, // offset in radians
}
fn des_offset() -> Vector2<f32> {
Vector2::new(0.0, 0.0)
}

fn des_offset_rotation() -> f32 {
0.0
}

impl Component for Hitbox2DComponent {
type Storage = DenseVecStorage<Self>;
Expand All @@ -26,10 +34,10 @@ impl Hitbox2DComponent {
transform_b: &Transform,
) -> bool {
if self.offset_rotation == 0.0 && hitbox_b.offset_rotation == 0.0 {
let x1 = transform_a.translation().x - (self.width / 2.0) + self.offset_x;
let y1 = transform_a.translation().y - (self.height / 2.0) + self.offset_y;
let x2 = transform_b.translation().x - (hitbox_b.width / 2.0) + hitbox_b.offset_x;
let y2 = transform_b.translation().y - (hitbox_b.height / 2.0) + hitbox_b.offset_y;
let x1 = transform_a.translation().x - (self.width / 2.0) + self.offset.x;
let y1 = transform_a.translation().y - (self.height / 2.0) + self.offset.y;
let x2 = transform_b.translation().x - (hitbox_b.width / 2.0) + hitbox_b.offset.x;
let y2 = transform_b.translation().y - (hitbox_b.height / 2.0) + hitbox_b.offset.y;

return x1 < (x2 + hitbox_b.width)
&& (x1 + self.width) > x2
Expand Down Expand Up @@ -57,8 +65,8 @@ impl Hitbox2DComponent {
let b_ll_y_temp = -hitbox_b.height / 2.0;

// Step 2: find rotated coordinates of four corners
let a_x_offset = transform_a.translation().x + self.offset_x;
let a_y_offset = transform_a.translation().y + self.offset_y;
let a_x_offset = transform_a.translation().x + self.offset.x;
let a_y_offset = transform_a.translation().y + self.offset.y;
let rotated_hitbox_1 = [
Vector(
rotate_x(a_ur_x_temp, a_ur_y_temp, self.offset_rotation) + a_x_offset,
Expand All @@ -78,8 +86,8 @@ impl Hitbox2DComponent {
),
];

let b_x_offset = transform_b.translation().x + hitbox_b.offset_x;
let b_y_offset = transform_b.translation().y + hitbox_b.offset_y;
let b_x_offset = transform_b.translation().x + hitbox_b.offset.x;
let b_y_offset = transform_b.translation().y + hitbox_b.offset.y;
let rotated_hitbox_2 = [
Vector(
rotate_x(b_ur_x_temp, b_ur_y_temp, hitbox_b.offset_rotation) + b_x_offset,
Expand Down
3 changes: 1 addition & 2 deletions src/components/weapons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ impl BlasterComponent {
let blast_hitbox = Hitbox2DComponent {
width: BLAST_HITBOX_DIAMETER * self.size_multiplier,
height: BLAST_HITBOX_DIAMETER * self.size_multiplier,
offset_x: 0.0,
offset_y: 0.0,
offset: Vector2::new(0.0, 0.0),
offset_rotation: 0.0,
};

Expand Down
3 changes: 1 addition & 2 deletions src/entities/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ pub fn spawn_item(
let hitbox_component = Hitbox2DComponent {
width: 14.0,
height: 14.0,
offset_x: 0.0,
offset_y: 0.0,
offset: Vector2::new(0.0, 0.0),
offset_rotation: 0.0,
};

Expand Down
3 changes: 1 addition & 2 deletions src/entities/spaceship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ pub fn initialize_spaceship(world: &mut World, sprite_sheet_handle: Handle<Sprit
let hitbox = Hitbox2DComponent {
width: SPACESHIP_HITBOX_WIDTH,
height: SPACESHIP_HITBOX_HEIGHT,
offset_x: 0.0,
offset_y: 0.0,
offset: Vector2::new(0.0, 0.0),
offset_rotation: 0.0,
};

Expand Down
13 changes: 10 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use amethyst::{
input::{InputBundle, StringBindings},
prelude::*,
renderer::{
plugins::{RenderFlat2D, RenderFlat3D, RenderToWindow},
plugins::{RenderDebugLines, RenderFlat2D, RenderFlat3D, RenderToWindow},
types::DefaultBackend,
RenderingBundle,
},
Expand All @@ -28,16 +28,21 @@ mod space_shooter;
pub mod systems;

use crate::space_shooter::SpaceShooter;
use resources::{ConsumablePool, EnemyPool, ItemPool, ThrusterPool};
use resources::{ConsumablePool, DebugLinesConfig, EnemyPool, ItemPool, ThrusterPool};

use amethyst::config::Config;

fn main() -> amethyst::Result<()> {
amethyst::start_logger(Default::default());

let app_root = application_root_dir()?;
let config_path = app_root.join("config");
let display_config_path = app_root.join("config").join("display_config_960.ron");
let bindings_path = app_root.join("config").join("bindings_config.ron");

let debug_lines = <DebugLinesConfig as Config>::load(config_path.join("debug_lines.ron"))
.expect("failed to load configuration file: debug_lines.ron");

let assets_path = app_root.join("assets");

let items = <ItemPool as Config>::load(assets_path.join("data").join("items.ron"))
Expand Down Expand Up @@ -65,14 +70,16 @@ fn main() -> amethyst::Result<()> {
.with_plugin(RenderFlat3D::default())
.with_plugin(RenderFlat2D::default())
//.with_plugin(RenderShaded3D::default())
.with_plugin(RenderUi::default()),
.with_plugin(RenderUi::default())
.with_plugin(RenderDebugLines::default()),
)?;

let mut game = Application::build(assets_path, SpaceShooter::default())?
.with_resource(items)
.with_resource(enemies)
.with_resource(thrusters)
.with_resource(consumables)
.with_resource(debug_lines)
.build(game_data)?;

game.run();
Expand Down
7 changes: 7 additions & 0 deletions src/resources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::components::{
AnimationComponent, AutoFireComponent, BlasterComponent, ConsumableComponent, EnemyComponent,
HealthComponent, Hitbox2DComponent, ItemComponent, Motion2DComponent,
};
use amethyst::renderer::palette::Srgba;
use serde::{Deserialize, Serialize};

mod sprite;
Expand Down Expand Up @@ -37,6 +38,12 @@ pub struct ConsumableEntityData {
pub hitbox_component: Hitbox2DComponent,
}

#[derive(Clone, Serialize, Deserialize, Debug, Default)]
pub struct DebugLinesConfig {
pub line_width: f32,
pub hitbox_color: Srgba,
}

pub type EnemyPool = std::collections::HashMap<String, EnemyEntityData>;
pub type ThrusterPool = std::collections::HashMap<String, ThrusterEntityData>;
pub type ItemPool = std::collections::HashMap<String, ItemEntityData>;
Expand Down
13 changes: 12 additions & 1 deletion src/space_shooter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
initialize_defense, initialize_enemy_spawner, initialize_gamemaster, initialize_planet,
initialize_side_panels, initialize_spaceship, initialize_status_bars, initialize_store,
},
resources::initialize_sprite_resource,
resources::{initialize_sprite_resource, DebugLinesConfig},
systems,
};
use amethyst::{
Expand All @@ -17,6 +17,7 @@ use amethyst::{
ecs::prelude::{Dispatcher, DispatcherBuilder, Entity},
input::{is_key_down, VirtualKeyCode},
prelude::*,
renderer::debug_drawing::{DebugLines, DebugLinesParams},
renderer::formats::texture::ImageFormat,
renderer::{Camera, SpriteRender, SpriteSheet, SpriteSheetFormat, Texture},
ui::{Anchor, LineMode, TtfFormat, UiText, UiTransform},
Expand Down Expand Up @@ -219,6 +220,16 @@ impl SimpleState for SpaceShooter {
initialize_side_panels(world, side_panel_sprite_sheet_handle);
initialize_store(world);
initialise_camera(world);

world.insert(DebugLines::new());
let debug_lines_params = {
let debug_lines_config = world.read_resource::<DebugLinesConfig>();

DebugLinesParams {
line_width: debug_lines_config.line_width,
}
};
world.insert(debug_lines_params);
}

fn on_pause(&mut self, data: StateData<'_, GameData<'_, '_>>) {
Expand Down
40 changes: 38 additions & 2 deletions src/systems/collision_detection.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use crate::{
components::{EnemyComponent, Hitbox2DComponent, Motion2DComponent, SpaceshipComponent},
events::{CollisionEvent, EnemyCollisionEvent, PlayerCollisionEvent},
resources::DebugLinesConfig,
};
use amethyst::{
core::{math::Vector2, transform::Transform},
core::{
math::{UnitQuaternion, Vector2},
transform::Transform,
},
ecs::*,
renderer::debug_drawing::DebugLines,
shrev::{EventChannel, ReaderId},
};

Expand All @@ -18,8 +23,20 @@ impl<'s> System<'s> for CollisionDetectionSystem {
ReadStorage<'s, Hitbox2DComponent>,
ReadStorage<'s, Transform>,
Write<'s, EventChannel<CollisionEvent>>,
Write<'s, DebugLines>,
Read<'s, DebugLinesConfig>,
);
fn run(&mut self, (entities, hitbox2ds, transforms, mut collision_channel): Self::SystemData) {
fn run(
&mut self,
(
entities,
hitbox2ds,
transforms,
mut collision_channel,
mut debug_lines,
debug_lines_config,
): Self::SystemData,
) {
for (entity_a, transform_a, hitbox_a) in (&entities, &transforms, &hitbox2ds).join() {
for (entity_b, transform_b, hitbox_b) in (&entities, &transforms, &hitbox2ds).join() {
if entity_a == entity_b {
Expand All @@ -30,6 +47,25 @@ impl<'s> System<'s> for CollisionDetectionSystem {
collision_channel.single_write(CollisionEvent::new(entity_a, entity_b));
}
}
if cfg!(debug_assertions) {
// draw debug lines for hitboxes
debug_lines.draw_rotated_box(
[
transform_a.translation().x + hitbox_a.offset.x - (hitbox_a.width / 2.0),
transform_a.translation().y + hitbox_a.offset.y - (hitbox_a.height / 2.0),
transform_a.translation().z,
]
.into(),
[
transform_a.translation().x + hitbox_a.offset.x + (hitbox_a.width / 2.0),
transform_a.translation().y + hitbox_a.offset.y + (hitbox_a.height / 2.0),
transform_a.translation().z,
]
.into(),
UnitQuaternion::from_euler_angles(0.0, 0.0, -hitbox_a.offset_rotation),
debug_lines_config.hitbox_color,
);
}
}
}
}
Expand Down

0 comments on commit c6214eb

Please sign in to comment.