ichou1のブログ

主に音声認識、時々、データ分析のことを書く

Godotメモ(その2)

前回の続き。

step by stepの説明に従って、新たなscene「Mob」と「Main」を追加する。
「Mob」は個々のEnemy(敵)に該当し、「Main」でインスタンス化される。

mobs will spawn randomly at the edges of the screen and move in a random direction in a straight line, then despawn when they go offscreen.

動きは直進。画面を飛び出せば消滅する。

「Mob」はRigitBody2Dノード(エンジンにより制御される)として生成する。

Mob.tscn
[node name="Mob" type="RigidBody2D"]
collision_mask = 0   ; mobs do not collide with each other
gravity_scale = 0.0  ; mob will not fall downward

続いて「Main」scene

The Main node will be spawning new mobs, and we want them to appear at a random location on the edge of the screen.

Mobを画面の端からランダムに生成する。

当初、画面の端を示す閉曲線(Closed Curve)を指定する操作が理解できなかった。

Select the middle one (“Add Point”) and draw the path by clicking to add the points at the corners shown.

画面上でのポインティング操作では微妙にズレるので、TSCNファイルに直接書く方がやりやすい。
曲線はclockwise order(時計回り)になるように指定する。
座標は画面左上が(0, 0)、左方向にxの正の増加、下方向にyの正の増加。

Main.tscn
[node name="MobPath" type="Path2D" parent="."]
curve = SubResource( 1 )

[node name="MobSpawnLocation" type="PathFollow2D" parent="MobPath"]
[sub_resource type="Curve2D" id=1]
_data = {
"points": PoolVector2Array(   0,   0,
                              0,   0,
                              0,   0, <-- upper left
                              0,   0,
                              0,   0,
                            480,   0, <-- upper right
                              0,   0,
                              0,   0,
                            480, 720, <-- bottom right
                              0,   0,
                              0,   0,
                              0, 720, <-- bottom left
                              0,   0,
                              0,   0,
                              0,   0  <-- upper left
                            )}

試しに反時計まわりにしてしてみると、ドキュメントの説明にあるとおり、

Draw the path in clockwise order, or your mobs will spawn pointing outwards instead of inwards!

外側に向かって生成された。
f:id:ichou1:20191107211306p:plain

Mobは0.5秒おきに生成する。

Main.tscn
[node name="MobTimer" type="Timer" parent="."]
wait_time = 0.5

[connection signal="timeout" from="MobTimer" to="." method="_on_MobTimer_timeout"]
Main.gd
func _on_MobTimer_timeout():
    $MobPath/MobSpawnLocation.offset = randi()
    var mob = Mob.instance()
    ...
    mob.position = $MobPath/MobSpawnLocation.position



「Main」sceneの説明で、分かりにくかった他2箇所についてメモしておく。

1つ目。下記の赤字がどこのことか分からなかった。

Drag Mob.tscn from the “FileSystem” panel and drop it in the Mob property under the Script Variables of the Main node.

下図赤枠のこと。
f:id:ichou1:20191107213503p:plain

SceneTreeでは、「Player.tscn」は出てくるが、「Mob.tscn」は出てこない。
f:id:ichou1:20191107213135p:plain

TSCNファイルを見ると、「Player.tscn」よりも前のindex番号で追加されている。

Main.tscn
[ext_resource path="res://Main.gd" type="Script" id=1]
[ext_resource path="res://Mob.tscn" type="PackedScene" id=2]  ; <--
[ext_resource path="res://Player.tscn" type="PackedScene" id=3]
...
[node name="Main" type="Node"]
script = ExtResource( 1 )
Mob = ExtResource( 2 )  ; <--
...



2つ目。下記の赤字がどこのことか分からなかった。

To have the points snap to the grid, make sure “Snap to Grid” is checked.
This option can be found under the “Snapping options” button to the left of the “Lock” button, appearing as a series of three vertical dots.

下図赤枠のこと。
f:id:ichou1:20191107220003p:plain

次回に続く。