2012-07-27 91 views
2

我有一個播放器class,NPC class,BattleManager class和遊戲class從另一個類訪問實例化對象-c#

玩家類商店/獲取/設置玩家屬性,如健康,耐力,水平,體驗。 NPC是相似的,但是是NPC。遊戲class實例化玩家class和NPC class。遊戲玻璃有兩個GameStates,戰鬥和非戰鬥。

當玩家在戰鬥中時,GameState進入戰鬥狀態,當戰鬥結束時,它切換到非戰鬥狀態。

我想要做的是讓BattleManager class管理NPC和玩家之間的戰鬥,然而,由於玩家和NPC對象是在Game類中實例化的,所以我需要知道如何傳遞該對象或訪問它從戰鬥管理器沒有實例化一個新的。

任何人都可以建議如何工作的一般流程?我知道這是錯誤的,但有沒有辦法像Game.Player.Health -= damage;這樣做?例如,在播放器class內部是publicint Health get/set,當Player類在Game類中實例化時,如何從其他類中編輯健康狀況?有沒有辦法通過播放器object繞過或僅從其他類訪問在Game類中創建的實例化對象?

+3

我建議你閱讀一些**設計模式**。它們是針對常見問題的精心設計的解決方案,例如您的解決方案。你應該模擬一個玩家應該做的一切(即* methods *),比如'TakeDamage'。責任鏈也包含在一些設計模式中。 – 2012-07-27 01:44:10

+1

'公共事件BattleStarted ITSON'和'public delegate void BattleStarted(object sended,BattleStartedEventArgs e)'''class BattleStartedEventArgs:EventArgs {public Player One {get; set;} public NPC Two [get; set;}}'' – Will 2012-07-27 01:45:55

回答

1

我喜歡以爲玩家和NPC都是演員在寫景...所以我用來做與單模式的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 
2

我同意第一個評論,它將是值得探索一些設計模式。

如果你想快速和骯髒的答案,那麼:

修改您的播放器和NPC類採取遊戲實例在構造函數(另一種模式是將遊戲的對象是全球性的單...另一種模式探索)。

使用這裏的簡單的方法:

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 
    } 
} 

打字這一點,我可憐的煩腦,這樣原諒任何錯誤。希望這可以幫助。

相關問題