@@ -45,43 +45,6 @@ Start by declaring the member variables this object will need:
4545 public Vector2 ScreenSize; // Size of the game window.
4646 }
4747
48- .. code-tab :: cpp
49-
50- // A `player.gdns ` file has already been created for you. Attach it to the Player node.
51-
52- // Create two files `player.cpp ` and `player.hpp ` next to `entry.cpp ` in `src `.
53- // This code goes in `player.hpp `. We also define the methods we'll be using here.
54- #ifndef PLAYER_H
55- #define PLAYER_H
56-
57- #include <AnimatedSprite2D.hpp>
58- #include <Area2D.hpp>
59- #include <CollisionShape2D.hpp>
60- #include <Godot.hpp>
61- #include <Input.hpp>
62-
63- class Player : public godot::Area2D {
64- GODOT_CLASS(Player, godot::Area2D)
65-
66- godot::AnimatedSprite2D *_animated_sprite;
67- godot::CollisionShape2D *_collision_shape;
68- godot::Input *_input;
69- godot::Vector2 _screen_size; // Size of the game window.
70-
71- public:
72- real_t speed = 400; // How fast the player will move (pixels/sec).
73-
74- void _init() {}
75- void _ready();
76- void _process(const double p_delta);
77- void start(const godot::Vector2 p_position);
78- void _on_body_entered(godot::Node2D *_body);
79-
80- static void _register_methods();
81- };
82-
83- #endif // PLAYER_H
84-
8548Using the ``export `` keyword on the first variable ``speed `` allows us to set
8649its value in the Inspector. This can be handy for values that you want to be
8750able to adjust just like a node's built-in properties. Click on the ``Player ``
@@ -121,18 +84,6 @@ a good time to find the size of the game window:
12184 ScreenSize = GetViewportRect().Size;
12285 }
12386
124- .. code-tab :: cpp
125-
126- // This code goes in `player.cpp `.
127- #include "player.hpp"
128-
129- void Player::_ready() {
130- _animated_sprite = get_node<godot::AnimatedSprite2D>("AnimatedSprite2D");
131- _collision_shape = get_node<godot::CollisionShape2D>("CollisionShape2D");
132- _input = godot::Input: :get_singleton();
133- _screen_size = get_viewport_rect().size;
134- }
135-
13687Now we can use the ``_process() `` function to define what the player will do.
13788``_process() `` is called every frame, so we'll use it to update elements of our
13889game, which we expect will change often. For the player, we need to do the
@@ -245,23 +196,6 @@ which returns ``true`` if it's pressed or ``false`` if it isn't.
245196 }
246197 }
247198
248- .. code-tab :: cpp
249-
250- // This code goes in `player.cpp `.
251- void Player::_process(const double p_delta) {
252- godot::Vector2 velocity(0, 0);
253-
254- velocity.x = _input->get_action_strength("move_right") - _input->get_action_strength("move_left");
255- velocity.y = _input->get_action_strength("move_down") - _input->get_action_strength("move_up");
256-
257- if (velocity.length() > 0) {
258- velocity = velocity.normalized() * speed;
259- _animated_sprite->play();
260- } else {
261- _animated_sprite->stop();
262- }
263- }
264-
265199We start by setting the ``velocity `` to ``(0, 0) `` - by default, the player
266200should not be moving. Then we check each input and add/subtract from the
267201``velocity `` to obtain a total direction. For example, if you hold ``right `` and
@@ -308,14 +242,6 @@ the ``_process`` function (make sure it's not indented under the `else`):
308242 y: Mathf.Clamp(Position.Y, 0, ScreenSize.Y)
309243 );
310244
311- .. code-tab :: cpp
312-
313- godot::Vector2 position = get_position();
314- position += velocity * (real_t)p_delta;
315- position.x = godot::Math: :clamp(position.x, (real_t)0.0, _screen_size.x);
316- position.y = godot::Math: :clamp(position.y, (real_t)0.0, _screen_size.y);
317- set_position(position);
318-
319245.. tip :: The `delta` parameter in the `_process()` function refers to the *frame
320246 length* - the amount of time that the previous frame took to complete.
321247 Using this value ensures that your movement will remain consistent even
@@ -370,18 +296,6 @@ movement. Let's place this code at the end of the ``_process()`` function:
370296 animatedSprite2D.FlipV = velocity.Y > 0;
371297 }
372298
373- .. code-tab :: cpp
374-
375- if (velocity.x != 0) {
376- _animated_sprite->set_animation("walk");
377- _animated_sprite->set_flip_v(false);
378- // See the note below about boolean assignment.
379- _animated_sprite->set_flip_h(velocity.x < 0);
380- } else if (velocity.y != 0) {
381- _animated_sprite->set_animation("up");
382- _animated_sprite->set_flip_v(velocity.y > 0);
383- }
384-
385299.. Note :: The boolean assignments in the code above are a common shorthand for
386300 programmers. Since we're doing a comparison test (boolean) and also
387301 *assigning * a boolean value, we can do both at the same time. Consider
@@ -426,10 +340,6 @@ When you're sure the movement is working correctly, add this line to
426340
427341 Hide();
428342
429- .. code-tab :: cpp
430-
431- hide();
432-
433343Preparing for collisions
434344~~~~~~~~~~~~~~~~~~~~~~~~
435345
@@ -452,21 +362,6 @@ Add the following at the top of the script. If you're using GDScript, add it aft
452362 [Signal]
453363 public delegate void HitEventHandler();
454364
455- .. code-tab :: cpp
456-
457- // This code goes in `player.cpp `.
458- // We need to register the signal here, and while we're here, we can also
459- // register the other methods and register the speed property.
460- void Player::_register_methods() {
461- godot::register_method("_ready", &Player::_ready);
462- godot::register_method("_process", &Player::_process);
463- godot::register_method("start", &Player::start);
464- godot::register_method("_on_body_entered", &Player::_on_body_entered);
465- godot::register_property("speed", &Player::speed, (real_t)400.0);
466- // This below line is the signal.
467- godot::register_signal<Player>("hit", godot::Dictionary());
468- }
469-
470365This defines a custom signal called "hit" that we will have our player emit
471366(send out) when it collides with an enemy. We will use ``Area2D `` to detect the
472367collision. Select the ``Player `` node and click the "Node" tab next to the
@@ -505,16 +400,6 @@ this code to the function:
505400 GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred(CollisionShape2D.PropertyName.Disabled, true);
506401 }
507402
508- .. code-tab :: cpp
509-
510- // This code goes in `player.cpp `.
511- void Player::_on_body_entered(godot::Node2D *_body) {
512- hide(); // Player disappears after being hit.
513- emit_signal("hit");
514- // Must be deferred as we can't change physics properties on a physics callback.
515- _collision_shape->set_deferred("disabled", true);
516- }
517-
518403Each time an enemy hits the player, the signal is going to be emitted. We need
519404to disable the player's collision so that we don't trigger the ``hit `` signal
520405more than once.
@@ -544,13 +429,4 @@ starting a new game.
544429 GetNode<CollisionShape2D>("CollisionShape2D").Disabled = false;
545430 }
546431
547- .. code-tab :: cpp
548-
549- // This code goes in `player.cpp `.
550- void Player::start(const godot::Vector2 p_position) {
551- set_position(p_position);
552- show();
553- _collision_shape->set_disabled(false);
554- }
555-
556432With the player working, we'll work on the enemy in the next lesson.
0 commit comments