从另一个类访问实例化的对象-C# [英] Accessing instantiated object from another class - c#

查看:385
本文介绍了从另一个类访问实例化的对象-C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Player class, NPC class, BattleManager class, and Game class.

玩家类存储/获取/设置玩家统计信息,例如健康状况,耐力,等级,经验. NPC与NPC类似,但适用于NPC.游戏class实例化播放器class和NPC class.游戏玻璃具有两个GameState,即战斗状态和非战斗状态.

Player class stores/gets/sets player stats such as health, stamina, level, experience. NPC is similar, but is for NPCs. Game class instantiates Player class and NPC class. Game glass has two GameStates, battle and non-battle.

玩家处于战斗状态时,GameState进入战斗状态,战斗结束后,游戏状态切换为非战斗状态.

When the player is in battle, GameState goes to battle, when the battle finishes, it switches to non-battle.

我想做的是让BattleManager class管理NPC和玩家之间的战斗,但是,由于玩家和NPC对象是在Game类中实例化的,因此我需要知道如何传递该对象或进行访问从BattleManager中获取它,而无需实例化新的.

What I'm trying to do is have the BattleManager class manage battles between the NPC and player, however, since the player and NPC object's are instantiated in Game class, I need to know how to pass that object or access it from BattleManager without instantiating a new one.

任何人都可以建议一个大概的工作流程吗?我知道这是错误的,但是有没有办法做Game.Player.Health -= damage;之类的事情?例如,在播放器class中是public int Health get/set,当在游戏类中实例化播放器类时,如何编辑其他类的运行状况?有没有办法绕过Player object或只是从其他类访问Game类中创建的实例化对象?

Can anyone suggest a general flow of how that might work? I know this is wrong, but is there a way to do something like Game.Player.Health -= damage;? For example, within the player class is public int Health get/set, how can I edit health from other classes when the Player class is instantiated in the Game class? Is there a way to pass the Player object around or just access the instantiated object created in the Game class from other classes?

推荐答案

我喜欢认为玩家和npc是场景中的演员...所以我以前用单例模式制作了一个ActorManager类...

I liked to think that players and npcs are actors in a scenary... so I used to make a ActorManager class with the singleton pattern...

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

这篇关于从另一个类访问实例化的对象-C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