从零开始制作类银河恶魔城的2d游戏之P65

因为之前没有想着记录,所以从现在开始记录一下和一些自己的改动。

 Clone Creating Ability(克隆)

1,创建一个名为clone的预制体

创建一个名为clone_AC的Animator Controller用来存放动画,可将player的Idle动画设置为初始动画。

记得需要将精灵排序图层改为Player(-1)

2,创建Clone_Skill_Controller(这是绑定在clone预制件上的)

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Clone_Skill_Controller : MonoBehaviour
{
    //获取精灵
    private SpriteRenderer sr;
    //失去颜色速度
    [SerializeField] private float clorLoosingSpeed;

    private float cloneTimer;
    private void Awake()
    {
        sr = GetComponent<SpriteRenderer>();
    }
    private void Update()
    {
        cloneTimer -= Time.deltaTime;
        if(cloneTimer<0)
        {
            sr.color = new Color(1, 1, 1, sr.color.a - (Time.deltaTime * clorLoosingSpeed));
        }
    }
    //设置克隆体的位置
    public void SetupClone(Transform _newTransform,float _cloneDuration)
    {
        transform.position = _newTransform.position;
        cloneTimer = _cloneDuration;
    }

 
}

 3,创建Clone_Skill (实现克隆体的生成)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//分身技能
public class Clone_Skill : Skill
{
    // 用于存储分身的预制体
    [SerializeField] private GameObject clonePrefab;
    // 克隆消失时间
    [SerializeField] private float cloneDuration;


    // 创建分身的方法
    // 参数 _clonePosition 表示分身的位置
    public void CreteClone(Transform _clonePosition)
    {
             实例化分身预制体,生成一个新的分身对象
            GameObject newClone = Instantiate(clonePrefab);
        // 获取新分身对象的 Clone_Skill_Controller 组件,并调用 SetupClone 方法进行分身的设置
        //传入分身位置和克隆消失时间作为参数
        newClone.GetComponent<Clone_Skill_Controller>().SetupClone(_clonePosition, cloneDuration);
    }
}

4,记得在SkillManager中导入public Clone_Skill clone { get; private set; }和 clone =GetComponent<Clone_Skill>();

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SkillManager : MonoBehaviour
{
    public static SkillManager instance;
    public Dash_Skill dash { get; private set; }
    public Clone_Skill clone { get; private set; }
    public Clone1_Skill clone1 { get; private set; }
    public void Awake()
    {
        if (instance != null)
            Destroy(instance.gameObject);
        else
            instance = this;
    }
    public void Start()
    {
        dash = GetComponent < Dash_Skill> ();
        clone =GetComponent<Clone_Skill>();
        clone1 = GetComponent<Clone1_Skill>();
    }
}

注意其中的clone1是我自己就老师的视频新增的效果

5,记得在playe中导入  
    public SkillManager skill { get; private set; }

    //声明技能组件
    public SkillManager skill { get; private set; }
     
    protected override void Start()
     {
     base.Start();
     skill = SkillManager.instance;
    //进入待机动画
     stateMachine.Initialize(idleState);
     }

6,最后记得在PlayerDashState中实现方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//冲刺状态
public class PlayerDashState : PlayerState
{
    public PlayerDashState(Player _player, PlayerStateMachine _stateMachine, string _animBooIName) : base(_player, _stateMachine, _animBooIName)
    {
    }

    public override void Enter()
    {
        base.Enter();
        //使用克隆技能1
        //player.skill.clone1.CreteClone(player.transform,player.dashDir);
        //使用克隆技能
        player.skill.clone.CreteClone(player.transform);
        //重置冲刺持续时间
        stateTimer = player.dashDuration;
        
    }

    public override void Exit()
    {
        base.Exit();
        player.SetVelocity(0, rb.velocity.y);
    }

    public override void Update()
    {
        base.Update();
        if(!player.IsGroundDetected() && player.IsWallDatected())
        {
            stateMachine.ChangeState(player.wallSlide);
        }
        player.SetVelocity(player.dashDir * player.dashSpeed, rb.velocity.y);
        if (stateTimer<0)
        {
            stateMachine.ChangeState(player.idleState);
        }
    }
}

7,最后是我的clone1我给他是新增了一个单独的Clone_Dash动画就一帧且给他实现了可以多个分身切跟随他身后生成

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


