从另一个类访问实例化对象-c#

从另一个类访问实例化对象-c#,c#,object,xna-4.0,instantiation,C#,Object,Xna 4.0,Instantiation,我有一个玩家类、NPC类、战地经理类和游戏类 玩家类存储/获取/设置玩家统计信息,如健康、耐力、等级、经验。NPC与之类似,但适用于NPC。游戏class实例化玩家class和NPCclass。游戏玻璃有两种游戏状态,战斗和非战斗 当玩家处于战斗状态时,游戏状态变为战斗状态,当战斗结束时,游戏状态变为非战斗状态 我想做的是让BattleManager类管理NPC和玩家之间的战斗,但是,由于玩家和NPC对象是在游戏类中实例化的,我需要知道如何在不实例化新对象的情况下从BattleManager传递

我有一个玩家
、NPC
、战地经理
和游戏

玩家类存储/获取/设置玩家统计信息,如健康、耐力、等级、经验。NPC与之类似,但适用于NPC。游戏
class
实例化玩家
class
和NPC
class
。游戏玻璃有两种游戏状态,战斗和非战斗

当玩家处于战斗状态时,游戏状态变为战斗状态,当战斗结束时,游戏状态变为非战斗状态

我想做的是让BattleManager
类管理NPC和玩家之间的战斗,但是,由于玩家和NPC对象是在游戏类中实例化的,我需要知道如何在不实例化新对象的情况下从BattleManager传递或访问该对象


有人能提出一个大概的工作流程吗?我知道这是错误的,但有没有办法做到像
Game.Player.Health-=伤害?例如,在player
is
public
int Health get/set
中,当player类在游戏类中实例化时,如何编辑其他类的健康?有没有一种方法可以传递玩家
对象
,或者直接从其他类访问在游戏类中创建的实例化对象?

我同意第一条评论,即值得探索一些设计模式

如果你想要一个快速而肮脏的答案,那么:

修改你的Player和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
    }
}

从我可怜的疲惫的大脑中输入这个,所以请原谅任何错误。希望这有帮助。

我喜欢认为玩家和NPC是场景中的演员。。。所以我用singleton模式创建了一个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
公共接口IActor{
统计数据{get;}
向量2位置{get;}
布尔正在战斗{get;}
bool是活动的{get;}//或任何你需要的东西
}
公共类ActorManager{
公共静态只读实例=新ActorManager();
ActorManager();
列表_actors=新列表();
公共IEnumerable Actors{get{return\u Actors;}
public void Addactor(IActor actor){…}
公共IEnumerable GetActorsNear(IActor演员,浮动半径)
{
return _actors.Where(
次要=>actor!=次要

&&Vector2.距离(actor.Position,secondary.Position)我建议您阅读一些设计模式。它们是针对常见问题(如您的问题)精心设计的解决方案。您应该用播放器应该做的一切(即方法)为播放器建模例如,
TakeDamage
。一些设计模式也涵盖了责任链。
public event BattleStarted ITSON
public delegate void BattleStarted(object send,BattleStartedEventArgs e)
class BattleStartedEventArgs:EventArgs{public Player One{get;set;}public NPC NPC Two[get;set;}}