游戏组件的{Javascript类结构

时间:2015-09-03 17:36:50

标签: javascript oop design-patterns

我今天在这里对如何处理我计划编码的特定课程场景有更多的设计/概念疑问。

这里的交易,我要实现一个类怪物,它将保存与我游戏中存在的各种怪物相关的大部分数据,但它不会保存有效数据,事实上,我想要的这里存储的是可能值的范围,所以当我实际创建一个怪物对象时,它会在遇到的怪物的种族/种类的范围内具有它的属性。

例示: 我会有不同类型的怪物,鼠,蜘蛛,蛇,狼。每个物种都会在一定范围内滚动属性:

Rat: Attack [5,9], Defense [3,5]
Spider: Attack [6,11], Defense [4,5]
...

我想在我的js文件中将这些数据放在JSON对象上,然后我将从中加载数据并准备在遇到这些值时创建怪物对象。

令我不安的是我应该为此做的设计。我想说一个很好的方法来引用怪物的类型就像Monster.Rat,Monster.Spider,并在遭遇发生时调用它们的构造函数:Monster.Spider() - >会给我一个蜘蛛范围内随机属性的怪物对象。

还有一些时间点我想创建特定区域来查找特定类型的怪物,但我没有清楚地看到如何以简单的方式做到这一点,而不会使代码难以阅读。

几个月前,我使用Swift做了类似的事情,我所采用的方法是在怪物上为我想要的每种类型的怪物设置一个类方法,但这很难维护,因为如果我想改变它方式怪物被创建我将不得不改变所有方法,也为每种新类型的怪物添加一种新方法。

您认为处理这类游戏设计的最佳方式是什么?我知道我不想为我拥有的每一种新型怪物创建新的Monster扩展类,因为这对我来说是非常错误和糟糕的设计。

感谢您的时间,我将使用我发现的任何有趣信息更新此帖子。

1 个答案:

答案 0 :(得分:3)

编辑:我也为您做了一些改进 - 只是为了突出可以做的事情:

var Monster = {
    monsters: {
        rat: {
            attack: [5, 9],
            defense: [3, 5],
            health: [5, 10],
            title: "Rat"
        },

        spider: {
            attack: [6, 11],
            defense: [4, 5],
            health: [4, 6],
            title: "Spider"
        }
    },

    create: function(type) {
        // choose type at random if not specified
        if(typeof type !== "string") {
            var keys = [ ];
            for(var key in this.monsters) {
                if(this.monsters.hasOwnProperty(key)) {
                    keys.push(key);
                }
            }

            type = keys[Math.floor(Math.random() * keys.length)];
        }

        // check if given monster type exists
        if(typeof this.monsters[type] === "undefined") {
            throw new TypeError('invalid monster type "' + type + '"');
        }

        var monster = { };

        /*
         * This allows you to add new attributes and not have to change any
         * of this code, except the attributes object for each monster.
         */
        for(var attribute in this.monsters[type]) {
            if(this.monsters[type].hasOwnProperty(attribute)) {
                var a = this.monsters[type][attribute];

                if(typeof a == "object") {
                    a = Math.floor(Math.random() * (a[1] - a[0] + 1) + a[0]);
                }

                monster[attribute] = a;
            }
        }

        return monster;
    }
};

console.log(Monster.create('rat'));
console.log(Monster.create('spider'));
console.log(Monster.create());

这就是我要做的,这很简单,并且可以在以后轻松添加怪物和属性:

var Monster = {
    monsters: {
        rat: {
            attack: [5, 9],
            defense: [3, 5],
        },

        spider: {
            attack: [6, 11],
            defense: [4, 5]
        }
    },

    create: function(type) {
        // check if given monster type exists
        if(typeof this.monsters[type] === "undefined") {
            throw new TypeError('invalid monster type "' + type + '"');
        }

        var monster = { };

        /*
         * This allows you to add new attributes and not have to change any
         * of this code, except the attributes object for each monster.
         */
        for(var attribute in this.monsters[type]) {
            if(this.monsters[type].hasOwnProperty(attribute)) {
                var a = this.monsters[type][attribute];
                monster[attribute] = Math.floor(Math.random() * (a[1] - a[0] + 1) + a[0]);
            }
        }

        return monster;
    }
};

console.log(Monster.create('rat'));
console.log(Monster.create('spider'));