AI也能写游戏啦,用ChatGPT生成cocos creator代码,编写回合制文字rpg游戏(五)

135 阅读2分钟

上一篇

AI也能写游戏啦,用ChatGPT生成cocos creator代码,编写回合制文字rpg游戏(四) - 掘金 (juejin.cn) 上一篇里,我让AI帮我生成打怪获取道具并加入背包,这一节,想让他生成使用道具的代码

提问

image.png

image.png

生成的代码

InventoryItem.ts
@property(cc.Node)
highlightSprite: cc.Node = null;

// 点击道具时触发的回调函数
onItemClick() {
// 清除之前选中的道具高亮状态
    this.node.parent.getComponent('InventoryUI').clearSelectedItemHighlight();


    // 高亮选中的道具
    highlightSelectedItem() {
        // 高亮选中的道具的处理逻辑
        // ...
        this.highlightSprite.active= true;
    }

    clearHighlight(){
        this.highlightSprite.active= false;
    }

    isHighlighted(){
        return this.highlightSprite.active;
    }


InventoryUI.ts
onLoad(): void {
    const backpackItems = this.itemListNode.children;
    for (let i = 0; i < backpackItems.length; i++) {
        backpackItems[i].on('click',  this.onBackpackItemClick, this);
    }
}





// 背包道具列表项的点击事件处理函数
onBackpackItemClick(event: cc.Event) {
    // 清除之前选中的道具高亮状态
    this.clearSelectedItemHighlight();
    cc.log("click")



    // 获取点击的道具节点
    const itemNode = event.target;
    // 设置点击的道具节点为选中状态
    itemNode.getComponent(InventoryItem).highlightSelectedItem();
}

// 清除选中的道具高亮状态
clearSelectedItemHighlight() {
    const backpackItems = this.itemListNode.children;
    for (let i = 0; i < backpackItems.length; i++) {
        backpackItems[i].getComponent(InventoryItem).clearHighlight();
    }
}

// 获取当前选中的道具ID
getSelectedItem(): number | null {
    const backpackItems = this.itemListNode.children;
    for (let i = 0; i < backpackItems.length; i++) {
        const itemNode = backpackItems[i];
        if (itemNode.getComponent(InventoryItem).isHighlighted()) {
            return itemNode.getComponent(InventoryItem).itemId;
        }
    }
    return null;
}


// 刷新物品列表
private refreshItemList(): void {
    const backpack = InventorySystem.getInstance();
    const items = backpack.getItems();
    this.itemListNode.removeAllChildren();
    items.forEach(item => {
        const itemNode = cc.instantiate(this.ItemPrefab);
        cc.log("addLck")
        itemNode.on(cc.Node.EventType.TOUCH_END, this.onBackpackItemClick, this);

        const itemComponent = itemNode.getComponent(InventoryItem);
        itemComponent.setItemInfo(item.id,item.name, 1);
        this.itemListNode.addChild(itemNode);
    });
}

// 使用按钮的点击事件处理函数
onUseButtonClick() {
    // 获取当前选中的道具ID
    const selectedItem = this.getSelectedItem();
    console.log("道具id:"+selectedItem)
    // 检查是否有选中的道具
    if (selectedItem !== null) {
        // 使用选中的道具
        this.useItem(selectedItem);
    } else {
        // 没有选中道具的处理逻辑
        // ...
    }
}

// 使用道具的函数
useItem(itemId: number) {
// 根据道具ID执行相应的使用逻辑
if (itemId === 1) {
    // 血瓶的使用逻辑
    //把玩家的血量+上血瓶的数值
    // ...
} else if (itemId === 2) {
    // 其他道具的使用逻辑

} else {
        // 未知道具的处理逻辑

    }
}
效果

6.gif

我想使用血瓶时增加玩家的血量

image.png

代码

class DataManager {
    private static instance: DataManager = null;

    private playerHealth: number = 100; // 玩家血量
    private maxPlayerHealth: number = 100; // 玩家最大血量
    private playerAttack: number = 10; // 玩家攻击

    private constructor() {
        // 私有构造函数,防止外部实例化
    }

    // 获取 DataManager 单例
    public static getInstance(): DataManager {
        if (this.instance === null) {
            this.instance = new DataManager();
        }
        return this.instance;
    }

    // 设置玩家血量
    public setPlayerHealth(health: number): void {
        this.playerHealth = Math.min(health, this.maxPlayerHealth); // 血量上限检查
    }

    // 获取玩家血量
    public getPlayerHealth(): number {
        return this.playerHealth;
    }

    // 设置玩家最大血量
    public setMaxPlayerHealth(maxHealth: number): void {
        this.maxPlayerHealth = maxHealth;
    }

    // 设置玩家攻击
    public setPlayerAttack(attack: number): void {
        this.playerAttack = attack;
    }

    // 获取玩家攻击
    public getPlayerAttack(): number {
        return this.playerAttack;
    }

    // 其他数据管理方法...

}

image.png

//把这段代码加在前面的使用道具里,使用血瓶开发成功
// 获取当前血量
let currentHealth = DataManager.getInstance().getPlayerHealth();

// 更新血量,加上雪饼的加血量(10),但不能超过玩家的最大血量
let newHealth = Math.min(currentHealth + 10, DataManager.getInstance().getMaxPlayerHealth());

// 设置玩家血量
DataManager.getInstance().setPlayerHealth(newHealth);

好了,本节结束。 下节,我想让AI帮我生成技能系统。