问题描述
我有以下GameObject 界面
:
I have the following GameObject interface
:
public interface GameObject {
void viewDetails();
}
字符接口
:
interface Character{
void pickUp(Weapon weapon);
void use(Weapon weapon);
}
和 abstract
武器 class
:
public abstract class Weapon implements GameObject {
//left out constructor to focus on methods
@Override
public abstract void viewDetails();
public abstract void attack(Enemy enemyObj);
//Could be bullets, could be a mystical item.
public abstract void replenish(ReplenishItem rpItem);
}
这个问题是,游戏对象有时候可以用于不同的方式。
例如,游戏武器的主要用途是攻击目标,但是如果我想重新加载怎么办?如何让我的角色界面重新加载或注意重载是一个选项?
The problem with this is, a GameObject sometimes can be used in different ways.For example, the primary use of a game weapon is to attack a target, but what if I wanted to reload? How do I let my character interface reload or beware that reload is an option?
推荐答案
我会使用以下方法。
我会申报接口:
interface MeleeWeapon {
void hit();
void cut();
}
interface FirearmWeapon {
void fire();
void reload();
}
interface MagicWeapon {
void throw();
void apply();
void recharge();
}
然后实现类,如下所示:
Then implement classes, like these:
class Knife implements MeleeWeapon {
public void hit() {
}
public void cut() {
}
}
class Dagger implements MeleeWeapon {
public void hit() {
}
public void cut() {
}
}
class GarandRifle implements FirearmWeapon {
public void fire() {
}
public void reload() {
}
}
class Fireball implements MagicWeapon {
public void throw() {
}
public void apply() {
}
public void recharge() {
}
}
然后,我会声明这些接口:
Then, I would declare these interfaces:
interface MeleeWeaponUser {
void use(MeleeWeapon weapon);
}
interface FirearmWeaponUser {
void use(FirearmWeapon weapon);
}
interface MagicWeaponUser {
void use(MagicWeapon weapon);
}
而且,我会声明字符类:
And, I would declare character classes:
class Peasant implements MeleeWeaponUser {
public void use(MeleeWeapon weapon) {
}
}
class Marine implements MeleeWeaponUser, FirearmWeaponUser {
public void use(FirearmWeapon weapon) {
}
public void use(MeleeWeapon weapon) {
}
}
class Sorcerer implements MeleeWeaponUser, MagicWeaponUser {
public void use(MeleeWeapon weapon) {
}
public void use(MagicWeapon weapon) {
}
}
这种方法让我们可以在以后不费力的情况下添加新的武器和角色。
This approach let us add new weapons and characters without sufficient effort later.
在你的中使用()
方法你可以调用 reload()
如果武器分配器中没有更多的弹药。
In your use()
method you can call reload()
if there is no more ammo in the weapon dispenser.
但是如果你的游戏角色从外部收到信号,例如,rel或者枪,即使有足够的弹药开火,然后实施 Event-> Listener
方法。
But if your game character receives signal from outside, for example, reload the gun, even there is enough ammo to fire, then have an Event->Listener
approach implemented.
创建一个 WeaponEvent
类,将此类扩展为 FirearmWeaponEvent
, MeleeWeaponEvent
等。
Create a WeaponEvent
class, extend this class to have FirearmWeaponEvent
, MeleeWeaponEvent
etc.
制作游戏角色类的监听器,然后在你的游戏角色类中有一个方法 processEvent(WeaponEvent)事件)
,并据此采取行动。
Make your game character class(es) as a listener to WeaponEvent
events, then in your game character class have a method processEvent(WeaponEvent event)
, and act accordingly to the event you have received.
这篇关于游戏对象和代码结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!