1. Starting Out
1. Godot Download, Orientation, and Setup
2. Creating the Player
3. Player Movement
# player.gd
extends CharacterBody2D
const MAX_SPEED = 200
# Called when the node enters the scene tree for the first time.
# 该节点位于场景树中, 一切都可以使用, 此时其子节点都加载完毕
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
# 每帧都会被调用的函数
func _process(delta):
var movement_vector = get_movement_vector()
# 归一化 -> 变成长度为1的向量
var direction = movement_vector.normalized()
# velocity: 角色的速度属性
velocity = direction * MAX_SPEED
# 根据velocity移动
move_and_slide()
func get_movement_vector():
var movement_vector = Vector2.ZERO
# 按下-> get_action_strength("move_right")==1, 没按为0, 左右都按抵消==0
var x_movement = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
var y_movement = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
return Vector2(x_movement, y_movement)
4. Creating a TileMap
5. Game Camera
# game_camera.gd
extends Camera2D
# Called when the node enters the scene tree for the first time.
func _ready():
make_current()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
# 获取当前场景树, 获取树中节点
var player_nodes = get_tree().get_nodes_in_group("player")
if player_nodes.size() > 0:
var player = player_nodes[0] as Node2D
global_position = player.global_position
让镜头比人物慢一点, 显得更自然, 我们可以使用独立的帧率来实现
# game_camera.gd
extends Camera2D
var target_position = Vector2.ZERO
# Called when the node enters the scene tree for the first time.
func _ready():
make_current()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
acquire_target()
# lerp 线性插值 有开始和结束点, 用百分比来表示两个位置的距离的百分比
# 每一帧都会移动到离target_position 1.0 - exp(-delta * 10)的位置
global_position = global_position.lerp(target_position, 1.0 - exp(-delta * 10))
func acquire_target():
# 获取当前场景树, 获取树中节点
var player_nodes = get_tree().get_nodes_in_group("player")
if player_nodes.size() > 0:
var player = player_nodes[0] as Node2D
target_position = player.global_position
global_position = player.global_position
2. Building the Foundation
1. Creating a Rat Enemy
extends CharacterBody2D
const MAX_SPEED = 75
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
var direction = get_direction_to_player()
velocity = direction * MAX_SPEED
move_and_slide()
func get_direction_to_player():
var player_node = get_tree().get_first_node_in_group("player") as Node2D
if player_node != null:
return (player_node.global_position - global_position).normalized()
return Vector2.ZERO
2. Creating the First Sword Ability
extends Node
# 导出全局变量
@export var sword_ability: PackedScene
# Called when the node enters the scene tree for the first time.
func _ready():
#get_node('Timer')
$Timer.timeout.connect(on_timer_timeout)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
# 每次触发计时器执行的操作
func on_timer_timeout():
var player = get_tree().get_first_node_in_group("player") as Node2D
if player == null:
return
# 插入到scene: main
var sword_instance = sword_ability.instantiate() as Node2D
player.get_parent().add_child(sword_instance)
sword_instance.global_position = player.global_position
3. Introduction to AnimationPlayer
4. Targeting Enemies With Sword Ability
extends Node
const MAX_RANGE = 150
# 导出全局变量
@export var sword_ability: PackedScene
# Called when the node enters the scene tree for the first time.
func _ready():
#get_node('Timer')
$Timer.timeout.connect(on_timer_timeout)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
# 每次触发计时器执行的操作
func on_timer_timeout():
var player = get_tree().get_first_node_in_group("player") as Node2D
if player == null:
return
var enemies = get_tree().get_nodes_in_group("enemy")
# 过滤掉距离玩家超过150方圆的敌人
enemies = enemies.filter(func(enemy: Node2D):
return enemy.global_position.distance_squared_to(player.global_position) < pow(MAX_RANGE, 2)
)
if enemies.size() == 0:
return
enemies.sort_custom(func(a: Node2D, b: Node2D):
var a_distance = a.global_position.distance_squared_to(player.global_position)
var b_distance = b.global_position.distance_squared_to(player.global_position)
return a_distance < b_distance
)
# 插入到scene: main
var sword_instance = sword_ability.instantiate() as Node2D
player.get_parent().add_child(sword_instance)
sword_instance.global_position = enemies[0].global_position