Javascript class 游戏组件结构
Javascript class structure for game component
我今天来到这里,对如何处理我计划编码的某个 class 场景有更多 design/conceptual 疑问。
这么说吧,我要实现一个class怪物,它会保存我游戏中存在的各种怪物的大部分数据,但不会保存有效数据,实际上,什么我想在这里存储可能值的范围,所以当我实际创建一个怪物对象时,它的属性将在遇到的怪物的 race/kind 范围内。
举例:
我会有不同类型的怪物,老鼠,蜘蛛,蛇,狼。每个物种都会在特定范围内滚动属性:
Rat: Attack [5,9], Defense [3,5]
Spider: Attack [6,11], Defense [4,5]
...
我想将这些数据放在我的 js 文件中的 JSON 对象上,然后我将从中加载它的数据并准备在遇到这些值时创建怪物对象。
困扰我的是我应该为此做的设计。我会说引用怪物类型的好方法是 Monster.Rat、Monster.Spider,并在遇到时调用它们的构造函数:Monster.Spider() ->会给我一个蜘蛛范围内随机属性的怪物对象。
还有一些时候我想创建特定的区域来寻找特定类型的怪物,但我看不清楚如何以简单的方式做到这一点,而不会使代码难以阅读。
几个月前我做了类似的事情,使用 Swift,我所做的方法是对我想要的每种类型的怪物都有一个 class 方法,但这很难维护,因为如果我想改变怪物的创建方式,我将不得不改变所有的方法,还要为每一种想要的新类型的怪物添加一个新方法。
您认为处理此类游戏设计的最佳方式是什么?我知道我不想为我拥有的每种新型怪物创建新的 classes 扩展怪物,因为这对我来说感觉非常错误和糟糕的设计。
感谢您抽出宝贵时间,我会post 更新我发现的任何有趣信息。
编辑:我也为您对这个脚本做了一些改进——只是为了强调可以做什么:
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'));
我今天来到这里,对如何处理我计划编码的某个 class 场景有更多 design/conceptual 疑问。
这么说吧,我要实现一个class怪物,它会保存我游戏中存在的各种怪物的大部分数据,但不会保存有效数据,实际上,什么我想在这里存储可能值的范围,所以当我实际创建一个怪物对象时,它的属性将在遇到的怪物的 race/kind 范围内。
举例: 我会有不同类型的怪物,老鼠,蜘蛛,蛇,狼。每个物种都会在特定范围内滚动属性:
Rat: Attack [5,9], Defense [3,5]
Spider: Attack [6,11], Defense [4,5]
...
我想将这些数据放在我的 js 文件中的 JSON 对象上,然后我将从中加载它的数据并准备在遇到这些值时创建怪物对象。
困扰我的是我应该为此做的设计。我会说引用怪物类型的好方法是 Monster.Rat、Monster.Spider,并在遇到时调用它们的构造函数:Monster.Spider() ->会给我一个蜘蛛范围内随机属性的怪物对象。
还有一些时候我想创建特定的区域来寻找特定类型的怪物,但我看不清楚如何以简单的方式做到这一点,而不会使代码难以阅读。
几个月前我做了类似的事情,使用 Swift,我所做的方法是对我想要的每种类型的怪物都有一个 class 方法,但这很难维护,因为如果我想改变怪物的创建方式,我将不得不改变所有的方法,还要为每一种想要的新类型的怪物添加一个新方法。
您认为处理此类游戏设计的最佳方式是什么?我知道我不想为我拥有的每种新型怪物创建新的 classes 扩展怪物,因为这对我来说感觉非常错误和糟糕的设计。
感谢您抽出宝贵时间,我会post 更新我发现的任何有趣信息。
编辑:我也为您对这个脚本做了一些改进——只是为了强调可以做什么:
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'));