Godot 游戏引擎 Your first game 学习笔记

1.控制玩家移动

func _process(delta): 中进行处理 :

    var velocity = Vector2()
    if Input.is_action_pressed("ui_up"):
        velocity.y -= 1
    if Input.is_action_pressed("ui_down"):
        velocity.y += 1
    if Input.is_action_pressed("ui_left"):
        velocity.x -= 1
    if Input.is_action_pressed("ui_right"):
        velocity.x += 1
    if velocity.length() > 0:
        velocity = velocity.normalized() * speed   #velocity.normalized() 将向量变成一个单位比例
        $AnimatedSprite.play() #播放动画
    else:
        $AnimatedSprite.stop()
    
    position += velocity *delta  # 变化位置
    # 限制在 屏幕区域内部
    # screensize = get_viewport_rect().size 获取 屏幕区域大小
    position.x = clamp(position.x , 0 ,screensize.x)  
    position.y = clamp(position.y , 0 ,screensize.y)
    
    # 设置动画方向,默认 右.上, 如果检测到速度变化,则 反转动画方向
    # 动画只需要设置一个方向即可。
    if velocity.x != 0:
        $AnimatedSprite.animation = "right"
        $AnimatedSprite.flip_v = false 
        $AnimatedSprite.flip_h = (velocity.x < 0 )
    
    if velocity.y != 0:
        $AnimatedSprite.animation = "up"
        $AnimatedSprite.flip_v = (velocity.y > 0 )
        $AnimatedSprite.flip_h = false

动画部分改进: 如果同时按下 上,右方向,则导致 不现实帧动画,具体改进如下:

# 设置动画方向,默认 右.上, 如果检测到速度变化,则 反转动画方向
    # 动画只需要设置一个方向即可。
    if velocity.x != 0 and (abs(velocity.x) > abs(velocity.y)):
        $AnimatedSprite.animation = "right"
        $AnimatedSprite.flip_v = false 
        $AnimatedSprite.flip_h = (velocity.x < 0 )
    
    if velocity.y != 0 and (abs(velocity.x) < abs(velocity.y)):
        $AnimatedSprite.animation = "up"
        $AnimatedSprite.flip_v = (velocity.y > 0 )

Player 为Area2D 类型,移动的敌人是 Rigidbody2D类型,可以连接 body_entered 信号,进行检测,是否与敌人发生碰撞

func _on_Player_body_entered(body):
    hide()
    emit_signal("hit")
    $CollisionShape2D.disabled =  true 
    print('debug--> ',body)

2.敌人相关:

VisibilityNotifier2D组建,可以检测是否移动出了屏幕,进行相关判断,如果移动出屏幕,则调度自己进行删除。

func _on_Visibility_screen_exited():
    queue_free()

GDSCript 控制分组相关:

  1. 将节点加入分组: xxx.add_to_group("enemies")
  2. 获取分组,并且调用对应的函数: get_tree().call_group("enemies", "player_was_discovered")
  3. var enemies = get_tree().get_nodes_in_group("enemies") 获取分组的所有节点

动态加载场景,实例化,并且绑定信号:

        var player =  load('res://Player.tscn')   # 加载场景
    player_inst = player.instance()    # 实例化
    add_child(player_inst)                 # 添加到节点 
    player_inst.connect('hit',self,'game_over') # 绑定信号

鼠标输入相关判断:

extends Sprite

signal shoot(bullet, direction, location)

var Bullet = preload("res://Bullet.tscn")

func _input(event):
    if event is InputEventMouseButton:
        if event.button_index == BUTTON_LEFT and event.pressed:
            emit_signal("shoot", Bullet, rotation, position)

func _process(delta):
    look_at(get_global_mouse_position())

触摸相关 屏幕相关:

需要在项目设置中开启 模拟触屏事件
第一步是打开”Project Settings”并找到 “Display” -> “Window” -> Handheld 部分。启用 Emulate Touchscreen 选项。这使得您可以将鼠标点击事件视为触摸事件

func _input(event):
    if event is  InputEventScreenTouch and event.is_pressed():
        _isTouchControl = true 
        target = event.position

func _process(delta):
#   # Called every frame. Delta is time since last frame.
#   # Update game logic here.

    if position.distance_to(target) > 10 :
        velocity = (target-position).normalized() * speed 
    else:
        #  键盘控制 
        velocity = Vector2()
        _isTouchControl = false

手动定义动画 tween 的用法

Tween interpolate_property方法。它需要七个参数:

  1. 对拥有该属性的节点进行动画的引用
  2. 属性的标识符作为字符串
  3. 起始值
  4. 端值
  5. 动画以秒为单位的持续时间
  6. 过渡的类型
  7. 过度类型的参数。

以下示例:相当于在0.3秒内,将animated_health 设置为 新值 health
然后在_process中 更新进玩家的生命值度条

func _process(delta):
    bar.value = animated_health
    number_label.text = str(round(animated_health))
    pass

# 更新 生命值
func update_health(health:int):
    tween.interpolate_property(self,"animated_health",animated_health,health,0.3,Tween.TRANS_LINEAR,Tween.EASE_IN)
    if !tween.is_active():
        tween.set_active(true)
    pass    

示例效果 如下:(由于插值有可能是浮点数,所以,需要使用函数将其转换为整数)

bar.value = animated_health
number_label.text = str(round(animated_health))
Godot 游戏引擎 Your first game 学习笔记_第1张图片
lifebar_tutorial_number_animation_messed_up.gif

切换场景

change_scene ( String path )
change_scene_to ( PackedScene packed_scene )
返回值Error

Kinematic character (2D) 控制运动

var speed = 200

var gravity = 200

var velocity =Vector2()

func _ready():
    pass # Replace with function body.

func get_input():
    
    if Input.is_action_pressed("ui_right"):
        velocity.x = speed  
        pass
    elif Input.is_action_pressed("ui_left"):
        velocity.x = -speed
    else:
        velocity.x = 0

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
    get_input()
    velocity.y += gravity*delta
    #move_and_collide(velocity)
    move_and_slide(velocity,Vector2(0,-1))

蓄能

蓄能时间 需要根据实际 进行适当调整 , 另外设置最大值 , 蓄能可以从
-max_impulse 到 + max_impulse:float ,方便进行方向控制

extends RigidBody2D

###  备注: 蓄能条核心代码 
###  
###  瞬间冲力大小,可以有方向,上方或者下方

var to_maximpulse_time =  2
var impulse:float =0  
var max_impulse:float = 500


var acceleration  # 计算得出

# Called when the node enters the scene tree for the first time.
func _ready():
    acceleration = max_impulse / to_maximpulse_time
    $ProgressBar.min_value = -max_impulse
    $ProgressBar.max_value = max_impulse
    pass # Replace with function body.

func _process(delta):
    
    if Input.is_action_pressed("ui_select"):
        #self.linear_velocity.y = -speed  # y is down, we need player up 
        #
        # 需要判断 目前最大速度,否则速度太大 会导致穿过模型出去,
        # 一个物理周期 的位移穿过了模型,导致检测不到 碰撞了。
        if self.linear_velocity.length() < 1000.0:
            self.apply_central_impulse( Vector2(0,-impulse).rotated(self.rotation ) )
    # update speed 
    _update_speed(delta)
    $ProgressBar.value = impulse
    pass

func _physics_process(delta):
    pass

func _update_speed(delta):
    if abs(impulse)  > abs(max_impulse):
        acceleration = -acceleration 
    impulse +=delta*acceleration
    

你可能感兴趣的:(Godot 游戏引擎 Your first game 学习笔记)