Rally Rush
Loading...
Searching...
No Matches
turret.hpp
Go to the documentation of this file.
1#ifndef TURRET_HPP
2#define TURRET_HPP
3
4#include <godot_cpp/classes/area3d.hpp>
5#include <godot_cpp/classes/packed_scene.hpp>
6#include <godot_cpp/classes/rigid_body3d.hpp>
7#include <godot_cpp/templates/hash_set.hpp>
8#include <godot_cpp/templates/vector.hpp>
9
10namespace godot {
12enum class TurretState {
14 WAITING = 0x0,
16 LOCKING = 0x1,
20 FIRING
21};
22
24class Turret : public RigidBody3D {
25 GDCLASS(Turret, RigidBody3D);
27 static void _bind_methods();
28public:
30 virtual void _enter_tree() override;
32 virtual void _process(double delta_time) override;
33
35 void awareness_changed();
37 Node3D *select_target();
38
40 void lead_target(double delta_time);
41
43 void detect_node(Node3D *node);
45 void lose_node(Node3D *node);
47 void try_next_state();
49 void create_beam();
52
54 void beam_ended();
55
57 void set_fire_time(float val);
59 float get_fire_time() const;
61 void set_charge_time(float val);
63 float get_charge_time() const;
65 void set_lock_time(float val);
67 float get_lock_time() const;
68
70 void set_invert_targets(bool value);
72 void set_attack_classes(Array array);
74 Array get_attack_classes() const;
76 void set_beam_scene(Ref<PackedScene> scene);
78 Ref<PackedScene> get_beam_scene() const;
80 void set_guiding_beam_scene(Ref<PackedScene> scene);
82 Ref<PackedScene> get_guiding_beam_scene() const;
83private:
85 Node3D *current_target{nullptr};
89 Vector3 aim_position{0.f, 0.f, 0.f};
91 Vector3 last_target_position{0.f, 0.f, 0.f};
95 float lead_distance{1.2f};
96
98 Node3D *gun_node{nullptr};
100 Vector3 gun_node_offset{0.f, 0.f, 0.f};
101
104
106 Vector<StringName> attack_classes{};
108 HashSet<Node3D*> awareness{};
109
111 Ref<PackedScene> beam_scene{};
113 Ref<PackedScene> guiding_beam_scene{};
114
115 float fire_time{1.0f};
116 float charge_time{1.f};
117 float lock_time{1.3f};
118};
119}
120
121#endif // !TURRET_HPP
Generic turret class with the ability to target any type of node as configured. Used for both the pla...
Definition turret.hpp:24
Ref< PackedScene > guiding_beam_scene
Scene to spawn when charging.
Definition turret.hpp:113
void set_charge_time(float val)
The duration of the guiding lazer.
Definition turret.cpp:177
virtual void _process(double delta_time) override
Update state machine. Update gun node position.
Definition turret.cpp:33
void set_lock_time(float val)
The delay between firing and charging the next shot.
Definition turret.cpp:185
float get_fire_time() const
The duration of the damaging lazer.
Definition turret.cpp:173
Node3D * select_target()
Select a target from the currently known nodes.
Definition turret.cpp:59
GDCLASS(Turret, RigidBody3D)
float get_lock_time() const
The delay between firing and charging the next shot.
Definition turret.cpp:189
Ref< PackedScene > beam_scene
Scene to spawn when firing.
Definition turret.hpp:111
void detect_node(Node3D *node)
Become aware of a node in the awareness area.
Definition turret.cpp:92
TurretState state
Current state-machine state of the turret.
Definition turret.hpp:103
HashSet< Node3D * > awareness
Set of all nodes the turret is currently aware of.
Definition turret.hpp:108
float fire_time
Definition turret.hpp:115
Vector3 last_target_position
Position of the current_target on the last frame.
Definition turret.hpp:91
void lose_node(Node3D *node)
Lose track of a node exiting the awareness area.
Definition turret.cpp:99
void beam_ended()
End the fire state.
Definition turret.cpp:164
void lead_target(double delta_time)
Aim ahead of the current target.
Definition turret.cpp:73
Array get_attack_classes() const
Set the node classes to attack while invert_targets is false.
Definition turret.cpp:212
float last_state_switch
Last time the state was changed.
Definition turret.hpp:93
float get_charge_time() const
The duration of the guiding lazer.
Definition turret.cpp:181
float lead_distance
Distance ahead of target to aim.
Definition turret.hpp:95
void set_attack_classes(Array array)
Set the node classes to attack while invert_targets is false.
Definition turret.cpp:202
Vector3 gun_node_offset
Position offset of the gun.
Definition turret.hpp:100
Ref< PackedScene > get_beam_scene() const
Set the scene to spawn when firing.
Definition turret.cpp:223
void awareness_changed()
Check if a new target needs to be found. Find a new target if required.
Definition turret.cpp:45
bool invert_attack_classes
If true, target classes not in attack_classes.
Definition turret.hpp:87
void set_fire_time(float val)
The duration of the damaging lazer.
Definition turret.cpp:169
static void _bind_methods()
register editor properties
Definition turret.cpp:12
Node3D * current_target
The current target node.
Definition turret.hpp:85
void set_invert_targets(bool value)
Set to true to target everything but the attack_classes.
Definition turret.cpp:193
void try_next_state()
Try transition to the next state if possible.
Definition turret.cpp:109
void create_guiding_beam()
Create a non-damaging guiding beam.
Definition turret.cpp:152
float lock_time
Definition turret.hpp:117
Node3D * gun_node
The child node representing the gun.
Definition turret.hpp:98
Ref< PackedScene > get_guiding_beam_scene() const
Set the scene to spawn when charging.
Definition turret.cpp:231
Vector3 aim_position
Position currently being aimed at.
Definition turret.hpp:89
Vector< StringName > attack_classes
Names of classes to attack.
Definition turret.hpp:106
virtual void _enter_tree() override
Freeze physics and connect awareness observers. Fetch child nodes and initialize them....
Definition turret.cpp:22
float charge_time
Definition turret.hpp:116
void create_beam()
Create a damaging beam.
Definition turret.cpp:139
void set_beam_scene(Ref< PackedScene > scene)
Set the scene to spawn when firing.
Definition turret.cpp:219
void set_guiding_beam_scene(Ref< PackedScene > scene)
Set the scene to spawn when charging.
Definition turret.cpp:227
Definition beacon_powerup.cpp:6
TurretState
Current state of a turret.
Definition turret.hpp:12
@ WAITING
No known valid targets.
@ FIRING
Wait for beam to end.
@ CHARGING
Hold still.
@ LOCKING
Aim in front of an enemy.