Skip to content

Commit

Permalink
spawn projectiles at firing pt marker instead of character centroid
Browse files Browse the repository at this point in the history
  • Loading branch information
vorlac committed Sep 28, 2023
1 parent f8c0a56 commit 11c632a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions project/scenes/characters/player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ 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
7 changes: 1 addition & 6 deletions project/scenes/levels/level1.tscn
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
[gd_scene load_steps=3 format=3 uid="uid://c8tur70b73kih"]
[gd_scene load_steps=2 format=3 uid="uid://c8tur70b73kih"]

[ext_resource type="Texture2D" uid="uid://b76tyx7byavoa" path="res://assets/art/prototype_textures/dark/texture_03.png" id="1_g3701"]

[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_prw1o"]

[node name="Level1" type="Level"]

[node name="BackgroundTexture" type="Sprite2D" parent="."]
z_index = -10
scale = Vector2(3, 3)
texture = ExtResource("1_g3701")
metadata/_edit_lock_ = true

[node name="CollisionShape2D" type="CollisionShape2D" parent="BackgroundTexture"]
shape = SubResource("WorldBoundaryShape2D_prw1o")
6 changes: 5 additions & 1 deletion src/nodes/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <vector>

#include <godot_cpp/classes/input.hpp>
#include <godot_cpp/classes/marker2d.hpp>
#include <godot_cpp/classes/rectangle_shape2d.hpp>
#include <godot_cpp/classes/ref.hpp>
#include <godot_cpp/classes/resource_loader.hpp>
Expand All @@ -38,6 +39,9 @@ namespace rl::inline node
this->add_child(m_camera);
this->add_child(m_player_controller);

m_firing_point = gdcast<godot::Marker2D>(this->find_child("FiringPoint", true, false));
runtime_assert(m_firing_point != nullptr);

signal<event::player_move>::connect<PlayerController>(m_player_controller) <=>
[this]() mutable {
return slot(this, on_player_movement);
Expand Down Expand Up @@ -82,7 +86,7 @@ namespace rl::inline node
void Character::on_player_shoot()
{
// TODO: fix this
this->emit_signal(event::spawn_projectile, this);
this->emit_signal(event::spawn_projectile, m_firing_point);
}

[[property]]
Expand Down
7 changes: 7 additions & 0 deletions src/nodes/character.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
#include <godot_cpp/classes/collision_shape2d.hpp>
#include <godot_cpp/classes/sprite2d.hpp>

namespace godot
{
class Marker2D;
}

namespace rl::inline node
{
class Character : public godot::CharacterBody2D
Expand Down Expand Up @@ -53,5 +58,7 @@ namespace rl::inline node
Camera* m_camera{ memnew(Camera) };
// handles all input related player controls
PlayerController* m_player_controller{ memnew(PlayerController) };
// marker identifying location where to spwwn projectiles
godot::Marker2D* m_firing_point{ nullptr };
};
}
9 changes: 5 additions & 4 deletions src/nodes/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "util/input.hpp"
#include "util/io.hpp"

#include <godot_cpp/classes/marker2d.hpp>
#include <godot_cpp/variant/callable.hpp>
#include <godot_cpp/variant/vector2.hpp>

Expand Down Expand Up @@ -74,13 +75,13 @@ namespace rl::inline node
if (projectile != nullptr)
{
projectile->set_position(node->get_global_position());
projectile->set_rotation(node->get_rotation() - godot::Math::deg_to_rad(45.0));
projectile->set_rotation(node->get_global_rotation() - godot::Math::deg_to_rad(45.0));

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

this->add_child(projectile);
Expand Down

0 comments on commit 11c632a

Please sign in to comment.