ichou1のブログ

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

Godotメモ(その1)

オープンソースゲームエンジン「Godot」を試してみる。

バージョンは「3.1.1」(2019年4月27日リリース、64bit LinuxのStandard版)を使用。
Godot Engine - Download | Linux

実行バイナリは60MBほど。

step by stepにある「Dodge the Creeps!」から始めてみる。
Your first game — Godot Engine latest documentation

Your character must move and avoid the enemies for as long as possible.

上下左右に動くことで、迫りくる敵をかわすゲーム(攻撃はしない)
f:id:ichou1:20191103123326p:plain

「Player scene」だけを作成した状態で、バックエンドで保存される以下3ファイルの中身を見てみる。

  • project.godot
  • Player.tscn
  • Player.gd
シーン実行中

f:id:ichou1:20191104110110p:plain

project.godot

game windowの設定はここに反映される。

[display]
window/size/width=480
window/size/height=720
Player.tscn

ファイルフォーマットの定義

external resourcesの定義

[ext_resource path="res://Player.gd" type="Script" id=1]
[ext_resource path="res://art/playerGrey_walk1.png" type="Texture" id=2]
[ext_resource path="res://art/playerGrey_walk2.png" type="Texture" id=3]
[ext_resource path="res://art/playerGrey_up1.png" type="Texture" id=4]
[ext_resource path="res://art/playerGrey_up2.png" type="Texture" id=5]

node(child node)の定義

[sub_resource type="SpriteFrames" id=1]  ; handle the appearance and animations 
[sub_resource type="CapsuleShape2D" id=2]  ; determine player’s collision area

衝突領域の定義。

[sub_resource type="CapsuleShape2D" id=2]
radius = 28.0
height = 14.0

Connectionsの定義

[connection signal="body_entered" from="." to="." method="_on_Player_body_entered"]
Player.gd

GDScriptについて

以下の関数を定義

  • _ready() : called when a node enters the scene tree
  • _process(delta) : define what the player will do
  • start(pos) : reset the player
  • _on_Player_body_entered(body) : define a custom signal called “hit”

フレームレートは60FPS。

Sprite画面は「flip_h」、「flip_v」プロパティで反転操作ができるので、反転画像を用意する必要はない。

func _process(delta):
    ...
    position += velocity * delta
    ...
    if velocity.x < 0:
        $AnimatedSprite.flip_h = true
    else:
        $AnimatedSprite.flip_h = false
    ...

Collision(衝突)を検知するとcustomシグナル"hit"を発行。
シグナルはCallbackにあたるもの。

signal hit

func _on_Player_body_entered(body):
    hide()  # Player disappears after being hit.
    emit_signal("hit")
    $CollisionShape2D.set_deferred("disabled", true)  # same as below
    # get_node("CollisionShape2D").set_deferred("disabled", true)

次回に続く。