//分身技能
public class Clone1_Skill : Skill
{
    // 用于存储分身的预制体
    [SerializeField] private GameObject clonePrefab;
    // 克隆消失时间
    [SerializeField] private float cloneDuration;
    // 分身的数量
    [SerializeField] private int cloneCount;
    // 分身之间的间距
    [SerializeField] private float cloneSpacing;
    // 分身产生的间隔时间
    [SerializeField] private float cloneSpawnInterval;
    // 存储冲刺方向
    private float dashDir;

    // 创建分身的方法
    // 参数 transform 表示玩家的 Transform 组件
    public void CreteClone(Transform playerTransform, float dashDir)
    {
        // 存储 dashDir
        this.dashDir = dashDir;
        Vector2 _dashDirection;
        if (dashDir == 1)
        {
            _dashDirection = playerTransform.right;
        }
        else if (dashDir == -1)
        {
            _dashDirection = -playerTransform.right;
        }
        else
        {
            // 处理异常情况,例如 dashDir 不为 1 或 -1 时,可以默认向右冲刺
            _dashDirection = playerTransform.right;
  
        }

        StartCoroutine(CreateClonesWithInterval(playerTransform, _dashDirection));
    }

    private IEnumerator CreateClonesWithInterval(Transform playerTransform, Vector2 _dashDirection)
    {
        for (int i = 0; i < cloneCount; i++)
        {
            Vector2 clonePosition = Vector2.zero;
            if (this.dashDir == 1)
            {
                // 计算当前分身的位置,根据玩家位置、冲刺方向和分身间距
                clonePosition = (Vector2)playerTransform.position + (i * cloneSpacing * _dashDirection);
            }
            else if (this.dashDir == -1)
            {
                // 计算当前分身的位置,根据玩家位置、冲刺方向和分身间距
                clonePosition = (Vector2)playerTransform.position - (i * cloneSpacing * _dashDirection);
            }

            // 实例化分身预制体,生成一个新的分身对象
            GameObject newClone = Instantiate(clonePrefab, clonePosition, Quaternion.identity);
         
            // 获取新分身对象的 Clone_Skill_Controller 组件,并调用 SetupClone 方法进行分身的设置
            // 传入分身位置和克隆消失时间作为参数
            // 将 Vector2 类型的 clonePosition 转换为 Transform 类型
            Transform cloneTransform = new GameObject().transform;
            cloneTransform.position = clonePosition;
            newClone.GetComponent<Clone_Skill_Controller>().SetupClone(cloneTransform, cloneDuration);

            // 根据冲刺方向翻转分身
            if (this.dashDir == -1)
            {
                newClone.transform.localScale = new Vector3(-newClone.transform.localScale.x, newClone.transform.localScale.y, newClone.transform.localScale.z);
            }

            // 等待一段时间再创建下一个分身
            yield return new WaitForSeconds(cloneSpawnInterval);
        }
    
    }
}

### 使用C++开发银河恶魔城游戏的方法和资源 #### 选择合适的游戏引擎 为了简化开发过程并提高效率,推荐使用成熟的游戏引擎来构建银河恶魔城游戏。一些流行的选项包括Unreal Engine[^1] 和 Godot (尽管Godot主要支持GDScript, C#, 和Visual Scripting)[^2] 。然而,如果坚持使用纯C++进行开发,则可以选择像SFML 或 Allegro这样的轻量级跨平台图形库。 #### 学习基础概念和技术栈 掌握面向对象编程(OOP),这是C++的核心特性之一;理解内存管理机制以及指针操作等高级主题也是必不可少的。此外还需要学习有关窗口创建、事件处理循环、图像渲染等方面的知识。 #### 获取必要的工具链和支持材料 安装编译器(如GCC/MinGW)、IDE(集成开发环境)(例如 Visual Studio Code 或 CLion), 并配置好项目结构以便于管理和维护源文件。同时利用在线教程、官方文档和其他社区贡献者分享的经验教训作为指导资料。 ```cpp // 初始化游戏窗口示例代码片段 #include <SFML/Graphics.hpp> int main() { sf::RenderWindow window(sf::VideoMode(800, 600), "Metroidvania Game"); while (window.isOpen()) { // 处理输入... // 更新逻辑... // 渲染画面... window.clear(); // 绘制场景中的物体到屏幕上 window.display(); } return 0; } ``` #### 寻找灵感与参考案例研究 观察其他成功的同作品可以帮助形成自己的设计理念。分析《空洞骑士》或《奥日与黑暗森林》这经典之作是如何设计关卡布局、角色动作模式及其背后的技术实现方式能够提供宝贵启示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值