Skip to content

Commit

Permalink
added projectile ricochet
Browse files Browse the repository at this point in the history
  • Loading branch information
vorlac committed Sep 30, 2023
1 parent 1c7199b commit f9cbd0d
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ BraceWrapping:
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBinaryOperators: true # None
BreakBeforeConceptDeclarations: true
BreakBeforeInheritanceComma: true
BreakInheritanceList: AfterComma
Expand Down
4 changes: 2 additions & 2 deletions project/scenes/characters/player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

[node name="Player" type="Character"]
scale = Vector2(2, 2)
collision_mask = 12
collision_mask = 8
platform_on_leave = 2

[node name="PlayerCollisionPoly" type="CollisionPolygon2D" parent="."]
position = Vector2(0.5, -1.5)
Expand All @@ -17,4 +18,3 @@ texture = ExtResource("1_ts0vk")
[node name="FiringPoint" type="Marker2D" parent="PlayerCollisionPoly"]
unique_name_in_owner = true
position = Vector2(23.5, 9.50009)
rotation = 1.5708
16 changes: 8 additions & 8 deletions project/scenes/projectiles/bullet.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ height = 35.0

[node name="Projectile" type="Projectile"]
collision_layer = 4
collision_mask = 122
input_pickable = false
priority = 1
linear_damp_space_override = 1
linear_damp = 0.25
angular_damp_space_override = 1
collision_mask = 74
mass = 0.01
continuous_cd = 1
max_contacts_reported = 1
contact_monitor = true
can_sleep = false

[node name="Bullet" type="Sprite2D" parent="."]
scale = Vector2(0.666665, 0.666665)
rotation = 1.5708
texture = ExtResource("1_sadn1")

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
scale = Vector2(0.5, 0.5)
rotation = 1.5708
shape = SubResource("CapsuleShape2D_i8hct")
12 changes: 6 additions & 6 deletions src/nodes/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ namespace rl
this->find_child(name::character::firing_pt, true, false));
runtime_assert(m_firing_point != nullptr);

signal<event::player_move>::connect<PlayerController>(m_player_controller) <=>
slot(this, on_player_movement);
signal<event::player_move>::connect<PlayerController>(m_player_controller)
<=> slot(this, on_player_movement);

signal<event::player_rotate>::connect<PlayerController>(m_player_controller) <=>
slot(this, on_player_rotate);
signal<event::player_rotate>::connect<PlayerController>(m_player_controller)
<=> slot(this, on_player_rotate);

signal<event::player_shoot>::connect<PlayerController>(m_player_controller) <=>
slot(this, on_player_shoot);
signal<event::player_shoot>::connect<PlayerController>(m_player_controller)
<=> slot(this, on_player_shoot);
}

PlayerController* Character::get_controller() const
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/character.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace rl
// Rate of acceleration/deceleration (unit/s/s)
double m_movement_friction{ 5.0 };
// Rate of rotational acceleration/deceleration (unit/s/s)
double m_rotation_speed{ 5.0 };
double m_rotation_speed{ 10.0 };
// target movement speed (units/s)
double m_movement_speed{ 500.0 };
// target facing angle (radians)
Expand Down
29 changes: 13 additions & 16 deletions src/nodes/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ namespace rl
this->add_child(m_player);
this->add_child(m_projectile_spawner);

signal<event::position_changed>::connect<PlayerController>(m_player->get_controller()) <=>
slot(this, on_character_position_changed);
signal<event::position_changed>::connect<PlayerController>(m_player->get_controller())
<=> slot(this, on_character_position_changed);

signal<event::spawn_projectile>::connect<Character>(m_player) <=>
slot(this, on_character_spawn_projectile);
signal<event::spawn_projectile>::connect<Character>(m_player)
<=> slot(this, on_character_spawn_projectile);
}

void Level::_process(double delta_time)
Expand Down Expand Up @@ -82,26 +82,23 @@ namespace rl
console::get()->print("{} < {}", io::red("projectile"), to<std::string>(node->get_name()));
}

