2012-07-27 92 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 
    } 
} 

打字这一点,我可怜的烦脑,这样原谅任何错误。希望这可以帮助。

相关问题