【问题标题】:Accessing instantiated object from another class - c#从另一个类访问实例化对象 - c#
【发布时间】:2012-07-30 23:41:33
【问题描述】:

我有一个玩家class、NPC class、BattleManager class 和游戏class

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

当玩家处于战斗状态时,GameState进入战斗状态,当战斗结束时,它切换到非战斗状态。

我想做的是让 BattleManager class 管理 NPC 和玩家之间的战斗,但是,由于玩家和 NPC 对象是在 Game 类中实例化的,我需要知道如何传递该对象或访问它来自 BattleManager 而无需实例化一个新的。

任何人都可以建议它如何工作的一般流程吗?我知道这是错误的,但有没有办法做类似Game.Player.Health -= damage; 的事情?例如,在玩家classpublic int Health get/set 内,当在 Game 类中实例化 Player 类时,如何从其他类编辑健康?有没有办法传递 Player object 或者只是从其他类访问在 Game 类中创建的实例化对象?

【问题讨论】:

  • 我建议您阅读一些设计模式。它们是针对常见问题(例如您的问题)的精心设计的解决方案。您应该使用玩家应该做的所有事情(即 methods )为玩家建模,例如TakeDamage。责任链也包含在一些设计模式中。
  • public event BattleStarted ITSONpublic delegate void BattleStarted(object sended, BattleStartedEventArgs e)class BattleStartedEventArgs : EventArgs { public Player One {get;set;} public NPC Two [get;set;} }

标签: c# object xna-4.0 instantiation


【解决方案1】:

我同意第一个评论,即探索一些设计模式是值得的。

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

修改您的 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
    }
}

然后,在你的 Player 和 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
    }
}

这是我可怜疲倦的大脑输入的,所以请原谅任何错误。希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我喜欢认为玩家和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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      • 1970-01-01
      • 2018-12-13
      • 2012-12-13
      • 2017-04-30
      • 1970-01-01
      • 2016-01-15
      相关资源
      最近更新 更多