gabeee3ee

Bullet.cpp

Aug 8th, 2025
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | Source Code | 0 0
  1. #include "bullet.h"
  2.  
  3. #include <godot_cpp/core/class_db.hpp>
  4. #include <godot_cpp/classes/engine.hpp>
  5. #include <godot_cpp/variant/utility_functions.hpp>
  6. #include <godot_cpp/classes/scene_tree.hpp>
  7. #include <godot_cpp/classes/window.hpp>
  8. #include <godot_cpp/classes/node.hpp>
  9. #include <godot_cpp/classes/node2D.hpp>
  10.  
  11. using namespace godot;
  12.  
  13. // Add member variable declaration for target_entity
  14. // (This should also be added to the header file, bullet.h)
  15.  
  16. void Bullet::_bind_methods() {
  17.     // Aquí puedes usar D_METHOD o BIND_METHOD para registrar los métodos
  18.     ClassDB::bind_method(D_METHOD("set_speed", "p_speed"), &Bullet::set_speed);
  19.     ClassDB::bind_method(D_METHOD("get_speed"), &Bullet::get_speed);
  20.     ClassDB::bind_method(D_METHOD("set_radio", "p_radio"), &Bullet::set_radio);
  21.     ClassDB::bind_method(D_METHOD("get_radio"), &Bullet::get_radio);
  22.     ClassDB::bind_method(D_METHOD("initialize", "p_direction", "p_rotation", "p_shape"), &Bullet::initialize);
  23.     ClassDB::bind_method(D_METHOD("die"), &Bullet::die);
  24.     ClassDB::bind_method(D_METHOD("disable"), &Bullet::disable);
  25.     ClassDB::bind_method(D_METHOD("is_active"), &Bullet::is_active);
  26.  
  27.     ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed"), "set_speed", "get_speed");
  28. }
  29.  
  30. Bullet::Bullet() {
  31.     speed = 50;
  32.     radio = 5;
  33.     direction_vector = Vector2(1, 1); // Dirección por defecto
  34.     bullet_position = Vector2(0, 0);
  35.     radio = 5;
  36.     direction_vector = Vector2(1, 1); // Dirección por defecto
  37. }
  38.  
  39. Bullet::~Bullet() {
  40.     // Si el nodo es padre de area, Godot lo liberará
  41. }
  42.  
  43. void Bullet::initialize(const Vector2 &p_position, const Vector2 &p_direction, int p_radio, int p_speed, int p_z_index) {
  44.    
  45.     direction_vector = p_direction;
  46.     bullet_position = p_position;
  47.     set_bullet_position(p_position);
  48.     radio = p_radio;
  49.     speed = p_speed;
  50.     direction_vector *= speed;
  51.     set_z_index(p_z_index);
  52.     set_visible(true);
  53.     _is_active = true;
  54.    
  55.     // Node* target = get_node<Node>("/root/Global");
  56.     // target->call("add_num_balas");
  57. }
  58.  
  59. void Bullet::_ready() {
  60.     // Llama al ready de la clase padre (AnimatedSprite2D)
  61.     Sprite2D::_ready();
  62.     // Node2D* target_entity =
  63.     disable();
  64. }
  65.  
  66. void Bullet::disable() {
  67.     _is_active = false;
  68.     set_visible(false);
  69. }
  70.  
  71. bool Bullet::is_active() {
  72.     return _is_active;
  73. }
  74.  
  75. void Bullet::set_radio(const int p_radio) {
  76.     radio = p_radio;
  77. }
  78.  
  79. int Bullet::get_radio() const {
  80.     return radio;
  81. }
  82.  
  83. void Bullet::set_speed(const float p_speed) {
  84.     speed = p_speed;
  85. }
  86.  
  87. float Bullet::get_speed() const {
  88.     return speed;
  89. }
  90.  
  91. void Bullet::set_bullet_position(const Vector2 &p_position) {
  92.     bullet_position = p_position;
  93.     set_global_position(bullet_position);
  94. }
  95.  
  96. Vector2 Bullet::get_bullet_position() {
  97.     return bullet_position;
  98. }
  99.  
  100. Vector2 Bullet:: get_direction_vector() {
  101.     return direction_vector;
  102. }
  103.  
  104. void Bullet::die() {
  105.     // Node* target = get_node<Node>("/root/Global");
  106.     // target->call("remove_num_balas");
  107.     // queue_free();
  108. }
Advertisement
Add Comment
Please, Sign In to add comment