Skip to content

Commit

Permalink
Your first 2D game: Set the physics parameters of the mob before addi…
Browse files Browse the repository at this point in the history
…ng it to the scene tree (#5688)
  • Loading branch information
rburing authored Mar 13, 2022
1 parent cfe1c25 commit 22fb8f1
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions getting_started/first_2d_game/05.the_main_game_scene.rst
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,13 @@ Note that a new instance must be added to the scene using ``add_child()``.
.. code-tab:: gdscript GDScript

func _on_MobTimer_timeout():
# Create a new instance of the Mob scene.
var mob = mob_scene.instance()

# Choose a random location on Path2D.
var mob_spawn_location = get_node("MobPath/MobSpawnLocation");
var mob_spawn_location = get_node("MobPath/MobSpawnLocation")
mob_spawn_location.offset = randi()

# Create a Mob instance and add it to the scene.
var mob = mob_scene.instance()
add_child(mob)

# Set the mob's direction perpendicular to the path direction.
var direction = mob_spawn_location.rotation + PI / 2

Expand All @@ -340,26 +339,28 @@ Note that a new instance must be added to the scene using ``add_child()``.
direction += rand_range(-PI / 4, PI / 4)
mob.rotation = direction

# Choose the velocity.
# Choose the velocity for the mob.
var velocity = Vector2(rand_range(150.0, 250.0), 0.0)
mob.linear_velocity = velocity.rotated(direction)

# Spawn the mob by adding it to the Main scene.
add_child(mob)

.. code-tab:: csharp

public void OnMobTimerTimeout()
{
// Note: Normally it is best to use explicit types rather than the `var`
// keyword. However, var is acceptable to use here because the types are
// obviously PathFollow2D and Mob, since they appear later on the line.
// obviously Mob and PathFollow2D, since they appear later on the line.

// Create a new instance of the Mob scene.
var mob = (Mob)MobScene.Instance();

// Choose a random location on Path2D.
var mobSpawnLocation = GetNode<PathFollow2D>("MobPath/MobSpawnLocation");
mobSpawnLocation.Offset = GD.Randi();

// Create a Mob instance and add it to the scene.
var mob = (Mob)MobScene.Instance();
AddChild(mob);

// Set the mob's direction perpendicular to the path direction.
float direction = mobSpawnLocation.Rotation + Mathf.Pi / 2;

Expand All @@ -373,19 +374,21 @@ Note that a new instance must be added to the scene using ``add_child()``.
// Choose the velocity.
var velocity = new Vector2((float)GD.RandRange(150.0, 250.0), 0);
mob.LinearVelocity = velocity.Rotated(direction);

// Spawn the mob by adding it to the Main scene.
AddChild(mob);
}

.. code-tab:: cpp

// This code goes in `main.cpp`.
void Main::_on_MobTimer_timeout() {
// Create a new instance of the Mob scene.
godot::Node *mob = mob_scene->instance();
// Choose a random location on Path2D.
_mob_spawn_location->set_offset((real_t)_random->randi());

// Create a Mob instance and add it to the scene.
godot::Node *mob = mob_scene->instance();
add_child(mob);
// Set the mob's direction perpendicular to the path direction.
real_t direction = _mob_spawn_location->get_rotation() + (real_t)Math_PI / 2;

Expand All @@ -399,6 +402,9 @@ Note that a new instance must be added to the scene using ``add_child()``.
// Choose the velocity for the mob.
godot::Vector2 velocity = godot::Vector2(_random->randf_range(150.0, 250.0), 0.0);
mob->set("linear_velocity", velocity.rotated(direction));

// Spawn the mob by adding it to the Main scene.
add_child(mob);
}

.. important:: Why ``PI``? In functions requiring angles, Godot uses *radians*,
Expand Down

0 comments on commit 22fb8f1

Please sign in to comment.