You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Character.cs 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. using Preparation.GameData;
  2. using Preparation.Interface;
  3. using Preparation.Utility;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Threading;
  7. namespace GameClass.GameObj
  8. {
  9. public partial class Character : GameObj, ICharacter // 负责人LHR摆烂终了
  10. {
  11. private readonly object beAttackedLock = new();
  12. #region 角色的基本属性及方法,包括与道具的交互方法
  13. /// <summary>
  14. /// 装弹冷却
  15. /// </summary>
  16. protected int cd;
  17. public int CD
  18. {
  19. get => cd;
  20. private
  21. set
  22. {
  23. lock (gameObjLock)
  24. {
  25. cd = value;
  26. Debugger.Output(this, string.Format("'s CD has been set to: {0}.", value));
  27. }
  28. }
  29. }
  30. public int OrgCD { get; protected set; }
  31. protected int maxBulletNum;
  32. public int MaxBulletNum => maxBulletNum; // 人物最大子弹数
  33. protected int bulletNum;
  34. public int BulletNum => bulletNum; // 目前持有的子弹数
  35. public int MaxHp { get; protected set; } // 最大血量
  36. protected int hp;
  37. public int HP
  38. {
  39. get => hp;
  40. set
  41. {
  42. lock (gameObjLock)
  43. hp = value <= MaxHp ? value : MaxHp;
  44. }
  45. }
  46. private int deathCount = 0;
  47. public int DeathCount => deathCount; // 玩家的死亡次数
  48. private int score = 0;
  49. public int Score
  50. {
  51. get => score;
  52. }
  53. public double AttackRange => BulletFactory.BulletAttackRange(this.BulletOfPlayer);
  54. private double vampire = 0; // 回血率:0-1之间
  55. public double Vampire
  56. {
  57. get => vampire;
  58. set
  59. {
  60. if (value > 1)
  61. lock (gameObjLock)
  62. vampire = 1;
  63. else if (value < 0)
  64. lock (gameObjLock)
  65. vampire = 0;
  66. else
  67. lock (gameObjLock)
  68. vampire = value;
  69. }
  70. }
  71. private double oriVampire = 0;
  72. public double OriVampire
  73. {
  74. get => oriVampire;
  75. set
  76. {
  77. if (value > 1)
  78. lock (gameObjLock)
  79. vampire = 1;
  80. else if (value < 0)
  81. lock (gameObjLock)
  82. vampire = 0;
  83. else
  84. lock (gameObjLock)
  85. vampire = value;
  86. }
  87. }
  88. public readonly BulletType OriBulletOfPlayer;
  89. private BulletType bulletOfPlayer;
  90. public BulletType BulletOfPlayer
  91. {
  92. get => bulletOfPlayer;
  93. set
  94. {
  95. lock (gameObjLock)
  96. bulletOfPlayer = value;
  97. }
  98. }
  99. private Prop? propInventory;
  100. public Prop? PropInventory // 持有的道具
  101. {
  102. get => propInventory;
  103. set
  104. {
  105. lock (gameObjLock)
  106. {
  107. propInventory = value;
  108. Debugger.Output(this, " prop becomes " + (PropInventory == null ? "null" : PropInventory.ToString()));
  109. }
  110. }
  111. }
  112. /// <summary>
  113. /// 使用物品栏中的道具
  114. /// </summary>
  115. /// <returns>被使用的道具</returns>
  116. public Prop? UseProp()
  117. {
  118. lock (gameObjLock)
  119. {
  120. var oldProp = PropInventory;
  121. PropInventory = null;
  122. return oldProp;
  123. }
  124. }
  125. /// <summary>
  126. /// 是否正在更换道具(包括捡起与抛出)
  127. /// </summary>
  128. private bool isModifyingProp = false;
  129. public bool IsModifyingProp
  130. {
  131. get => isModifyingProp;
  132. set
  133. {
  134. lock (gameObjLock)
  135. {
  136. isModifyingProp = value;
  137. }
  138. }
  139. }
  140. /// <summary>
  141. /// 是否在隐身
  142. /// </summary>
  143. private bool isInvisible = false;
  144. public bool IsInvisible
  145. {
  146. get => isInvisible;
  147. set
  148. {
  149. lock (gameObjLock)
  150. {
  151. isInvisible = value;
  152. }
  153. }
  154. }
  155. /// <summary>
  156. /// 进行一次远程攻击
  157. /// </summary>
  158. /// <param name="posOffset">子弹初始位置偏差值</param>
  159. /// <returns>攻击操作发出的子弹</returns>
  160. public Bullet? RemoteAttack(XY posOffset)
  161. {
  162. if (TrySubBulletNum())
  163. return ProduceOneBullet(this.Position + posOffset);
  164. else
  165. return null;
  166. }
  167. protected Bullet? ProduceOneBullet(XY initPos)
  168. {
  169. var newBullet = BulletFactory.GetBullet(this);
  170. newBullet?.SetPosition(initPos);
  171. return newBullet;
  172. }
  173. /// <summary>
  174. /// 尝试将子弹数量减1
  175. /// </summary>
  176. /// <returns>减操作是否成功</returns>
  177. private bool TrySubBulletNum()
  178. {
  179. lock (gameObjLock)
  180. {
  181. if (bulletNum > 0)
  182. {
  183. --bulletNum;
  184. return true;
  185. }
  186. return false;
  187. }
  188. }
  189. /// <summary>
  190. /// 尝试将子弹数量加1
  191. /// </summary>
  192. /// <returns>加操作是否成功</returns>
  193. public bool TryAddBulletNum()
  194. {
  195. lock (gameObjLock)
  196. {
  197. if (bulletNum < maxBulletNum)
  198. {
  199. ++bulletNum;
  200. return true;
  201. }
  202. return false;
  203. }
  204. }
  205. /// <summary>
  206. /// 尝试加血
  207. /// </summary>
  208. /// <param name="add">欲加量</param>
  209. /// <returns>加操作是否成功</returns>
  210. public bool TryAddHp(int add)
  211. {
  212. if (hp < MaxHp)
  213. {
  214. lock (gameObjLock)
  215. hp = MaxHp > hp + add ? hp + add : MaxHp;
  216. Debugger.Output(this, " hp has added to: " + hp.ToString());
  217. return true;
  218. }
  219. return false;
  220. }
  221. /// <summary>
  222. /// 尝试减血
  223. /// </summary>
  224. /// <param name="sub">减血量</param>
  225. /// <returns>减操作是否成功</returns>
  226. public bool TrySubHp(int sub)
  227. {
  228. if (hp > 0)
  229. {
  230. lock (gameObjLock)
  231. hp = 0 >= hp - sub ? 0 : hp - sub;
  232. Debugger.Output(this, " hp has subed to: " + hp.ToString());
  233. return true;
  234. }
  235. return false;
  236. }
  237. /// <summary>
  238. /// 增加死亡次数
  239. /// </summary>
  240. /// <returns>当前死亡次数</returns>
  241. private int AddDeathCount()
  242. {
  243. lock (gameObjLock)
  244. {
  245. ++deathCount;
  246. return deathCount;
  247. }
  248. }
  249. /// <summary>
  250. /// 加分
  251. /// </summary>
  252. /// <param name="add">增加量</param>
  253. public void AddScore(int add)
  254. {
  255. lock (gameObjLock)
  256. {
  257. score += add;
  258. Debugger.Output(this, " 's score has been added to: " + score.ToString());
  259. }
  260. }
  261. /// <summary>
  262. /// 减分
  263. /// </summary>
  264. /// <param name="sub">减少量</param>
  265. public void SubScore(int sub)
  266. {
  267. lock (gameObjLock)
  268. {
  269. score -= sub;
  270. Debugger.Output(this, " 's score has been subed to: " + score.ToString());
  271. }
  272. }
  273. /// <summary>
  274. /// 遭受攻击
  275. /// </summary>
  276. /// <param name="subHP"></param>
  277. /// <param name="hasSpear"></param>
  278. /// <param name="attacker">伤害来源</param>
  279. /// <returns>人物在受到攻击后死了吗</returns>
  280. public bool BeAttack(Bullet bullet)
  281. {
  282. lock (beAttackedLock)
  283. {
  284. if (hp <= 0)
  285. return false; // 原来已经死了
  286. if (bullet.Parent.TeamID != this.TeamID)
  287. {
  288. if (HasShield)
  289. {
  290. if (bullet.HasSpear)
  291. _ = TrySubHp(bullet.AP);
  292. else
  293. return false;
  294. }
  295. else
  296. {
  297. TrySubHp(bullet.AP);
  298. }
  299. #if DEBUG
  300. Console.WriteLine($"PlayerID:{ID} is being shot! Now his hp is {hp}.");
  301. #endif
  302. if (hp <= 0)
  303. TryActivatingLIFE(); // 如果有复活甲
  304. }
  305. return hp <= 0;
  306. }
  307. }
  308. /// <summary>
  309. /// 攻击被反弹,反弹伤害不会再被反弹
  310. /// </summary>
  311. /// <param name="subHP"></param>
  312. /// <param name="hasSpear"></param>
  313. /// <param name="bouncer">反弹伤害者</param>
  314. /// <returns>是否因反弹伤害而死</returns>
  315. private bool BeBounced(int subHP, bool hasSpear, Character? bouncer)
  316. {
  317. lock (beAttackedLock)
  318. {
  319. if (hp <= 0)
  320. return false;
  321. if (!(bouncer?.TeamID == this.TeamID))
  322. {
  323. if (hasSpear || !HasShield)
  324. _ = TrySubHp(subHP);
  325. if (hp <= 0)
  326. TryActivatingLIFE();
  327. }
  328. return hp <= 0;
  329. }
  330. }
  331. /// <summary>
  332. /// 角色所属队伍ID
  333. /// </summary>
  334. private long teamID = long.MaxValue;
  335. public long TeamID
  336. {
  337. get => teamID;
  338. set
  339. {
  340. lock (gameObjLock)
  341. {
  342. teamID = value;
  343. Debugger.Output(this, " joins in the team: " + value.ToString());
  344. }
  345. }
  346. }
  347. private long playerID = long.MaxValue;
  348. public long PlayerID
  349. {
  350. get => playerID;
  351. set
  352. {
  353. lock (gameObjLock)
  354. {
  355. playerID = value;
  356. }
  357. }
  358. }
  359. /// <summary>
  360. /// 角色携带的信息
  361. /// </summary>
  362. private string message = "THUAI5";
  363. public string Message
  364. {
  365. get => message;
  366. set
  367. {
  368. lock (gameObjLock)
  369. {
  370. message = value;
  371. }
  372. }
  373. }
  374. #endregion
  375. #region 角色拥有的buff相关属性、方法
  376. public void AddMoveSpeed(int buffTime, double add = 2.0) => buffManeger.AddMoveSpeed(add, buffTime, newVal =>
  377. { MoveSpeed = newVal < GameData.characterMaxSpeed ? newVal : GameData.characterMaxSpeed; },
  378. OrgMoveSpeed);
  379. public bool HasFasterSpeed => buffManeger.HasFasterSpeed;
  380. public void AddShield(int shieldTime) => buffManeger.AddShield(shieldTime);
  381. public bool HasShield => buffManeger.HasShield;
  382. public void AddLIFE(int LIFETime) => buffManeger.AddLIFE(LIFETime);
  383. public bool HasLIFE => buffManeger.HasLIFE;
  384. public void AddSpear(int spearTime) => buffManeger.AddSpear(spearTime);
  385. public bool HasSpear => buffManeger.HasSpear;
  386. private Array buffTypeArray = Enum.GetValues(typeof(BuffType));
  387. public Dictionary<BuffType, bool> Buff
  388. {
  389. get
  390. {
  391. Dictionary<BuffType, bool> buff = new Dictionary<BuffType, bool>();
  392. foreach (BuffType type in buffTypeArray)
  393. {
  394. if (type != BuffType.Null)
  395. buff.Add(type, GetBuffStatus(type));
  396. }
  397. return buff;
  398. }
  399. }
  400. private bool GetBuffStatus(BuffType type)
  401. {
  402. switch (type)
  403. {
  404. case BuffType.Spear:
  405. return this.HasSpear;
  406. case BuffType.AddSpeed:
  407. return this.HasFasterSpeed;
  408. case BuffType.Shield:
  409. return this.HasShield;
  410. case BuffType.AddLIFE:
  411. return this.HasLIFE;
  412. default:
  413. return false;
  414. }
  415. }
  416. private void TryActivatingLIFE()
  417. {
  418. if (buffManeger.TryActivatingLIFE())
  419. {
  420. hp = MaxHp;
  421. }
  422. }
  423. #endregion
  424. public override void Reset() // 要加锁吗?
  425. {
  426. _ = AddDeathCount();
  427. base.Reset();
  428. this.MoveSpeed = OrgMoveSpeed;
  429. HP = MaxHp;
  430. PropInventory = null;
  431. // BulletOfPlayer = OriBulletOfPlayer;
  432. // lock (gameObjLock)
  433. // bulletNum = maxBulletNum;
  434. buffManeger.ClearAll();
  435. IsInvisible = false;
  436. this.Vampire = this.OriVampire;
  437. }
  438. public override bool IsRigid => true;
  439. public override ShapeType Shape => ShapeType.Circle;
  440. protected override bool IgnoreCollideExecutor(IGameObj targetObj)
  441. {
  442. if (targetObj.Type == GameObjType.BirthPoint)
  443. {
  444. if (object.ReferenceEquals(((BirthPoint)targetObj).Parent, this)) // 自己的出生点可以忽略碰撞
  445. {
  446. return true;
  447. }
  448. }
  449. else if (targetObj.Type == GameObjType.Prop) // 自己队的地雷忽略碰撞
  450. {
  451. return true;
  452. }
  453. return false;
  454. }
  455. }
  456. }