Godot学习笔记(6)—— 简单2D项目(2)

1. 创建敌人场景

  1.1 新建场景

  1.2 添加RigidBody2D节点

  刚体节点,用于检测碰撞,及各种物理影响。

  改名为mob,所有敌人都可以统称为这个。

  1.3 为 Mob 节点添加子节点

  AnimatedSprite、CollisionShape2D、VisibilityNotifier2D,设置为无法选中。

  1.4 设置节点

  将RigidBody2D的Gravity Scale属性设置为0,这将防止Mob随重力掉落。

  将RigidBody2D的CollisionObject2D——Collision——Mask的复选框去除。这个选项用于扫描物理碰撞,去除复选框以确保Mob不会互相碰撞。

  为AnimatedSprite添加动画,分别为fly, swim, walk. 将所有动画的帧数设定为3,将检查器的“Playing”改为“启用”。

  将AnimatedSprite的Scale(缩放)设定为0.75,0.75.

  为CollisionShape2D增加CapsuleShape2D(胶囊)形状,将Rotation Degrees(旋转角度)设定为90,缩放以使形状匹配动画。

2. 添加敌人脚本

  2.1 创建脚本

  2.2 为随机敌人做准备

func _ready():
    $AnimatedSprite.playing = true
    var mob_types = $AnimatedSprite.frames.get_animation_names()
    $AnimatedSprite.animation = mob_types[randi() % mob_types.size()]

  从AnimatedSprite的frames中提取动画名。

  使用X%Y取余数(取模)的方式,获取随机的mob种类。

  randi() % n会返回0到n-1之间的一个数。

  mob_types.size() 是上面提取的动画名的总个数。

  2.3 在敌人超出区域时删除自己

  连接 VisibilityNotifier2D 的 screen_exited() 信号。

  使用 queue_free() 释放资源。

3. 添加游戏主场景

  3.1 新建场景,新建 Node 节点,命名为 Main

  注意不要建立 Node2D 节点。

  3.2 实例化玩家

  点击界面左上角的“实例化子场景”,选择Player.tscn。

  3.3 为Main添加子节点

  Timer命名为 MobTimer,Wait Time 设定为 0.5

  Timer命名为 ScoreTimer,Wait Time 设定为 1

  Timer命名为 StartTimer,Wait Time 设定为 2,One Shot启用

  Position2D,命名为 StartPosition,Position 设定为 (240, 450)

4. 生成怪物

  4.1 添加Path2D节点,命名为 MobPath

  使用吸附功能,顺时针沿屏幕区域绘制路径,并在添加4个顶点后点击“闭合曲线”。

  吸附功能在界面顶端,锁定功能左侧。

  如果不沿顺时针绘制,怪物可能向外侧或意外生成。

  4.2 为 MobPath 添加 PathFollow2D 子节点,命名为 MobSpawnLocation

5. 创建 Main 节点脚本

  5.1 使用 export (PackedScene) 选择要实例化的场景。

extends Node

export(PackedScene) var mob_scene
var score

  5.2 创建随机数

func _ready():
    randomize()

  5.3 创建敌人

  点击 Main 节点,在 Script Variables 的 Mob Scene 中,选择之前创建的 Mob.tscn

  5.4 设置玩家信号

  选中Player节点,找到 hit() 节点,双击,创建 game_over() 函数,并创建new_game()函数以初始化游戏

func game_over():
    $ScoreTimer.stop()
    $MobTimer.stop()

func new_game():
    score = 0
    $Player.start($StartPosition.position)
    $StartTimer.start()

  5.5 连接Timer信号

  将三个Timer的timeout()信号连接到 main脚本。

func _on_ScoreTimer_timeout():
    score += 1

func _on_StartTimer_timeout():
    $MobTimer.start()
    $ScoreTimer.start()

  ScoreTimer用于加分。

  StartTimer用于启动 MobTimer和ScoreTimer。

  在MobTimer的信号中,完成敌人的初始化、随机选择初始地点,并让敌人开始移动。

复制代码
func _on_MobTimer_timeout():
    # Choose a random location on Path2D.
    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

    # Set the mob's position to a random location.
mob.position = mob_spawn_location.position # Add some randomness to the direction. direction += rand_range(-PI / 4, PI / 4) mob.rotation = direction # Choose the velocity. var velocity = Vector2(rand_range(150.0, 250.0), 0.0) mob.linear_velocity = velocity.rotated(direction)
复制代码

  randi() 生成均匀的伪随机数

  add_child() 将实例 (instance) 添加进场景

  rotation使用弧度进行计算,+ PI / 2 敌人生成角度指向画面中间

  5.6 在游戏开始时初始化玩家位置和计时器

func _ready():
    randomize()
    new_game()

·  测试完成后移除 new_game()

 

posted @   羅生門  阅读(749)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示