简体   繁体   English

我无法弄清楚如何在Java中以模块化方式创建具有基本统计信息的可玩角色单位

[英]I cant figure out how to create units, playable characters with basic stats, in a modular way in java

I want to preface this with the fact that I am new to java and might be simply missing some basic understanding that could cover all of this so I deeply appreciate your understanding and help. 我想以我刚接触Java的事实作为开头,并且可能只是缺少一些可以涵盖所有这些内容的基本知识,因此,我非常感谢您的理解和帮助。

I'm looking to somehow create a program with java that allows for the creation of a playable character that will face a variety of npcs and eventually work with a party of playable characters and be modified by items from an inventory. 我正在寻找一种以java创建程序的方法,该程序允许创建可玩角色,该角色将面对各种npc,并最终与一组可玩角色一起使用,并可以通过库存中的物品进行修改。 This is the really important part, eventually I want build a game that could have any number of playable characters and any number of npcs all equipped with any number of inventory items so I must have a solution for generating characters, npcs, and inventory items that is modular. 这是非常重要的部分,最终我想构建一个可以具有任意数量的可玩角色和任意数量的NPC的游戏,并且这些游戏都配备了任意数量的库存物品,因此我必须具有一种生成角色,NPC和库存物品的解决方案是模块化的。

Here is what I am trying to specifically work out, I want to create characters with at least the following attributes: 这是我要具体解决的问题,我要创建至少具有以下属性的字符:

Unit Name, Unit Health, Unit Moves (action points), Unit Base Damage 单位名称,单位健康,单位移动(动作点),单位基础伤害

Beyond this I want my units and npcs to be able to be modified by inventory. 除此之外,我希望我的单位和NPC能够通过库存进行修改。 Basically say if one of my characters is holding a weapon I want that units base damage to be modified. 基本上说,如果我的角色之一拿着武器,我希望修改该单位的基础伤害。 You could imagine the same types of changes for armor, special items, and so on. 您可以想象盔甲,特殊物品等的相同类型的更改。 This all seems straight forward but I cant figure out how to reconcile all of these properties together. 这一切看起来都是直截了当的,但是我无法弄清楚如何将所有这些属性协调在一起。

for example, this seems straight forward 例如,这似乎很简单

//ex 1-1)

// from L to R: name, health, moves, damage
Ranger rangerObj0 = new Ranger("Ranger", 100, 4, 10);

// from L to R: name, description, damage modifying amount
Weapon weaponObj0 = new Weapon("Weapon Name", "Weapon description", 10); 

but, how would I "relate" an item with this ranger? 但是,我将如何与该护林员“相关”? How do I semi-permanently, the item could be equipped or unequipped, modify the rangers stats with the value of gear? 我如何半永久性地装备或不装备该物品,用装备的值修改护林员的数据? It seems like I would want to put all of these things in some kind of array so they could all be related and modified by their respective array locations like so: 似乎我想将所有这些内容放入某种数组中,以便它们可以通过各自的数组位置进行关联和修改,如下所示:

Ranger array 游侠数组

//ex 1-2)

[0] rangerObj0
[1] rangerAttack1
[2] rangerAttack2
[3] weapon
[4] armor 
[5] special item

The problem I run into is that I am now dealing with different objects, types of variables, and calling methods that will have to reconcile the total stats a unit might have based on its base stats and innovatory in order to preform an attack for example. 我遇到的问题是,我现在正在处理不同的对象,变量类型和调用方法,这些方法必须根据单位的基本统计信息和创新性来协调一个单元可能具有的总统计信息,以便进行例如一次攻击。

This has been a long question but ultimately i am looking for a solution that would allow me to combine objects together like in ex 1-2 in a way that I could preform a method call like follows 这是一个很长的问题,但是最终我正在寻找一种解决方案,使我可以像在ex 1-2中那样将对象组合在一起,从而可以执行如下的方法调用

//ex 1-3)

/*I would create code for the body of this method based on how I was able 
to relate all of a unit or npcs stats together*/

public attack1(unit attacking, unit getting attacked) {
    take attacking unit's base damage + damage from all equipped items + 
base damage from this attack and -= the unit's health that is getting 
attacked with regard to any armor or defensive stats the attacked unit 
might have;
} 

this showcases the ultimate problem I have. 这展示了我的终极问题。 I must find a way to create a method that takes in all off the parameters that my collection of objects and methods from ex 1-1 might have and use them against the collection of objects that comprises whatever unit I am attacking. 我必须找到一种创建一种方法的方法,该方法可以吸收我的对象和方法1-1中的对象可能具有的所有参数,并将它们用于包含我所攻击的单元的对象的集合中。

Does anyone have any ideas about how I would achieve this kind of relational programming like shown in ex 1-2? 有人对我将如何实现如示例1-2中所示的这种关系编程有任何想法吗?

Your question is manifold, but I try to answer it as concise as possible. 您的问题是多种多样的,但我会尽力回答。 I would suggest to introduce an interface that all Objects implement which gives you the stats, like 我建议介绍一个所有对象都实现的接口,该接口为您提供统计信息,例如

interface HasStats {

    int getForce();

    int getArmor();

    // ... REST
}

Then, your Ranger Class should inherit that. 然后,您的Ranger类应该继承该类。

public class Ranger implements HasStats {

    // Implementation here

}

All other Weaposn, Actions and so on also. 其他所有Weaposn,Action等。

Furthermore, your Ranger gets all possible Slots as Fields like that 此外,您的游侠将所有可能的老虎机都获取为像这样的字段

public class Ranger implements HasStats {

    Weapon currentWeapon;
    Action primaryAction;

    // Implementation here

    public void setWeapon(Weapon weapon) {
        this.currentWeapon = weapon;    
    }

    // more getters here

}

Finally, you can implement the getStats() mehtods in the ranger based on its equippment, like 最后,您可以根据其装备在游侠中实现getStats()方法,例如

public class Ranger implements HasStats {

    // Implementation here

    @Override
    public int getArmor() {
        return baseArmor + weapon.getArmor() * 2; // Just a dummy example
    }

    // Implementation here

}

I hope you can see the pattern and get the idea? 希望您能看到这种模式并有所想法?


Edit: If the interaction between the equipment and the stats is more complex you could use the visitor pattern ( https://en.wikipedia.org/wiki/Visitor_pattern ). 编辑:如果设备和统计信息之间的交互更为复杂,则可以使用访问者模式( https://en.wikipedia.org/wiki/Visitor_pattern )。 This would allow you to pass some stats to the equipment and let it do a calculation and return that back. 这将允许您将一些统计信息传递给设备,并让它进行计算并将其返回。 But, this becomes easily pretty complicated so I'm not going into detail here. 但是,这很容易变得很复杂,因此在此不再赘述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM