2

プレイヤーclass、NPC class、バトルマネージャーclass、ゲームがありclassます。

プレイヤークラスは、ヘルス、スタミナ、レベル、経験などのプレイヤー統計を保存/取得/設定します。NPCも同様ですが、NPC用です。ゲームclassはPlayerclassとNPCをインスタンス化しますclass。ゲームグラスには、戦闘と非戦闘の2つのGameStateがあります。

プレイヤーが戦闘中の場合、GameStateは戦闘に移行し、戦闘が終了すると、非戦闘に切り替わります。

私がやろうとしているのは、BattleManagerclassにNPCとプレイヤー間の戦闘を管理させることですが、プレイヤーとNPCオブジェクトはGameクラスでインスタンス化されるため、そのオブジェクトを渡す方法、またはインスタンス化せずにBattleManagerからアクセスする方法を知る必要があります。新しいもの。

誰かがそれがどのように機能するかについての一般的な流れを提案できますか?私はこれが間違っていることを知っていますが、次のようなことをする方法はありGame.Player.Health -= damage;ますか?たとえば、プレーヤー内で、プレーヤークラスがゲームクラスでインスタンス化されている場合、他のクラスのヘルスを編集するにclasspublic int Health get/setどうすればよいですか?プレーヤーを渡す方法objectや、ゲームクラスで作成されたインスタンス化されたオブジェクトに他のクラスからアクセスする方法はありますか?

4

2 に答える 2

2

いくつかのデザインパターンを調査する価値があるという最初のコメントに同意します。

迅速で汚い答えが必要な場合は、次のようにします。

PlayerクラスとNPCクラスを変更して、コンストラクターでGameインスタンスを取り込みます(別のパターンは、Gameオブジェクトをグローバルシングルトンにすることです...探索する別のパターンです)。

ここでより単純なアプローチを使用する:

public class Game
{
    public Player PlayerProperty {get; set;}
    public NPC NPCProperty {get; set;}

    public foo() //some method to instantiate Player and NPC
    {
       PlayerProperty = new Player(this); //hand in the current game instance
       NPCProperty = new NPC(this);  //hand in the current game instance
    }
}

次に、プレイヤーとNPCクラスで...

//Only example for Player class here... NPC would be exact same implementation
public class Player
{

    public Game CurrentGame {get; set;}

    public Player(Game gameInstance)
    {
        CurrentGame = gameInstance;
    }

    //then anywhere else in your Player and NPC classes...
    public bar() //some method in your Player and NPC classes...
    {
        var pointerToNPCFromGame = this.CurrentGame.NPCProperty;
        //here you can access the game and NPC from the Player class
        //would be the exact same for NPC to access Player class
    }
}

私の貧しい疲れた脳からこれをタイプするので、どんな誤りも許してください。お役に立てれば。

于 2012-07-27T03:07:31.617 に答える
1

私はplayersとnpcsが風景の中の俳優だと思うのが好きでした...それで私はシングルトンパターンでActorManagerクラスを作成していました...

public interface IActor {
   Stats Stats {get;}
   Vector2 Position {get;}
   bool IsFighting {get;}
   bool IsActive {get;}  // Or whatever you need
}

public class ActorManager {

     public static readonly Instance = new ActorManager();

     ActorManager();

     List<IActor> _actors = new List<IActor>();

     public IEnumerable<IActor> Actors {get{ return _actors;}}

     public void Addactor(IActor actor) { ... }

     public IEnumerable<IActor> GetActorsNear(IActor actor, float Radius)
     {
         return _actors.Where( 
               secondary => actor != secondary 
               && Vector2.Distance(actor.Position, secondary.Position)<Radius);
     }

     // or whatever you want to do with actors

}

public abstract class Actor : IActor
{
   public Stats Stats {get; protected set;}
   public Vector2 Position {get;protected set;}
   public bool IsFighting {get;protected set;}
   public bool IsActive {get;protected set;} 

       public Actor() {
           ActorManager.Instance.Add(this);
       }

   public abstract void Controller();
}

public class Player : Actor { }  // Implements an input controller
public class Npc : Actor { } // Implements a cpu IA controller
于 2012-07-30T15:41:33.153 に答える