[[signal_slot]] void Level::on_character_spawn_projectile(godot::Node* obj)
[[signal_slot]]
void Level::on_character_spawn_projectile(godot::Node* obj)
{
godot::Node2D* node{ gdcast<godot::Node2D>(obj) };
Projectile* projectile{ m_projectile_spawner->spawn_projectile() };
if (projectile != nullptr)
{
projectile->set_position(node->get_global_position());
projectile->set_rotation(node->get_global_rotation());

godot::Marker2D* firing_pt{ gdcast<godot::Marker2D>(node) };
godot::Marker2D* firing_pt{ gdcast<godot::Marker2D>(obj) };
if (firing_pt != nullptr)
{
projectile->set_velocity(
godot::Vector2(0, -1).rotated(firing_pt->get_global_rotation()));
projectile->set_position(firing_pt->get_global_position());
projectile->set_rotation(firing_pt->get_global_rotation());

signal<event::body_entered>::connect<Projectile>(projectile) <=>
slot(this, on_physics_box_entered);
signal<event::body_entered>::connect<Projectile>(projectile)
<=> slot(this, on_physics_box_entered);

signal<event::body_exited>::connect<Projectile>(projectile) <=>
slot(this, on_physics_box_exited);
signal<event::body_exited>::connect<Projectile>(projectile)
<=> slot(this, on_physics_box_exited);
}

this->add_child(projectile);
Expand Down
8 changes: 4 additions & 4 deletions src/nodes/player_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ namespace rl
[[fallthrough]];
case InputMode::MouseAndKeyboard:
{
godot::Vector2 rotation_dir{ this->get_global_mouse_position() -
this->get_global_position() };
godot::Vector2 rotation_dir{ this->get_global_mouse_position()
- this->get_global_position() };

m_rotation_angle = rotation_dir.angle() + godot::Math::deg_to_rad(90.0);
break;
Expand All @@ -95,8 +95,8 @@ namespace rl
godot::Vector2 target_rotation{ input->get_vector("rotate_left", "rotate_right",
"rotate_up", "rotate_down") };
if (!target_rotation.is_zero_approx())
m_rotation_angle = godot::Vector2(0, 0).angle_to_point(target_rotation) +
godot::Math::deg_to_rad(90.0);
m_rotation_angle = godot::Vector2(0, 0).angle_to_point(target_rotation)
+ godot::Math::deg_to_rad(90.0);
}
break;
}
Expand Down
31 changes: 20 additions & 11 deletions src/nodes/projectile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@ namespace rl
void Projectile::_ready()
{
m_start_pos = this->get_global_position();
auto forward{ this->get_transform()[0].normalized() };
this->apply_impulse(forward * m_velocity);
}

void Projectile::_process(double delta_time)
{
if (engine::editor_active())
return;

godot::Vector2 position{ this->get_position() };
position += m_velocity * m_movement_speed * delta_time;
this->set_position(position);

// check time to live
m_time_to_live -= delta_time;
if (m_time_to_live <= 0)
{
Expand Down Expand Up @@ -49,38 +46,50 @@ namespace rl
return m_time_to_live;
}

[[property]]
double Projectile::get_acceleration() const
{
return m_acceleration;
}

[[property]]
double Projectile::get_max_travel_dist() const
{
return godot::Math::sqrt(m_max_travel_dist);
}

[[property]]
godot::Vector2 Projectile::get_velocity() const
double Projectile::get_velocity() const
{
return m_velocity;
}

[[property]]
void Projectile::set_movement_speed(const double speed)
void Projectile::set_movement_speed(double speed)
{
m_movement_speed = speed;
}

[[property]]
void Projectile::set_time_to_live(const double ttl)
void Projectile::set_time_to_live(double ttl)
{
m_time_to_live = ttl;
}

[[property]]
void Projectile::set_max_travel_dist(const double dist)
void Projectile::set_acceleration(double acceleration)
{
m_acceleration = acceleration;
}

[[property]]
void Projectile::set_max_travel_dist(double dist)
{
m_max_travel_dist = { dist * dist };
m_max_travel_dist = dist * dist;
}

[[property]]
void Projectile::set_velocity(const godot::Vector2& velocity)
void Projectile::set_velocity(double velocity)
{
m_velocity = velocity;
}
Expand Down
36 changes: 22 additions & 14 deletions src/nodes/projectile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#include "util/conversions.hpp"
#include "util/io.hpp"

#include <godot_cpp/classes/area2d.hpp>
#include <godot_cpp/classes/rigid_body2d.hpp>
#include <godot_cpp/variant/vector2.hpp>

namespace rl
{
class Projectile : public godot::Area2D
class Projectile : public godot::RigidBody2D
{
GDCLASS(Projectile, godot::Area2D);
GDCLASS(Projectile, godot::RigidBody2D);

public:
Projectile() = default;
Expand All @@ -26,12 +26,12 @@ namespace rl
[[property]] double get_time_to_live() const;
[[property]] double get_max_travel_dist() const;
[[property]] double get_acceleration() const;
[[property]] godot::Vector2 get_velocity() const;
[[property]] void set_movement_speed(const double speed);
[[property]] void set_time_to_live(const double ttl);
[[property]] void set_max_travel_dist(const double dist);
[[property]] void set_acceleration(const double acceleration);
[[property]] void set_velocity(const godot::Vector2& velocity);
[[property]] double get_velocity() const;
[[property]] void set_movement_speed(double speed);
[[property]] void set_time_to_live(double ttl);
[[property]] void set_max_travel_dist(double dist);
[[property]] void set_acceleration(double acceleration);
[[property]] void set_velocity(double velocity);

// [[signal_slot]]
// void on_body_enter(godot::Node* node) const
Expand All @@ -50,20 +50,28 @@ namespace rl
protected:
static void _bind_methods()
{
// bind_member_function(Projectile, on_body_enter);
// bind_member_function(Projectile, on_body_exit);
bind_member_function(Projectile, get_movement_speed);
bind_member_function(Projectile, get_time_to_live);
bind_member_function(Projectile, get_max_travel_dist);
bind_member_function(Projectile, get_acceleration);
bind_member_function(Projectile, get_velocity);
bind_member_function(Projectile, set_movement_speed);
bind_member_function(Projectile, set_time_to_live);
bind_member_function(Projectile, set_max_travel_dist);
bind_member_function(Projectile, set_acceleration);
bind_member_function(Projectile, set_velocity);
}

protected:
godot::Vector2 m_start_pos{ 0.0, 0.0 };
// projectile movement velocity (pixels)
godot::Vector2 m_velocity{ 1.0, 1.0 };
double m_velocity{ 1500 };
// projectile movement speed (pixels/s)
double m_movement_speed{ 1000.0 };
// projectile acceleration (pixels/s/s)
double m_accelleration{ 100.0 };
double m_acceleration{ 100.0 };
// max time duration alive (seconds)
double m_time_to_live{ 1.0 };
double m_time_to_live{ 2.5 };
// max travel distance (pixels) - uint32_t?
double m_max_travel_dist{ 1000.0 * 1000.0 };
};
Expand Down

0 comments on commit f9cbd0d

Please sign in to comment.