Skip to content

Commit e016360

Browse files
authored
Merge pull request godotengine#8473 from chocola-mint/master
Remove outdated C++ tutorial in "Your first 2D game"
2 parents 408599e + e2f9034 commit e016360

File tree

5 files changed

+0
-467
lines changed

5 files changed

+0
-467
lines changed

getting_started/first_2d_game/03.coding_the_player.rst

Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
8548
Using the ``export`` keyword on the first variable ``speed`` allows us to set
8649
its value in the Inspector. This can be handy for values that you want to be
8750
able 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-
13687
Now 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
13889
game, 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-
265199
We start by setting the ``velocity`` to ``(0, 0)`` - by default, the player
266200
should 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-
433343
Preparing 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-
470365
This 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
472367
collision. 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-
518403
Each time an enemy hits the player, the signal is going to be emitted. We need
519404
to disable the player's collision so that we don't trigger the ``hit`` signal
520405
more 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-
556432
With the player working, we'll work on the enemy in the next lesson.

getting_started/first_2d_game/04.creating_the_enemy.rst

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -78,35 +78,6 @@ Add a script to the ``Mob`` like this:
7878
// Don't forget to rebuild the project.
7979
}
8080

81-
.. code-tab:: cpp
82-
83-
// Copy `player.gdns` to `mob.gdns` and replace `Player` with `Mob`.
84-
// Attach the `mob.gdns` file to the Mob node.
85-
86-
// Create two files `mob.cpp` and `mob.hpp` next to `entry.cpp` in `src`.
87-
// This code goes in `mob.hpp`. We also define the methods we'll be using here.
88-
#ifndef MOB_H
89-
#define MOB_H
90-
91-
#include <AnimatedSprite2D.hpp>
92-
#include <Godot.hpp>
93-
#include <RigidBody2D.hpp>
94-
95-
class Mob : public godot::RigidBody2D {
96-
GODOT_CLASS(Mob, godot::RigidBody2D)
97-
98-
godot::AnimatedSprite2D *_animated_sprite;
99-
100-
public:
101-
void _init() {}
102-
void _ready();
103-
void _on_visible_on_screen_notifier_2d_screen_exited();
104-
105-
static void _register_methods();
106-
};
107-
108-
#endif // MOB_H
109-
11081
Now let's look at the rest of the script. In ``_ready()`` we play the animation
11182
and randomly choose one of the three animation types:
11283

@@ -126,22 +97,6 @@ and randomly choose one of the three animation types:
12697
animatedSprite2D.Play(mobTypes[GD.Randi() % mobTypes.Length]);
12798
}
12899

129-
.. code-tab:: cpp
130-
131-
// This code goes in `mob.cpp`.
132-
#include "mob.hpp"
133-
134-
#include <RandomNumberGenerator.hpp>
135-
#include <SpriteFrames.hpp>
136-
137-
void Mob::_ready() {
138-
godot::Ref<godot::RandomNumberGenerator> random = godot::RandomNumberGenerator::_new();
139-
_animated_sprite = get_node<godot::AnimatedSprite2D>("AnimatedSprite2D");
140-
_animated_sprite->set_playing(true);
141-
godot::PoolStringArray mob_types = _animated_sprite->get_sprite_frames()->get_animation_names();
142-
_animated_sprite->set_animation(mob_types[random->randi() % mob_types.size()]);
143-
}
144-
145100
First, we get the list of animation names from the AnimatedSprite2D's ``sprite_frames``
146101
property. This returns an Array containing all three animation names: ``["walk",
147102
"swim", "fly"]``.
@@ -167,13 +122,6 @@ to the ``Mob`` and add this code:
167122
QueueFree();
168123
}
169124

170-
.. code-tab:: cpp
171-
172-
// This code goes in `mob.cpp`.
173-
void Mob::_on_visible_on_screen_notifier_2d_screen_exited() {
174-
queue_free();
175-
}
176-
177125
This completes the `Mob` scene.
178126

179127
With the player and enemies ready, in the next part, we'll bring them together

0 commit comments

Comments
 (0)